Skip to content

Commit 7711853

Browse files
authored
[libc] Remove obsolete LIBC_HAS_BUILTIN macro (#86554)
Fixes #86546 and removes the macro `LIBC_HAS_BUILTIN`. This was necessary to support older compilers that did not support `__has_builtin`. All of the compilers we support already have this builtin. See: https://libc.llvm.org/compiler_support.html All uses now use `__has_builtin` directly cc @nickdesaulniers
1 parent 78f0871 commit 7711853

26 files changed

+77
-107
lines changed

libc/docs/dev/code_style.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ We define two kinds of macros:
5555
* ``src/__support/macros/config.h`` - Important compiler and platform
5656
features. Such macros can be used to produce portable code by
5757
parameterizing compilation based on the presence or lack of a given
58-
feature. e.g., ``LIBC_HAS_BUILTIN``
58+
feature. e.g., ``LIBC_HAS_FEATURE``
5959
* ``src/__support/macros/attributes.h`` - Attributes for functions, types,
6060
and variables. e.g., ``LIBC_UNUSED``
6161
* ``src/__support/macros/optimization.h`` - Portable macros for performance

libc/src/__support/CPP/CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ add_header_library(
1818
.limits
1919
.type_traits
2020
libc.src.__support.macros.attributes
21-
libc.src.__support.macros.config
2221
libc.src.__support.macros.sanitizer
2322
)
2423

@@ -157,7 +156,6 @@ add_header_library(
157156
DEPENDS
158157
libc.include.llvm-libc-macros.stdfix_macros
159158
libc.src.__support.macros.attributes
160-
libc.src.__support.macros.config
161159
libc.src.__support.macros.properties.types
162160
)
163161

libc/src/__support/CPP/atomic.h

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,11 @@ template <typename T> struct Atomic {
7171

7272
T load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
7373
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
74-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_load_n))
75-
return __scoped_atomic_load_n(&val, int(mem_ord), (int)(mem_scope));
76-
else
77-
return __atomic_load_n(&val, int(mem_ord));
74+
#if __has_builtin(__scoped_atomic_load_n)
75+
return __scoped_atomic_load_n(&val, int(mem_ord), (int)(mem_scope));
76+
#else
77+
return __atomic_load_n(&val, int(mem_ord));
78+
#endif
7879
}
7980

8081
// Atomic store.
@@ -85,10 +86,11 @@ template <typename T> struct Atomic {
8586

8687
void store(T rhs, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
8788
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
88-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_store_n))
89-
__scoped_atomic_store_n(&val, rhs, int(mem_ord), (int)(mem_scope));
90-
else
91-
__atomic_store_n(&val, rhs, int(mem_ord));
89+
#if __has_builtin(__scoped_atomic_store_n)
90+
__scoped_atomic_store_n(&val, rhs, int(mem_ord), (int)(mem_scope));
91+
#else
92+
__atomic_store_n(&val, rhs, int(mem_ord));
93+
#endif
9294
}
9395

9496
// Atomic compare exchange
@@ -101,47 +103,51 @@ template <typename T> struct Atomic {
101103

102104
T exchange(T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
103105
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
104-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_exchange_n))
105-
return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
106-
(int)(mem_scope));
107-
else
108-
return __atomic_exchange_n(&val, desired, int(mem_ord));
106+
#if __has_builtin(__scoped_atomic_exchange_n)
107+
return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
108+
(int)(mem_scope));
109+
#else
110+
return __atomic_exchange_n(&val, desired, int(mem_ord));
111+
#endif
109112
}
110113

111114
T fetch_add(T increment, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
112115
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
113-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_add))
114-
return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
115-
(int)(mem_scope));
116-
else
117-
return __atomic_fetch_add(&val, increment, int(mem_ord));
116+
#if __has_builtin(__scoped_atomic_fetch_add)
117+
return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
118+
(int)(mem_scope));
119+
#else
120+
return __atomic_fetch_add(&val, increment, int(mem_ord));
121+
#endif
118122
}
119123

120124
T fetch_or(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
121125
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
122-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_or))
123-
return __scoped_atomic_fetch_or(&val, mask, int(mem_ord),
124-
(int)(mem_scope));
125-
else
126-
return __atomic_fetch_or(&val, mask, int(mem_ord));
126+
#if __has_builtin(__scoped_atomic_fetch_or)
127+
return __scoped_atomic_fetch_or(&val, mask, int(mem_ord), (int)(mem_scope));
128+
#else
129+
return __atomic_fetch_or(&val, mask, int(mem_ord));
130+
#endif
127131
}
128132

129133
T fetch_and(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
130134
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
131-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_and))
132-
return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
133-
(int)(mem_scope));
134-
else
135-
return __atomic_fetch_and(&val, mask, int(mem_ord));
135+
#if __has_builtin(__scoped_atomic_fetch_and)
136+
return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
137+
(int)(mem_scope));
138+
#else
139+
return __atomic_fetch_and(&val, mask, int(mem_ord));
140+
#endif
136141
}
137142

138143
T fetch_sub(T decrement, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
139144
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
140-
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_sub))
141-
return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
142-
(int)(mem_scope));
143-
else
144-
return __atomic_fetch_sub(&val, decrement, int(mem_ord));
145+
#if __has_builtin(__scoped_atomic_fetch_sub)
146+
return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
147+
(int)(mem_scope));
148+
#else
149+
return __atomic_fetch_sub(&val, decrement, int(mem_ord));
150+
#endif
145151
}
146152

147153
// Set the value without using an atomic operation. This is useful
@@ -166,7 +172,7 @@ LIBC_INLINE void atomic_thread_fence([[maybe_unused]] MemoryOrder mem_ord) {
166172
// except no instructions for memory ordering are issued. Only reordering of
167173
// the instructions by the compiler is suppressed as order instructs.
168174
LIBC_INLINE void atomic_signal_fence([[maybe_unused]] MemoryOrder mem_ord) {
169-
#if LIBC_HAS_BUILTIN(__atomic_signal_fence)
175+
#if __has_builtin(__atomic_signal_fence)
170176
__atomic_signal_fence(static_cast<int>(mem_ord));
171177
#else
172178
// if the builtin is not ready, use asm as a full compiler barrier.

libc/src/__support/CPP/bit.h

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,13 @@
1414
#include "src/__support/CPP/limits.h" // numeric_limits
1515
#include "src/__support/CPP/type_traits.h"
1616
#include "src/__support/macros/attributes.h"
17-
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
1817
#include "src/__support/macros/sanitizer.h"
1918

2019
#include <stdint.h>
2120

2221
namespace LIBC_NAMESPACE::cpp {
2322

24-
#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
23+
#if __has_builtin(__builtin_memcpy_inline)
2524
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
2625
#endif
2726

@@ -36,20 +35,20 @@ LIBC_INLINE constexpr cpp::enable_if_t<
3635
To>
3736
bit_cast(const From &from) {
3837
MSAN_UNPOISON(&from, sizeof(From));
39-
#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
38+
#if __has_builtin(__builtin_bit_cast)
4039
return __builtin_bit_cast(To, from);
4140
#else
4241
To to;
4342
char *dst = reinterpret_cast<char *>(&to);
4443
const char *src = reinterpret_cast<const char *>(&from);
45-
#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
44+
#if __has_builtin(__builtin_memcpy_inline)
4645
__builtin_memcpy_inline(dst, src, sizeof(To));
4746
#else
4847
for (unsigned i = 0; i < sizeof(To); ++i)
4948
dst[i] = src[i];
50-
#endif // LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
49+
#endif // __has_builtin(__builtin_memcpy_inline)
5150
return to;
52-
#endif // LIBC_HAS_BUILTIN(__builtin_bit_cast)
51+
#endif // __has_builtin(__builtin_bit_cast)
5352
}
5453

5554
template <typename T>
@@ -94,7 +93,7 @@ countr_zero(T value) {
9493
}
9594
return zero_bits;
9695
}
97-
#if LIBC_HAS_BUILTIN(__builtin_ctzs)
96+
#if __has_builtin(__builtin_ctzs)
9897
ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
9998
#endif
10099
ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
@@ -124,7 +123,7 @@ countl_zero(T value) {
124123
}
125124
return zero_bits;
126125
}
127-
#if LIBC_HAS_BUILTIN(__builtin_clzs)
126+
#if __has_builtin(__builtin_clzs)
128127
ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
129128
#endif
130129
ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)

libc/src/__support/CPP/type_traits/add_pointer.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
#include "src/__support/CPP/type_traits/remove_reference.h"
1212
#include "src/__support/CPP/type_traits/type_identity.h"
13-
#include "src/__support/macros/config.h"
1413

1514
namespace LIBC_NAMESPACE::cpp {
1615

libc/src/__support/CPP/type_traits/decay.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_DECAY_H
1010

1111
#include "src/__support/macros/attributes.h"
12-
#include "src/__support/macros/config.h"
1312

1413
#include "src/__support/CPP/type_traits/add_pointer.h"
1514
#include "src/__support/CPP/type_traits/conditional.h"

libc/src/__support/CPP/type_traits/is_destructible.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@
1616
#include "src/__support/CPP/type_traits/true_type.h"
1717
#include "src/__support/CPP/type_traits/type_identity.h"
1818
#include "src/__support/macros/attributes.h"
19-
#include "src/__support/macros/config.h"
2019

2120
namespace LIBC_NAMESPACE::cpp {
2221

2322
// is_destructible
24-
#if LIBC_HAS_BUILTIN(__is_destructible)
23+
#if __has_builtin(__is_destructible)
2524
template <typename T>
2625
struct is_destructible : bool_constant<__is_destructible(T)> {};
2726
#else

libc/src/__support/CPP/type_traits/is_function.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
#include "src/__support/CPP/type_traits/is_const.h"
1313
#include "src/__support/CPP/type_traits/is_reference.h"
1414
#include "src/__support/macros/attributes.h"
15-
#include "src/__support/macros/config.h"
1615

1716
namespace LIBC_NAMESPACE::cpp {
1817

1918
// is_function
20-
#if LIBC_HAS_BUILTIN(__is_function)
19+
#if __has_builtin(__is_function)
2120
template <typename T>
2221
struct is_function : integral_constant<bool, __is_function(T)> {};
2322
#else

libc/src/__support/CPP/type_traits/is_lvalue_reference.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
#include "src/__support/CPP/type_traits/false_type.h"
1313
#include "src/__support/CPP/type_traits/true_type.h"
1414
#include "src/__support/macros/attributes.h"
15-
#include "src/__support/macros/config.h"
1615

1716
namespace LIBC_NAMESPACE::cpp {
1817

1918
// is_lvalue_reference
20-
#if LIBC_HAS_BUILTIN(__is_lvalue_reference)
19+
#if __has_builtin(__is_lvalue_reference)
2120
template <typename T>
2221
struct is_lvalue_reference : bool_constant<__is_lvalue_reference(T)> {};
2322
#else

libc/src/__support/CPP/type_traits/is_reference.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
#include "src/__support/CPP/type_traits/false_type.h"
1313
#include "src/__support/CPP/type_traits/true_type.h"
1414
#include "src/__support/macros/attributes.h"
15-
#include "src/__support/macros/config.h"
1615

1716
namespace LIBC_NAMESPACE::cpp {
1817

1918
// is_reference
20-
#if LIBC_HAS_BUILTIN(__is_reference)
19+
#if __has_builtin(__is_reference)
2120
template <typename T> struct is_reference : bool_constant<__is_reference(T)> {};
2221
#else
2322
template <typename T> struct is_reference : public false_type {};

libc/src/__support/CPP/type_traits/is_rvalue_reference.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
#include "src/__support/CPP/type_traits/false_type.h"
1313
#include "src/__support/CPP/type_traits/true_type.h"
1414
#include "src/__support/macros/attributes.h"
15-
#include "src/__support/macros/config.h"
1615

1716
namespace LIBC_NAMESPACE::cpp {
1817

1918
// is_rvalue_reference
20-
#if LIBC_HAS_BUILTIN(__is_rvalue_reference)
19+
#if __has_builtin(__is_rvalue_reference)
2120
template <typename T>
2221
struct is_rvalue_reference : bool_constant<__is_rvalue_reference(T)> {};
2322
#else

libc/src/__support/CPP/type_traits/is_trivially_copyable.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_IS_TRIVIALLY_COPYABLE_H
1010

1111
#include "src/__support/CPP/type_traits/integral_constant.h"
12-
#include "src/__support/macros/config.h"
1312

1413
namespace LIBC_NAMESPACE::cpp {
1514

libc/src/__support/CPP/type_traits/is_trivially_destructible.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@
1111
#include "src/__support/CPP/type_traits/bool_constant.h"
1212
#include "src/__support/CPP/type_traits/is_destructible.h"
1313
#include "src/__support/macros/attributes.h"
14-
#include "src/__support/macros/config.h"
1514

1615
namespace LIBC_NAMESPACE::cpp {
1716

1817
// is_trivially_destructible
19-
#if LIBC_HAS_BUILTIN(__is_trivially_destructible)
18+
#if __has_builtin(__is_trivially_destructible)
2019
template <typename T>
2120
struct is_trivially_destructible
2221
: public bool_constant<__is_trivially_destructible(T)> {};
@@ -25,7 +24,7 @@ template <typename T>
2524
struct is_trivially_destructible
2625
: public bool_constant<cpp::is_destructible_v<T> &&__has_trivial_destructor(
2726
T)> {};
28-
#endif // LIBC_HAS_BUILTIN(__is_trivially_destructible)
27+
#endif // __has_builtin(__is_trivially_destructible)
2928
template <typename T>
3029
LIBC_INLINE_VAR constexpr bool is_trivially_destructible_v =
3130
is_trivially_destructible<T>::value;

libc/src/__support/CPP/type_traits/remove_all_extents.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,13 @@
99
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_REMOVE_ALL_EXTENTS_H
1010

1111
#include "src/__support/CPP/type_traits/type_identity.h"
12-
#include "src/__support/macros/config.h"
1312

1413
#include <stddef.h> // size_t
1514

1615
namespace LIBC_NAMESPACE::cpp {
1716

1817
// remove_all_extents
19-
#if LIBC_HAS_BUILTIN(__remove_all_extents)
18+
#if __has_builtin(__remove_all_extents)
2019
template <typename T> using remove_all_extents_t = __remove_all_extents(T);
2120
template <typename T>
2221
struct remove_all_extents : cpp::type_identity<remove_all_extents_t<T>> {};

libc/src/__support/FPUtil/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_header_library(
66
libc.include.fenv
77
libc.include.math
88
libc.src.__support.macros.attributes
9-
libc.src.__support.macros.config
109
libc.src.errno.errno
1110
)
1211

libc/src/__support/FPUtil/FEnvImpl.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
#include "include/llvm-libc-macros/math-macros.h"
1313
#include "src/__support/macros/attributes.h" // LIBC_INLINE
14-
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
1514
#include "src/__support/macros/properties/architectures.h"
1615
#include "src/errno/libc_errno.h"
1716
#include <fenv.h>

libc/src/__support/FPUtil/gpu/FMA.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GPU_FMA_H
1111

1212
#include "src/__support/CPP/type_traits.h"
13-
#include "src/__support/macros/config.h"
1413

15-
// These intrinsics map to the FMA instrunctions in the target ISA for the GPU.
14+
// These intrinsics map to the FMA instructions in the target ISA for the GPU.
1615
// The default rounding mode generated from these will be to the nearest even.
17-
static_assert(LIBC_HAS_BUILTIN(__builtin_fma), "FMA builtins must be defined");
18-
static_assert(LIBC_HAS_BUILTIN(__builtin_fmaf), "FMA builtins must be defined");
16+
#if !__has_builtin(__builtin_fma) || !__has_builtin(__builtin_fmaf)
17+
#error "FMA builtins must be defined");
18+
#endif
1919

2020
namespace LIBC_NAMESPACE {
2121
namespace fputil {

libc/src/__support/macros/config.h

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,6 @@
1313
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
1414
#define LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
1515

16-
// LIBC_HAS_BUILTIN()
17-
//
18-
// Checks whether the compiler supports a Clang Feature Checking Macro, and if
19-
// so, checks whether it supports the provided builtin function "x" where x
20-
// is one of the functions noted in
21-
// https://clang.llvm.org/docs/LanguageExtensions.html
22-
//
23-
// Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
24-
// http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html
25-
26-
// Compiler builtin-detection.
27-
// clang.llvm.org/docs/LanguageExtensions.html#has-builtin
28-
#ifdef __has_builtin
29-
#define LIBC_HAS_BUILTIN(x) __has_builtin(x)
30-
#else
31-
#define LIBC_HAS_BUILTIN(x) 0
32-
#endif
33-
3416
// Compiler feature-detection.
3517
// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
3618
#ifdef __has_feature

0 commit comments

Comments
 (0)