[clang] [CIR][CIRGen] Add support for `__sync_val/bool_compare_and_swap` builtins (PR #186529)

Andy Kaylor via cfe-commits cfe-commits at lists.llvm.org
Tue Mar 17 15:15:40 PDT 2026


================
@@ -172,6 +172,37 @@ static mlir::Value makeBinaryAtomicValue(
   return rmwi->getResult(0);
 }
 
+/// Utility to insert an atomic cmpxchg instruction.
+static mlir::Value makeAtomicCmpXchgValue(CIRGenFunction &cgf,
+                                          const CallExpr *expr,
+                                          bool returnBool) {
+  QualType typ = returnBool ? expr->getArg(1)->getType() : expr->getType();
+  Address destAddr = checkAtomicAlignment(cgf, expr);
+  clang::CIRGen::CIRGenBuilderTy &builder = cgf.getBuilder();
+
+  cir::IntType intType =
+      expr->getArg(0)->getType()->getPointeeType()->isUnsignedIntegerType()
+          ? builder.getUIntNTy(cgf.getContext().getTypeSize(typ))
+          : builder.getSIntNTy(cgf.getContext().getTypeSize(typ));
+  mlir::Value cmpVal = cgf.emitScalarExpr(expr->getArg(1));
+  cmpVal = emitToInt(cgf, cmpVal, typ, intType);
----------------
andykaylor wrote:

Similar to my comment above, I don't understand the need for `emitToInt` here. Can you explain? It looks like whoever wrote the classic codegen implementation of this was trying to coerce everything to the same type, but I'm pretty sure the AST builder will have already done that.

https://github.com/llvm/llvm-project/pull/186529


More information about the cfe-commits mailing list