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 a set of macros for checking the presence of
9// important compiler and platform features. Such macros can be used to
10// produce portable code by parameterizing compilation based on the presence or
11// lack of a given feature.
12
13#ifndef LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
14#define LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
15
16#include "src/__support/macros/properties/architectures.h"
17#include "src/__support/macros/properties/compiler.h"
18
19#ifdef LIBC_COMPILER_IS_MSVC
20#include <intrin.h>
21#endif // LIBC_COMPILER_IS_MSVC
22
23// Workaround for compilers that do not support builtin detection.
24// FIXME: This is only required for the GPU portion which should be moved.
25#ifndef __has_builtin
26#define __has_builtin(b) 0
27#endif
28
29#ifndef __has_warning
30#define __has_warning(w) 0
31#endif
32
33// Compiler feature-detection.
34// clang.llvm.org/docs/LanguageExtensions.html#has-feature-and-has-extension
35#ifdef __has_feature
36#define LIBC_HAS_FEATURE(f) __has_feature(f)
37#else
38#define LIBC_HAS_FEATURE(f) 0
39#endif
40
41#ifdef LIBC_COMPILER_IS_MSVC
42
43// __builtin_trap replacement
44#ifdef LIBC_TARGET_ARCH_IS_X86
45#define __builtin_trap __ud2
46#else // arm64
47#define __builtin_trap() __break(1)
48#endif
49
50#define __builtin_expect(value, expectation) (value)
51#define __builtin_unreachable() __assume(0)
52
53#define __builtin_prefetch(X, Y, Z)
54
55#define LIBC_HAS_BUILTIN_IS_CONSTANT_EVALUATED 1
56#define LIBC_HAS_BUILTIN_IS_ASSIGNABLE 1
57#define LIBC_HAS_BUILTIN_IS_CONSTRUCTIBLE 1
58
59#endif // LIBC_COMPILER_IS_MSVC
60
61#ifdef __clang__
62// Declare a LIBC_NAMESPACE with hidden visibility. `namespace
63// LIBC_NAMESPACE_DECL {` should be used around all declarations and definitions
64// for libc internals as opposed to just `namespace LIBC_NAMESPACE {`. This
65// ensures that all declarations within this namespace have hidden
66// visibility, which optimizes codegen for uses of symbols defined in other
67// translation units in ways that can be necessary for correctness by avoiding
68// dynamic relocations. This does not affect the public C symbols which are
69// controlled independently via `LLVM_LIBC_FUNCTION_ATTR`.
70#define LIBC_NAMESPACE_DECL [[gnu::visibility("hidden")]] LIBC_NAMESPACE
71#else
72// TODO(#98548): GCC emits a warning when using the visibility attribute which
73// needs to be diagnosed and addressed.
74#define LIBC_NAMESPACE_DECL LIBC_NAMESPACE
75#endif
76
77// IMPORTANT (USE WITH CAUTION): This macro is intended to be used at the top of
78// the file and set to 1. It alters the signatures of some functions to have
79// constexpr qualifier and forces the use of constexpr-compatible
80// implementation, which might be a completely different code path or
81// instructions. Some of these functions exploit platform-specific non-constexpr
82// implementations to achieve certain goals, thus it is disabled by default.
83#ifndef LIBC_ENABLE_CONSTEXPR
84#define LIBC_ENABLE_CONSTEXPR 0
85#endif
86
87#endif // LLVM_LIBC_SRC___SUPPORT_MACROS_CONFIG_H
88