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#define LIBC_INLINE inline
28#define LIBC_INLINE_VAR inline
29#define LIBC_INLINE_ASM __asm__ __volatile__
30#define LIBC_UNUSED __attribute__((unused))
31
32#if __has_attribute(always_inline)
33#define LIBC_ALWAYS_INLINE LIBC_INLINE __attribute__((always_inline))
34#elif defined(LIBC_COMPILER_IS_MSVC) || defined(_MSC_VER)
35#define LIBC_ALWAYS_INLINE __forceinline
36#else
37#define LIBC_ALWAYS_INLINE LIBC_INLINE
38#endif // __has_attribute(always_inline)
39
40#ifndef LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
41#if (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 900)) || \
42 (defined(LIBC_COMPILER_IS_CLANG) && LIBC_COMPILER_CLANG_VER >= 900)
43#define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED 1
44#else
45#define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED \
46 (__has_builtin(__builtin_is_constant_evaluated))
47#endif // (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 900)) ||
48 // (defined(LIBC_COMPILER_IS_CLANG) && LIBC_COMPILER_CLANG
49#endif // LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED
50
51#ifndef LIBC_HAS_BUILTIN_BIT_CAST
52#if __has_builtin(__builtin_bit_cast) || defined(LIBC_COMPILER_IS_MSVC)
53#define LIBC_HAS_BUILTIN_BIT_CAST 1
54#else
55#define LIBC_HAS_BUILTIN_BIT_CAST 0
56#endif // has_builtin(__builtin_bit_cast)
57#endif // LIBC_HAS_BUILTIN_BIT_CAST
58
59#if LIBC_HAS_BUILTIN_BIT_CAST
60#define LIBC_BIT_CAST_CONSTEXPR constexpr
61#define LIBC_BIT_CAST_CONSTEXPR_VAR constexpr
62#else
63#define LIBC_BIT_CAST_CONSTEXPR
64#define LIBC_BIT_CAST_CONSTEXPR_VAR const
65#endif // LIBC_HAS_BUILTIN_BIT_CAST
66
67#ifndef LIBC_HAS_CONSTANT_EVALUATION
68#define LIBC_HAS_CONSTANT_EVALUATION \
69 (LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED && LIBC_HAS_BUILTIN_BIT_CAST)
70#endif // LIBC_HAS_CONSTANT_EVALUATION
71
72#if LIBC_HAS_CONSTANT_EVALUATION
73#define LIBC_CONSTEXPR_DEFAULT constexpr
74#define LIBC_CONSTEXPR_VAR_DEFAULT constexpr
75#else
76#define LIBC_CONSTEXPR_DEFAULT
77#define LIBC_CONSTEXPR_VAR_DEFAULT const
78#endif // LIBC_HAS_CONSTANT_EVALUATION
79
80// TODO: Remove the macro once Clang/LLVM bump their minimum compilers' version.
81// The reason for indirection is GCC is known to fail with constexpr qualified
82// functions that doesn't produce constant expression.
83// Also, there are some circular dependency in the generic functions without
84// __builtin_func for the following functions:
85// fputil::fma
86// fputil::sqrt
87#if LIBC_ENABLE_CONSTEXPR && LIBC_HAS_CONSTANT_EVALUATION
88#define LIBC_USE_CONSTEXPR
89#define LIBC_CONSTEXPR constexpr
90#define LIBC_CONSTEXPR_VAR constexpr
91#else
92#define LIBC_CONSTEXPR
93#define LIBC_CONSTEXPR_VAR const
94#endif // LIBC_USE_CONSTEXPR
95
96#ifndef LIBC_HAS_BUILTIN_IS_ASSIGNABLE
97#if (__has_builtin(__is_assignable) || \
98 (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 800)))
99#define LIBC_HAS_BUILTIN_IS_ASSIGNABLE 1
100#else
101#define LIBC_HAS_BUILTIN_IS_ASSIGNABLE 0
102#endif
103#endif // LIBC_HAS_BUILTIN_IS_ASSIGNABLE
104
105#ifndef LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE
106#if (__has_builtin(__is_constructible) || \
107 (defined(LIBC_COMPILER_IS_GCC) && (LIBC_COMPILER_GCC_VER >= 800)))
108#define LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE 1
109#else
110#define LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE 0
111#endif
112#endif // LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE
113
114// Uses the platform specific specialization
115#define LIBC_THREAD_MODE_PLATFORM 0
116
117// Mutex guards nothing, used in single-threaded implementations
118#define LIBC_THREAD_MODE_SINGLE 1
119
120// Vendor provides implementation
121#define LIBC_THREAD_MODE_EXTERNAL 2
122
123// libcxx doesn't define LIBC_THREAD_MODE, unless that is passed in the command
124// line in the CMake invocation. This defaults to the original implementation
125// (before changes in https://github.com/llvm/llvm-project/pull/145358)
126#ifndef LIBC_THREAD_MODE
127#define LIBC_THREAD_MODE LIBC_THREAD_MODE_PLATFORM
128#endif // LIBC_THREAD_MODE
129
130#if LIBC_THREAD_MODE != LIBC_THREAD_MODE_PLATFORM && \
131 LIBC_THREAD_MODE != LIBC_THREAD_MODE_SINGLE && \
132 LIBC_THREAD_MODE != LIBC_THREAD_MODE_EXTERNAL
133#error LIBC_THREAD_MODE must be one of the following values: \
134LIBC_THREAD_MODE_PLATFORM, \
135LIBC_THREAD_MODE_SINGLE, \
136LIBC_THREAD_MODE_EXTERNAL.
137#endif
138
139#if LIBC_THREAD_MODE == LIBC_THREAD_MODE_SINGLE
140#define LIBC_THREAD_LOCAL
141#else
142#define LIBC_THREAD_LOCAL thread_local
143#endif
144
145#if __cplusplus >= 202002L
146#define LIBC_CONSTINIT constinit
147#elif __has_attribute(__require_constant_initialization__)
148#define LIBC_CONSTINIT __attribute__((__require_constant_initialization__))
149#else
150#define LIBC_CONSTINIT
151#endif
152
153#if defined(__clang__) && __has_attribute(preferred_type)
154#define LIBC_PREFERED_TYPE(TYPE) [[clang::preferred_type(TYPE)]]
155#else
156#define LIBC_PREFERED_TYPE(TYPE)
157#endif
158
159#if __has_attribute(ext_vector_type) && \
160 LIBC_HAS_FEATURE(ext_vector_type_boolean)
161#define LIBC_HAS_VECTOR_TYPE 1
162#else
163#define LIBC_HAS_VECTOR_TYPE 0
164#endif
165
166#if __has_attribute(no_sanitize)
167// Disable regular and hardware-supported ASan for functions that may
168// intentionally make out-of-bounds access. Disable TSan as well, as it detects
169// out-of-bounds accesses to heap memory.
170#define LIBC_NO_SANITIZE_OOB_ACCESS \
171 __attribute__((no_sanitize("address", "hwaddress", "thread")))
172#else
173#define LIBC_NO_SANITIZE_OOB_ACCESS
174#endif
175
176#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_ATTRIBUTES_H
177