[libcxx-commits] [libcxx] [libc++][atomic_ref] Use __atomic_fetch_{add, sub} builtins on floating-points whenever possible (PR #135685)
Damien L-G via libcxx-commits
libcxx-commits at lists.llvm.org
Mon Apr 14 14:33:04 PDT 2025
https://github.com/dalg24 created https://github.com/llvm/llvm-project/pull/135685
Fix #135109
Clang is able to emit an `atomicrmw` instruction from the `__atomic_fetch_add` and `__atomic_fetch_sub` builtins on floating-point types.
>From d8d9b5a53e438a27a8d3aefb681dd96e2d0b8232 Mon Sep 17 00:00:00 2001
From: Damien L-G <dalg24 at gmail.com>
Date: Mon, 14 Apr 2025 17:21:34 -0400
Subject: [PATCH] [libc++][atomic_ref] Use __atomic_fetch_{add,sub} builtins on
floating-points whenever possible
Fix #135109
Clang is able to emit an `atomicrmw` instruction from the
`__atomic_fetch_add` and `__atomic_fetch_sub` builtins on floating-point
types.
---
libcxx/include/__atomic/atomic_ref.h | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/libcxx/include/__atomic/atomic_ref.h b/libcxx/include/__atomic/atomic_ref.h
index b5493662c518e..baffd1ffb6094 100644
--- a/libcxx/include/__atomic/atomic_ref.h
+++ b/libcxx/include/__atomic/atomic_ref.h
@@ -322,20 +322,28 @@ struct atomic_ref<_Tp> : public __atomic_ref_base<_Tp> {
atomic_ref& operator=(const atomic_ref&) = delete;
_LIBCPP_HIDE_FROM_ABI _Tp fetch_add(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+# ifdef _LIBCPP_COMPILER_CLANG_BASED
+ return __atomic_fetch_add(this->__ptr_, __arg, std::__to_gcc_order(__order));
+# else
_Tp __old = this->load(memory_order_relaxed);
_Tp __new = __old + __arg;
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
__new = __old + __arg;
}
return __old;
+# endif
}
_LIBCPP_HIDE_FROM_ABI _Tp fetch_sub(_Tp __arg, memory_order __order = memory_order_seq_cst) const noexcept {
+# ifdef _LIBCPP_COMPILER_CLANG_BASED
+ return __atomic_fetch_sub(this->__ptr_, __arg, std::__to_gcc_order(__order));
+# else
_Tp __old = this->load(memory_order_relaxed);
_Tp __new = __old - __arg;
while (!this->compare_exchange_weak(__old, __new, __order, memory_order_relaxed)) {
__new = __old - __arg;
}
return __old;
+# endif
}
_LIBCPP_HIDE_FROM_ABI _Tp operator+=(_Tp __arg) const noexcept { return fetch_add(__arg) + __arg; }
More information about the libcxx-commits
mailing list