clang 24.0.0git
__clang_gpu_intrinsics.h
Go to the documentation of this file.
1//===--- __clang_gpu_intrinsics.h - Device-side GPU intrinsic wrappers ------===
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#ifndef __CLANG_GPU_INTRINSICS_H__
10#define __CLANG_GPU_INTRINSICS_H__
11
12#if defined(__HIP__) || defined(__CUDA__)
13
15#include <gpuintrin.h>
16
17static inline __attribute__((device)) const struct {
18 __attribute__((device, always_inline, const)) operator int() const noexcept {
19 return __gpu_num_lanes();
20 }
21} warpSize{};
22
23#pragma push_macro("__GPU_DEVICE__")
24#define __GPU_DEVICE__ static __inline__ __attribute__((device, always_inline))
25#pragma push_macro("MAYBE_UNDEF")
26#define MAYBE_UNDEF __attribute__((maybe_undef))
27
28template <typename __T>
29__GPU_DEVICE__ __T __gpu_shuffle_idx_impl(__T __v, unsigned int __idx,
30 int __w) {
31 if constexpr (sizeof(__T) == sizeof(unsigned long long)) {
32 return __builtin_bit_cast(
34 __builtin_bit_cast(unsigned long long, __v),
35 (unsigned int)__w));
36 } else {
37 return __builtin_bit_cast(
39 __builtin_bit_cast(unsigned int, __v),
40 (unsigned int)__w));
41 }
42}
43
44template <typename __T>
45__GPU_DEVICE__ __T __shfl(MAYBE_UNDEF __T __var, int __src_lane,
46 int __width = warpSize) {
47 return __gpu_shuffle_idx_impl(
48 __var, (unsigned int)(__src_lane & (__width - 1)), __width);
49}
50template <typename __T>
51__GPU_DEVICE__ __T __shfl_up(MAYBE_UNDEF __T __var, unsigned int __delta,
52 int __width = warpSize) {
53 int __rel = int(__gpu_lane_id() & (unsigned int)(__width - 1));
54 int __tgt = __rel - int(__delta);
55 return __gpu_shuffle_idx_impl(
56 __var, (unsigned int)(__tgt < 0 ? __rel : __tgt), __width);
57}
58template <typename __T>
59__GPU_DEVICE__ __T __shfl_down(MAYBE_UNDEF __T __var, unsigned int __delta,
60 int __width = warpSize) {
61 int __rel = int(__gpu_lane_id() & (unsigned int)(__width - 1));
62 int __tgt = __rel + int(__delta);
63 return __gpu_shuffle_idx_impl(
64 __var, (unsigned int)(__tgt >= __width ? __rel : __tgt), __width);
65}
66template <typename __T>
67__GPU_DEVICE__ __T __shfl_xor(MAYBE_UNDEF __T __var, int __lane_mask,
68 int __width = warpSize) {
69 int __rel = int(__gpu_lane_id() & (unsigned int)(__width - 1));
70 int __tgt = __rel ^ __lane_mask;
71 return __gpu_shuffle_idx_impl(
72 __var, (unsigned int)(__tgt >= __width ? __rel : __tgt), __width);
73}
74
75__GPU_DEVICE__ void __syncwarp(unsigned long long __mask = -1) {
76 __scoped_atomic_thread_fence(__ATOMIC_RELEASE, __MEMORY_SCOPE_WVFRNT);
77 __gpu_sync_lane(__mask);
78 __scoped_atomic_thread_fence(__ATOMIC_ACQUIRE, __MEMORY_SCOPE_WVFRNT);
79}
80
81template <typename __MaskT>
82__GPU_DEVICE__ unsigned long long __ballot_sync(__MaskT __mask, int __pred) {
83 return __ballot(__pred) & (unsigned long long)__mask;
84}
85template <typename __MaskT>
86__GPU_DEVICE__ int __all_sync(__MaskT __mask, int __pred) {
87 return __ballot_sync(__mask, __pred) == (unsigned long long)__mask;
88}
89template <typename __MaskT>
90__GPU_DEVICE__ int __any_sync(__MaskT __mask, int __pred) {
91 return __ballot_sync(__mask, __pred) != 0ull;
92}
93
94template <typename __MaskT, typename __T>
95__GPU_DEVICE__ __T __shfl_sync(__MaskT __mask, MAYBE_UNDEF __T __var,
96 int __src_lane, int __width = warpSize) {
97 (void)__mask;
98 return __shfl(__var, __src_lane, __width);
99}
100template <typename __MaskT, typename __T>
101__GPU_DEVICE__ __T __shfl_up_sync(__MaskT __mask, MAYBE_UNDEF __T __var,
102 unsigned int __delta,
103 int __width = warpSize) {
104 (void)__mask;
105 return __shfl_up(__var, __delta, __width);
106}
107template <typename __MaskT, typename __T>
108__GPU_DEVICE__ __T __shfl_down_sync(__MaskT __mask, MAYBE_UNDEF __T __var,
109 unsigned int __delta,
110 int __width = warpSize) {
111 (void)__mask;
112 return __shfl_down(__var, __delta, __width);
113}
114template <typename __MaskT, typename __T>
115__GPU_DEVICE__ __T __shfl_xor_sync(__MaskT __mask, MAYBE_UNDEF __T __var,
116 int __lane_mask, int __width = warpSize) {
117 (void)__mask;
118 return __shfl_xor(__var, __lane_mask, __width);
119}
120
121template <typename __T>
122__GPU_DEVICE__ unsigned long long __match_any(__T __value) {
123 if constexpr (sizeof(__T) == sizeof(unsigned long long)) {
125 __builtin_bit_cast(unsigned long long, __value));
126 } else {
128 __builtin_bit_cast(unsigned int, __value));
129 }
130}
131template <typename __MaskT, typename __T>
132__GPU_DEVICE__ unsigned long long __match_any_sync(__MaskT __mask,
133 __T __value) {
134 return __match_any(__value) & (unsigned long long)__mask;
135}
136
137template <typename __T>
138__GPU_DEVICE__ unsigned long long __match_all(__T __value, int *__pred) {
139 unsigned long long __r;
140 if constexpr (sizeof(__T) == sizeof(unsigned long long)) {
142 __builtin_bit_cast(unsigned long long, __value));
143 } else {
145 __builtin_bit_cast(unsigned int, __value));
146 }
147 *__pred = __r != 0;
148 return __r;
149}
150template <typename __MaskT, typename __T>
151__GPU_DEVICE__ unsigned long long __match_all_sync(__MaskT __mask, __T __value,
152 int *__pred) {
153 (void)__mask;
154 return __match_all(__value, __pred);
155}
156
157template <typename __MaskT>
158__GPU_DEVICE__ unsigned int __reduce_add_sync(__MaskT __mask,
159 unsigned int __val) {
160 return __gpu_lane_add_u32((unsigned long long)__mask, __val);
161}
162template <typename __MaskT>
163__GPU_DEVICE__ int __reduce_add_sync(__MaskT __mask, int __val) {
164 return int(
165 __gpu_lane_add_u32((unsigned long long)__mask, (unsigned int)__val));
166}
167template <typename __MaskT>
168__GPU_DEVICE__ unsigned int __reduce_min_sync(__MaskT __mask,
169 unsigned int __val) {
170 return __gpu_lane_min_u32((unsigned long long)__mask, __val);
171}
172template <typename __MaskT>
173__GPU_DEVICE__ int __reduce_min_sync(__MaskT __mask, int __val) {
174 unsigned int __r = __gpu_lane_min_u32((unsigned long long)__mask,
175 (unsigned int)__val ^ 0x80000000u);
176 return int(__r ^ 0x80000000u);
177}
178template <typename __MaskT>
179__GPU_DEVICE__ unsigned int __reduce_max_sync(__MaskT __mask,
180 unsigned int __val) {
181 return __gpu_lane_max_u32((unsigned long long)__mask, __val);
182}
183template <typename __MaskT>
184__GPU_DEVICE__ int __reduce_max_sync(__MaskT __mask, int __val) {
185 unsigned int __r = __gpu_lane_max_u32((unsigned long long)__mask,
186 (unsigned int)__val ^ 0x80000000u);
187 return int(__r ^ 0x80000000u);
188}
189template <typename __MaskT>
190__GPU_DEVICE__ unsigned int __reduce_and_sync(__MaskT __mask,
191 unsigned int __val) {
192 return __gpu_lane_and_u32((unsigned long long)__mask, __val);
193}
194template <typename __MaskT>
195__GPU_DEVICE__ unsigned int __reduce_or_sync(__MaskT __mask,
196 unsigned int __val) {
197 return __gpu_lane_or_u32((unsigned long long)__mask, __val);
198}
199template <typename __MaskT>
200__GPU_DEVICE__ unsigned int __reduce_xor_sync(__MaskT __mask,
201 unsigned int __val) {
202 return __gpu_lane_xor_u32((unsigned long long)__mask, __val);
203}
204
205__GPU_DEVICE__ unsigned int
206__funnelshift_l(unsigned int __lo, unsigned int __hi, unsigned int __shift) {
207 unsigned int __s = __shift & 31u;
208 return (unsigned int)((((unsigned long long)__hi << 32 | __lo) << __s) >> 32);
209}
210__GPU_DEVICE__ unsigned int
211__funnelshift_lc(unsigned int __lo, unsigned int __hi, unsigned int __shift) {
212 unsigned int __s = __shift >= 32u ? 32u : __shift;
213 return (unsigned int)((((unsigned long long)__hi << 32 | __lo) << __s) >> 32);
214}
215__GPU_DEVICE__ unsigned int
216__funnelshift_r(unsigned int __lo, unsigned int __hi, unsigned int __shift) {
217 unsigned int __s = __shift & 31u;
218 return (unsigned int)(((unsigned long long)__hi << 32 | __lo) >> __s);
219}
220__GPU_DEVICE__ unsigned int
221__funnelshift_rc(unsigned int __lo, unsigned int __hi, unsigned int __shift) {
222 unsigned int __s = __shift >= 32u ? 32u : __shift;
223 return (unsigned int)(((unsigned long long)__hi << 32 | __lo) >> __s);
224}
225
226#pragma pop_macro("MAYBE_UNDEF")
227#pragma pop_macro("__GPU_DEVICE__")
228
229#endif // device compile
230#endif // __CLANG_GPU_INTRINSICS_H__
__DEVICE__ unsigned int __ballot(int __a)
__device__ unsigned __funnelshift_lc(unsigned low32, unsigned high32, unsigned shiftWidth)
__device__ unsigned __funnelshift_rc(unsigned low32, unsigned high32, unsigned shiftWidth)
__device__ unsigned __funnelshift_r(unsigned low32, unsigned high32, unsigned shiftWidth)
__device__ unsigned __funnelshift_l(unsigned low32, unsigned high32, unsigned shiftWidth)
_Float16 __2f16 __attribute__((ext_vector_type(2)))
Zeroes the upper 128 bits (bits 255:128) of all YMM registers.
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_lane_id(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_lane_mask(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x, uint32_t __width)
static _DEFAULT_FN_ATTRS __inline__ void __gpu_sync_lane(uint64_t __lane_mask)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_lanes(void)
return __v
Definition arm_acle.h:88
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x)
Definition gpuintrin.h:270
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_shuffle_idx_u64(uint64_t __lane_mask, uint32_t __idx, uint64_t __x, uint32_t __width)
Definition gpuintrin.h:150
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x)
Definition gpuintrin.h:318
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x)
Definition gpuintrin.h:330
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x)
Definition gpuintrin.h:294
static __inline__ void unsigned int __value