Skip to content

cbe: @bitCast between bool vectors and integers differs to LLVM backend #16677

@notcancername

Description

@notcancername

Zig Version

0.11.0-dev.4403+e84cda0eb

Steps to Reproduce and Observed Behavior

  1. Create a source file containing the following code:
test "bool vector bitcast" {
    var v: @Vector(8, bool) = .{ false, false, false, false, false, false, false, false };
    try std.testing.expectEqual(@as(u8, 0b0000_0000), @as(u8, @bitCast(v)));
    v[0] = true;
    try std.testing.expectEqual(@as(u8, 0b0000_0001), @as(u8, @bitCast(v)));
    v[1] = true;
    try std.testing.expectEqual(@as(u8, 0b0000_0011), @as(u8, @bitCast(v)));
    v[2] = true;
    try std.testing.expectEqual(@as(u8, 0b0000_0111), @as(u8, @bitCast(v)));
    v[0] = false;
    try std.testing.expectEqual(@as(u8, 0b0000_0110), @as(u8, @bitCast(v)));
    v[7] = true;
    try std.testing.expectEqual(@as(u8, 0b1000_0110), @as(u8, @bitCast(v)));
}
  1. Run zig test source.zig -O ReleaseSmall on your x86_64 machine. The test passes.
  2. Run zig test source.zig -ofmt=c -O ReleaseSmall. The test fails:
Test [1/1] test.bool vector bitcast... expected 3, found 1
Test [1/1] test.bool vector bitcast... FAIL (TestExpectedEqual)
0 passed; 0 skipped; 1 failed.
error: the following test command failed with exit code 1:
/home/user/Downloads/zig/zig run -I /home/user/Downloads/zig/lib /home/user/src/random/zig-cache/o/c108104757f433d5803b478dd6f1fe39/test.c

Note that the generated code for this source:

export fn return_bool_vector_bitcast() u8 {
    var v: @Vector(8, bool) = .{false, false, false, false, false, false, true, true };
    return @bitCast(v);
}

is:

uint8_t return_bool_vector_bitcast(void) {
 static bool const t1[8] = {false,false,false,false,false,false,true,true};
 bool t2[8];
 bool t0[8];
 uint8_t t3;
 memcpy((char *)&t0, t1, sizeof(bool[8]));
 memcpy(t2, (const char *)&t0, sizeof(bool[8]));
 memcpy(&t3, &t2, sizeof(uint8_t));
 t3 = zig_wrap_u8(t3, UINT8_C(8));
 return t3;
}

Note that in the generated code, sizeof(uint8_t) clearly is not equal to sizeof(bool[8]). Note that no check for this is emitted, even in debug mode.

Expected Behavior

I expected the behavior to match that of the LLVM backend, although the semantics of @bitCast between bool vectors and integers are not yet specified.

Metadata

Metadata

Assignees

No one assigned

    Labels

    backend-cThe C backend (CBE) outputs C source code.proposalThis issue suggests modifications. If it also has the "accepted" label then it is planned.

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions