Block-Structured AMR Software Framework
Loading...
Searching...
No Matches
AMReX_Extension.H
Go to the documentation of this file.
1#ifndef AMREX_EXTENSION_H_
2#define AMREX_EXTENSION_H_
3#include <AMReX_Config.H>
4
9
10#if defined(__INTEL_COMPILER) || (defined(__INTEL_LLVM_COMPILER) && !defined(SYCL_LANGUAGE_VERSION))
11#define AMREX_CXX_INTEL
12#elif defined(_CRAYC) || defined(__cray__)
13#define AMREX_CXX_CRAY
14#elif defined(__PGI)
15#define AMREX_CXX_PGI
16#elif defined(__NVCOMPILER)
17#define AMREX_CXX_NVHPC
18#elif defined(__NEC__)
19#define AMREX_CXX_NEC
20#elif defined(__ibmxl__)
21#define AMREX_CXX_IBM
22#elif defined(__clang__)
23#define AMREX_CXX_CLANG
24#elif defined(__GNUC__)
25#define AMREX_CXX_GCC
26#endif
27
28#if !defined(BL_LANG_FORT)
29
30// restrict
31
32#ifdef __cplusplus
33
34#ifdef _WIN32
35#define AMREX_RESTRICT __restrict
36#else
37#define AMREX_RESTRICT __restrict__
38#endif
39
40#else
41
42#define AMREX_RESTRICT restrict
43
44#endif
45
46// simd
47
48#if defined(AMREX_DEBUG)
49#define AMREX_PRAGMA_SIMD
50
51#elif defined(__CUDA_ARCH__) && defined(AMREX_USE_CUDA)
52#define AMREX_PRAGMA_SIMD
53
54#elif defined(__HIP_DEVICE_COMPILE__) && defined(AMREX_USE_HIP)
55#define AMREX_PRAGMA_SIMD
56
57//#elif defined(AMREX_USE_OMP) && defined(_OPENMP) && (_OPENMP >= 201307) && !defined(__PGI)
58//#define AMREX_PRAGMA_SIMD _Pragma("omp simd")
59
60#elif defined(AMREX_CXX_INTEL)
61#define AMREX_PRAGMA_SIMD _Pragma("ivdep")
62
63#elif defined(AMREX_CXX_CRAY)
64#define AMREX_PRAGMA_SIMD _Pragma("ivdep")
65
66#elif defined(AMREX_CXX_PGI) || defined(AMREX_CXX_NVHPC)
67// nvc++ also defines __PGI. No "loop ivdep": nvc++ 26.9 miscompiles
68// loops marked with it at -O2 and above (#6197).
69#define AMREX_PRAGMA_SIMD
70
71#elif defined(AMREX_CXX_NEC)
72#define AMREX_PRAGMA_SIMD
73
74#elif defined(AMREX_CXX_IBM)
75#define AMREX_PRAGMA_SIMD _Pragma("ibm independent_loop")
76
77#elif defined(__clang__)
78#define AMREX_PRAGMA_SIMD
79
80#elif defined(__GNUC__)
81#define AMREX_PRAGMA_SIMD _Pragma("GCC ivdep")
82
83#else
84#define AMREX_PRAGMA_SIMD
85
86#endif /* simd */
87
88// force inline
89#if defined(__CUDA_ARCH__) && defined(AMREX_USE_CUDA)
90#define AMREX_FORCE_INLINE __forceinline__
91
92#elif defined(__HIP_DEVICE_COMPILE__) && defined(AMREX_USE_HIP)
93#define AMREX_FORCE_INLINE __forceinline__
94
95#elif defined(AMREX_CXX_INTEL)
96#define AMREX_FORCE_INLINE inline __attribute__((always_inline))
97
98#elif defined(AMREX_CXX_CRAY)
99#define AMREX_FORCE_INLINE inline
100
101#elif defined(AMREX_CXX_PGI)
102#define AMREX_FORCE_INLINE inline
103
104#elif defined(AMREX_CXX_NVHPC)
105#define AMREX_FORCE_INLINE inline
106
107#elif defined(AMREX_CXX_NEC)
108#define AMREX_FORCE_INLINE inline
109
110#elif defined(AMREX_CXX_IBM)
111#define AMREX_FORCE_INLINE inline __attribute__((always_inline))
112
113#elif defined(__clang__)
114#define AMREX_FORCE_INLINE inline __attribute__((always_inline))
115
116#elif defined(__GNUC__)
117#define AMREX_FORCE_INLINE inline __attribute__((always_inline))
118
119#elif defined(_MSC_VER)
120#define AMREX_FORCE_INLINE inline __forceinline
121
122#else
123#define AMREX_FORCE_INLINE inline
124
125#endif /* force inline */
126
127
128#ifdef AMREX_USE_FORCE_INLINE
129#define AMREX_INLINE AMREX_FORCE_INLINE
130#else
131#define AMREX_INLINE inline
132#endif
133
134// no inline
135#if defined(_MSC_VER)
136#define AMREX_NO_INLINE __declspec(noinline)
137#elif (defined(__GNUC__) || defined(__clang__) || defined(__CUDACC__) || defined(__HIP__) || defined(__INTEL_CLANG_COMPILER))
138#define AMREX_NO_INLINE __attribute__((noinline))
139#else
140#define AMREX_NO_INLINE
141#endif
142
143// flatten
144#if defined(_MSC_VER)
145#define AMREX_FLATTEN [[msvc::flatten]]
146#elif defined(__clang__) || defined(__GNUC__)
147#define AMREX_FLATTEN __attribute__((flatten))
148#else
149#define AMREX_FLATTEN
150#endif
151
152#ifdef AMREX_USE_FLATTEN_FOR
153#define AMREX_ATTRIBUTE_FLATTEN_FOR AMREX_FLATTEN
154#else
155#define AMREX_ATTRIBUTE_FLATTEN_FOR
156#endif
157
158// unroll loop
159#define AMREX_TO_STRING_HELPER(X) #X
160#define AMREX_TO_STRING(X) AMREX_TO_STRING_HELPER(X)
161
162#if defined(__clang__) || defined(__CUDACC__) || defined(__HIP__) || defined(__INTEL_CLANG_COMPILER)
163#define AMREX_UNROLL_LOOP(n) _Pragma(AMREX_TO_STRING(unroll n))
164#elif defined(__GNUC__)
165#define AMREX_UNROLL_LOOP(n) _Pragma(AMREX_TO_STRING(GCC unroll n))
166#else
167#define AMREX_UNROLL_LOOP(n)
168#endif
169
170// __COUNTER__
171//
172// __COUNTER__ has been a de-facto extension in all compilers AMReX supports for
173// a very long time, but it was only recently adopted into C2y. Clang 22 and
174// later therefore diagnose it as a C2y extension under -Wpedantic, even in C++
175// (https://github.com/llvm/llvm-project/issues/196557). Bracket a use of
176// __COUNTER__ with these macros to silence that diagnostic. The nested
177// -Wunknown-warning-option suppression keeps older Clang, which does not know
178// -Wc2y-extensions, quiet as well.
179//
180// Each AMREX_WARNING_PUSH_* macro silences a specific diagnostic, while the
181// single AMREX_WARNING_POP restores the previous state of whatever was pushed
182// last. The naming is intentionally asymmetric: the pops are not tied to a
183// particular warning, so pushes and pops must nest, not interleave.
184
185#if defined(__clang__)
186#define AMREX_WARNING_PUSH_COUNTER \
187 _Pragma("clang diagnostic push") \
188 _Pragma("clang diagnostic ignored \"-Wunknown-warning-option\"") \
189 _Pragma("clang diagnostic ignored \"-Wc2y-extensions\"")
190#define AMREX_WARNING_POP \
191 _Pragma("clang diagnostic pop")
192#else
193#define AMREX_WARNING_PUSH_COUNTER
194#define AMREX_WARNING_POP
195#endif
196
197// __attribute__((weak))
198
199#if defined(AMREX_TYPECHECK)
200#define AMREX_ATTRIBUTE_WEAK
201#elif defined(_WIN32)
202#define AMREX_ATTRIBUTE_WEAK
203#elif defined(__clang__) && defined(__apple_build_version__)
204#define AMREX_ATTRIBUTE_WEAK __attribute__((weak_import))
205#else
206#define AMREX_ATTRIBUTE_WEAK __attribute__((weak))
207#endif
208
209// public globals
210// https://stackoverflow.com/questions/54560832/cmake-windows-export-all-symbols-does-not-cover-global-variables/54568678#54568678
211#if defined(_MSC_VER)
212# if defined(AMREX_IS_DLL)
213# if defined(AMREX_IS_DLL_BUILDING)
214# define AMREX_EXPORT __declspec(dllexport)
215# else
216# define AMREX_EXPORT __declspec(dllimport)
217# endif
218# else
219# define AMREX_EXPORT
220# endif
221#else
222# define AMREX_EXPORT
223#endif
224
225#if defined(__cplusplus) && defined(_WIN32)
226#include <ciso646>
227#endif
228
229#if defined(__INTEL_COMPILER) && defined(__EDG__) && (__cplusplus < 201703L)
230// Classical EDG based Intel compiler does not support fallthrough when std=c++14
231# define AMREX_FALLTHROUGH ((void)0)
232#elif defined(__has_cpp_attribute) && __has_cpp_attribute(fallthrough) >= 201603L
233# define AMREX_FALLTHROUGH [[fallthrough]]
234#elif defined(__clang__)
235# define AMREX_FALLTHROUGH [[clang::fallthrough]]
236#elif defined(__GNUC__) && (__GNUC__ >= 7)
237# define AMREX_FALLTHROUGH [[gnu::fallthrough]]
238#else
239# define AMREX_FALLTHROUGH ((void)0)
240#endif
241
242// Note: following compilers support [[likely]] and [[unlikely]]
243// - Clang >= 12.0
244// - GCC >= 9.0
245// - Intel >= 2021.7
246// - MSVC >= 19.26
247// - nvcc >= 12
248#if defined(__has_cpp_attribute) && __has_cpp_attribute(likely) >= 201803L
249# define AMREX_LIKELY [[likely]]
250# define AMREX_UNLIKELY [[unlikely]]
251#else
252# define AMREX_LIKELY
253# define AMREX_UNLIKELY
254#endif
255
256// Note: following compilers support assumptions, at least using builtin functions:
257// - Clang >= 3.7
258// - GCC >= 5.1
259// - MSVC >= 19.20
260// - nvcc >= 11.1.0
261// - icx >= 2021.1.2
262//
263// [[assume]] itself is C++23. Clang >= 19 and GCC >= 13 advertise it through
264// __has_cpp_attribute in older language modes as well, and Clang then diagnoses
265// every use as a C++23 extension under -Wpedantic
266// (-Wc++23-attribute-extensions). So only take the attribute when the language
267// mode really is C++23 and rely on the compiler-specific spellings below
268// otherwise; they carry the same information to the optimizer.
269#if defined(__has_cpp_attribute) && __has_cpp_attribute(assume) >= 202207L && \
270 defined(__cplusplus) && __cplusplus >= 202302L
271# define AMREX_ASSUME(ASSUMPTION) [[assume(ASSUMPTION)]]
272#else
273# if defined(__CUDA_ARCH__) && defined(__CUDACC__)
274# define AMREX_ASSUME(ASSUMPTION) __builtin_assume(ASSUMPTION)
275# elif defined(AMREX_CXX_INTEL) || defined(__clang__)
276# define AMREX_ASSUME(ASSUMPTION) __builtin_assume(ASSUMPTION)
277# elif defined(_MSC_VER)
278# define AMREX_ASSUME(ASSUMPTION) __assume(ASSUMPTION)
279# elif defined(__GNUC__) && __GNUC__ >= 13
280 // like [[assume]], this does not evaluate ASSUMPTION
281# define AMREX_ASSUME(ASSUMPTION) __attribute__((assume(ASSUMPTION)))
282# elif defined(__GNUC__)
283# define AMREX_ASSUME(ASSUMPTION) \
284 do { if (!(ASSUMPTION)) { __builtin_unreachable(); } } while (0)
285# else
286# define AMREX_ASSUME(ASSUMPTION)
287# endif
288#endif
289
290// CI uses -Werror -Wc++17-extension, thus we need to add the __cplusplus clause
291#if !defined(AMREX_NO_NODISCARD) && defined(__has_cpp_attribute) && __has_cpp_attribute(nodiscard) >= 201603L
292# define AMREX_NODISCARD [[nodiscard]]
293#else
294# define AMREX_NODISCARD
295#endif
296
297// Note: following compilers support [[no_unique_address]]
298// - Clang >= 9.0
299// - GCC >= 9.0
300// - MSVC >= 19.26
301// Using no unique address makes empty base class optimization for multiple policies much easier
302#if !defined(AMREX_NO_NO_UNIQUE_ADDRESS) && defined(__has_cpp_attribute) && __has_cpp_attribute(no_unique_address) >= 201803L
303# define AMREX_NO_UNIQUE_ADDRESS [[no_unique_address]]
304# define AMREX_HAS_NO_UNIQUE_ADDRESS 1
305#else
306# define AMREX_NO_UNIQUE_ADDRESS
307#endif
308
309#if defined(__cpp_if_constexpr) && __cpp_if_constexpr >= 201606L
310# define AMREX_IF_CONSTEXPR if constexpr
311#else
312# define AMREX_IF_CONSTEXPR if
313#endif
314
315#endif /* !BL_LANG_FORT */
316
317#endif