commit f7e045c80619b3728c54a8aecad65ea12cbe4b75 (tree)
parent 6705cbd5eb8f242a567e24ec21cd3c9b82eb3343
Author: Jacob Young <jacobly0@users.noreply.github.com>
Date: Sun, 23 Mar 2025 21:46:46 -0400
Merge pull request #23256 from xtexx/fix-gh-20113
x86_64: fix packedStore miscomp by spilling EFLAGS
Diffstat:
2 files changed, 34 insertions(+), 3 deletions(-)
diff --git a/src/arch/x86_64/CodeGen.zig b/src/arch/x86_64/CodeGen.zig
@@ -88178,12 +88178,15 @@ fn airStore(self: *CodeGen, inst: Air.Inst.Index, safety: bool) !void {
const reg_locks = self.register_manager.lockRegsAssumeUnused(3, .{ .rdi, .rsi, .rcx });
defer for (reg_locks) |lock| self.register_manager.unlockReg(lock);
+ const ptr_ty = self.typeOf(bin_op.lhs);
+ const ptr_info = ptr_ty.ptrInfo(zcu);
+ const is_packed = ptr_info.flags.vector_index != .none or ptr_info.packed_offset.host_size > 0;
+ if (is_packed) try self.spillEflagsIfOccupied();
+
const src_mcv = try self.resolveInst(bin_op.rhs);
const ptr_mcv = try self.resolveInst(bin_op.lhs);
- const ptr_ty = self.typeOf(bin_op.lhs);
- const ptr_info = ptr_ty.ptrInfo(zcu);
- if (ptr_info.flags.vector_index != .none or ptr_info.packed_offset.host_size > 0) {
+ if (is_packed) {
try self.packedStore(ptr_ty, ptr_mcv, src_mcv);
} else {
try self.store(ptr_ty, ptr_mcv, src_mcv, .{ .safety = safety });
diff --git a/test/behavior/packed-struct.zig b/test/behavior/packed-struct.zig
@@ -1349,3 +1349,31 @@ test "byte-aligned packed relocation" {
try expect(S.packed_value.x == 111);
try expect(S.packed_value.y == &S.global);
}
+
+test "packed struct store of comparison result" {
+ if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
+
+ const S1 = packed struct {
+ val1: u3,
+ val2: u3,
+ };
+ const S2 = packed struct {
+ a: bool,
+ b: bool,
+ };
+
+ var A: S1 = .{ .val1 = 1, .val2 = 1 };
+ A.val2 += 1;
+ try expectEqual(1, A.val1);
+ try expectEqual(2, A.val2);
+ try expect((A.val2 & 1) != 1);
+ const result1: S2 = .{ .a = (A.val2 & 1) != 1, .b = (A.val1 & 1) != 1 };
+ try expect(result1.a);
+ try expect(!result1.b);
+
+ try expect((A.val2 == 3) == false);
+ try expect((A.val2 == 2) == true);
+ const result2: S2 = .{ .a = !(A.val2 == 3), .b = (A.val1 == 2) };
+ try expect(result2.a);
+ try expect(!result2.b);
+}