https://github.com/dingcyrus updated https://github.com/llvm/llvm-project/pull/214606
>From ff69b486ccc62971deae082299f70b71ee944d03 Mon Sep 17 00:00:00 2001 From: Cyrus Ding <[email protected]> Date: Fri, 7 Aug 2026 09:53:16 +0800 Subject: [PATCH] [CIR] Implement missing __sync_* atomic builtins Add CIR codegen support for five legacy __sync_* builtins that previously emitted "Not Yet Implemented" errors: __sync_val_compare_and_swap -> cir.atomic.cmpxchg (seq_cst) __sync_bool_compare_and_swap -> cir.atomic.cmpxchg + cmp (seq_cst) __sync_swap -> cir.atomic.xchg (seq_cst) __sync_lock_test_and_set -> cir.atomic.xchg (acquire) __sync_lock_release -> cir.store 0 (release) Both unsuffixed and size-suffixed forms (_1, _2, _4, _8, _16) are handled, covering 30 builtin variants in total. The existing emitBinaryAtomic / emitBinaryAtomicPost infrastructure already handled the arithmetic __sync_* family (fetch_and_add, etc.). This patch adds the remaining compare-and-swap, exchange, and lock-release helpers: emitAtomicCmpXchgValue, emitAtomicCmpXchgBool, emitAtomicXchg, and emitAtomicLockRelease. Also fix a cosmetic issue in errorBuiltinNYI: the diagnostic hardcoded "X86" in the message even for target-independent builtins such as __sync_* compiled with --target=aarch64. Replace "unimplemented X86 builtin call" with "unimplemented builtin call". Fixes #214445. --- clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 91 ++++++++++++++++++- .../CodeGenBuiltins/builtin-undef-rvalue.cpp | 2 +- 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp index afb13572df5bf..32187d79f2d90 100644 --- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp @@ -247,6 +247,73 @@ static RValue emitBinaryAtomicPost(CIRGenFunction &cgf, return RValue::get(result); } +/// Emit a `cir.atomic.cmpxchg` for __sync_val_compare_and_swap_N. +/// Returns the old value (type matches the pointee of ptr). +static mlir::Value emitAtomicCmpXchgValue(CIRGenFunction &cgf, + const CallExpr *e, + cir::MemOrder succOrder, + cir::MemOrder failOrder) { + Address destAddr = checkAtomicAlignment(cgf, e); + CIRGenBuilderTy &builder = cgf.getBuilder(); + mlir::Value destValue = destAddr.emitRawPointer(); + mlir::Value expected = cgf.emitScalarExpr(e->getArg(1)); + mlir::Value desired = cgf.emitScalarExpr(e->getArg(2)); + + auto cmpxchg = cir::AtomicCmpXchgOp::create( + builder, cgf.getLoc(e->getSourceRange()), destValue, expected, desired, + succOrder, failOrder, cir::SyncScopeKind::System, + /*alignment=*/nullptr, /*weak=*/false, /*is_volatile=*/false); + return cmpxchg.getOld(); +} + +/// Emit a `cir.atomic.cmpxchg` for __sync_bool_compare_and_swap_N. +/// Returns a boolean: true if the exchange succeeded (old == expected). +static RValue emitAtomicCmpXchgBool(CIRGenFunction &cgf, const CallExpr *e) { + mlir::Value old = + emitAtomicCmpXchgValue(cgf, e, cir::MemOrder::SequentiallyConsistent, + cir::MemOrder::SequentiallyConsistent); + CIRGenBuilderTy &builder = cgf.getBuilder(); + // __sync_bool_compare_and_swap returns true on success. + // The classic llvm/cmpxchg produces {old, success}. We only have old here, + // so compare old == expected. + mlir::Value expected = cgf.emitScalarExpr(e->getArg(1)); + mlir::Value cmp = builder.createCompare(cgf.getLoc(e->getSourceRange()), + cir::CmpOpKind::eq, old, expected); + mlir::Value result = + builder.createIntCast(cmp, cgf.convertType(e->getType())); + return RValue::get(result); +} + +/// Emit a `cir.atomic.xchg` for __sync_swap_N and __sync_lock_test_and_set_N. +static mlir::Value emitAtomicXchg(CIRGenFunction &cgf, const CallExpr *e, + cir::MemOrder ordering) { + Address destAddr = checkAtomicAlignment(cgf, e); + CIRGenBuilderTy &builder = cgf.getBuilder(); + mlir::Value destValue = destAddr.emitRawPointer(); + mlir::Value val = cgf.emitScalarExpr(e->getArg(1)); + + auto xchg = cir::AtomicXchgOp::create( + builder, cgf.getLoc(e->getSourceRange()), destValue, val, ordering, + cir::SyncScopeKind::System, /*is_volatile=*/false); + return xchg.getResult(); +} + +/// Emit a release store of 0 for __sync_lock_release_N. +static void emitAtomicLockRelease(CIRGenFunction &cgf, const CallExpr *e) { + CIRGenBuilderTy &builder = cgf.getBuilder(); + Address destAddr = checkAtomicAlignment(cgf, e); + mlir::Location loc = cgf.getLoc(e->getSourceRange()); + mlir::Type pointeeTy = destAddr.getType(); + mlir::Value zero = builder.getConstant(loc, cir::IntAttr::get(pointeeTy, 0)); + auto orderAttr = + cir::MemOrderAttr::get(&cgf.getMLIRContext(), cir::MemOrder::Release); + auto scopeAttr = cir::SyncScopeKindAttr::get(&cgf.getMLIRContext(), + cir::SyncScopeKind::System); + builder.createStore(loc, zero, destAddr, /*isVolatile=*/false, + /*isNontemporal=*/false, + /*align=*/mlir::IntegerAttr{}, scopeAttr, orderAttr); +} + static void emitAtomicFenceOp(CIRGenFunction &cgf, const CallExpr *expr, cir::SyncScopeKind syncScope) { CIRGenBuilderTy &builder = cgf.getBuilder(); @@ -432,11 +499,11 @@ static RValue errorBuiltinNYI(CIRGenFunction &cgf, const CallExpr *e, if (cgf.getContext().BuiltinInfo.isLibFunction(builtinID)) { cgf.cgm.errorNYI( e->getSourceRange(), - std::string("unimplemented X86 library function builtin call: ") + + std::string("unimplemented library function builtin call: ") + cgf.getContext().BuiltinInfo.getName(builtinID)); } else { cgf.cgm.errorNYI(e->getSourceRange(), - std::string("unimplemented X86 builtin call: ") + + std::string("unimplemented builtin call: ") + cgf.getContext().BuiltinInfo.getName(builtinID)); } @@ -2048,11 +2115,19 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BI__sync_xor_and_fetch: case Builtin::BI__sync_nand_and_fetch: case Builtin::BI__sync_val_compare_and_swap: + return RValue::get( + emitAtomicCmpXchgValue(*this, e, cir::MemOrder::SequentiallyConsistent, + cir::MemOrder::SequentiallyConsistent)); case Builtin::BI__sync_bool_compare_and_swap: + return emitAtomicCmpXchgBool(*this, e); case Builtin::BI__sync_lock_test_and_set: + return RValue::get(emitAtomicXchg(*this, e, cir::MemOrder::Acquire)); case Builtin::BI__sync_lock_release: + emitAtomicLockRelease(*this, e); + return RValue::get(nullptr); case Builtin::BI__sync_swap: - return errorBuiltinNYI(*this, e, builtinID); + return RValue::get( + emitAtomicXchg(*this, e, cir::MemOrder::SequentiallyConsistent)); case Builtin::BI__sync_fetch_and_add_1: case Builtin::BI__sync_fetch_and_add_2: case Builtin::BI__sync_fetch_and_add_4: @@ -2141,27 +2216,35 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID, case Builtin::BI__sync_val_compare_and_swap_4: case Builtin::BI__sync_val_compare_and_swap_8: case Builtin::BI__sync_val_compare_and_swap_16: + return RValue::get( + emitAtomicCmpXchgValue(*this, e, cir::MemOrder::SequentiallyConsistent, + cir::MemOrder::SequentiallyConsistent)); case Builtin::BI__sync_bool_compare_and_swap_1: case Builtin::BI__sync_bool_compare_and_swap_2: case Builtin::BI__sync_bool_compare_and_swap_4: case Builtin::BI__sync_bool_compare_and_swap_8: case Builtin::BI__sync_bool_compare_and_swap_16: + return emitAtomicCmpXchgBool(*this, e); case Builtin::BI__sync_swap_1: case Builtin::BI__sync_swap_2: case Builtin::BI__sync_swap_4: case Builtin::BI__sync_swap_8: case Builtin::BI__sync_swap_16: + return RValue::get( + emitAtomicXchg(*this, e, cir::MemOrder::SequentiallyConsistent)); case Builtin::BI__sync_lock_test_and_set_1: case Builtin::BI__sync_lock_test_and_set_2: case Builtin::BI__sync_lock_test_and_set_4: case Builtin::BI__sync_lock_test_and_set_8: case Builtin::BI__sync_lock_test_and_set_16: + return RValue::get(emitAtomicXchg(*this, e, cir::MemOrder::Acquire)); case Builtin::BI__sync_lock_release_1: case Builtin::BI__sync_lock_release_2: case Builtin::BI__sync_lock_release_4: case Builtin::BI__sync_lock_release_8: case Builtin::BI__sync_lock_release_16: - return errorBuiltinNYI(*this, e, builtinID); + emitAtomicLockRelease(*this, e); + return RValue::get(nullptr); case Builtin::BI__sync_synchronize: { // We assume this is supposed to correspond to a C++0x-style // sequentially-consistent fence (i.e. this is only usable for diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-undef-rvalue.cpp b/clang/test/CIR/CodeGenBuiltins/builtin-undef-rvalue.cpp index 335383c075dac..dac0ddf366334 100644 --- a/clang/test/CIR/CodeGenBuiltins/builtin-undef-rvalue.cpp +++ b/clang/test/CIR/CodeGenBuiltins/builtin-undef-rvalue.cpp @@ -6,7 +6,7 @@ typedef int v4si __attribute__((vector_size(16))); int test_builtin_reduce_add_undef_rvalue(v4si x) { - // expected-error@+1 {{unimplemented X86 builtin call: __builtin_reduce_add}} + // expected-error@+1 {{unimplemented builtin call: __builtin_reduce_add}} return __builtin_reduce_add(x); } _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
