Skip to content

Commit 77f98fb

Browse files
vishruth-thimmaiahlanza
authored andcommitted
[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 0c5feec commit 77f98fb

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
@@ -6012,7 +6012,8 @@ def CIR_AtomicFetchKind : CIR_I32EnumAttr<
60126012
I32EnumAttrCase<"Or", 4, "or">,
60136013
I32EnumAttrCase<"Nand", 5, "nand">,
60146014
I32EnumAttrCase<"Max", 6, "max">,
6015-
I32EnumAttrCase<"Min", 7, "min">
6015+
I32EnumAttrCase<"Min", 7, "min">,
6016+
I32EnumAttrCase<"Xchg", 8, "xchg">
60166017
]>;
60176018

60186019
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
@@ -3782,6 +3782,8 @@ mlir::LLVM::AtomicBinOp CIRToLLVMAtomicFetchLowering::getLLVMAtomicBinOp(
37823782
return isSignedInt ? mlir::LLVM::AtomicBinOp::min
37833783
: mlir::LLVM::AtomicBinOp::umin;
37843784
}
3785+
case cir::AtomicFetchKind::Xchg:
3786+
return mlir::LLVM::AtomicBinOp::xchg;
37853787
}
37863788
llvm_unreachable("Unknown atomic fetch opcode");
37873789
}

clang/test/CIR/CodeGen/atomic.cpp

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

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

0 commit comments

Comments
 (0)