-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[libc][CPP] correct cmpxchg failure ordering inference #133127
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
SchrodingerZhu
merged 5 commits into
llvm:main
from
SchrodingerZhu:fix-atomic-cmpxchg-ordering
Mar 26, 2025
Merged
[libc][CPP] correct cmpxchg failure ordering inference #133127
SchrodingerZhu
merged 5 commits into
llvm:main
from
SchrodingerZhu:fix-atomic-cmpxchg-ordering
Mar 26, 2025
Conversation
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
@llvm/pr-subscribers-libc Author: Schrodinger ZHU Yifan (SchrodingerZhu) ChangesFull diff: https://github.com/llvm/llvm-project/pull/133127.diff 1 Files Affected:
diff --git a/libc/src/__support/CPP/atomic.h b/libc/src/__support/CPP/atomic.h
index 15242a131c63b..3a30c7d33faea 100644
--- a/libc/src/__support/CPP/atomic.h
+++ b/libc/src/__support/CPP/atomic.h
@@ -62,6 +62,14 @@ template <typename T> struct Atomic {
return static_cast<int>(mem_scope);
}
+ LIBC_INLINE static constexpr int infer_failure_order(MemoryOrder mem_ord) {
+ if (mem_ord == MemoryOrder::RELEASE)
+ return order(MemoryOrder::RELAXED);
+ if (mem_ord == MemoryOrder::ACQ_REL)
+ return order(MemoryOrder::ACQUIRE);
+ return order(mem_ord);
+ }
+
LIBC_INLINE static T *addressof(T &ref) { return __builtin_addressof(ref); }
// Require types that are 1, 2, 4, 8, or 16 bytes in length to be aligned to
@@ -129,7 +137,7 @@ template <typename T> struct Atomic {
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
return __atomic_compare_exchange(addressof(val), addressof(expected),
addressof(desired), false, order(mem_ord),
- order(mem_ord));
+ infer_failure_order(mem_ord));
}
// Atomic compare exchange (separate success and failure memory orders)
@@ -148,7 +156,7 @@ template <typename T> struct Atomic {
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
return __atomic_compare_exchange(addressof(val), addressof(expected),
addressof(desired), true, order(mem_ord),
- order(mem_ord));
+ infer_failure_order(mem_ord));
}
// Atomic compare exchange (weak version with separate success and failure
@@ -252,6 +260,14 @@ template <typename T> struct AtomicRef {
return static_cast<int>(mem_scope);
}
+ LIBC_INLINE static constexpr int infer_failure_order(MemoryOrder mem_ord) {
+ if (mem_ord == MemoryOrder::RELEASE)
+ return order(MemoryOrder::RELAXED);
+ if (mem_ord == MemoryOrder::ACQ_REL)
+ return order(MemoryOrder::ACQUIRE);
+ return order(mem_ord);
+ }
+
public:
// Constructor from T reference
LIBC_INLINE explicit constexpr AtomicRef(T &obj) : ptr(&obj) {}
@@ -298,7 +314,8 @@ template <typename T> struct AtomicRef {
T &expected, T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) const {
return __atomic_compare_exchange(ptr, &expected, &desired, false,
- order(mem_ord), order(mem_ord));
+ order(mem_ord),
+ infer_failure_order(mem_ord));
}
// Atomic compare exchange (strong, separate success/failure memory orders)
|
jhuber6
reviewed
Mar 26, 2025
jhuber6
reviewed
Mar 26, 2025
jhuber6
approved these changes
Mar 26, 2025
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.
See https://en.cppreference.com/w/cpp/atomic/atomic/compare_exchange. The failure order should be inferred from the success order if it is not explicitly specified.