Skip to content

Regression/Miscompilation: Self-hosted x86_64 backend disagrees on @bitSizeOf value of arrays with the LLVM backend #25554

@sagehane

Description

@sagehane

Zig Version

0.16.0-dev.699+529aa9f27

Steps to Reproduce and Observed Behavior

Reporting on behalf of a discussion in the #zig:tchncs.de Matrix group. The issue seems to be a regression from 0.14, presumably since https://ziglang.org/devlog/2025/#2025-06-08 due to the new self-hosted x86 backend. The regression can be observed on 0.15.1 too, and is likely x86_64-specific.

$ uname -a
Linux nixos-laptop 6.12.51 #1-NixOS SMP PREEMPT_DYNAMIC Mon Oct  6 09:17:53 UTC 2025 x86_64 GNU/Linux

Sample code:

const std = @import("std");

fn sizeOfArray(N: comptime_int, comptime T: type) comptime_int {
    return @bitSizeOf([N]T);
}

comptime {
    @compileLog(sizeOfArray(2, u2));
}

Run the above code with -fno-llvm and -fllvm:

$ zig test test.zig -fno-llvm
test.zig:8:5: error: found compile log statement
    @compileLog(sizeOfArray(2, u2));
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(comptime_int, 4)
$ zig test test.zig -fllvm
test.zig:8:5: error: found compile log statement
    @compileLog(sizeOfArray(2, u2));
    ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Compile Log Output:
@as(comptime_int, 10)

Expected Behavior

In Zig 0.14 and prior, -fno-llvm and -fllvm would agree on the @bitSizeOf value. The status quo of LLVM seems to be something like:

const std = @import("std");

fn foo(comptime T: type, N: comptime_int) !void {
    const Array = [N]T;
    const len = @sizeOf(Array) - @alignOf(Array);
    try std.testing.expectEqual(8 * len + @bitSizeOf(T), @bitSizeOf(Array));
}

test "-fllvm" {
    try foo(u2, 2);
    try foo(u3, 2);
    try foo(u3, 3);
    try foo(u9, 17);
    try foo(u64, 17);
}

Quoting #19755:

[N]T, for runtime-allowed T: N * @bitSizeOf(T) bits

It seems like the current behaviour of the new self-hosted backend might be the desired behaviour, so it's debatable which backend is in need of fixing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions