[CIR] Implement __sync_synchronize builtin#200423
Merged
Merged
Conversation
This showed up on a spec test, but is a very simple system-sequentially consistent fence instruction.
|
@llvm/pr-subscribers-clang @llvm/pr-subscribers-clangir Author: Erich Keane (erichkeane) ChangesThis 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:
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
+}
|
andykaylor
approved these changes
May 29, 2026
Contributor
andykaylor
left a comment
There was a problem hiding this comment.
lgtm, with a nit about moving the case
| emitAtomicFenceOp(*this, e, cir::SyncScopeKind::SingleThread); | ||
| return RValue::get(nullptr); | ||
| } | ||
| case Builtin::BI__sync_synchronize: { |
Contributor
There was a problem hiding this comment.
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?
| return RValue::get(nullptr); | ||
| } | ||
| case Builtin::BI__sync_synchronize: { | ||
| cir::AtomicFenceOp::create( |
Contributor
There was a problem hiding this comment.
You didn't think it's worth copying the comment from classic codegen about how badly designed this intrinsic is?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This showed up on a spec test, but is a very simple system-sequentially consistent fence instruction.