Skip to content

Commit 45bf787

Browse files
feat: add support for the __sync_swap builtin
Adds support for the __sync_swap builtin Signed-off-by: vishruth-thimmaiah <[email protected]>
1 parent 657d213 commit 45bf787

File tree

2 files changed

+73
-1
lines changed

2 files changed

+73
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2119,7 +2119,7 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
21192119
case Builtin::BI__sync_swap_4:
21202120
case Builtin::BI__sync_swap_8:
21212121
case Builtin::BI__sync_swap_16:
2122-
llvm_unreachable("BI__sync_swap1 like NYI");
2122+
return emitBinaryAtomic(*this, cir::AtomicFetchKind::Xchg, E);
21232123

21242124
case Builtin::BI__sync_lock_test_and_set_1:
21252125
case Builtin::BI__sync_lock_test_and_set_2:

clang/test/CIR/CodeGen/atomic.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,3 +1271,75 @@ void lock_test_and_set(unsigned short* a, short b) {
12711271
void lock_test_and_set(unsigned char* a, char b) {
12721272
unsigned char c = __sync_lock_test_and_set(a, b);
12731273
}
1274+
1275+
// CHECK-LABEL: @_Z4swapPii
1276+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s32i>, {{.*}} : !s32i, seq_cst) fetch_first : !s32i
1277+
1278+
// LLVM-LABEL: @_Z4swapPii
1279+
// LLVM: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1280+
void swap(int* a, int b) {
1281+
int c = __sync_swap(a, b);
1282+
}
1283+
1284+
// CHECK-LABEL: @_Z4swapPll
1285+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s64i>, {{.*}} : !s64i, seq_cst) fetch_first : !s64i
1286+
1287+
// LLVM-LABEL: @_Z4swapPll
1288+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1289+
void swap(long* a, long b) {
1290+
long c = __sync_swap(a, b);
1291+
}
1292+
1293+
// CHECK-LABEL: @_Z4swapPss
1294+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s16i>, {{.*}} : !s16i, seq_cst) fetch_first : !s16i
1295+
1296+
// LLVM-LABEL: @_Z4swapPss
1297+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1298+
void swap(short* a, short b) {
1299+
short c = __sync_swap(a, 2);
1300+
}
1301+
1302+
// CHECK-LABEL: @_Z4swapPcc
1303+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s8i>, {{.*}} : !s8i, seq_cst) fetch_first : !s8i
1304+
1305+
// LLVM-LABEL: @_Z4swapPcc
1306+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1307+
void swap(char* a, char b) {
1308+
char c = __sync_swap(a, b);
1309+
}
1310+
1311+
// CHECK-LABEL: @_Z4swapPji
1312+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u32i>, {{.*}} : !u32i, seq_cst) fetch_first : !u32i
1313+
1314+
// LLVM-LABEL: @_Z4swapPji
1315+
// LLVM: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1316+
void swap(unsigned int* a, int b) {
1317+
unsigned int c = __sync_swap(a, b);
1318+
}
1319+
1320+
// CHECK-LABEL: @_Z4swapPml
1321+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u64i>, {{.*}} : !u64i, seq_cst) fetch_first : !u64i
1322+
1323+
// LLVM-LABEL: @_Z4swapPml
1324+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1325+
void swap(unsigned long* a, long b) {
1326+
unsigned long c = __sync_swap(a, b);
1327+
}
1328+
1329+
// CHECK-LABEL: @_Z4swapPts
1330+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u16i>, {{.*}} : !u16i, seq_cst) fetch_first : !u16i
1331+
//
1332+
// LLVM-LABEL: @_Z4swapPts
1333+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1334+
void swap(unsigned short* a, short b) {
1335+
unsigned long long c = __sync_swap(a, b);
1336+
}
1337+
1338+
// CHECK-LABEL: @_Z4swapPhc
1339+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u8i>, {{.*}} : !u8i, seq_cst) fetch_first : !u8i
1340+
1341+
// LLVM-LABEL: @_Z4swapPhc
1342+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1343+
void swap(unsigned char* a, char b) {
1344+
unsigned char c = __sync_swap(a, b);
1345+
}

0 commit comments

Comments
 (0)