-
Notifications
You must be signed in to change notification settings - Fork 14.9k
[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
[libc++][atomic_ref] Use __atomic_fetch_{add,sub} builtins on floating-points whenever possible #135685
Changes from 7 commits
d8d9b5a
246d885
f8de9b0
c3453b6
e3879b3
b0d63bd
ed5f402
1fbf2ab
8cca6e5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef _LIBCPP___ATOMIC_FLOATING_POINT_HELPER_H | ||
#define _LIBCPP___ATOMIC_FLOATING_POINT_HELPER_H | ||
|
||
#include <__config> | ||
#include <__type_traits/is_floating_point.h> | ||
#include <__type_traits/is_same.h> | ||
|
||
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) | ||
# pragma GCC system_header | ||
#endif | ||
|
||
_LIBCPP_BEGIN_NAMESPACE_STD | ||
|
||
#if _LIBCPP_STD_VER >= 20 | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr bool __is_fp80_long_double() { | ||
// Only x87-fp80 long double has 64-bit mantissa | ||
return __LDBL_MANT_DIG__ == 64 && std::is_same_v<_Tp, long double>; | ||
} | ||
|
||
template <class _Tp> | ||
_LIBCPP_HIDE_FROM_ABI constexpr bool __has_rmw_builtin() { | ||
static_assert(std::is_floating_point_v<_Tp>); | ||
# 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 !std::__is_fp80_long_double<_Tp>(); | ||
# endif | ||
} | ||
|
||
#endif | ||
|
||
_LIBCPP_END_NAMESPACE_STD | ||
|
||
#endif // _LIBCPP___ATOMIC_FLOATING_POINT_HELPER_H |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -7,6 +7,7 @@ | |||||||||
|
||||||||||
// UNSUPPORTED: c++03, c++11, c++14, c++17 | ||||||||||
// XFAIL: !has-64-bit-atomics | ||||||||||
// XFAIL: target={{x86_64-.*}} && tsan | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Copied from llvm-project/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp Lines 15 to 16 in 08d747c
Note that the #72893 issue referenced in the comment seems to indicate that only llvm-project/libcxx/test/std/atomics/atomics.types.generic/atomics.types.float/fetch_add.pass.cpp Lines 119 to 120 in 08d747c
Per a conversation on Discord with @huixie90, I propose to not hold up this PR and just use the same |
||||||||||
|
||||||||||
// integral-type fetch_add(integral-type, memory_order = memory_order::seq_cst) const noexcept; | ||||||||||
// floating-point-type fetch_add(floating-point-type, memory_order = memory_order::seq_cst) const noexcept; | ||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is not an issue but just a note to myself and others who might be confused in the future.
In
std::atomic
, we have been using the "generic" API that abstracts the platform dependent code is__cxx_atomic_fetch_add
, where on clang it calls__c11_atomic_fetch_add
, and__atomic_fetch_add
on gcc.However, the abstraction layer takes a
__cxx_atomic_base_impl
, which does not exist inatomic_ref
. So, instead of introducing a new abstraction layer that works foratomic_ref
, we decided early on that we only use the gcc builtin__atomic_fetch_add
.clang has the gcc builtin as well
llvm-project/clang/include/clang/Basic/Builtins.td
Line 1999 in b3a1c77
and its implementation is the same as the
__c11_atomic_fetch_add
llvm-project/clang/lib/CodeGen/CGAtomic.cpp
Line 634 in b3a1c77
So it is fine