ziglang / zig

General-purpose programming language and toolchain for maintaining robust, optimal, and reusable software.
https://ziglang.org
MIT License
34.85k stars 2.55k forks source link

`@bitCast of packed struct containing pointer` test regression with LLVM 19 #21463

Open alexrp opened 1 month ago

alexrp commented 1 month ago

Zig Version

c6ad452

Steps to Reproduce and Observed Behavior

This test regressed with LLVM 19 due to this change: https://discourse.llvm.org/t/rfc-remove-most-constant-expressions/63179

Our lowering was changed in a27f407 to deal with this change for the majority of cases, but this isn't enough for this particular test. The test was disabled in bdae7d9 (see commit message for failure details).

Expected Behavior

No failure.

gooncreeper commented 1 week ago

Another similar case that has regressed

test {
    const A = packed struct(u64) {
        c: *allowzero u64,
    };

    const a: A = @bitCast(@as(u64, 0));
    _ = a;
}

The stack trace is slightly different

/home/dev/git/ziglang/zig/src/Value.zig:172:17: 0xabc93f0 in intFromEnum (zig)
        else => unreachable,
                ^
/home/dev/git/ziglang/zig/src/Value.zig:494:55: 0xb015093 in writeToPackedMemory (zig)
            switch (ip.indexToKey((try val.intFromEnum(ty, pt)).toIntern()).int.storage) {
                                                      ^
/home/dev/git/ziglang/zig/src/Value.zig:568:43: 0xb014d30 in writeToPackedMemory (zig)
            return val.writeToPackedMemory(Type.usize, pt, buffer, bit_offset);
                                          ^
/home/dev/git/ziglang/zig/src/Value.zig:544:50: 0xb0147e3 in writeToPackedMemory (zig)
                try field_val.writeToPackedMemory(field_ty, pt, buffer, bit_offset + bits);
                                                 ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:3976:32: 0xabdd43e in lowerValueToInt (zig)
        val.writeToPackedMemory(ty, pt, std.mem.sliceAsBytes(limbs)[0..bytes], 0) catch unreachable;
                               ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:4353:49: 0xa8f7514 in lowerValue (zig)
                        return o.lowerValueToInt(llvm_int_ty, arg_val);
                                                ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:5125:42: 0xbc251f7 in resolveValue (zig)
        const llvm_val = try o.lowerValue(val.toIntern());
                                         ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:5116:47: 0xbc24f83 in resolveInst (zig)
        const llvm_val = try self.resolveValue((try self.air.value(inst, self.ng.object.pt)).?);
                                              ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:7479:45: 0xbc8ffb4 in airDbgVarVal (zig)
        const operand = try self.resolveInst(pl_op.operand);
                                            ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:5400:54: 0xb7ee635 in genBody (zig)
                .dbg_var_val => try self.airDbgVarVal(inst, false),
                                                     ^
/home/dev/git/ziglang/zig/src/codegen/llvm.zig:1860:19: 0xb7e84c8 in updateFunc (zig)
        fg.genBody(air.getMainBody(), .poi) catch |err| switch (err) {
                  ^
/home/dev/git/ziglang/zig/src/link/Elf.zig:2336:70: 0xbc11145 in updateFunc (zig)
    if (self.llvm_object) |llvm_object| return llvm_object.updateFunc(pt, func_index, air, liveness);
                                                                     ^
/home/dev/git/ziglang/zig/src/link.zig:725:82: 0xb7f4ba5 in updateFunc (zig)
                return @as(*tag.Type(), @fieldParentPtr("base", base)).updateFunc(pt, func_index, air, liveness);
                                                                                 ^
/home/dev/git/ziglang/zig/src/Zcu/PerThread.zig:917:22: 0xb3c8f2c in linkerUpdateFunc (zig)
        lf.updateFunc(pt, func_index, air, liveness) catch |err| switch (err) {
                     ^
/home/dev/git/ziglang/zig/src/link.zig:1548:32: 0xaf54741 in doTask (zig)
            pt.linkerUpdateFunc(func.func, func.air) catch |err| switch (err) {
                               ^
/home/dev/git/ziglang/zig/src/Compilation.zig:3869:20: 0xab65b19 in dispatchCodegenTask (zig)
        link.doTask(comp, tid, link_task);
                   ^
/home/dev/git/ziglang/zig/src/Compilation.zig:3646:37: 0xa8d8a9e in processOneJob (zig)
            comp.dispatchCodegenTask(tid, .{ .codegen_func = func });
                                    ^
/home/dev/git/ziglang/zig/src/Compilation.zig:3603:30: 0xa6b1b6c in performAllTheWorkInner (zig)
            try processOneJob(@intFromEnum(Zcu.PerThread.Id.main), comp, job, main_progress_node);
                             ^
/home/dev/git/ziglang/zig/src/Compilation.zig:3463:36: 0xa516c80 in performAllTheWork (zig)
    try comp.performAllTheWorkInner(main_progress_node);
                                   ^
/home/dev/git/ziglang/zig/src/Compilation.zig:2232:31: 0xa512291 in update (zig)
    try comp.performAllTheWork(main_progress_node);
                              ^
/home/dev/git/ziglang/zig/src/main.zig:4414:20: 0xa55482d in updateModule (zig)
    try comp.update(prog_node);
                   ^
/home/dev/git/ziglang/zig/src/main.zig:3606:21: 0xa5bf664 in buildOutputType (zig)
        updateModule(comp, color, root_prog_node) catch |err| switch (err) {
                    ^
/home/dev/git/ziglang/zig/src/main.zig:268:31: 0xa41b884 in mainArgs (zig)
        return buildOutputType(gpa, arena, args, .zig_test);
                              ^
/home/dev/git/ziglang/zig/src/main.zig:200:20: 0xa418685 in main (zig)
    return mainArgs(gpa, arena, args);
                   ^
/home/dev/git/ziglang/zig/lib/std/start.zig:617:37: 0xa41818e in main (zig)
            const result = root.main() catch |err| {