| 1 | //===-- Portable attributes -------------------------------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // This header file defines macros for declaring attributes for functions, |
| 9 | // types, and variables. |
| 10 | // |
| 11 | // These macros are used within llvm-libc and allow the compiler to optimize, |
| 12 | // where applicable, certain function calls. |
| 13 | // |
| 14 | // Most macros here are exposing GCC or Clang features, and are stubbed out for |
| 15 | // other compilers. |
| 16 | |
| 17 | #ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H |
| 18 | #define LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H |
| 19 | |
| 20 | #include "config.h" |
| 21 | #include "properties/architectures.h" |
| 22 | |
| 23 | #ifndef __has_attribute |
| 24 | #define __has_attribute(x) 0 |
| 25 | #endif |
| 26 | |
| 27 | #ifndef __has_cpp_attribute |
| 28 | #define __has_cpp_attribute(x) 0 |
| 29 | #endif |
| 30 | |
| 31 | #define LIBC_INLINE inline |
| 32 | #define LIBC_INLINE_VAR inline |
| 33 | #define LIBC_INLINE_ASM __asm__ __volatile__ |
| 34 | #define LIBC_UNUSED [[maybe_unused]] |
| 35 | |
| 36 | #if defined(LIBC_TARGET_ARCH_IS_AARCH64) && defined(__linux__) |
| 37 | #define LIBC_VECTOR_PCS __attribute__((aarch64_vector_pcs)) |
| 38 | #else |
| 39 | #define LIBC_VECTOR_PCS |
| 40 | #endif |
| 41 | |
| 42 | #if __has_attribute(always_inline) |
| 43 | #define LIBC_ALWAYS_INLINE LIBC_INLINE __attribute__((always_inline)) |
| 44 | #elif defined(LIBC_COMPILER_IS_MSVC) || defined(_MSC_VER) |
| 45 | #define LIBC_ALWAYS_INLINE __forceinline |
| 46 | #else |
| 47 | #define LIBC_ALWAYS_INLINE LIBC_INLINE |
| 48 | #endif // __has_attribute(always_inline) |
| 49 | |
| 50 | #if __has_attribute(noinline) |
| 51 | #define LIBC_NOINLINE __attribute__((noinline)) |
| 52 | #elif defined(LIBC_COMPILER_IS_MSVC) || defined(_MSC_VER) |
| 53 | #define LIBC_NOINLINE __declspec(noinline) |
| 54 | #else |
| 55 | #define LIBC_NOINLINE |
| 56 | #endif // __has_attribute(noinline) |
| 57 | |
| 58 | #if __has_attribute(cold) |
| 59 | #define LIBC_COLD __attribute__((cold)) |
| 60 | #else |
| 61 | #define LIBC_COLD |
| 62 | #endif // __has_attribute(cold) |
| 63 | |
| 64 | #if __has_attribute(flatten) |
| 65 | #define LIBC_FLATTEN __attribute__((flatten)) |
| 66 | #else |
| 67 | #define LIBC_FLATTEN |
| 68 | #endif // __has_attribute(flatten) |
| 69 | |
| 70 | #if __has_attribute(may_alias) |
| 71 | #define LIBC_MAY_ALIAS __attribute__((may_alias)) |
| 72 | #else |
| 73 | #define LIBC_MAY_ALIAS |
| 74 | #endif // __has_attribute(may_alias) |
| 75 | |
| 76 | #ifndef LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED |
| 77 | #if (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 900)) || \ |
| 78 | (defined(LIBC_COMPILER_IS_CLANG) && LIBC_COMPILER_CLANG_VER >= 900) |
| 79 | #define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED 1 |
| 80 | #else |
| 81 | #define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED \ |
| 82 | (__has_builtin(__builtin_is_constant_evaluated)) |
| 83 | #endif // (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 900)) || |
| 84 | // (defined(LIBC_COMPILER_IS_CLANG) && LIBC_COMPILER_CLANG |
| 85 | #endif // LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED |
| 86 | |
| 87 | #ifndef LIBC_HAS_BUILTIN_BIT_CAST |
| 88 | #if __has_builtin(__builtin_bit_cast) || defined(LIBC_COMPILER_IS_MSVC) |
| 89 | #define LIBC_HAS_BUILTIN_BIT_CAST 1 |
| 90 | #else |
| 91 | #define LIBC_HAS_BUILTIN_BIT_CAST 0 |
| 92 | #endif // has_builtin(__builtin_bit_cast) |
| 93 | #endif // LIBC_HAS_BUILTIN_BIT_CAST |
| 94 | |
| 95 | #if LIBC_HAS_BUILTIN_BIT_CAST |
| 96 | #define LIBC_BIT_CAST_CONSTEXPR constexpr |
| 97 | #define LIBC_BIT_CAST_CONSTEXPR_VAR constexpr |
| 98 | #else |
| 99 | #define LIBC_BIT_CAST_CONSTEXPR |
| 100 | #define LIBC_BIT_CAST_CONSTEXPR_VAR const |
| 101 | #endif // LIBC_HAS_BUILTIN_BIT_CAST |
| 102 | |
| 103 | #ifndef LIBC_HAS_CONSTANT_EVALUATION |
| 104 | #define LIBC_HAS_CONSTANT_EVALUATION \ |
| 105 | (LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED && LIBC_HAS_BUILTIN_BIT_CAST) |
| 106 | #endif // LIBC_HAS_CONSTANT_EVALUATION |
| 107 | |
| 108 | #if LIBC_HAS_CONSTANT_EVALUATION |
| 109 | #define LIBC_CONSTEXPR_DEFAULT constexpr |
| 110 | #define LIBC_CONSTEXPR_VAR_DEFAULT constexpr |
| 111 | #else |
| 112 | #define LIBC_CONSTEXPR_DEFAULT |
| 113 | #define LIBC_CONSTEXPR_VAR_DEFAULT const |
| 114 | #endif // LIBC_HAS_CONSTANT_EVALUATION |
| 115 | |
| 116 | // TODO: Remove the macro once Clang/LLVM bump their minimum compilers' version. |
| 117 | // The reason for indirection is GCC is known to fail with constexpr qualified |
| 118 | // functions that doesn't produce constant expression. |
| 119 | // Also, there are some circular dependency in the generic functions without |
| 120 | // __builtin_func for the following functions: |
| 121 | // fputil::fma |
| 122 | // fputil::sqrt |
| 123 | #if LIBC_ENABLE_CONSTEXPR && LIBC_HAS_CONSTANT_EVALUATION |
| 124 | #define LIBC_USE_CONSTEXPR |
| 125 | #define LIBC_CONSTEXPR constexpr |
| 126 | #define LIBC_CONSTEXPR_VAR constexpr |
| 127 | #else |
| 128 | #define LIBC_CONSTEXPR |
| 129 | #define LIBC_CONSTEXPR_VAR const |
| 130 | #endif // LIBC_USE_CONSTEXPR |
| 131 | |
| 132 | #ifndef LIBC_HAS_BUILTIN_IS_ASSIGNABLE |
| 133 | #if (__has_builtin(__is_assignable) || \ |
| 134 | (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 800))) |
| 135 | #define LIBC_HAS_BUILTIN_IS_ASSIGNABLE 1 |
| 136 | #else |
| 137 | #define LIBC_HAS_BUILTIN_IS_ASSIGNABLE 0 |
| 138 | #endif |
| 139 | #endif // LIBC_HAS_BUILTIN_IS_ASSIGNABLE |
| 140 | |
| 141 | #ifndef LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE |
| 142 | #if (__has_builtin(__is_constructible) || \ |
| 143 | (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 800))) |
| 144 | #define LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE 1 |
| 145 | #else |
| 146 | #define LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE 0 |
| 147 | #endif |
| 148 | #endif // LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE |
| 149 | |
| 150 | // Uses the platform specific specialization |
| 151 | #define LIBC_THREAD_MODE_PLATFORM 0 |
| 152 | |
| 153 | // Mutex guards nothing, used in single-threaded implementations |
| 154 | #define LIBC_THREAD_MODE_SINGLE 1 |
| 155 | |
| 156 | // Vendor provides implementation |
| 157 | #define LIBC_THREAD_MODE_EXTERNAL 2 |
| 158 | |
| 159 | // libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command |
| 160 | // line in the CMake invocation. This defaults to the original implementation |
| 161 | // (before changes in https://github.com/llvm/llvm-project/pull/145358) |
| 162 | #ifndef LIBC_THREAD_MODE |
| 163 | #define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM |
| 164 | #endif // LIBC_THREAD_MODE |
| 165 | |
| 166 | #if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \ |
| 167 | LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \ |
| 168 | LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL |
| 169 | #error LIBC_THREAD_MODE must be one of the following values: \ |
| 170 | LIBC_THREAD_MODE_PLATFORM, \ |
| 171 | LIBC_THREAD_MODE_SINGLE, \ |
| 172 | LIBC_THREAD_MODE_EXTERNAL. |
| 173 | #endif |
| 174 | |
| 175 | #if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE |
| 176 | #define LIBC_THREAD_LOCAL |
| 177 | #else |
| 178 | #define LIBC_THREAD_LOCAL thread_local |
| 179 | #endif |
| 180 | |
| 181 | #if __cplusplus >= 202002L |
| 182 | #define LIBC_CONSTINIT constinit |
| 183 | #elif __has_attribute(__require_constant_initialization__) |
| 184 | #define LIBC_CONSTINIT __attribute__((__require_constant_initialization__)) |
| 185 | #else |
| 186 | #define LIBC_CONSTINIT |
| 187 | #endif |
| 188 | |
| 189 | #if defined(__clang__) && __has_attribute(preferred_type) |
| 190 | #define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]] |
| 191 | #else |
| 192 | #define LIBC_PREFERED_TYPE(TYPE) |
| 193 | #endif |
| 194 | |
| 195 | #if __has_cpp_attribute(no_unique_address) |
| 196 | #define LIBC_NO_UNIQUE_ADDRESS [[no_unique_address]] |
| 197 | #else |
| 198 | #define LIBC_NO_UNIQUE_ADDRESS |
| 199 | #endif |
| 200 | |
| 201 | #if __has_attribute(trivial_abi) |
| 202 | #define LIBC_TRIVIAL_ABI [[clang::trivial_abi]] |
| 203 | #else |
| 204 | #define LIBC_TRIVIAL_ABI |
| 205 | #endif |
| 206 | |
| 207 | #if __has_attribute(ext_vector_type) && \ |
| 208 | LIBC_HAS_FEATURE(ext_vector_type_boolean) |
| 209 | #define LIBC_HAS_VECTOR_TYPE 1 |
| 210 | #else |
| 211 | #define LIBC_HAS_VECTOR_TYPE 0 |
| 212 | #endif |
| 213 | |
| 214 | #if __has_attribute(no_sanitize) |
| 215 | // Disable regular and hardware-supported ASan for functions that may |
| 216 | // intentionally make out-of-bounds access. Disable TSan as well, as it detects |
| 217 | // out-of-bounds accesses to heap memory. |
| 218 | #define LIBC_NO_SANITIZE_OOB_ACCESS \ |
| 219 | __attribute__((no_sanitize("address", "hwaddress", "thread"))) |
| 220 | #else |
| 221 | #define LIBC_NO_SANITIZE_OOB_ACCESS |
| 222 | #endif |
| 223 | |
| 224 | #endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H |
| 225 | |