Skip to content

Commit d69ce8a

Browse files
[CIR] Add support for __sync_lock_test_and_set (#1949)
closes #1794 This PR adds support for the `__sync_lock_set_and_set` builtin. Signed-off-by: vishruth-thimmaiah <[email protected]>
1 parent a8000bb commit d69ce8a

File tree

4 files changed

+80
-2
lines changed

4 files changed

+80
-2
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6015,7 +6015,8 @@ def CIR_AtomicFetchKind : CIR_I32EnumAttr<
60156015
I32EnumAttrCase<"Or", 4, "or">,
60166016
I32EnumAttrCase<"Nand", 5, "nand">,
60176017
I32EnumAttrCase<"Max", 6, "max">,
6018-
I32EnumAttrCase<"Min", 7, "min">
6018+
I32EnumAttrCase<"Min", 7, "min">,
6019+
I32EnumAttrCase<"Xchg", 8, "xchg">
60196020
]>;
60206021

60216022
def CIR_AtomicFetch : CIR_Op<"atomic.fetch", [

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2126,7 +2126,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
21262126
case Builtin::BI__sync_lock_test_and_set_4:
21272127
case Builtin::BI__sync_lock_test_and_set_8:
21282128
case Builtin::BI__sync_lock_test_and_set_16:
2129-
llvm_unreachable("BI__sync_lock_test_and_set_1 like NYI");
2129+
return emitBinaryAtomic(*this, cir::AtomicFetchKind::Xchg, E);
21302130

21312131
case Builtin::BI__sync_lock_release_1:
21322132
case Builtin::BI__sync_lock_release_2:

clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVM.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3602,6 +3602,8 @@ mlir::LLVM::AtomicBinOp CIRToLLVMAtomicFetchLowering::getLLVMAtomicBinOp(
36023602
return isSignedInt ? mlir::LLVM::AtomicBinOp::min
36033603
: mlir::LLVM::AtomicBinOp::umin;
36043604
}
3605+
case cir::AtomicFetchKind::Xchg:
3606+
return mlir::LLVM::AtomicBinOp::xchg;
36053607
}
36063608
llvm_unreachable("Unknown atomic fetch opcode");
36073609
}

clang/test/CIR/CodeGen/atomic.cpp

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1196,3 +1196,78 @@ void clear(void *p, volatile void *vp) {
11961196

11971197
// LLVM: store atomic volatile i8 0, ptr %{{.+}} seq_cst, align 1
11981198
}
1199+
1200+
// CHECK-LABEL: @_Z17lock_test_and_setPii
1201+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s32i>, {{.*}} : !s32i, seq_cst) fetch_first : !s32i
1202+
1203+
// LLVM-LABEL: @_Z17lock_test_and_setPii
1204+
// LLVM: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1205+
void lock_test_and_set(int* a, int b) {
1206+
int c = __sync_lock_test_and_set(a, b);
1207+
}
1208+
1209+
1210+
// CHECK-LABEL: @_Z17lock_test_and_setPll
1211+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s64i>, {{.*}} : !s64i, seq_cst) fetch_first : !s64i
1212+
1213+
// LLVM-LABEL: @_Z17lock_test_and_setPll
1214+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1215+
void lock_test_and_set(long* a, long b) {
1216+
long c = __sync_lock_test_and_set(a, b);
1217+
}
1218+
1219+
// CHECK-LABEL: @_Z17lock_test_and_setPss
1220+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s16i>, {{.*}} : !s16i, seq_cst) fetch_first : !s16i
1221+
1222+
// LLVM-LABEL: @_Z17lock_test_and_setPss
1223+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1224+
void lock_test_and_set(short* a, short b) {
1225+
short c = __sync_lock_test_and_set(a, 2);
1226+
}
1227+
1228+
1229+
// CHECK-LABEL: @_Z17lock_test_and_setPcc
1230+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s8i>, {{.*}} : !s8i, seq_cst) fetch_first : !s8i
1231+
1232+
// LLVM-LABEL: @_Z17lock_test_and_setPcc
1233+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1234+
void lock_test_and_set(char* a, char b) {
1235+
char c = __sync_lock_test_and_set(a, b);
1236+
}
1237+
1238+
// CHECK-LABEL: @_Z17lock_test_and_setPji
1239+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u32i>, {{.*}} : !u32i, seq_cst) fetch_first : !u32i
1240+
1241+
// LLVM-LABEL: @_Z17lock_test_and_setPji
1242+
// LLVM: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1243+
void lock_test_and_set(unsigned int* a, int b) {
1244+
unsigned int c = __sync_lock_test_and_set(a, b);
1245+
}
1246+
1247+
1248+
// CHECK-LABEL: @_Z17lock_test_and_setPml
1249+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u64i>, {{.*}} : !u64i, seq_cst) fetch_first : !u64i
1250+
1251+
// LLVM-LABEL: @_Z17lock_test_and_setPml
1252+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1253+
void lock_test_and_set(unsigned long* a, long b) {
1254+
unsigned long c = __sync_lock_test_and_set(a, b);
1255+
}
1256+
1257+
// CHECK-LABEL: @_Z17lock_test_and_setPts
1258+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u16i>, {{.*}} : !u16i, seq_cst) fetch_first : !u16i
1259+
//
1260+
// LLVM-LABEL: @_Z17lock_test_and_setPts
1261+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1262+
void lock_test_and_set(unsigned short* a, short b) {
1263+
unsigned long long c = __sync_lock_test_and_set(a, b);
1264+
}
1265+
1266+
// CHECK-LABEL: @_Z17lock_test_and_setPhc
1267+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u8i>, {{.*}} : !u8i, seq_cst) fetch_first : !u8i
1268+
1269+
// LLVM-LABEL: @_Z17lock_test_and_setPhc
1270+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1271+
void lock_test_and_set(unsigned char* a, char b) {
1272+
unsigned char c = __sync_lock_test_and_set(a, b);
1273+
}

0 commit comments

Comments
 (0)