xref: /llvm-project/compiler-rt/lib/orc/compiler.h (revision 9189a26664b97fdf22c6f7f57194134f707338bc)
1bd6c93c0SLang Hames //===--------- compiler.h - Compiler abstraction support --------*- C++ -*-===//
2bd6c93c0SLang Hames //
3bd6c93c0SLang Hames // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bd6c93c0SLang Hames // See https://llvm.org/LICENSE.txt for license information.
5bd6c93c0SLang Hames // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bd6c93c0SLang Hames //
7bd6c93c0SLang Hames //===----------------------------------------------------------------------===//
8bd6c93c0SLang Hames //
9bd6c93c0SLang Hames // This file is a part of the ORC runtime support library.
10bd6c93c0SLang Hames //
11bd6c93c0SLang Hames // Most functionality in this file was swiped from llvm/Support/Compiler.h.
12bd6c93c0SLang Hames //
13bd6c93c0SLang Hames //===----------------------------------------------------------------------===//
14bd6c93c0SLang Hames 
15bd6c93c0SLang Hames #ifndef ORC_RT_COMPILER_H
16bd6c93c0SLang Hames #define ORC_RT_COMPILER_H
17bd6c93c0SLang Hames 
18*9189a266SSunho Kim #if defined(_WIN32)
19*9189a266SSunho Kim #define ORC_RT_INTERFACE extern "C"
20*9189a266SSunho Kim #define ORC_RT_HIDDEN
21*9189a266SSunho Kim #define ORC_RT_IMPORT extern "C" __declspec(dllimport)
22*9189a266SSunho Kim #else
23bd6c93c0SLang Hames #define ORC_RT_INTERFACE extern "C" __attribute__((visibility("default")))
24bd6c93c0SLang Hames #define ORC_RT_HIDDEN __attribute__((visibility("hidden")))
25*9189a266SSunho Kim #define ORC_RT_IMPORT extern "C"
26*9189a266SSunho Kim #endif
27bd6c93c0SLang Hames 
28eaa329e7SLang Hames #ifndef __has_builtin
29eaa329e7SLang Hames # define __has_builtin(x) 0
30eaa329e7SLang Hames #endif
31eaa329e7SLang Hames 
32bd6c93c0SLang Hames // Only use __has_cpp_attribute in C++ mode. GCC defines __has_cpp_attribute in
33bd6c93c0SLang Hames // C mode, but the :: in __has_cpp_attribute(scoped::attribute) is invalid.
34bd6c93c0SLang Hames #ifndef ORC_RT_HAS_CPP_ATTRIBUTE
35bd6c93c0SLang Hames #if defined(__cplusplus) && defined(__has_cpp_attribute)
36bd6c93c0SLang Hames #define ORC_RT_HAS_CPP_ATTRIBUTE(x) __has_cpp_attribute(x)
37bd6c93c0SLang Hames #else
38bd6c93c0SLang Hames #define ORC_RT_HAS_CPP_ATTRIBUTE(x) 0
39bd6c93c0SLang Hames #endif
40bd6c93c0SLang Hames #endif
41bd6c93c0SLang Hames 
42bd6c93c0SLang Hames // Use the 'nodiscard' attribute in C++17 or newer mode.
43bd6c93c0SLang Hames #if defined(__cplusplus) && __cplusplus > 201402L &&                           \
44bd6c93c0SLang Hames     ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
45bd6c93c0SLang Hames #define ORC_RT_NODISCARD [[nodiscard]]
46bd6c93c0SLang Hames #elif ORC_RT_HAS_CPP_ATTRIBUTE(clang::warn_unused_result)
47bd6c93c0SLang Hames #define ORC_RT_NODISCARD [[clang::warn_unused_result]]
48bd6c93c0SLang Hames // Clang in C++14 mode claims that it has the 'nodiscard' attribute, but also
49bd6c93c0SLang Hames // warns in the pedantic mode that 'nodiscard' is a C++17 extension (PR33518).
50bd6c93c0SLang Hames // Use the 'nodiscard' attribute in C++14 mode only with GCC.
51bd6c93c0SLang Hames // TODO: remove this workaround when PR33518 is resolved.
52bd6c93c0SLang Hames #elif defined(__GNUC__) && ORC_RT_HAS_CPP_ATTRIBUTE(nodiscard)
53bd6c93c0SLang Hames #define ORC_RT_NODISCARD [[nodiscard]]
54bd6c93c0SLang Hames #else
55bd6c93c0SLang Hames #define ORC_RT_NODISCARD
56bd6c93c0SLang Hames #endif
57bd6c93c0SLang Hames 
58bd6c93c0SLang Hames #if __has_builtin(__builtin_expect)
59bd6c93c0SLang Hames #define ORC_RT_LIKELY(EXPR) __builtin_expect((bool)(EXPR), true)
60bd6c93c0SLang Hames #define ORC_RT_UNLIKELY(EXPR) __builtin_expect((bool)(EXPR), false)
61bd6c93c0SLang Hames #else
62bd6c93c0SLang Hames #define ORC_RT_LIKELY(EXPR) (EXPR)
63bd6c93c0SLang Hames #define ORC_RT_UNLIKELY(EXPR) (EXPR)
64bd6c93c0SLang Hames #endif
65bd6c93c0SLang Hames 
66*9189a266SSunho Kim #if defined(__APPLE__)
67ebec9559SLang Hames #define ORC_RT_WEAK_IMPORT __attribute__((weak_import))
68*9189a266SSunho Kim #elif defined(_WIN32)
69*9189a266SSunho Kim #define ORC_RT_WEAK_IMPORT
70aa69f0d8SLang Hames #else
71ebec9559SLang Hames #define ORC_RT_WEAK_IMPORT __attribute__((weak))
72aa69f0d8SLang Hames #endif
73aa69f0d8SLang Hames 
74bd6c93c0SLang Hames #endif // ORC_RT_COMPILER_H
75