Low-Level Abstraction of Memory Access
macros.hpp
Go to the documentation of this file.
1 // Copyright 2022 Alexander Matthes, Bernhard Manfred Gruber
2 // SPDX-License-Identifier: MPL-2.0
3 
4 #pragma once
5 
6 #ifdef __INTEL_COMPILER
7 # error LLAMA has stopped supporting the Intel Classic Compiler after Intel announced its planned deprecation and \
8  replacement by the Intel LLVM-based compiler. Please migrate to the Intel LLVM-based compiler.
9 #endif
10 
11 #if defined(__INTEL_LLVM_COMPILER)
12 // icx supports #pragma ivdep, but it will issue a diagnostic that it needs vectorize(assume_safety) to vectorize.
13 // Let's keep both pragmas for now.
14 # define LLAMA_INDEPENDENT_DATA \
15  _Pragma("ivdep") _Pragma("clang loop vectorize(assume_safety) interleave(assume_safety)")
16 #elif defined(__clang__)
17 # define LLAMA_INDEPENDENT_DATA _Pragma("clang loop vectorize(assume_safety) interleave(assume_safety)")
18 #elif defined(__NVCOMPILER)
19 # define LLAMA_INDEPENDENT_DATA _Pragma("ivdep")
20 #elif defined(__GNUC__)
21 # define LLAMA_INDEPENDENT_DATA _Pragma("GCC ivdep")
22 #elif defined(_MSC_VER)
23 # define LLAMA_INDEPENDENT_DATA __pragma(loop(ivdep))
24 #else
33 # define LLAMA_INDEPENDENT_DATA
34 #endif
35 
36 #ifndef LLAMA_FORCE_INLINE
37 # if defined(__NVCC__) || defined(__HIP__)
38 # define LLAMA_FORCE_INLINE __forceinline__
39 # elif defined(__GNUC__) || defined(__clang__)
40 # define LLAMA_FORCE_INLINE inline __attribute__((always_inline))
41 # elif defined(_MSC_VER) || defined(__INTEL_LLVM_COMPILER)
42 # define LLAMA_FORCE_INLINE __forceinline
43 # else
45 # define LLAMA_FORCE_INLINE inline
46 # warning LLAMA_FORCE_INLINE is only defined to "inline" for this compiler
47 # endif
48 #endif
49 
50 #ifndef LLAMA_PRAGMA
51 # define LLAMA_PRAGMA(tokens) _Pragma(#tokens)
52 #endif
53 
54 #ifndef LLAMA_UNROLL
55 # if defined(__HIP__) || defined(__NVCC__) || defined(__NVCOMPILER) || defined(__clang__) \
56  || defined(__INTEL_LLVM_COMPILER)
57 # define LLAMA_UNROLL(...) LLAMA_PRAGMA(unroll __VA_ARGS__)
58 # elif defined(__GNUG__)
59 # define LLAMA_UNROLL(...) LLAMA_PRAGMA(GCC unroll __VA_ARGS__)
60 # elif defined(_MSC_VER)
61 // MSVC does not support a pragma for unrolling
62 # define LLAMA_UNROLL(...)
63 # else
66 # define LLAMA_UNROLL(...)
67 # warning LLAMA_UNROLL is not implemented for your compiler
68 # endif
69 #endif
70 
71 #ifndef LLAMA_ACC
72 # if defined(__HIP__) || defined(__NVCC__) || (defined(__clang__) && defined(__CUDA__))
73 # define LLAMA_ACC __device__
74 # elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) || defined(__INTEL_LLVM_COMPILER)
75 # define LLAMA_ACC
76 # else
77 # define LLAMA_ACC
78 # warning LLAMA_HOST_ACC is only defined empty for this compiler
79 # endif
80 #endif
81 
82 #ifndef LLAMA_HOST_ACC
83 # if defined(__HIP__) || defined(__NVCC__) || (defined(__clang__) && defined(__CUDA__))
84 # define LLAMA_HOST_ACC __host__ __device__
85 # elif defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) || defined(__INTEL_LLVM_COMPILER)
86 # define LLAMA_HOST_ACC
87 # else
91 # define LLAMA_HOST_ACC
92 # warning LLAMA_HOST_ACC is only defined empty for this compiler
93 # endif
94 #endif
95 
96 #define LLAMA_FN_HOST_ACC_INLINE LLAMA_FORCE_INLINE LLAMA_HOST_ACC
97 
98 #ifndef LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS
99 # if defined(__clang__) || defined(__INTEL_LLVM_COMPILER)
100 # define LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS(...) __attribute__((always_inline)) __VA_ARGS__
101 # elif defined(__GNUC__) || (defined(__NVCC__) && !defined(_MSC_VER))
102 # define LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS(...) __VA_ARGS__ __attribute__((always_inline))
103 # elif defined(_MSC_VER)
104 # define LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS(...) \
105  __VA_ARGS__ /* FIXME: MSVC cannot combine constexpr and [[msvc::forceinline]] */
106 # else
107 # define LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS(...) __VA_ARGS__
108 # warning LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS not defined for this compiler
109 # endif
110 #endif
111 #ifndef LLAMA_LAMBDA_INLINE
113 # define LLAMA_LAMBDA_INLINE LLAMA_LAMBDA_INLINE_WITH_SPECIFIERS()
114 #endif
115 
116 #ifndef LLAMA_SUPPRESS_HOST_DEVICE_WARNING
117 # if defined(__NVCC__) && !defined(__clang__)
118 # define LLAMA_SUPPRESS_HOST_DEVICE_WARNING _Pragma("nv_exec_check_disable")
119 # else
122 # define LLAMA_SUPPRESS_HOST_DEVICE_WARNING
123 # endif
124 #endif
125 
126 #ifndef LLAMA_BEGIN_SUPPRESS_HOST_DEVICE_WARNING
127 # ifdef __NVCC__
128 # ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
129 # define LLAMA_BEGIN_SUPPRESS_HOST_DEVICE_WARNING \
130  _Pragma("nv_diag_suppress 20011") _Pragma("nv_diag_suppress 20014")
131 # else
132 # define LLAMA_BEGIN_SUPPRESS_HOST_DEVICE_WARNING \
133  _Pragma("diag_suppress 20011") _Pragma("diag_suppress 20014")
134 # endif
135 # else
141 # define LLAMA_BEGIN_SUPPRESS_HOST_DEVICE_WARNING
142 # endif
143 #endif
144 #ifndef LLAMA_END_SUPPRESS_HOST_DEVICE_WARNING
145 # ifdef __NVCC__
146 # ifdef __NVCC_DIAG_PRAGMA_SUPPORT__
147 # define LLAMA_END_SUPPRESS_HOST_DEVICE_WARNING \
148  _Pragma("nv_diag_default 20011") _Pragma("nv_diag_default 20014")
149 # else
150 # define LLAMA_END_SUPPRESS_HOST_DEVICE_WARNING _Pragma("diag_default 20011") _Pragma("diag_default 20014")
151 # endif
152 # else
153 # define LLAMA_END_SUPPRESS_HOST_DEVICE_WARNING
154 # endif
155 #endif
156 
158 #define LLAMA_COPY(x) decltype(x)(x)
159 
160 // https://devblogs.microsoft.com/cppblog/optimizing-the-layout-of-empty-base-classes-in-vs2015-update-2-3/
161 #if defined(_MSC_VER)
162 # define LLAMA_DECLSPEC_EMPTY_BASES __declspec(empty_bases)
163 #else
164 # define LLAMA_DECLSPEC_EMPTY_BASES
165 #endif
166 
168 #if __has_cpp_attribute(likely)
169 # define LLAMA_LIKELY likely
170 #else
171 # define LLAMA_LIKELY
172 #endif
173 
175 #if __has_cpp_attribute(unlikely)
176 # define LLAMA_UNLIKELY unlikely
177 #else
178 # define LLAMA_UNLIKELY
179 #endif
180 
182 // TODO(bgruber): reevalute with nvhpc in the future or find workaround
183 #if defined(__cpp_consteval) && !defined(__NVCOMPILER)
184 # define LLAMA_CONSTEVAL consteval
185 #else
186 # define LLAMA_CONSTEVAL constexpr
187 #endif
188 
189 #ifndef LLAMA_EXPORT
192 # define LLAMA_EXPORT
193 #endif
194 
195 // TODO(bgruber): clang 12-15 (libstdc++ from gcc 11.2 or gcc 12.1) fail to compile this currently with the issue
196 // described here:
197 // https://stackoverflow.com/questions/64300832/why-does-clang-think-gccs-subrange-does-not-satisfy-gccs-ranges-begin-functi
198 // Intel LLVM compiler is also using the clang frontend
199 #define CAN_USE_RANGES 0
200 #if __has_include(<version>)
201 # include <version>
202 # if defined(__cpp_concepts) && defined(__cpp_lib_ranges) && (!defined(__clang__) || __clang_major__ >= 16) \
203  && !defined(__INTEL_LLVM_COMPILER) && (!defined(_MSC_VER) || _MSC_VER > 1932) && !defined(__NVCOMPILER)
204 # undef CAN_USE_RANGES
205 # define CAN_USE_RANGES 1
206 # endif
207 #endif