Skip to content

Commit 11b3488

Browse files
FantasqueXlanza
authored andcommitted
[CIR][CIRGen] Atomics: handle atomic_compare_exchange_weak (#1359)
Traditional Clang implementation: https://github.com/llvm/clangir/blob/a0091e38f1027e35d17819e02ee1ae257a12d296/clang/lib/CodeGen/CGAtomic.cpp#L545-L550
1 parent 6c8cc7e commit 11b3488

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenAtomic.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -565,7 +565,8 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *E, Address Dest,
565565
case AtomicExpr::AO__c11_atomic_compare_exchange_weak:
566566
case AtomicExpr::AO__opencl_atomic_compare_exchange_weak:
567567
case AtomicExpr::AO__hip_atomic_compare_exchange_weak:
568-
llvm_unreachable("NYI");
568+
emitAtomicCmpXchgFailureSet(CGF, E, true, Dest, Ptr, Val1, Val2,
569+
FailureOrder, Size, Order, Scope);
569570
return;
570571
case AtomicExpr::AO__atomic_compare_exchange:
571572
case AtomicExpr::AO__atomic_compare_exchange_n:

clang/test/CIR/CodeGen/atomic.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,21 @@ bool fi4c(atomic_int *i) {
308308
// LLVM-LABEL: @_Z4fi4cPU7_Atomici
309309
// LLVM: cmpxchg ptr {{.*}}, i32 {{.*}}, i32 {{.*}} seq_cst seq_cst, align 4
310310

311+
bool fi4d(atomic_int *i) {
312+
int cmp = 0;
313+
return atomic_compare_exchange_weak(i, &cmp, 1);
314+
}
315+
316+
// CHECK-LABEL: @_Z4fi4dPU7_Atomici
317+
// CHECK: %old, %cmp = cir.atomic.cmp_xchg({{.*}} : !cir.ptr<!s32i>, {{.*}} : !s32i, {{.*}} : !s32i, success = seq_cst, failure = seq_cst) align(4) weak : (!s32i, !cir.bool)
318+
// CHECK: %[[CMP:.*]] = cir.unary(not, %cmp) : !cir.bool, !cir.bool
319+
// CHECK: cir.if %[[CMP:.*]] {
320+
// CHECK: cir.store %old, {{.*}} : !s32i, !cir.ptr<!s32i>
321+
// CHECK: }
322+
323+
// LLVM-LABEL: @_Z4fi4dPU7_Atomici
324+
// LLVM: cmpxchg weak ptr {{.*}}, i32 {{.*}}, i32 {{.*}} seq_cst seq_cst, align 4
325+
311326
bool fsb(bool *c) {
312327
return __atomic_exchange_n(c, 1, memory_order_seq_cst);
313328
}

0 commit comments

Comments
 (0)