diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index 103252ac297d..c24888362865 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -240,7 +240,10 @@ makeBinaryAtomicValue(CIRGenFunction &cgf, mlir::cir::AtomicFetchKind kind, Address destAddr = checkAtomicAlignment(cgf, expr); auto &builder = cgf.getBuilder(); - auto intType = builder.getSIntNTy(cgf.getContext().getTypeSize(typ)); + auto intType = + expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType() + ? builder.getUIntNTy(cgf.getContext().getTypeSize(typ)) + : builder.getSIntNTy(cgf.getContext().getTypeSize(typ)); mlir::Value val = cgf.buildScalarExpr(expr->getArg(1)); mlir::Type valueType = val.getType(); val = buildToInt(cgf, val, typ, intType); diff --git a/clang/test/CIR/CodeGen/atomic.cpp b/clang/test/CIR/CodeGen/atomic.cpp index 026aee84fa2f..43ba4e12f7ed 100644 --- a/clang/test/CIR/CodeGen/atomic.cpp +++ b/clang/test/CIR/CodeGen/atomic.cpp @@ -471,3 +471,30 @@ void cmp_val_short(short* p, short x, short u) { void cmp_val_byte(char* p, char x, char u) { char r = __sync_val_compare_and_swap(p, x, u); } + +// CHECK-LABEL: @_Z8inc_uint +// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr, {{.*}} : !u32i, seq_cst) fetch_first : !u32i + +// LLVM-LABEL: @_Z8inc_uint +// LLVM: atomicrmw add ptr {{.*}}, i32 {{.*}} seq_cst, align 4 +void inc_uint(unsigned int* a, int b) { + unsigned int c = __sync_fetch_and_add(a, b); +} + +// CHECK-LABEL: @_Z9inc_ulong +// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr, {{.*}} : !u64i, seq_cst) fetch_first : !u64i + +// LLVM-LABEL: @_Z9inc_ulong +// LLVM: atomicrmw add ptr {{.*}}, i64 {{.*}} seq_cst, align 8 +void inc_ulong(unsigned long* a, long b) { + unsigned long c = __sync_fetch_and_add(a, b); +} + +// CHECK-LABEL: @_Z9inc_uchar +// CHECK: cir.atomic.fetch(add, {{.*}} : !cir.ptr, {{.*}} : !u8i, seq_cst) fetch_first : !u8i + +// LLVM-LABEL: @_Z9inc_uchar +// LLVM: atomicrmw add ptr {{.*}}, i8 {{.*}} seq_cst, align 1 +void inc_uchar(unsigned char* a, char b) { + unsigned char c = __sync_fetch_and_add(a, b); +} \ No newline at end of file diff --git a/clang/test/CodeGen/atomic.c b/clang/test/CodeGen/atomic.c index 16c29e282ddd..48e3c3304816 100644 --- a/clang/test/CodeGen/atomic.c +++ b/clang/test/CodeGen/atomic.c @@ -160,4 +160,4 @@ void force_global_uses(void) { // X86: call void @__atomic_load(i32 noundef 16, ptr noundef @glob_longdbl, ptr noundef %atomic-temp // X86-NEXT: %0 = load x86_fp80, ptr %atomic-temp, align 16 // SYSTEMZ: load atomic fp128, ptr @[[GLOB_LONGDBL]] seq_cst -} +} \ No newline at end of file