Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

miscompilation possibly related to union layout #16392

Open
Techatrix opened this issue Jul 13, 2023 · 6 comments
Open

miscompilation possibly related to union layout #16392

Techatrix opened this issue Jul 13, 2023 · 6 comments
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. regression It worked in a previous version of Zig, but stopped working. upstream An issue with a third party project that Zig uses.
Milestone

Comments

@Techatrix
Copy link
Contributor

Techatrix commented Jul 13, 2023

Zig Version

0.11.0-dev.3978+711b4e93e

Steps to Reproduce and Observed Behavior

const std = @import("std");

const Inner = union {
    integer: u32,
    string: [8]u8, // modifying the size of this field will change the behavior
                   // you could try out [4]u8 or [12]u8
};

const Outer = union {
    unreachable_inner: Inner,
    params: [16]u8,
};

pub fn foo(params: [16]u8) Outer {
    var id: ?u32 = null;
    std.mem.doNotOptimizeAway(&id);
    if (id) |id_obj| {
        return .{ .unreachable_inner = .{ .integer = id_obj } };
    }
    return .{ .params = params };
}

pub fn main() void {
    var val1 = [16]u8{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
    std.debug.print("{any}\n", .{val1});
    const val2 = foo(val1);
    std.debug.print("{any}\n", .{val2.params});
}

running zig run file.zig -O ReleaseSafe will output:

{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
{ 0, 1, 2, 3, 4, 5, 6, 7, 0, 9, 10, 11, 12, 13, 14, 15 }

There is a zero at index 8.
This only happens in ReleaseSafe. I did not observe this behavior in 0.10.1.

https://godbolt.org/z/qc3MsW7d6

Expected Behavior

{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }
@Techatrix Techatrix added the bug Observed behavior contradicts documented or intended behavior label Jul 13, 2023
Techatrix added a commit to Techatrix/zls that referenced this issue Jul 13, 2023
Techatrix added a commit to Techatrix/zls that referenced this issue Jul 13, 2023
leecannon pushed a commit to zigtools/zls that referenced this issue Jul 13, 2023
@andrewrk andrewrk added miscompilation The compiler reports success but produces semantically incorrect code. regression It worked in a previous version of Zig, but stopped working. labels Jul 22, 2023
@andrewrk andrewrk added this to the 0.11.0 milestone Jul 22, 2023
@jacobly0
Copy link
Member

jacobly0 commented Jul 24, 2023

We output llvm for unions in the same way as clang does, as can be seen by the fact that this bug reproduces with clang.

@jacobly0 jacobly0 added the upstream An issue with a third party project that Zig uses. label Jul 24, 2023
@andrewrk andrewrk added the backend-llvm The LLVM backend outputs an LLVM IR Module. label Jul 24, 2023
@andrewrk
Copy link
Member

Moving this to the 0.12.0 milestone since it's an issue to be solved in LLVM 17.

@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jul 24, 2023
Techatrix added a commit to zigtools/zls that referenced this issue Aug 26, 2023
@henrikolsen549
Copy link

henrikolsen549 commented Sep 22, 2023

Not fixed by a63a1c5 (LLVM 17).

This miscompilation frequently happens when parsing a JSON value with two or more nested objects, so maybe it makes sense to implement the workaround suggested in the LLVM issue.

Techatrix added a commit to zigtools/zls that referenced this issue Sep 28, 2023
Techatrix added a commit to zigtools/zls that referenced this issue Sep 28, 2023
CI doesn't run in Release mode but it can still be manually invoked
llogick pushed a commit to zigtools/zls that referenced this issue Sep 29, 2023
llogick pushed a commit to zigtools/zls that referenced this issue Sep 29, 2023
CI doesn't run in Release mode but it can still be manually invoked
KoltPenny pushed a commit to KoltPenny/zls that referenced this issue Oct 18, 2023
KoltPenny pushed a commit to KoltPenny/zls that referenced this issue Oct 18, 2023
KoltPenny pushed a commit to KoltPenny/zls that referenced this issue Oct 18, 2023
CI doesn't run in Release mode but it can still be manually invoked
jacobly0 added a commit to jacobly0/zig that referenced this issue Nov 10, 2023
@henrikolsen549
Copy link

Fixed by #17963.

@jacobly0
Copy link
Member

jacobly0 commented Nov 12, 2023

Fixed by #17963.

That's a workaround, the issue stays open to track the upstream bug and a revert of the workaround.

@henrikolsen549
Copy link

@jacobly0, with LLVM 18 the workaround can probably now be reverted.

@andrewrk andrewrk modified the milestones: 0.14.0, 0.14.1 Mar 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend-llvm The LLVM backend outputs an LLVM IR Module. bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code. regression It worked in a previous version of Zig, but stopped working. upstream An issue with a third party project that Zig uses.
Projects
None yet
Development

No branches or pull requests

4 participants