Debian Codegraph @

IGC/VectorCompiler/CMCL/lib/Headers/cm-cl/detail/builtins.h

raw
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2021-2024 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #ifndef CM_CL_DETAIL_BUILTINS_H
10 #define CM_CL_DETAIL_BUILTINS_H
11
12 #include "vector_impl.h"
13 #include <cm-cl/define.h>
14
15 #include <opencl_def.h>
16 #include <opencl_utility.h>
17
18 namespace cm {
19 namespace detail {
20
21 //=========================== builtin declarations ===========================//
22
23 template <typename T, int width>
24 vector_impl<T, width> __cm_cl_select(vector_impl<char, width> cond,
25 vector_impl<T, width> true_val,
26 vector_impl<T, width> false_val);
27
28 template <int dst_width, typename T, int src_width>
29 vector_impl<T, dst_width>
30 __cm_cl_rdregion_int(vector_impl<T, src_width> src, int vstride, int width,
31 int stride, vector_offset_type offset);
32
33 template <int dst_width, typename T, int src_width>
34 vector_impl<T, dst_width>
35 __cm_cl_rdregion_float(vector_impl<T, src_width> src, int vstride, int width,
36 int stride, vector_offset_type offset);
37
38 template <int dst_width, typename T, int src_width>
39 vector_impl<T, dst_width>
40 __cm_cl_wrregion_int(vector_impl<T, dst_width> dst,
41 vector_impl<T, src_width> src, int vstride, int width,
42 int stride, vector_offset_type offset);
43
44 template <int dst_width, typename T, int src_width>
45 vector_impl<T, dst_width>
46 __cm_cl_wrregion_float(vector_impl<T, dst_width> dst,
47 vector_impl<T, src_width> src, int vstride, int width,
48 int stride, vector_offset_type offset);
49
50 void __cm_cl_debugtrap();
51 _Noreturn void __cm_cl_trap();
52
53 // FIXME: For legacy issues 64-bit pointer is always returned.
54 uint64_t __cm_cl_assert_buffer();
55 uint64_t __cm_cl_printf_buffer();
56 uint64_t __cm_cl_sync_buffer();
57
58 int __cm_cl_printf_format_index(__constant const char *str);
59 // DPC++ tend to place constant strings in global address space.
60 int __cm_cl_printf_format_index(__global const char *str);
61 // FIXME: Need this overload as a workaround for some frontends that didn't
62 // switch to using addrspaces.
63 int __cm_cl_printf_format_index(__private const char *str);
64
65 template <int width> bool __cm_cl_all(vector_impl<char, width> src);
66 template <int width> bool __cm_cl_any(vector_impl<char, width> src);
67
68 // clang does not allow vector of pointers so passing vector of uint64_t(s)
69 // and creating vector of pointers with inttoptr llvm instruction
70 template <typename T, int width>
71 vector_impl<T, width>
72 __cm_cl_gather(int addrspace, vector_impl<uint64_t, width> ptrs, int alignment,
73 vector_impl<char, width> mask, vector_impl<T, width> passthru);
74
75 template <typename T, int width>
76 void __cm_cl_scatter(vector_impl<T, width> data, int addrspace,
77 vector_impl<uint64_t, width> ptrs, int alignment,
78 vector_impl<char, width> mask);
79
80 // vector BTI atomic
81 template <typename T, int width, int cachelevels>
82 vector_impl<T, width> __cm_cl_vector_atomic_bti(
83 vector_impl<char, width> mask, char opcode, char addrsize, char elementsize,
84 vector_impl<char, cachelevels> cachecontrols, int bti,
85 vector_impl<int, width> index, short scale, int offset,
86 vector_impl<T, width> src1, vector_impl<T, width> src2,
87 vector_impl<T, width> passthru);
88
89 // vector SLM atomic
90 template <typename T, int width, int cachelevels>
91 vector_impl<T, width> __cm_cl_vector_atomic_slm(
92 vector_impl<char, width> mask, char opcode, char addrsize, char elementsize,
93 vector_impl<char, cachelevels> cachecontrols, int base,
94 vector_impl<int, width> index, short scale, int offset,
95 vector_impl<T, width> src1, vector_impl<T, width> src2,
96 vector_impl<T, width> passthru);
97
98 // vector UGM atomic
99 template <typename T, int width, int cachelevels>
100 vector_impl<T, width> __cm_cl_vector_atomic_ugm(
101 vector_impl<char, width> mask, char opcode, char addrsize, char elementsize,
102 vector_impl<char, cachelevels> cachecontrols, long base,
103 vector_impl<long, width> index, short scale, int offset,
104 vector_impl<T, width> src1, vector_impl<T, width> src2,
105 vector_impl<T, width> passthru);
106
107 uint32_t __cm_cl_lzd(uint32_t src);
108 template <int width>
109 vector_impl<uint32_t, width> __cm_cl_lzd(vector_impl<uint32_t, width> src);
110
111 uint32_t __cm_cl_addc(uint32_t *sum, uint32_t src0, uint32_t src1);
112 template <int width>
113 vector_impl<uint32_t, width> __cm_cl_addc(vector_impl<uint32_t, width> *sum,
114 vector_impl<uint32_t, width> src0,
115 vector_impl<uint32_t, width> src1);
116
117 template <typename T> uint32_t __cm_cl_cbit(T src);
118 template <typename T, int width>
119 vector_impl<uint32_t, width> __cm_cl_cbit(vector_impl<T, width> src);
120
121 template <typename T> T __cm_cl_smin(T src0, T src1);
122 template <typename T> T __cm_cl_smax(T src0, T src1);
123 template <typename T> T __cm_cl_umin(T src0, T src1);
124 template <typename T> T __cm_cl_umax(T src0, T src1);
125
126 template <typename T> T __cm_cl_fma(T src0, T src1, T src2);
127
128 uint32_t __cm_cl_bfrev(uint32_t src);
129 template <int width>
130 vector_impl<uint32_t, width> __cm_cl_bfrev(vector_impl<uint32_t, width> src);
131
132 template <typename T> T __cm_cl_abs_int(T src);
133 template <typename T> T __cm_cl_abs_float(T src);
134 template <typename T> T __cm_cl_frcp(T src);
135
136 template <typename T> T __cm_cl_ceil(T src);
137 template <typename T> T __cm_cl_floor(T src);
138 template <typename T> T __cm_cl_trunc(T src);
139 template <typename T> T __cm_cl_roundne(T src);
140
141 template <typename T> T __cm_cl_minnum(T src0, T src1);
142 template <typename T> T __cm_cl_maxnum(T src0, T src1);
143
144 template <typename T> T __cm_cl_sqrt(T src, bool use_fast);
145 template <typename T> T __cm_cl_log2(T src, bool use_fast);
146 template <typename T> T __cm_cl_exp2(T src, bool use_fast);
147 template <typename T> T __cm_cl_powr(T src0, T src1, bool use_fast);
148 template <typename T> T __cm_cl_sin(T src, bool use_fast);
149 template <typename T> T __cm_cl_cos(T src, bool use_fast);
150
151 template <typename T> T __cm_cl_fdiv_ieee(T src0, T src1);
152 template <typename T> T __cm_cl_rsqrt(T src);
153
154 template <typename T, int width>
155 vector_impl<T, width> __cm_cl_mrsqrt(vector_impl<char, width> *dst_pred,
156 vector_impl<T, width> src);
157
158 template <typename T, int width>
159 cl::pair<vector_impl<T, width>, vector_impl<char, width>>
160 mrsqrt(vector_impl<T, width> src0) {
161 vector_impl<char, width> dst_pred;
162 vector_impl<T, width> res = __cm_cl_mrsqrt(&dst_pred, src0);
163 return {res, dst_pred};
164 }
165
166 template <typename T, int width>
167 vector_impl<T, width> __cm_cl_invm(vector_impl<char, width> *out,
168 vector_impl<T, width> src0,
169 vector_impl<T, width> src1);
170
171 template <typename T, int width>
172 cl::pair<vector_impl<T, width>, vector_impl<char, width>>
173 invm(vector_impl<T, width> src0, vector_impl<T, width> src1) {
174 vector_impl<char, width> dst_pred;
175 vector_impl<T, width> res = __cm_cl_invm(&dst_pred, src0, src1);
176 return {res, dst_pred};
177 }
178
179 vector_impl<uint32_t, 3> __cm_cl_local_id();
180 vector_impl<uint32_t, 3> __cm_cl_local_size();
181 vector_impl<uint32_t, 3> __cm_cl_group_count();
182 uint32_t __cm_cl_group_id_x();
183 uint32_t __cm_cl_group_id_y();
184 uint32_t __cm_cl_group_id_z();
185
186 int __cm_cl_hw_thread_id();
187
188 void __cm_cl_barrier();
189 void __cm_cl_sbarrier(uint8_t);
190 void __cm_cl_fence(memory_order semantics, memory_scope scope);
191
192 template <typename T>
193 T __cm_cl_atomicrmw(__global T *ptr, atomic::operation operation, T operand,
194 memory_order semantics, memory_scope scope);
195
196 template <typename T>
197 T __cm_cl_atomicrmw(__local T *ptr, atomic::operation operation, T operand,
198 memory_order semantics, memory_scope scope);
199
200 template <typename T>
201 T __cm_cl_atomicrmw(__generic T *ptr, atomic::operation operation, T operand,
202 memory_order semantics, memory_scope scope);
203
204 template <typename T>
205 T __cm_cl_cmpxchg(__global T *ptr, T operand0, T operand1,
206 memory_order semantics_on_success,
207 memory_order semantics_on_failure, memory_scope scope);
208
209 template <typename T>
210 T __cm_cl_cmpxchg(__local T *ptr, T operand0, T operand1,
211 memory_order semantics_on_success,
212 memory_order semantics_on_failure, memory_scope scope);
213
214 template <typename T>
215 T __cm_cl_cmpxchg(__generic T *ptr, T operand0, T operand1,
216 memory_order semantics_on_success,
217 memory_order semantics_on_failure, memory_scope scope);
218
219 //========================= soft implementation part =========================//
220 //
221 // This implementations are enabled via CM_CL_SOFT_BUILTINS option.
222 // The idea is to implement all builtins using only vanilla clang means, without
223 // requirment of cm-cl builtins support.
224
225 template <typename T, int width>
226 vector_impl<T, width> select_impl(vector_impl<char, width> cond,
227 vector_impl<T, width> true_val,
228 vector_impl<T, width> false_val) {
229 vector_impl<T, width> res;
230 for (int i = 0; i != width; ++i)
231 res[i] = cond[i] ? true_val[i] : false_val[i];
232 return res;
233 }
234
235 //============================= helper functions =============================//
236
237 constexpr bool is_legal_region(int vwidth, int vstride, int width, int stride) {
238 return vwidth > 0 && width > 0 && vstride >= 0 && stride >= 0;
239 }
240
241 constexpr int encode_num_blocks(int num_blocks) {
242 switch (num_blocks) {
243 case 1:
244 return 0;
245 case 2:
246 return 1;
247 case 4:
248 return 2;
249 case 8:
250 return 3;
251 default:
252 return -1;
253 }
254 }
255
256 //============================= builtin wrappers =============================//
257
258 template <typename T, int width>
259 vector_impl<T, width> select(vector_impl<char, width> cond,
260 vector_impl<T, width> true_val,
261 vector_impl<T, width> false_val) {
262 #ifdef CM_CL_SOFT_BUILTINS
263 return select_impl(cond, true_val, false_val);
264 #else // CM_CL_SOFT_BUILTINS
265 return __cm_cl_select(cond, true_val, false_val);
266 #endif // CM_CL_SOFT_BUILTINS
267 }
268
269 // Unlike __cm_cl_rdregion \p offset here is in T elements, not bytes.
270 template <int vwidth, int vstride, int width, int stride, typename T,
271 int src_width>
272 vector_impl<T, vwidth * width> read_region(vector_impl<T, src_width> src,
273 vector_offset_type offset) {
274 static_assert(is_legal_region(vwidth, vstride, width, stride),
275 "provided region is illegal");
276 if constexpr (width == 1 && vwidth == 1)
277 return src[offset];
278 else {
279 if constexpr (cl::is_floating_point<T>::value)
280 return __cm_cl_rdregion_float<vwidth * width>(src, vstride, width, stride,
281 offset * sizeof(T));
282 else
283 return __cm_cl_rdregion_int<vwidth * width>(src, vstride, width, stride,
284 offset * sizeof(T));
285 }
286 }
287
288 // Unlike __cm_cl_wrregion \p offset here is in T elements, not bytes.
289 template <int vstride, int width, int stride, typename T, int dst_width,
290 int src_width>
291 void write_region(vector_impl<T, dst_width> &dst, vector_impl<T, src_width> src,
292 vector_offset_type offset) {
293 static_assert(is_legal_region((src_width / width), vstride, width, stride) &&
294 src_width % width == 0,
295 "provided region is illegal");
296 if constexpr (src_width == 1 && width == 1)
297 dst[offset] = src[0];
298 else {
299 if constexpr (cl::is_floating_point<T>::value)
300 dst = __cm_cl_wrregion_float(dst, src, vstride, width, stride,
301 offset * sizeof(T));
302 else
303 dst = __cm_cl_wrregion_int(dst, src, vstride, width, stride,
304 offset * sizeof(T));
305 }
306 }
307
308 struct __assert_buffer_t {
309 uint32_t size;
310 uint32_t flags;
311 uint32_t begin;
312 };
313
314 inline __global uint32_t *assert_flags() {
315 auto addr = static_cast<uintptr_t>(__cm_cl_assert_buffer());
316 __global __assert_buffer_t *p =
317 reinterpret_cast<__global __assert_buffer_t *>(addr);
318 return &p->flags;
319 }
320
321 inline __global void *assert_buffer() {
322 auto addr = static_cast<uintptr_t>(__cm_cl_assert_buffer());
323 __global __assert_buffer_t *p =
324 reinterpret_cast<__global __assert_buffer_t *>(addr);
325 return &p->begin;
326 }
327
328 inline __global void *printf_buffer() {
329 auto ptr = static_cast<uintptr_t>(__cm_cl_printf_buffer());
330 return reinterpret_cast<__global void *>(ptr);
331 }
332
333 inline __global uint8_t *sync_buffer() {
334 auto ptr = static_cast<uintptr_t>(__cm_cl_sync_buffer());
335 return reinterpret_cast<__global uint8_t *>(ptr);
336 }
337
338 inline int printf_format_index(__constant const char *str) {
339 return __cm_cl_printf_format_index(str);
340 }
341
342 inline int printf_format_index(__global const char *str) {
343 return __cm_cl_printf_format_index(str);
344 }
345
346 inline int printf_format_index(__private const char *str) {
347 return __cm_cl_printf_format_index(str);
348 }
349
350 inline uint32_t lzd(uint32_t src) { return __cm_cl_lzd(src); }
351
352 template <int width>
353 vector_impl<uint32_t, width> lzd(vector_impl<uint32_t, width> src) {
354 return __cm_cl_lzd(src);
355 }
356
357 // Sum is the first output, carry - the second.
358 inline cl::pair<uint32_t, char> addc(uint32_t src0, uint32_t src1) {
359 uint32_t res;
360 uint32_t carry = __cm_cl_addc(&res, src0, src1);
361 return {res, carry};
362 }
363
364 template <int width>
365 cl::pair<vector_impl<uint32_t, width>, vector_impl<char, width>>
366 addc(vector_impl<uint32_t, width> src0, vector_impl<uint32_t, width> src1) {
367 vector_impl<uint32_t, width> res;
368 vector_impl<uint32_t, width> carry = __cm_cl_addc(&res, src0, src1);
369 return {res, __builtin_convertvector(carry, vector_impl<char, width>)};
370 }
371
372 inline vector_impl<uint32_t, 3> get_local_id() { return __cm_cl_local_id(); }
373
374 inline vector_impl<uint32_t, 3> get_local_size() {
375 return __cm_cl_local_size();
376 }
377
378 inline vector_impl<uint32_t, 3> get_group_count() {
379 return __cm_cl_group_count();
380 }
381
382 inline uint32_t get_group_id_x() { return __cm_cl_group_id_x(); }
383
384 inline uint32_t get_group_id_y() { return __cm_cl_group_id_y(); }
385
386 inline uint32_t get_group_id_z() { return __cm_cl_group_id_z(); }
387
388 template <typename T, int width>
389 vector_impl<uint32_t, width> cbit(vector_impl<T, width> src) {
390 static_assert(cl::is_integral<T>::value && !cl::is_bool<T>::value &&
391 sizeof(T) <= sizeof(uint32_t),
392 "illegal type provided in cbit");
393 return __cm_cl_cbit(src);
394 }
395
396 template <typename T> uint32_t cbit(T src) {
397 static_assert(cl::is_integral<T>::value && !cl::is_bool<T>::value &&
398 sizeof(T) <= sizeof(uint32_t),
399 "illegal type provided in cbit");
400 return __cm_cl_cbit(src);
401 }
402
403 template <typename T> T fma(T src0, T src1, T src2) {
404 static_assert(cl::is_floating_point<T>::value,
405 "illegal type provided in fma");
406 return __cm_cl_fma(src0, src1, src2);
407 }
408
409 inline uint32_t bfrev(uint32_t src) { return __cm_cl_bfrev(src); }
410
411 template <int width>
412 vector_impl<uint32_t, width> bfrev(vector_impl<uint32_t, width> src) {
413 return __cm_cl_bfrev(src);
414 }
415
416 template <typename T, int width>
417 vector_impl<T, width> reciprocal(vector_impl<T, width> src) {
418 static_assert(cl::is_floating_point<T>::value,
419 "illegal type provided in reciprocal");
420 return __cm_cl_frcp(src);
421 }
422
423 // Usigned values are returned without a change.
424 template <typename T> T absolute(T src) {
425 if constexpr (cl::is_floating_point<T>::value)
426 return __cm_cl_abs_float(src);
427 else
428 static_assert(cl::is_integral<T>::value && !cl::is_bool<T>::value,
429 "Absolute function expects integer or floating point type.");
430
431 if constexpr (cl::is_signed<T>::value)
432 return __cm_cl_abs_int(src);
433 return src;
434 }
435
436 template <typename T> T min(T src0, T src1) {
437 static_assert(cl::is_arithmetic<T>::value,
438 "Min only supports arithmetic types");
439 if constexpr (cl::is_floating_point<T>::value)
440 return __cm_cl_minnum(src0, src1);
441
442 if constexpr (cl::is_signed<T>::value)
443 return __cm_cl_smin(src0, src1);
444
445 return __cm_cl_umin(src0, src1);
446 }
447
448 template <typename T> T max(T src0, T src1) {
449 static_assert(cl::is_arithmetic<T>::value,
450 "Max only supports arithmetic types");
451 if constexpr (cl::is_floating_point<T>::value)
452 return __cm_cl_maxnum(src0, src1);
453
454 if constexpr (cl::is_signed<T>::value)
455 return __cm_cl_smax(src0, src1);
456
457 return __cm_cl_umax(src0, src1);
458 }
459
460 template <typename T> T ceil(T src) {
461 static_assert(cl::is_floating_point<T>::value,
462 "Ceil function expects floating poing type.");
463 return __cm_cl_ceil(src);
464 }
465
466 template <typename T> T floor(T src) {
467 static_assert(cl::is_floating_point<T>::value,
468 "Floor function expects floating poing type.");
469 return __cm_cl_floor(src);
470 }
471
472 template <typename T> T trunc(T src) {
473 static_assert(cl::is_floating_point<T>::value,
474 "Trunc function expects floating poing type.");
475 return __cm_cl_trunc(src);
476 }
477
478 template <typename T> T roundne(T src) {
479 static_assert(cl::is_floating_point<T>::value,
480 "Roundne function expects floating poing type.");
481 return __cm_cl_roundne(src);
482 }
483
484 template <bool use_fast, typename T> T sqrt(T src) {
485 static_assert(cl::is_floating_point<T>::value,
486 "illegal type provided in sqrt");
487 return __cm_cl_sqrt(src, use_fast);
488 }
489 template <bool use_fast, typename T> T log2(T src) {
490 static_assert(cl::is_floating_point<T>::value,
491 "illegal type provided in log2");
492 return __cm_cl_log2(src, use_fast);
493 }
494 template <bool use_fast, typename T> T exp2(T src) {
495 static_assert(cl::is_floating_point<T>::value,
496 "illegal type provided in exp2");
497 return __cm_cl_exp2(src, use_fast);
498 }
499 template <bool use_fast, typename T> T powr(T src0, T src1) {
500 static_assert(cl::is_floating_point<T>::value,
501 "illegal type provided in powr");
502 return __cm_cl_powr(src0, src1, use_fast);
503 }
504 template <bool use_fast, typename T> T sin(T src) {
505 static_assert(cl::is_floating_point<T>::value,
506 "illegal type provided in sin");
507 return __cm_cl_sin(src, use_fast);
508 }
509 template <bool use_fast, typename T> T cos(T src) {
510 static_assert(cl::is_floating_point<T>::value,
511 "illegal type provided in cos");
512 return __cm_cl_cos(src, use_fast);
513 }
514
515 template <atomic::operation operation, memory_order semantics,
516 memory_scope scope, typename T>
517 T atomicrmw(__global T *ptr, T operand) {
518 static_assert(cl::is_arithmetic<T>::value,
519 "illegal type provided in atomicrmw");
520 return __cm_cl_atomicrmw(ptr, operation, operand, semantics, scope);
521 }
522
523 template <atomic::operation operation, memory_order semantics,
524 memory_scope scope, typename T>
525 T atomicrmw(__local T *ptr, T operand) {
526 static_assert(cl::is_arithmetic<T>::value,
527 "illegal type provided in atomicrmw");
528 return __cm_cl_atomicrmw(ptr, operation, operand, semantics, scope);
529 }
530
531 template <atomic::operation operation, memory_order semantics,
532 memory_scope scope, typename T>
533 T atomicrmw(__generic T *ptr, T operand) {
534 static_assert(cl::is_arithmetic<T>::value,
535 "illegal type provided in atomicrmw");
536 return __cm_cl_atomicrmw(ptr, operation, operand, semantics, scope);
537 }
538
539 template <memory_order semantics_on_success, memory_order semantics_on_failure,
540 memory_scope scope, typename T>
541 T cmpxchg(__global T *ptr, T operand0, T operand1) {
542 static_assert(cl::is_integral<T>::value, "illegal type provided in cmpxchg");
543 return __cm_cl_cmpxchg(ptr, operand0, operand1, semantics_on_success,
544 semantics_on_failure, scope);
545 }
546
547 template <memory_order semantics_on_success, memory_order semantics_on_failure,
548 memory_scope scope, typename T>
549 T cmpxchg(__local T *ptr, T operand0, T operand1) {
550 static_assert(cl::is_integral<T>::value, "illegal type provided in cmpxchg");
551 return __cm_cl_cmpxchg(ptr, operand0, operand1, semantics_on_success,
552 semantics_on_failure, scope);
553 }
554
555 template <memory_order semantics_on_success, memory_order semantics_on_failure,
556 memory_scope scope, typename T>
557 T cmpxchg(__generic T *ptr, T operand0, T operand1) {
558 static_assert(cl::is_integral<T>::value, "illegal type provided in cmpxchg");
559 return __cm_cl_cmpxchg(ptr, operand0, operand1, semantics_on_success,
560 semantics_on_failure, scope);
561 }
562
563 template <memory_order semantics, memory_scope scope> void fence() {
564 static_assert(semantics != memory_order_relaxed);
565 return __cm_cl_fence(semantics, scope);
566 }
567
568 } // namespace detail
569 } // namespace cm
570
571 #endif // CM_CL_DETAIL_BUILTINS_H