Skip to content

Commit

Permalink
[libc] Remove obsolete LIBC_HAS_BUILTIN macro
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer committed Mar 25, 2024
1 parent 3e3f0c3 commit 506251a
Show file tree
Hide file tree
Showing 19 changed files with 44 additions and 66 deletions.
2 changes: 1 addition & 1 deletion libc/docs/dev/code_style.rst
Expand Up @@ -55,7 +55,7 @@ We define two kinds of macros:
* ``src/__support/macros/config.h`` - Important compiler and platform
features. Such macros can be used to produce portable code by
parameterizing compilation based on the presence or lack of a given
feature. e.g., ``LIBC_HAS_BUILTIN``
feature. e.g., ``LIBC_HAS_FEATURE``
* ``src/__support/macros/attributes.h`` - Attributes for functions, types,
and variables. e.g., ``LIBC_UNUSED``
* ``src/__support/macros/optimization.h`` - Portable macros for performance
Expand Down
23 changes: 12 additions & 11 deletions libc/src/__support/CPP/atomic.h
Expand Up @@ -71,10 +71,11 @@ template <typename T> struct Atomic {

T load(MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_load_n))
return __scoped_atomic_load_n(&val, int(mem_ord), (int)(mem_scope));
else
return __atomic_load_n(&val, int(mem_ord));
#if __has_builtin(__scoped_atomic_load_n)
return __scoped_atomic_load_n(&val, int(mem_ord), (int)(mem_scope));
#else
return __atomic_load_n(&val, int(mem_ord));
#endif
}

// Atomic store.
Expand All @@ -85,7 +86,7 @@ template <typename T> struct Atomic {

void store(T rhs, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_store_n))
if constexpr (__has_builtin(__scoped_atomic_store_n))
__scoped_atomic_store_n(&val, rhs, int(mem_ord), (int)(mem_scope));
else
__atomic_store_n(&val, rhs, int(mem_ord));
Expand All @@ -101,7 +102,7 @@ template <typename T> struct Atomic {

T exchange(T desired, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_exchange_n))
if constexpr (__has_builtin(__scoped_atomic_exchange_n))
return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
(int)(mem_scope));
else
Expand All @@ -110,7 +111,7 @@ template <typename T> struct Atomic {

T fetch_add(T increment, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_add))
if constexpr (__has_builtin(__scoped_atomic_fetch_add))
return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
(int)(mem_scope));
else
Expand All @@ -119,7 +120,7 @@ template <typename T> struct Atomic {

T fetch_or(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_or))
if constexpr (__has_builtin(__scoped_atomic_fetch_or))
return __scoped_atomic_fetch_or(&val, mask, int(mem_ord),
(int)(mem_scope));
else
Expand All @@ -128,7 +129,7 @@ template <typename T> struct Atomic {

T fetch_and(T mask, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_and))
if constexpr (__has_builtin(__scoped_atomic_fetch_and))
return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
(int)(mem_scope));
else
Expand All @@ -137,7 +138,7 @@ template <typename T> struct Atomic {

T fetch_sub(T decrement, MemoryOrder mem_ord = MemoryOrder::SEQ_CST,
[[maybe_unused]] MemoryScope mem_scope = MemoryScope::DEVICE) {
if constexpr (LIBC_HAS_BUILTIN(__scoped_atomic_fetch_sub))
if constexpr (__has_builtin(__scoped_atomic_fetch_sub))
return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
(int)(mem_scope));
else
Expand Down Expand Up @@ -166,7 +167,7 @@ LIBC_INLINE void atomic_thread_fence([[maybe_unused]] MemoryOrder mem_ord) {
// except no instructions for memory ordering are issued. Only reordering of
// the instructions by the compiler is suppressed as order instructs.
LIBC_INLINE void atomic_signal_fence([[maybe_unused]] MemoryOrder mem_ord) {
#if LIBC_HAS_BUILTIN(__atomic_signal_fence)
#if __has_builtin(__atomic_signal_fence)
__atomic_signal_fence(static_cast<int>(mem_ord));
#else
// if the builtin is not ready, use asm as a full compiler barrier.
Expand Down
15 changes: 7 additions & 8 deletions libc/src/__support/CPP/bit.h
Expand Up @@ -14,14 +14,13 @@
#include "src/__support/CPP/limits.h" // numeric_limits
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/__support/macros/sanitizer.h"

#include <stdint.h>

namespace LIBC_NAMESPACE::cpp {

#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#if __has_builtin(__builtin_memcpy_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
#endif

Expand All @@ -36,20 +35,20 @@ LIBC_INLINE constexpr cpp::enable_if_t<
To>
bit_cast(const From &from) {
MSAN_UNPOISON(&from, sizeof(From));
#if LIBC_HAS_BUILTIN(__builtin_bit_cast)
#if __has_builtin(__builtin_bit_cast)
return __builtin_bit_cast(To, from);
#else
To to;
char *dst = reinterpret_cast<char *>(&to);
const char *src = reinterpret_cast<const char *>(&from);
#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#if __has_builtin(__builtin_memcpy_inline)
__builtin_memcpy_inline(dst, src, sizeof(To));
#else
for (unsigned i = 0; i < sizeof(To); ++i)
dst[i] = src[i];
#endif // LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#endif // __has_builtin(__builtin_memcpy_inline)
return to;
#endif // LIBC_HAS_BUILTIN(__builtin_bit_cast)
#endif // __has_builtin(__builtin_bit_cast)
}

template <typename T>
Expand Down Expand Up @@ -94,7 +93,7 @@ countr_zero(T value) {
}
return zero_bits;
}
#if LIBC_HAS_BUILTIN(__builtin_ctzs)
#if __has_builtin(__builtin_ctzs)
ADD_SPECIALIZATION(countr_zero, unsigned short, __builtin_ctzs)
#endif
ADD_SPECIALIZATION(countr_zero, unsigned int, __builtin_ctz)
Expand Down Expand Up @@ -124,7 +123,7 @@ countl_zero(T value) {
}
return zero_bits;
}
#if LIBC_HAS_BUILTIN(__builtin_clzs)
#if __has_builtin(__builtin_clzs)
ADD_SPECIALIZATION(countl_zero, unsigned short, __builtin_clzs)
#endif
ADD_SPECIALIZATION(countl_zero, unsigned int, __builtin_clz)
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_destructible.h
Expand Up @@ -21,7 +21,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_destructible
#if LIBC_HAS_BUILTIN(__is_destructible)
#if __has_builtin(__is_destructible)
template <typename T>
struct is_destructible : bool_constant<__is_destructible(T)> {};
#else
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_function.h
Expand Up @@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_function
#if LIBC_HAS_BUILTIN(__is_function)
#if __has_builtin(__is_function)
template <typename T>
struct is_function : integral_constant<bool, __is_function(T)> {};
#else
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_lvalue_reference.h
Expand Up @@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_lvalue_reference
#if LIBC_HAS_BUILTIN(__is_lvalue_reference)
#if __has_builtin(__is_lvalue_reference)
template <typename T>
struct is_lvalue_reference : bool_constant<__is_lvalue_reference(T)> {};
#else
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_reference.h
Expand Up @@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_reference
#if LIBC_HAS_BUILTIN(__is_reference)
#if __has_builtin(__is_reference)
template <typename T> struct is_reference : bool_constant<__is_reference(T)> {};
#else
template <typename T> struct is_reference : public false_type {};
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/is_rvalue_reference.h
Expand Up @@ -17,7 +17,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_rvalue_reference
#if LIBC_HAS_BUILTIN(__is_rvalue_reference)
#if __has_builtin(__is_rvalue_reference)
template <typename T>
struct is_rvalue_reference : bool_constant<__is_rvalue_reference(T)> {};
#else
Expand Down
Expand Up @@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE::cpp {

// is_trivially_destructible
#if LIBC_HAS_BUILTIN(__is_trivially_destructible)
#if __has_builtin(__is_trivially_destructible)
template <typename T>
struct is_trivially_destructible
: public bool_constant<__is_trivially_destructible(T)> {};
Expand All @@ -25,7 +25,7 @@ template <typename T>
struct is_trivially_destructible
: public bool_constant<cpp::is_destructible_v<T> &&__has_trivial_destructor(
T)> {};
#endif // LIBC_HAS_BUILTIN(__is_trivially_destructible)
#endif // __has_builtin(__is_trivially_destructible)
template <typename T>
LIBC_INLINE_VAR constexpr bool is_trivially_destructible_v =
is_trivially_destructible<T>::value;
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/CPP/type_traits/remove_all_extents.h
Expand Up @@ -16,7 +16,7 @@
namespace LIBC_NAMESPACE::cpp {

// remove_all_extents
#if LIBC_HAS_BUILTIN(__remove_all_extents)
#if __has_builtin(__remove_all_extents)
template <typename T> using remove_all_extents_t = __remove_all_extents(T);
template <typename T>
struct remove_all_extents : cpp::type_identity<remove_all_extents_t<T>> {};
Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/FPUtil/FEnvImpl.h
Expand Up @@ -11,7 +11,6 @@

#include "include/llvm-libc-macros/math-macros.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/__support/macros/properties/architectures.h"
#include "src/errno/libc_errno.h"
#include <fenv.h>
Expand Down
7 changes: 4 additions & 3 deletions libc/src/__support/FPUtil/gpu/FMA.h
Expand Up @@ -12,10 +12,11 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/macros/config.h"

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

namespace LIBC_NAMESPACE {
namespace fputil {
Expand Down
18 changes: 0 additions & 18 deletions libc/src/__support/macros/config.h
Expand Up @@ -13,24 +13,6 @@
#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
#define LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H

// LIBC_HAS_BUILTIN()
//
// Checks whether the compiler supports a Clang Feature Checking Macro, and if
// so, checks whether it supports the provided builtin function "x" where x
// is one of the functions noted in
// https://clang.llvm.org/docs/LanguageExtensions.html
//
// Note: Use this macro to avoid an extra level of #ifdef __has_builtin check.
// http://releases.llvm.org/3.3/tools/clang/docs/LanguageExtensions.html

// Compiler builtin-detection.
// clang.llvm.org/docs/LanguageExtensions.html#has-builtin
#ifdef __has_builtin
#define LIBC_HAS_BUILTIN(x) __has_builtin(x)
#else
#define LIBC_HAS_BUILTIN(x) 0
#endif

// Compiler feature-detection.
// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
#ifdef __has_feature
Expand Down
1 change: 0 additions & 1 deletion libc/src/__support/macros/optimization.h
Expand Up @@ -11,7 +11,6 @@
#define LLVM_LIBC_SRC___SUPPORT_MACROS_OPTIMIZATION_H

#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/__support/macros/properties/compiler.h" // LIBC_COMPILER_IS_CLANG

// We use a template to implement likely/unlikely to make sure that we don't
Expand Down
3 changes: 1 addition & 2 deletions libc/src/__support/macros/sanitizer.h
Expand Up @@ -47,8 +47,7 @@
// Functions to unpoison memory
//-----------------------------------------------------------------------------

#if defined(LIBC_HAVE_MEMORY_SANITIZER) && \
LIBC_HAS_BUILTIN(__builtin_constant_p)
#if defined(LIBC_HAVE_MEMORY_SANITIZER) && __has_builtin(__builtin_constant_p)
// Only perform MSAN unpoison in non-constexpr context.
#include <sanitizer/msan_interface.h>
#define MSAN_UNPOISON(addr, size) \
Expand Down
9 changes: 4 additions & 5 deletions libc/src/__support/math_extras.h
Expand Up @@ -14,7 +14,6 @@
#include "src/__support/CPP/limits.h" // CHAR_BIT, numeric_limits
#include "src/__support/CPP/type_traits.h" // is_unsigned_v
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN

namespace LIBC_NAMESPACE {

Expand Down Expand Up @@ -61,7 +60,7 @@ add_with_carry(T a, T b, T carry_in) {
return add_with_carry_const<T>(a, b, carry_in);
}

#if LIBC_HAS_BUILTIN(__builtin_addc)
#if __has_builtin(__builtin_addc)
// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins

template <>
Expand Down Expand Up @@ -129,7 +128,7 @@ add_with_carry<unsigned long long>(unsigned long long a, unsigned long long b,
}
}

#endif // LIBC_HAS_BUILTIN(__builtin_addc)
#endif // __has_builtin(__builtin_addc)

// Subtract with borrow
template <typename T> struct DiffBorrow {
Expand Down Expand Up @@ -157,7 +156,7 @@ sub_with_borrow(T a, T b, T borrow_in) {
return sub_with_borrow_const<T>(a, b, borrow_in);
}

#if LIBC_HAS_BUILTIN(__builtin_subc)
#if __has_builtin(__builtin_subc)
// https://clang.llvm.org/docs/LanguageExtensions.html#multiprecision-arithmetic-builtins

template <>
Expand Down Expand Up @@ -225,7 +224,7 @@ sub_with_borrow<unsigned long long>(unsigned long long a, unsigned long long b,
}
}

#endif // LIBC_HAS_BUILTIN(__builtin_subc)
#endif // __has_builtin(__builtin_subc)

template <typename T>
[[nodiscard]] LIBC_INLINE constexpr cpp::enable_if_t<cpp::is_unsigned_v<T>, int>
Expand Down
2 changes: 1 addition & 1 deletion libc/src/__support/memory_size.h
Expand Up @@ -19,7 +19,7 @@
namespace LIBC_NAMESPACE {
namespace internal {
template <class T> LIBC_INLINE bool mul_overflow(T a, T b, T *res) {
#if LIBC_HAS_BUILTIN(__builtin_mul_overflow)
#if __has_builtin(__builtin_mul_overflow)
return __builtin_mul_overflow(a, b, res);
#else
T max = cpp::numeric_limits<T>::max();
Expand Down
8 changes: 4 additions & 4 deletions libc/src/string/memory_utils/generic/builtin.h
Expand Up @@ -10,16 +10,16 @@
#define LLVM_LIBC_SRC_STRING_MEMORY_UTILS_GENERIC_BUILTIN_H

#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/string/memory_utils/utils.h" // Ptr, CPtr

#include <stddef.h> // size_t

namespace LIBC_NAMESPACE {

static_assert(LIBC_HAS_BUILTIN(__builtin_memcpy), "Builtin not defined");
static_assert(LIBC_HAS_BUILTIN(__builtin_memset), "Builtin not defined");
static_assert(LIBC_HAS_BUILTIN(__builtin_memmove), "Builtin not defined");
#if !__has_builtin(__builtin_memcpy) || !__has_builtin(__builtin_memset) || \
!__has_builtin(__builtin_memmove)
#error "Builtin not defined");
#endif

[[maybe_unused]] LIBC_INLINE void
inline_memcpy_builtin(Ptr dst, CPtr src, size_t count, size_t offset = 0) {
Expand Down
5 changes: 2 additions & 3 deletions libc/src/string/memory_utils/utils.h
Expand Up @@ -14,7 +14,6 @@
#include "src/__support/CPP/type_traits.h"
#include "src/__support/endian.h"
#include "src/__support/macros/attributes.h" // LIBC_INLINE
#include "src/__support/macros/config.h" // LIBC_HAS_BUILTIN
#include "src/__support/macros/properties/architectures.h"

#include <stddef.h> // size_t
Expand Down Expand Up @@ -71,11 +70,11 @@ LIBC_INLINE bool is_disjoint(const void *p1, const void *p2, size_t size) {
return sdiff >= 0 ? size <= udiff : size <= neg_udiff;
}

#if LIBC_HAS_BUILTIN(__builtin_memcpy_inline)
#if __has_builtin(__builtin_memcpy_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMCPY_INLINE
#endif

#if LIBC_HAS_BUILTIN(__builtin_memset_inline)
#if __has_builtin(__builtin_memset_inline)
#define LLVM_LIBC_HAS_BUILTIN_MEMSET_INLINE
#endif

Expand Down

0 comments on commit 506251a

Please sign in to comment.