Skip to content

[CIR] Implement __sync_synchronize builtin#200423

Merged
erichkeane merged 3 commits into
llvm:mainfrom
erichkeane:sync-sync
May 29, 2026
Merged

[CIR] Implement __sync_synchronize builtin#200423
erichkeane merged 3 commits into
llvm:mainfrom
erichkeane:sync-sync

Conversation

@erichkeane
Copy link
Copy Markdown
Contributor

This showed up on a spec test, but is a very simple system-sequentially consistent fence instruction.

This showed up on a spec test, but is a very simple system-sequentially
consistent fence instruction.
@llvmorg-github-actions llvmorg-github-actions Bot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels May 29, 2026
@llvmorg-github-actions
Copy link
Copy Markdown

llvmorg-github-actions Bot commented May 29, 2026

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clangir

Author: Erich Keane (erichkeane)

Changes

This showed up on a spec test, but is a very simple system-sequentially consistent fence instruction.


Full diff: https://github.com/llvm/llvm-project/pull/200423.diff

2 Files Affected:

  • (modified) clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp (+8-1)
  • (added) clang/test/CIR/CodeGenBuiltins/builtin-sync_synchronize.c (+9)
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
+}

Copy link
Copy Markdown
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, with a nit about moving the case

Comment thread clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp Outdated
emitAtomicFenceOp(*this, e, cir::SyncScopeKind::SingleThread);
return RValue::get(nullptr);
}
case Builtin::BI__sync_synchronize: {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We've been trying to keep these in the same order that they have in classic codegen. Can you leave it where it was and just introduce a new errorNYI call above it?

Comment thread clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp Outdated
return RValue::get(nullptr);
}
case Builtin::BI__sync_synchronize: {
cir::AtomicFenceOp::create(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You didn't think it's worth copying the comment from classic codegen about how badly designed this intrinsic is?

@erichkeane erichkeane merged commit bd5c724 into llvm:main May 29, 2026
9 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants