authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 14:47:26-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2019-08-02 14:47:26-04:00
log90e64bc620edc3f3c3a2c16d01b7ca2eefc02429
tree42280ec202a86a5fab37ed5b6e38f38792ab8042
parenta5cb0f77d11bdcc504fe3e6afa928c88de821518
signature Commit is signed but in an unrecognized format.

fix cmpxchg with discarded result


2 files changed, 17 insertions(+), 1 deletions(-)

src/codegen.cpp+7-1
......@@ -4458,8 +4458,14 @@ static LLVMValueRef ir_render_cmpxchg(CodeGen *g, IrExecutable *executable, IrIn
44584458 return LLVMBuildSelect(g->builder, success_bit, LLVMConstNull(get_llvm_type(g, child_type)), payload_val, "");
44594459 }
44604460
4461 // When the cmpxchg is discarded, the result location will have no bits.
4462 if (!type_has_bits(instruction->result_loc->value.type)) {
4463 return nullptr;
4464 }
4465
44614466 LLVMValueRef result_loc = ir_llvm_value(g, instruction->result_loc);
4462 assert(type_has_bits(child_type));
4467 src_assert(result_loc != nullptr, instruction->base.source_node);
4468 src_assert(type_has_bits(child_type), instruction->base.source_node);
44634469
44644470 LLVMValueRef payload_val = LLVMBuildExtractValue(g->builder, result_val, 0, "");
44654471 LLVMValueRef val_ptr = LLVMBuildStructGEP(g->builder, result_loc, maybe_child_index, "");
test/stage1/behavior/atomics.zig+10
......@@ -1,5 +1,6 @@
11const std = @import("std");
22const expect = std.testing.expect;
3const expectEqual = std.testing.expectEqual;
34const builtin = @import("builtin");
45const AtomicRmwOp = builtin.AtomicRmwOp;
56const AtomicOrder = builtin.AtomicOrder;
......@@ -90,3 +91,12 @@ test "cmpxchg with ptr" {
9091// expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
9192// expect(x == 42);
9293//}
94
95test "cmpxchg with ignored result" {
96 var x: i32 = 1234;
97 var ptr = &x;
98
99 _ = @cmpxchgStrong(i32, &x, 1234, 5678, .Monotonic, .Monotonic);
100
101 expectEqual(i32(5678), x);
102}