Skip to content

Commit 7d2c207

Browse files
vishruth-thimmaiahlanza
authored andcommitted
[CIR] Add support for __sync_swap builtin (#1971)
Adds support for the `__sync_swap` builtin. Signed-off-by: vishruth-thimmaiah <[email protected]>
1 parent e1a4a3a commit 7d2c207

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

0 commit comments

Comments
 (0)