Commit f7dc9b50ab

Kendall Condon <goon.pri.low@gmail.com>
2025-06-27 19:38:54
llvm: fix atomic widening of packed structs
Additionally, disable failing big-endian atomic test also improve test paramaters to catch this when condition is removed also some other cleanups
1 parent 4a1594f
Changed files (2)
src
codegen
test
behavior
src/codegen/llvm.zig
@@ -4339,9 +4339,11 @@ pub const Object = struct {
     /// types to work around a LLVM deficiency when targeting ARM/AArch64.
     fn getAtomicAbiType(o: *Object, pt: Zcu.PerThread, ty: Type, is_rmw_xchg: bool) Allocator.Error!Builder.Type {
         const zcu = pt.zcu;
+        const ip = &zcu.intern_pool;
         const int_ty = switch (ty.zigTypeTag(zcu)) {
             .int => ty,
             .@"enum" => ty.intTagType(zcu),
+            .@"struct" => Type.fromInterned(ip.loadStructType(ty.toIntern()).backingIntTypeUnordered(ip)),
             .float => {
                 if (!is_rmw_xchg) return .none;
                 return o.builder.intType(@intCast(ty.abiSize(zcu) * 8));
@@ -11424,7 +11426,7 @@ pub const FuncGen = struct {
 
         if (workaround_disable_truncate) {
             // see https://github.com/llvm/llvm-project/issues/64222
-            // disable the truncation codepath for larger that 32bits value - with this heuristic, the backend passes the test suite.
+            // disable the truncation codepath for larger than 32bits value - with this heuristic, the backend passes the test suite.
             return try fg.wip.load(access_kind, payload_llvm_ty, payload_ptr, payload_alignment, "");
         }
 
test/behavior/atomics.zig
@@ -1,7 +1,6 @@
 const std = @import("std");
 const builtin = @import("builtin");
 const expect = std.testing.expect;
-const expectEqual = std.testing.expectEqual;
 
 const supports_128_bit_atomics = switch (builtin.cpu.arch) {
     // TODO: Ideally this could be sync'd with the logic in Sema.
@@ -364,25 +363,32 @@ test "atomics with different types" {
     if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
     if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
     if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
+    if (builtin.target.cpu.arch.endian() == .big) return error.SkipZigTest; // #24282
 
     try testAtomicsWithType(bool, true, false);
 
     try testAtomicsWithType(u1, 0, 1);
-    try testAtomicsWithType(i4, 0, 1);
-    try testAtomicsWithType(u5, 0, 1);
-    try testAtomicsWithType(i15, 0, 1);
-    try testAtomicsWithType(u24, 0, 1);
+    try testAtomicsWithType(i4, 2, 1);
+    try testAtomicsWithType(u5, 2, 1);
+    try testAtomicsWithType(i15, 2, 1);
+    try testAtomicsWithType(u24, 2, 1);
 
     try testAtomicsWithType(u0, 0, 0);
     try testAtomicsWithType(i0, 0, 0);
 
     try testAtomicsWithType(enum(u32) { x = 1234, y = 5678 }, .x, .y);
+    try testAtomicsWithType(enum(u19) { x = 1234, y = 5678 }, .x, .y);
 
     try testAtomicsWithPackedStruct(
         packed struct { x: u7, y: u24, z: bool },
         .{ .x = 1, .y = 2, .z = true },
         .{ .x = 3, .y = 4, .z = false },
     );
+    try testAtomicsWithPackedStruct(
+        packed struct { x: u19, y: bool },
+        .{ .x = 1, .y = true },
+        .{ .x = 3, .y = false },
+    );
 }
 
 fn testAtomicsWithType(comptime T: type, a: T, b: T) !void {