Skip to content

Miscompilation when bitcasting numbers to vector in x86-64 backend #23851

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

Closed
AndrewKraevskii opened this issue May 10, 2025 · 2 comments
Closed
Labels
arch-x86_64 64-bit x86 backend-self-hosted bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.

Comments

@AndrewKraevskii
Copy link
Contributor

Zig Version

0.15.0-dev.479+6eb5e5630

Steps to Reproduce and Observed Behavior

Run following program using zig run main.zig -fno-llvm -fno-lld on x86-64_linux. It finishes triggering asserts.

const std = @import("std");

pub fn main() !void {
    var number: u24 = 255;
    _ = &i;

    const array: [8]u3 = @as(@Vector(8, u3), @bitCast(number));
    const first_three_bits: u3 = array[0];

    std.debug.assert(first_three_bits <= 7);
    std.debug.assert(first_three_bits >= 7);
    std.debug.assert(first_three_bits != 7);

    var buffer: [4]u8 = undefined;
    const formated = std.fmt.bufPrint(&buffer, "0x{x}", .{first_three_bits}) catch unreachable;
    std.debug.assert(std.mem.eql(u8, formated, "0xff"));
}

Expected Behavior

I expect it to crash on third assert as it does in llvm backend.

@AndrewKraevskii AndrewKraevskii added the bug Observed behavior contradicts documented or intended behavior label May 10, 2025
@RetroDev256
Copy link
Contributor

RetroDev256 commented May 11, 2025

Reduction:

pub fn main() void {
    var number: u24 = 255;
    _ = &number;

    const array: @Vector(8, u3) = @bitCast(number);
    if (array[0] == 7) unreachable;
}

Logging the value of "array":

retrodev@lime ~ $ zig build-exe main.zig -fno-llvm; ./main 
vec: { 255, 0, 0, 0, 0, 0, 0, 0 }
retrodev@lime ~ $ zig build-exe main.zig; ./main 
vec: { 7, 7, 3, 0, 0, 0, 0, 0 }

@alexrp alexrp added arch-x86_64 64-bit x86 backend-self-hosted miscompilation The compiler reports success but produces semantically incorrect code. labels May 11, 2025
@alexrp alexrp added this to the 0.15.0 milestone May 11, 2025
@linusg linusg changed the title Misscompilation when bitcasting numbers to vector in x86-64 backend Miscompilation when bitcasting numbers to vector in x86-64 backend May 11, 2025
@jacobly0
Copy link
Member

@Vector(8, u3) has a different representation on different backends, therefore it has different behavior as per the pre-#19755 @bitCast semantics.

@jacobly0 jacobly0 removed this from the 0.15.0 milestone May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
arch-x86_64 64-bit x86 backend-self-hosted bug Observed behavior contradicts documented or intended behavior miscompilation The compiler reports success but produces semantically incorrect code.
Projects
None yet
Development

No branches or pull requests

4 participants