Skip to content

Commit 9975749

Browse files
authored
[CIR][CIRGen][Builtin] Implement builtin __sync_fetch_and_sub (#932)
as title. Notice this is not target specific nor neon intrinsics.
1 parent 7051000 commit 9975749

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1164,6 +1164,16 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
11641164
return buildBinaryAtomic(*this, mlir::cir::AtomicFetchKind::Add, E);
11651165
}
11661166

1167+
case Builtin::BI__sync_fetch_and_sub:
1168+
llvm_unreachable("Shouldn't make it through sema");
1169+
case Builtin::BI__sync_fetch_and_sub_1:
1170+
case Builtin::BI__sync_fetch_and_sub_2:
1171+
case Builtin::BI__sync_fetch_and_sub_4:
1172+
case Builtin::BI__sync_fetch_and_sub_8:
1173+
case Builtin::BI__sync_fetch_and_sub_16: {
1174+
return buildBinaryAtomic(*this, mlir::cir::AtomicFetchKind::Sub, E);
1175+
}
1176+
11671177
case Builtin::BI__sync_val_compare_and_swap_1:
11681178
case Builtin::BI__sync_val_compare_and_swap_2:
11691179
case Builtin::BI__sync_val_compare_and_swap_4:

clang/test/CIR/CodeGen/atomic.cpp

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,19 @@ void inc_int(int* a, int b) {
351351
// LLVM-LABEL: @_Z7inc_int
352352
// LLVM: atomicrmw add ptr {{.*}}, i32 {{.*}} seq_cst, align 4
353353

354+
void sub_int(int* a, int b) {
355+
int c = __sync_fetch_and_sub(a, b);
356+
}
357+
358+
// CHECK-LABEL: _Z7sub_int
359+
// CHECK: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
360+
// CHECK: %[[VAL:.*]] = cir.load {{.*}} : !cir.ptr<!s32i>, !s32i
361+
// CHECK: %[[RES:.*]] = cir.atomic.fetch(sub, %[[PTR]] : !cir.ptr<!s32i>, %[[VAL]] : !s32i, seq_cst) fetch_first : !s32i
362+
// CHECK: cir.store %[[RES]], {{.*}} : !s32i, !cir.ptr<!s32i>
363+
364+
// LLVM-LABEL: _Z7sub_int
365+
// LLVM: atomicrmw sub ptr {{.*}}, i32 {{.*}} seq_cst, align 4
366+
354367

355368
// CHECK-LABEL: @_Z8inc_long
356369
// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr<!s64i>, {{.*}} : !s64i, seq_cst) fetch_first : !s64i
@@ -362,6 +375,17 @@ void inc_long(long* a, long b) {
362375
long c = __sync_fetch_and_add(a, 2);
363376
}
364377

378+
// CHECK-LABEL: @_Z8sub_long
379+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!s64i>, {{.*}} : !s64i, seq_cst) fetch_first : !s64i
380+
381+
// LLVM-LABEL: @_Z8sub_long
382+
// LLVM: atomicrmw sub ptr {{.*}}, i64 {{.*}} seq_cst, align 8
383+
384+
void sub_long(long* a, long b) {
385+
long c = __sync_fetch_and_sub(a, 2);
386+
}
387+
388+
365389
// CHECK-LABEL: @_Z9inc_short
366390
// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr<!s16i>, {{.*}} : !s16i, seq_cst) fetch_first : !s16i
367391

@@ -371,6 +395,16 @@ void inc_short(short* a, short b) {
371395
short c = __sync_fetch_and_add(a, 2);
372396
}
373397

398+
// CHECK-LABEL: @_Z9sub_short
399+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!s16i>, {{.*}} : !s16i, seq_cst) fetch_first : !s16i
400+
401+
// LLVM-LABEL: @_Z9sub_short
402+
// LLVM: atomicrmw sub ptr {{.*}}, i16 {{.*}} seq_cst, align 2
403+
void sub_short(short* a, short b) {
404+
short c = __sync_fetch_and_sub(a, 2);
405+
}
406+
407+
374408
// CHECK-LABEL: @_Z8inc_byte
375409
// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr<!s8i>, {{.*}} : !s8i, seq_cst) fetch_first : !s8i
376410

@@ -380,6 +414,14 @@ void inc_byte(char* a, char b) {
380414
char c = __sync_fetch_and_add(a, b);
381415
}
382416

417+
// CHECK-LABEL: @_Z8sub_byte
418+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!s8i>, {{.*}} : !s8i, seq_cst) fetch_first : !s8i
419+
420+
// LLVM-LABEL: @_Z8sub_byte
421+
// LLVM: atomicrmw sub ptr {{.*}}, i8 {{.*}} seq_cst, align 1
422+
void sub_byte(char* a, char b) {
423+
char c = __sync_fetch_and_sub(a, b);
424+
}
383425

384426
// CHECK-LABEL: @_Z12cmp_bool_int
385427
// CHECK: %[[PTR:.*]] = cir.load {{.*}} : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
@@ -481,6 +523,15 @@ void inc_uint(unsigned int* a, int b) {
481523
unsigned int c = __sync_fetch_and_add(a, b);
482524
}
483525

526+
// CHECK-LABEL: @_Z8sub_uint
527+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!u32i>, {{.*}} : !u32i, seq_cst) fetch_first : !u32i
528+
529+
// LLVM-LABEL: @_Z8sub_uint
530+
// LLVM: atomicrmw sub ptr {{.*}}, i32 {{.*}} seq_cst, align 4
531+
void sub_uint(unsigned int* a, int b) {
532+
unsigned int c = __sync_fetch_and_sub(a, b);
533+
}
534+
484535
// CHECK-LABEL: @_Z9inc_ulong
485536
// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr<!u64i>, {{.*}} : !u64i, seq_cst) fetch_first : !u64i
486537

@@ -490,11 +541,30 @@ void inc_ulong(unsigned long* a, long b) {
490541
unsigned long c = __sync_fetch_and_add(a, b);
491542
}
492543

544+
// CHECK-LABEL: @_Z9sub_ulong
545+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!u64i>, {{.*}} : !u64i, seq_cst) fetch_first : !u64i
546+
547+
// LLVM-LABEL: @_Z9sub_ulong
548+
// LLVM: atomicrmw sub ptr {{.*}}, i64 {{.*}} seq_cst, align 8
549+
void sub_ulong(unsigned long* a, long b) {
550+
unsigned long c = __sync_fetch_and_sub(a, b);
551+
}
552+
553+
493554
// CHECK-LABEL: @_Z9inc_uchar
494555
// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr<!u8i>, {{.*}} : !u8i, seq_cst) fetch_first : !u8i
495556

496557
// LLVM-LABEL: @_Z9inc_uchar
497558
// LLVM: atomicrmw add ptr {{.*}}, i8 {{.*}} seq_cst, align 1
498559
void inc_uchar(unsigned char* a, char b) {
499560
unsigned char c = __sync_fetch_and_add(a, b);
500-
}
561+
}
562+
563+
// CHECK-LABEL: @_Z9sub_uchar
564+
// CHECK: cir.atomic.fetch(sub, {{.*}} : !cir.ptr<!u8i>, {{.*}} : !u8i, seq_cst) fetch_first : !u8i
565+
566+
// LLVM-LABEL: @_Z9sub_uchar
567+
// LLVM: atomicrmw sub ptr {{.*}}, i8 {{.*}} seq_cst, align 1
568+
void sub_uchar(unsigned char* a, char b) {
569+
unsigned char c = __sync_fetch_and_sub(a, b);
570+
}

0 commit comments

Comments
 (0)