From 08a8aa100d73bd87e6c39563d8f5c6186217afb3 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Fri, 21 Apr 2023 20:44:16 -0400 Subject: [PATCH] cbe: fix local aliasing issues in atomic ops --- src/codegen/c.zig | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/codegen/c.zig b/src/codegen/c.zig index cde44686400f90082d394c44a310bbe089ff600e..903d2449fd6f47a34100d9e0068eb3d95ab306b1 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -5946,19 +5946,20 @@ fn airCmpxchg(f: *Function, inst: Air.Inst.Index, flavor: [*:0]const u8) !CValue const ptr = try f.resolveInst(extra.ptr); const expected_value = try f.resolveInst(extra.expected_value); const new_value = try f.resolveInst(extra.new_value); - try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); - const writer = f.object.writer(); const ptr_ty = f.air.typeOf(extra.ptr); const ty = ptr_ty.childType(); - const target = f.object.dg.module.getTarget(); - var repr_pl = Type.Payload.Bits{ - .base = .{ .tag = .int_unsigned }, - .data = @intCast(u16, ty.abiSize(target) * 8), - }; - const repr_ty = if (ty.isRuntimeFloat()) Type.initPayload(&repr_pl.base) else ty; - + const writer = f.object.writer(); const new_value_mat = try Materialize.start(f, inst, writer, ty, new_value); + try reap(f, inst, &.{ extra.ptr, extra.expected_value, extra.new_value }); + + const target = f.object.dg.module.getTarget(); + var repr_pl = Type.Payload.Bits{ + .base = .{ .tag = .int_unsigned }, + .data = @intCast(u16, ty.abiSize(target) * 8), + }; + const repr_ty = if (ty.isRuntimeFloat()) Type.initPayload(&repr_pl.base) else ty; + const local = try f.allocLocal(inst, inst_ty); if (inst_ty.isPtrLikeOptional()) { { @@ -6052,9 +6053,10 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { const ty = ptr_ty.childType(); const ptr = try f.resolveInst(pl_op.operand); const operand = try f.resolveInst(extra.operand); + + const writer = f.object.writer(); + const operand_mat = try Materialize.start(f, inst, writer, ty, operand); try reap(f, inst, &.{ pl_op.operand, extra.operand }); - const writer = f.object.writer(); - const local = try f.allocLocal(inst, inst_ty); const target = f.object.dg.module.getTarget(); var repr_pl = Type.Payload.Bits{ @@ -6065,7 +6067,7 @@ fn airAtomicRmw(f: *Function, inst: Air.Inst.Index) !CValue { const is_128 = repr_pl.data == 128; const repr_ty = if (is_float) Type.initPayload(&repr_pl.base) else ty; - const operand_mat = try Materialize.start(f, inst, writer, ty, operand); + const local = try f.allocLocal(inst, inst_ty); try writer.print("zig_atomicrmw_{s}", .{toAtomicRmwSuffix(extra.op())}); if (is_float) try writer.writeAll("_float") else if (is_128) try writer.writeAll("_int128"); try writer.writeByte('('); @@ -6144,17 +6146,18 @@ fn airAtomicStore(f: *Function, inst: Air.Inst.Index, order: [*:0]const u8) !CVa const ty = ptr_ty.childType(); const ptr = try f.resolveInst(bin_op.lhs); const element = try f.resolveInst(bin_op.rhs); - try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); + const writer = f.object.writer(); - - const target = f.object.dg.module.getTarget(); - var repr_pl = Type.Payload.Bits{ - .base = .{ .tag = .int_unsigned }, - .data = @intCast(u16, ty.abiSize(target) * 8), - }; - const repr_ty = if (ty.isRuntimeFloat()) Type.initPayload(&repr_pl.base) else ty; - const element_mat = try Materialize.start(f, inst, writer, ty, element); + try reap(f, inst, &.{ bin_op.lhs, bin_op.rhs }); + + const target = f.object.dg.module.getTarget(); + var repr_pl = Type.Payload.Bits{ + .base = .{ .tag = .int_unsigned }, + .data = @intCast(u16, ty.abiSize(target) * 8), + }; + const repr_ty = if (ty.isRuntimeFloat()) Type.initPayload(&repr_pl.base) else ty; + try writer.writeAll("zig_atomic_store((zig_atomic("); try f.renderType(writer, ty); try writer.writeByte(')'); -- 2.54.0