Skip to content

x86 backend miscompilation w/ packed structs and bitwise operation #20581

Open
@MasonRemaley

Description

@MasonRemaley

Zig Version

0.14.0-dev.616+ab4eeb770

Steps to Reproduce and Observed Behavior

I know the x86 backend isn't passing all tests yet anyway, so I'm not sure if documenting these sorts of bugs is useful or not--if not let me know! I'm just very excited about how fast my project compiles under this backend haha.

Here's a reproduction of the problem:

const std = @import("std");
pub fn main() void {
    // Create a packed struct
    const Packed = packed struct {
        should_be_true: bool,
    };

    // Create a variable and then set it to zero. If we set it immediately to zero the bug
    // doesn't occur, presumably because the bitwise math can then be trivially optimized out.
    var zero: u32 = 5;
    zero -= 5;
    std.debug.assert(zero == 0); // <-- All good

    // This bitwise math results in true as expected
    const should_be_true = (zero & 1) != 1;
    std.debug.assert(should_be_true); // <-- All good

    // Store the same bitwise math in this packed struct. Now the value is true instead of
    // false.
    const result: Packed = .{ .should_be_true = (zero & 1) != 1 };
    std.debug.assert(result.should_be_true); // <-- This fails
}

Expected Behavior

I expected the result of the bitwise operation to be the same in both cases.

Metadata

Metadata

Assignees

No one assigned

    Labels

    arch-x86_6464-bit x86backend-self-hostedbugObserved behavior contradicts documented or intended behaviormiscompilationThe compiler reports success but produces semantically incorrect code.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions