https://github.com/erichkeane updated 
https://github.com/llvm/llvm-project/pull/200423

>From 45eb8185279d8b411d69f4b38b70634709dd0efd Mon Sep 17 00:00:00 2001
From: erichkeane <[email protected]>
Date: Fri, 29 May 2026 07:38:34 -0700
Subject: [PATCH 1/2] [CIR] Implement __sync_synchronize builtin

This showed up on a spec test, but is a very simple system-sequentially
consistent fence instruction.
---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp                  | 9 ++++++++-
 .../test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c  | 9 +++++++++
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 clang/test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index afa7e5b91251b..bfe29a3928961 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -2089,7 +2089,6 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   case Builtin::BI__sync_lock_release_4:
   case Builtin::BI__sync_lock_release_8:
   case Builtin::BI__sync_lock_release_16:
-  case Builtin::BI__sync_synchronize:
   case Builtin::BI__builtin_nontemporal_load:
   case Builtin::BI__builtin_nontemporal_store:
   case Builtin::BI__c11_atomic_is_lock_free:
@@ -2107,6 +2106,14 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
     emitAtomicFenceOp(*this, e, cir::SyncScopeKind::SingleThread);
     return RValue::get(nullptr);
   }
+  case Builtin::BI__sync_synchronize: {
+    cir::AtomicFenceOp::create(
+        builder, getLoc(e->getSourceRange()),
+        cir::MemOrder::SequentiallyConsistent,
+        cir::SyncScopeKindAttr::get(&getMLIRContext(),
+                                    cir::SyncScopeKind::System));
+    return RValue::get(nullptr);
+  }
   case Builtin::BI__scoped_atomic_thread_fence:
     return errorBuiltinNYI(*this, e, builtinID);
   case Builtin::BI__builtin_signbit:
diff --git a/clang/test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c 
b/clang/test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c
new file mode 100644
index 0000000000000..e4047d44124a4
--- /dev/null
+++ b/clang/test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-cir %s -o - | 
FileCheck %s -check-prefix=CIR
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm %s -o 
- | FileCheck %s -check-prefix=LLVM
+// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | 
FileCheck %s -check-prefix=LLVM
+
+void use() {
+  __sync_synchronize();
+  // CIR: cir.atomic.fence syncscope(system) seq_cst
+  // LLVM: fence seq_cst
+}

>From ead1a81149dac58516827324575cb44109746bd7 Mon Sep 17 00:00:00 2001
From: erichkeane <[email protected]>
Date: Fri, 29 May 2026 09:58:23 -0700
Subject: [PATCH 2/2] Fixup review comments

---
 clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp 
b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
index bfe29a3928961..1bb3c729e6279 100644
--- a/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
+++ b/clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp
@@ -2089,6 +2089,22 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
   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);
+  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
+    // synchronization, not device I/O or anything like that). This intrinsic
+    // is really badly designed in the sense that in theory, there isn't
+    // any way to safely use it... but in practice, it mostly works
+    // to use it with non-atomic loads and stores to get acquire/release
+    // semantics.
+    cir::AtomicFenceOp::create(
+        builder, getLoc(e->getSourceRange()),
+        cir::MemOrder::SequentiallyConsistent,
+        cir::SyncScopeKindAttr::get(&getMLIRContext(),
+                                    cir::SyncScopeKind::System));
+    return RValue::get(nullptr);
+  }
   case Builtin::BI__builtin_nontemporal_load:
   case Builtin::BI__builtin_nontemporal_store:
   case Builtin::BI__c11_atomic_is_lock_free:
@@ -2106,14 +2122,6 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl 
&gd, unsigned builtinID,
     emitAtomicFenceOp(*this, e, cir::SyncScopeKind::SingleThread);
     return RValue::get(nullptr);
   }
-  case Builtin::BI__sync_synchronize: {
-    cir::AtomicFenceOp::create(
-        builder, getLoc(e->getSourceRange()),
-        cir::MemOrder::SequentiallyConsistent,
-        cir::SyncScopeKindAttr::get(&getMLIRContext(),
-                                    cir::SyncScopeKind::System));
-    return RValue::get(nullptr);
-  }
   case Builtin::BI__scoped_atomic_thread_fence:
     return errorBuiltinNYI(*this, e, builtinID);
   case Builtin::BI__builtin_signbit:

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to