Skip to content

Commit 31b96d1

Browse files
feat: add support for the __sync_swap builtin
Adds support for the __sync_swap builtin Signed-off-by: vishruth-thimmaiah <vishruththimmaiah@gmail.com>
1 parent de26ca3 commit 31b96d1

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-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: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,3 +1271,99 @@ 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+
1281+
// OGCG-LABEL: @_Z4swapPii
1282+
// OGCG: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1283+
void swap(int* a, int b) {
1284+
int c = __sync_swap(a, b);
1285+
}
1286+
1287+
// CHECK-LABEL: @_Z4swapPll
1288+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s64i>, {{.*}} : !s64i, seq_cst) fetch_first : !s64i
1289+
1290+
// LLVM-LABEL: @_Z4swapPll
1291+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1292+
1293+
// OGCG-LABEL: @_Z4swapPll
1294+
// OGCG: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1295+
void swap(long* a, long b) {
1296+
long c = __sync_swap(a, b);
1297+
}
1298+
1299+
// CHECK-LABEL: @_Z4swapPss
1300+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s16i>, {{.*}} : !s16i, seq_cst) fetch_first : !s16i
1301+
1302+
// LLVM-LABEL: @_Z4swapPss
1303+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1304+
1305+
// OGCG-LABEL: @_Z4swapPss
1306+
// OGCG: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1307+
void swap(short* a, short b) {
1308+
short c = __sync_swap(a, 2);
1309+
}
1310+
1311+
// CHECK-LABEL: @_Z4swapPcc
1312+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!s8i>, {{.*}} : !s8i, seq_cst) fetch_first : !s8i
1313+
1314+
// LLVM-LABEL: @_Z4swapPcc
1315+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1316+
1317+
// OGCG-LABEL: @_Z4swapPcc
1318+
// OGCG: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1319+
void swap(char* a, char b) {
1320+
char c = __sync_swap(a, b);
1321+
}
1322+
1323+
// CHECK-LABEL: @_Z4swapPji
1324+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u32i>, {{.*}} : !u32i, seq_cst) fetch_first : !u32i
1325+
1326+
// LLVM-LABEL: @_Z4swapPji
1327+
// LLVM: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1328+
1329+
// OGCG-LABEL: @_Z4swapPji
1330+
// OGCG: atomicrmw xchg ptr {{.*}}, i32 {{.*}} seq_cst, align 4
1331+
void swap(unsigned int* a, int b) {
1332+
unsigned int c = __sync_swap(a, b);
1333+
}
1334+
1335+
// CHECK-LABEL: @_Z4swapPml
1336+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u64i>, {{.*}} : !u64i, seq_cst) fetch_first : !u64i
1337+
1338+
// LLVM-LABEL: @_Z4swapPml
1339+
// LLVM: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1340+
1341+
// OGCG-LABEL: @_Z4swapPml
1342+
// OGCG: atomicrmw xchg ptr {{.*}}, i64 {{.*}} seq_cst, align 8
1343+
void swap(unsigned long* a, long b) {
1344+
unsigned long c = __sync_swap(a, b);
1345+
}
1346+
1347+
// CHECK-LABEL: @_Z4swapPts
1348+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u16i>, {{.*}} : !u16i, seq_cst) fetch_first : !u16i
1349+
//
1350+
// LLVM-LABEL: @_Z4swapPts
1351+
// LLVM: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1352+
1353+
// OGCG-LABEL: @_Z4swapPts
1354+
// OGCG: atomicrmw xchg ptr {{.*}}, i16 {{.*}} seq_cst, align 2
1355+
void swap(unsigned short* a, short b) {
1356+
unsigned long long c = __sync_swap(a, b);
1357+
}
1358+
1359+
// CHECK-LABEL: @_Z4swapPhc
1360+
// CHECK: cir.atomic.fetch(xchg, {{.*}} : !cir.ptr<!u8i>, {{.*}} : !u8i, seq_cst) fetch_first : !u8i
1361+
1362+
// LLVM-LABEL: @_Z4swapPhc
1363+
// LLVM: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1364+
1365+
// OGCG-LABEL: @_Z4swapPhc
1366+
// OGCG: atomicrmw xchg ptr {{.*}}, i8 {{.*}} seq_cst, align 1
1367+
void swap(unsigned char* a, char b) {
1368+
unsigned char c = __sync_swap(a, b);
1369+
}

0 commit comments

Comments
 (0)