Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[libc++][atomic_ref] Use __atomic_fetch_{add,sub} builtins on floating-points whenever possible #135685

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

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

dalg24
Copy link
Member

@dalg24 dalg24 commented Apr 14, 2025

Fix #135109

Clang is able to emit an atomicrmw instruction from the __atomic_fetch_add and __atomic_fetch_sub builtins on floating-point types.

…g-points whenever possible

Fix llvm#135109

Clang is able to emit an `atomicrmw` instruction from the
`__atomic_fetch_add` and `__atomic_fetch_sub` builtins on floating-point
types.
@dalg24 dalg24 requested a review from huixie90 April 14, 2025 21:35
@@ -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
Copy link

@kc9jud kc9jud Apr 15, 2025

Choose a reason for hiding this comment

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

Does this need this fp80 workaround for #68602?

_LIBCPP_HIDE_FROM_ABI static constexpr bool __has_rmw_builtin() {
# ifndef _LIBCPP_COMPILER_CLANG_BASED
return false;
# else
// The builtin __cxx_atomic_fetch_add errors during compilation for
// long double on platforms with fp80 format.
// For more details, see
// lib/Sema/SemaChecking.cpp function IsAllowedValueType
// LLVM Parser does not allow atomicrmw with x86_fp80 type.
// if (ValType->isSpecificBuiltinType(BuiltinType::LongDouble) &&
// &Context.getTargetInfo().getLongDoubleFormat() ==
// &llvm::APFloat::x87DoubleExtended())
// For more info
// https://github.com/llvm/llvm-project/issues/68602
// https://reviews.llvm.org/D53965
return !__is_fp80_long_double();
# endif
}

Copy link
Contributor

@huixie90 huixie90 left a comment

Choose a reason for hiding this comment

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

I would make atomic_ref and atomic share the similar logic (or just share the implantation) for these rmw operations. The reason why atomic has special branch for fp80 long double is that , the clang builtin that emit the llvm ir, has serious bug that affects all types whose data size is not power of 2. ( I think I have a patch but I never got a chance to finalise the tests. )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[libc++] atomic_ref<float>::fetch_add has CAS loop rather than atomicrmw
3 participants