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 0fe0ef4 commit 781a49a
Show file tree
Hide file tree
Showing 22 changed files with 72 additions and 104 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
2 changes: 0 additions & 2 deletions libc/src/__support/CPP/CMakeLists.txt
Expand Up @@ -18,7 +18,6 @@ add_header_library(
.limits
.type_traits
libc.src.__support.macros.attributes
libc.src.__support.macros.config
libc.src.__support.macros.sanitizer
)

Expand Down Expand Up @@ -157,7 +156,6 @@ add_header_library(
DEPENDS
libc.include.llvm-libc-macros.stdfix_macros
libc.src.__support.macros.attributes
libc.src.__support.macros.config
libc.src.__support.macros.properties.types
)

Expand Down
74 changes: 40 additions & 34 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,10 +86,11 @@ 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))
__scoped_atomic_store_n(&val, rhs, int(mem_ord), (int)(mem_scope));
else
__atomic_store_n(&val, rhs, int(mem_ord));
#if __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));
#endif
}

// Atomic compare exchange
Expand All @@ -101,47 +103,51 @@ 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))
return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
(int)(mem_scope));
else
return __atomic_exchange_n(&val, desired, int(mem_ord));
#if __has_builtin(__scoped_atomic_exchange_n)
return __scoped_atomic_exchange_n(&val, desired, int(mem_ord),
(int)(mem_scope));
#else
return __atomic_exchange_n(&val, desired, int(mem_ord));
#endif
}

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))
return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
(int)(mem_scope));
else
return __atomic_fetch_add(&val, increment, int(mem_ord));
#if __has_builtin(__scoped_atomic_fetch_add)
return __scoped_atomic_fetch_add(&val, increment, int(mem_ord),
(int)(mem_scope));
#else
return __atomic_fetch_add(&val, increment, int(mem_ord));
#endif
}

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))
return __scoped_atomic_fetch_or(&val, mask, int(mem_ord),
(int)(mem_scope));
else
return __atomic_fetch_or(&val, mask, int(mem_ord));
#if __has_builtin(__scoped_atomic_fetch_or)
return __scoped_atomic_fetch_or(&val, mask, int(mem_ord), (int)(mem_scope));
#else
return __atomic_fetch_or(&val, mask, int(mem_ord));
#endif
}

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))
return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
(int)(mem_scope));
else
return __atomic_fetch_and(&val, mask, int(mem_ord));
#if __has_builtin(__scoped_atomic_fetch_and)
return __scoped_atomic_fetch_and(&val, mask, int(mem_ord),
(int)(mem_scope));
#else
return __atomic_fetch_and(&val, mask, int(mem_ord));
#endif
}

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))
return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
(int)(mem_scope));
else
return __atomic_fetch_sub(&val, decrement, int(mem_ord));
#if __has_builtin(__scoped_atomic_fetch_sub)
return __scoped_atomic_fetch_sub(&val, decrement, int(mem_ord),
(int)(mem_scope));
#else
return __atomic_fetch_sub(&val, decrement, int(mem_ord));
#endif
}

// Set the value without using an atomic operation. This is useful
Expand All @@ -166,7 +172,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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/is_destructible.h
Expand Up @@ -16,12 +16,11 @@
#include "src/__support/CPP/type_traits/true_type.h"
#include "src/__support/CPP/type_traits/type_identity.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/is_function.h
Expand Up @@ -12,12 +12,11 @@
#include "src/__support/CPP/type_traits/is_const.h"
#include "src/__support/CPP/type_traits/is_reference.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/is_lvalue_reference.h
Expand Up @@ -12,12 +12,11 @@
#include "src/__support/CPP/type_traits/false_type.h"
#include "src/__support/CPP/type_traits/true_type.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/is_reference.h
Expand Up @@ -12,12 +12,11 @@
#include "src/__support/CPP/type_traits/false_type.h"
#include "src/__support/CPP/type_traits/true_type.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/is_rvalue_reference.h
Expand Up @@ -12,12 +12,11 @@
#include "src/__support/CPP/type_traits/false_type.h"
#include "src/__support/CPP/type_traits/true_type.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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 @@ -11,12 +11,11 @@
#include "src/__support/CPP/type_traits/bool_constant.h"
#include "src/__support/CPP/type_traits/is_destructible.h"
#include "src/__support/macros/attributes.h"
#include "src/__support/macros/config.h"

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 +24,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
3 changes: 1 addition & 2 deletions libc/src/__support/CPP/type_traits/remove_all_extents.h
Expand Up @@ -9,14 +9,13 @@
#define LLVM_LIBC_SRC___SUPPORT_CPP_TYPE_TRAITS_REMOVE_ALL_EXTENTS_H

#include "src/__support/CPP/type_traits/type_identity.h"
#include "src/__support/macros/config.h"

#include <stddef.h> // size_t

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/CMakeLists.txt
Expand Up @@ -6,7 +6,6 @@ add_header_library(
libc.include.fenv
libc.include.math
libc.src.__support.macros.attributes
libc.src.__support.macros.config
libc.src.errno.errno
)

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
8 changes: 4 additions & 4 deletions libc/src/__support/FPUtil/gpu/FMA.h
Expand Up @@ -10,12 +10,12 @@
#define LLVM_LIBC_SRC___SUPPORT_FPUTIL_GPU_FMA_H

#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

0 comments on commit 781a49a

Please sign in to comment.