Debian Codegraph @

clang/lib/Headers/gpuintrin.h

raw
1 //===-- gpuintrin.h - Generic GPU intrinsic functions ---------------------===//
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 //
9 // Provides wrappers around the clang builtins for accessing GPU hardware
10 // features. The interface is intended to be portable between architectures, but
11 // some targets may provide different implementations. This header can be
12 // included for all the common GPU programming languages, namely OpenMP, HIP,
13 // CUDA, and OpenCL.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #ifndef __GPUINTRIN_H
18 #define __GPUINTRIN_H
19
20 #if !defined(_DEFAULT_FN_ATTRS)
21 #if defined(__HIP__) || defined(__CUDA__)
22 #define _DEFAULT_FN_ATTRS __attribute__((device))
23 #else
24 #define _DEFAULT_FN_ATTRS
25 #endif
26 #endif
27
28 #include <stdint.h>
29
30 #if !defined(__cplusplus)
31 _Pragma("push_macro(\"bool\")");
32 #define bool _Bool
33 #endif
34
35 _Pragma("omp begin declare target device_type(nohost)");
36 _Pragma("omp begin declare variant match(device = {kind(gpu)})");
37
38 // Forward declare a few functions for the implementation header.
39
40 // Returns a bitmask marking all lanes that have the same value of __x.
41 _DEFAULT_FN_ATTRS static __inline__ uint64_t
42 __gpu_match_any_u32_impl(uint64_t __lane_mask, uint32_t __x);
43
44 // Returns a bitmask marking all lanes that have the same value of __x.
45 _DEFAULT_FN_ATTRS static __inline__ uint64_t
46 __gpu_match_any_u64_impl(uint64_t __lane_mask, uint64_t __x);
47
48 // Returns the current lane mask if every lane contains __x.
49 _DEFAULT_FN_ATTRS static __inline__ uint64_t
50 __gpu_match_all_u32_impl(uint64_t __lane_mask, uint32_t __x);
51
52 // Returns the current lane mask if every lane contains __x.
53 _DEFAULT_FN_ATTRS static __inline__ uint64_t
54 __gpu_match_all_u64_impl(uint64_t __lane_mask, uint64_t __x);
55
56 _Pragma("omp end declare variant");
57 _Pragma("omp end declare target");
58
59 #if defined(__NVPTX__)
60 #include <nvptxintrin.h>
61 #elif defined(__AMDGPU__)
62 #include <amdgpuintrin.h>
63 #elif defined(__SPIRV__)
64 #include <spirvintrin.h>
65 #elif !defined(_OPENMP)
66 #error "This header is only meant to be used on GPU architectures."
67 #endif
68
69 _Pragma("omp begin declare target device_type(nohost)");
70 _Pragma("omp begin declare variant match(device = {kind(gpu)})");
71
72 #define __GPU_X_DIM 0
73 #define __GPU_Y_DIM 1
74 #define __GPU_Z_DIM 2
75
76 // Returns the number of blocks in the requested dimension.
77 _DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks(int __dim) {
78 switch (__dim) {
79 case 0:
80 return __gpu_num_blocks_x();
81 case 1:
82 return __gpu_num_blocks_y();
83 case 2:
84 return __gpu_num_blocks_z();
85 default:
86 return 1;
87 }
88 }
89
90 // Returns the number of block id in the requested dimension.
91 _DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id(int __dim) {
92 switch (__dim) {
93 case 0:
94 return __gpu_block_id_x();
95 case 1:
96 return __gpu_block_id_y();
97 case 2:
98 return __gpu_block_id_z();
99 default:
100 return 0;
101 }
102 }
103
104 // Returns the number of threads in the requested dimension.
105 _DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads(int __dim) {
106 switch (__dim) {
107 case 0:
108 return __gpu_num_threads_x();
109 case 1:
110 return __gpu_num_threads_y();
111 case 2:
112 return __gpu_num_threads_z();
113 default:
114 return 1;
115 }
116 }
117
118 // Returns the thread id in the requested dimension.
119 _DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id(int __dim) {
120 switch (__dim) {
121 case 0:
122 return __gpu_thread_id_x();
123 case 1:
124 return __gpu_thread_id_y();
125 case 2:
126 return __gpu_thread_id_z();
127 default:
128 return 0;
129 }
130 }
131
132 // Get the first active thread inside the lane.
133 _DEFAULT_FN_ATTRS static __inline__ uint64_t
134 __gpu_first_lane_id(uint64_t __lane_mask) {
135 return __builtin_ffsll(__lane_mask) - 1;
136 }
137
138 // Conditional that is only true for a single thread in a lane.
139 _DEFAULT_FN_ATTRS static __inline__ bool
140 __gpu_is_first_in_lane(uint64_t __lane_mask) {
141 return __gpu_lane_id() == __gpu_first_lane_id(__lane_mask);
142 }
143
144 // Copies the value from the first active thread to the rest.
145 _DEFAULT_FN_ATTRS static __inline__ uint64_t
146 __gpu_read_first_lane_u64(uint64_t __lane_mask, uint64_t __x) {
147 uint32_t __hi = (uint32_t)(__x >> 32ull);
148 uint32_t __lo = (uint32_t)(__x & 0xFFFFFFFFull);
149 return ((uint64_t)__gpu_read_first_lane_u32(__lane_mask, __hi) << 32ull) |
150 ((uint64_t)__gpu_read_first_lane_u32(__lane_mask, __lo) &
151 0xFFFFFFFFull);
152 }
153
154 // Gets the first floating point value from the active lanes.
155 _DEFAULT_FN_ATTRS static __inline__ float
156 __gpu_read_first_lane_f32(uint64_t __lane_mask, float __x) {
157 return __builtin_bit_cast(
158 float, __gpu_read_first_lane_u32(__lane_mask,
159 __builtin_bit_cast(uint32_t, __x)));
160 }
161
162 // Gets the first floating point value from the active lanes.
163 _DEFAULT_FN_ATTRS static __inline__ double
164 __gpu_read_first_lane_f64(uint64_t __lane_mask, double __x) {
165 return __builtin_bit_cast(
166 double, __gpu_read_first_lane_u64(__lane_mask,
167 __builtin_bit_cast(uint64_t, __x)));
168 }
169
170 // Shuffles the the lanes according to the given index.
171 _DEFAULT_FN_ATTRS static __inline__ uint64_t
172 __gpu_shuffle_idx_u64(uint64_t __lane_mask, uint32_t __idx, uint64_t __x,
173 uint32_t __width) {
174 uint32_t __hi = (uint32_t)(__x >> 32ull);
175 uint32_t __lo = (uint32_t)(__x & 0xFFFFFFFF);
176 uint32_t __mask = (uint32_t)__lane_mask;
177 return ((uint64_t)__gpu_shuffle_idx_u32(__mask, __idx, __hi, __width)
178 << 32ull) |
179 ((uint64_t)__gpu_shuffle_idx_u32(__mask, __idx, __lo, __width));
180 }
181
182 // Shuffles the the lanes according to the given index.
183 _DEFAULT_FN_ATTRS static __inline__ float
184 __gpu_shuffle_idx_f32(uint64_t __lane_mask, uint32_t __idx, float __x,
185 uint32_t __width) {
186 return __builtin_bit_cast(
187 float, __gpu_shuffle_idx_u32(__lane_mask, __idx,
188 __builtin_bit_cast(uint32_t, __x), __width));
189 }
190
191 // Shuffles the the lanes according to the given index.
192 _DEFAULT_FN_ATTRS static __inline__ double
193 __gpu_shuffle_idx_f64(uint64_t __lane_mask, uint32_t __idx, double __x,
194 uint32_t __width) {
195 return __builtin_bit_cast(
196 double,
197 __gpu_shuffle_idx_u64(__lane_mask, __idx,
198 __builtin_bit_cast(uint64_t, __x), __width));
199 }
200
201 // Gets the accumulator scan of the threads in the warp or wavefront.
202 #define __DO_LANE_SCAN(__type, __bitmask_type, __suffix) \
203 _DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_scan_##__suffix( \
204 uint64_t __lane_mask, uint32_t __x) { \
205 uint64_t __first = __lane_mask >> __builtin_ctzll(__lane_mask); \
206 bool __divergent = __gpu_read_first_lane_##__suffix( \
207 __lane_mask, __first & (__first + 1)); \
208 if (__divergent) { \
209 __type __accum = 0; \
210 for (uint64_t __mask = __lane_mask; __mask; __mask &= __mask - 1) { \
211 __type __index = __builtin_ctzll(__mask); \
212 __type __tmp = __gpu_shuffle_idx_##__suffix(__lane_mask, __index, __x, \
213 __gpu_num_lanes()); \
214 __x = __gpu_lane_id() == __index ? __accum + __tmp : __x; \
215 __accum += __tmp; \
216 } \
217 } else { \
218 for (uint32_t __step = 1; __step < __gpu_num_lanes(); __step *= 2) { \
219 uint32_t __index = __gpu_lane_id() - __step; \
220 __bitmask_type bitmask = __gpu_lane_id() >= __step; \
221 __x += __builtin_bit_cast( \
222 __type, \
223 -bitmask & __builtin_bit_cast(__bitmask_type, \
224 __gpu_shuffle_idx_##__suffix( \
225 __lane_mask, __index, __x, \
226 __gpu_num_lanes()))); \
227 } \
228 } \
229 return __x; \
230 }
231 __DO_LANE_SCAN(uint32_t, uint32_t, u32); // uint32_t __gpu_lane_scan_u32(m, x)
232 __DO_LANE_SCAN(uint64_t, uint64_t, u64); // uint64_t __gpu_lane_scan_u64(m, x)
233 __DO_LANE_SCAN(float, uint32_t, f32); // float __gpu_lane_scan_f32(m, x)
234 __DO_LANE_SCAN(double, uint64_t, f64); // double __gpu_lane_scan_f64(m, x)
235 #undef __DO_LANE_SCAN
236
237 // Gets the sum of all lanes inside the warp or wavefront.
238 #define __DO_LANE_SUM(__type, __suffix) \
239 _DEFAULT_FN_ATTRS static __inline__ __type __gpu_lane_sum_##__suffix( \
240 uint64_t __lane_mask, __type __x) { \
241 uint64_t __first = __lane_mask >> __builtin_ctzll(__lane_mask); \
242 bool __divergent = __gpu_read_first_lane_##__suffix( \
243 __lane_mask, __first & (__first + 1)); \
244 if (__divergent) { \
245 return __gpu_shuffle_idx_##__suffix( \
246 __lane_mask, 63 - __builtin_clzll(__lane_mask), \
247 __gpu_lane_scan_##__suffix(__lane_mask, __x), __gpu_num_lanes()); \
248 } else { \
249 for (uint32_t __step = 1; __step < __gpu_num_lanes(); __step *= 2) { \
250 uint32_t __index = __step + __gpu_lane_id(); \
251 __x += __gpu_shuffle_idx_##__suffix(__lane_mask, __index, __x, \
252 __gpu_num_lanes()); \
253 } \
254 return __gpu_read_first_lane_##__suffix(__lane_mask, __x); \
255 } \
256 }
257 __DO_LANE_SUM(uint32_t, u32); // uint32_t __gpu_lane_sum_u32(m, x)
258 __DO_LANE_SUM(uint64_t, u64); // uint64_t __gpu_lane_sum_u64(m, x)
259 __DO_LANE_SUM(float, f32); // float __gpu_lane_sum_f32(m, x)
260 __DO_LANE_SUM(double, f64); // double __gpu_lane_sum_f64(m, x)
261 #undef __DO_LANE_SUM
262
263 // Returns a bitmask marking all lanes that have the same value of __x.
264 _DEFAULT_FN_ATTRS static __inline__ uint64_t
265 __gpu_match_any_u32_impl(uint64_t __lane_mask, uint32_t __x) {
266 uint64_t __match_mask = 0;
267
268 bool __done = 0;
269 for (uint64_t __active_mask = __lane_mask; __active_mask;
270 __active_mask = __gpu_ballot(__lane_mask, !__done)) {
271 if (!__done) {
272 uint32_t __first = __gpu_read_first_lane_u32(__active_mask, __x);
273 if (__first == __x) {
274 __match_mask = __gpu_lane_mask();
275 __done = 1;
276 }
277 }
278 }
279 __gpu_sync_lane(__lane_mask);
280 return __match_mask;
281 }
282
283 // Returns a bitmask marking all lanes that have the same value of __x.
284 _DEFAULT_FN_ATTRS static __inline__ uint64_t
285 __gpu_match_any_u64_impl(uint64_t __lane_mask, uint64_t __x) {
286 uint64_t __match_mask = 0;
287
288 bool __done = 0;
289 for (uint64_t __active_mask = __lane_mask; __active_mask;
290 __active_mask = __gpu_ballot(__lane_mask, !__done)) {
291 if (!__done) {
292 uint64_t __first = __gpu_read_first_lane_u64(__active_mask, __x);
293 if (__first == __x) {
294 __match_mask = __gpu_lane_mask();
295 __done = 1;
296 }
297 }
298 }
299 __gpu_sync_lane(__lane_mask);
300 return __match_mask;
301 }
302
303 // Returns the current lane mask if every lane contains __x.
304 _DEFAULT_FN_ATTRS static __inline__ uint64_t
305 __gpu_match_all_u32_impl(uint64_t __lane_mask, uint32_t __x) {
306 uint32_t __first = __gpu_read_first_lane_u32(__lane_mask, __x);
307 uint64_t __ballot = __gpu_ballot(__lane_mask, __x == __first);
308 __gpu_sync_lane(__lane_mask);
309 return __ballot == __gpu_lane_mask() ? __gpu_lane_mask() : 0ull;
310 }
311
312 // Returns the current lane mask if every lane contains __x.
313 _DEFAULT_FN_ATTRS static __inline__ uint64_t
314 __gpu_match_all_u64_impl(uint64_t __lane_mask, uint64_t __x) {
315 uint64_t __first = __gpu_read_first_lane_u64(__lane_mask, __x);
316 uint64_t __ballot = __gpu_ballot(__lane_mask, __x == __first);
317 __gpu_sync_lane(__lane_mask);
318 return __ballot == __gpu_lane_mask() ? __gpu_lane_mask() : 0ull;
319 }
320
321 _Pragma("omp end declare variant");
322 _Pragma("omp end declare target");
323
324 #if !defined(__cplusplus)
325 _Pragma("pop_macro(\"bool\")");
326 #endif
327
328 #undef _DEFAULT_FN_ATTRS
329
330 #endif // __GPUINTRIN_H