clang 23.0.0git
amdgpuintrin.h
Go to the documentation of this file.
1//===-- amdgpuintrin.h - AMDPGU 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#ifndef __AMDGPUINTRIN_H
10#define __AMDGPUINTRIN_H
11
12#ifndef __AMDGPU__
13#error "This file is intended for AMDGPU targets or offloading to AMDGPU"
14#endif
15
16#ifndef __GPUINTRIN_H
17#error "Never use <amdgpuintrin.h> directly; include <gpuintrin.h> instead"
18#endif
19
20_Pragma("omp begin declare target device_type(nohost)");
21_Pragma("omp begin declare variant match(device = {arch(amdgcn)})");
22
23// Type aliases to the address spaces used by the AMDGPU backend.
24#define __gpu_private __attribute__((address_space(5)))
25#define __gpu_constant __attribute__((address_space(4)))
26#define __gpu_local __attribute__((address_space(3)))
27#define __gpu_global __attribute__((address_space(1)))
28#define __gpu_generic __attribute__((address_space(0)))
29
30// Returns the number of workgroups in the 'x' dimension of the grid.
31_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_x(void) {
32 return __builtin_amdgcn_grid_size_x() / __builtin_amdgcn_workgroup_size_x();
33}
34
35// Returns the number of workgroups in the 'y' dimension of the grid.
36_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_y(void) {
37 return __builtin_amdgcn_grid_size_y() / __builtin_amdgcn_workgroup_size_y();
38}
39
40// Returns the number of workgroups in the 'z' dimension of the grid.
41_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_z(void) {
42 return __builtin_amdgcn_grid_size_z() / __builtin_amdgcn_workgroup_size_z();
43}
44
45// Returns the 'x' dimension of the current AMD workgroup's id.
46_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_x(void) {
47 return __builtin_amdgcn_workgroup_id_x();
48}
49
50// Returns the 'y' dimension of the current AMD workgroup's id.
51_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_y(void) {
52 return __builtin_amdgcn_workgroup_id_y();
53}
54
55// Returns the 'z' dimension of the current AMD workgroup's id.
56_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_z(void) {
57 return __builtin_amdgcn_workgroup_id_z();
58}
59
60// Returns the number of workitems in the 'x' dimension.
61_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_x(void) {
62 return __builtin_amdgcn_workgroup_size_x();
63}
64
65// Returns the number of workitems in the 'y' dimension.
66_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_y(void) {
67 return __builtin_amdgcn_workgroup_size_y();
68}
69
70// Returns the number of workitems in the 'z' dimension.
71_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_z(void) {
72 return __builtin_amdgcn_workgroup_size_z();
73}
74
75// Returns the 'x' dimension id of the workitem in the current AMD workgroup.
76_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_x(void) {
77 return __builtin_amdgcn_workitem_id_x();
78}
79
80// Returns the 'y' dimension id of the workitem in the current AMD workgroup.
81_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_y(void) {
82 return __builtin_amdgcn_workitem_id_y();
83}
84
85// Returns the 'z' dimension id of the workitem in the current AMD workgroup.
86_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_z(void) {
87 return __builtin_amdgcn_workitem_id_z();
88}
89
90// Returns the size of an AMD wavefront, either 32 or 64 depending on hardware
91// and compilation options.
92_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_lanes(void) {
93 return __builtin_amdgcn_wavefrontsize();
94}
95
96// Returns the id of the thread inside of an AMD wavefront executing together.
97_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_id(void) {
98 return __builtin_amdgcn_mbcnt_hi(~0u, __builtin_amdgcn_mbcnt_lo(~0u, 0u));
99}
100
101// Returns the bit-mask of active threads in the current wavefront.
102_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_lane_mask(void) {
103 return __builtin_amdgcn_read_exec();
104}
105
106// Copies the value from the first active thread in the wavefront to the rest.
107_DEFAULT_FN_ATTRS static __inline__ uint32_t
108__gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x) {
109 return __builtin_amdgcn_readfirstlane(__x);
110}
111
112// Returns a bitmask of threads in the current lane for which \p x is true.
113_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_ballot(uint64_t __lane_mask,
114 bool __x) {
115 // The lane_mask & gives the nvptx semantics when lane_mask is a subset of
116 // the active threads
117 return __lane_mask & __builtin_amdgcn_ballot_w64(__x);
118}
119
120// Waits for all the threads in the block to converge and issues a fence.
121_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_threads(void) {
122 __builtin_amdgcn_s_barrier();
123 __builtin_amdgcn_fence(__ATOMIC_SEQ_CST, "workgroup");
124}
125
126// Wait for all threads in the wavefront to converge, this is a noop on AMDGPU.
127_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_lane(uint64_t __lane_mask) {
128 __builtin_amdgcn_wave_barrier();
129}
130
131// Shuffles the the lanes inside the wavefront according to the given index.
132_DEFAULT_FN_ATTRS static __inline__ uint32_t
133__gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x,
134 uint32_t __width) {
135 uint32_t __lane = __idx + (__gpu_lane_id() & ~(__width - 1));
136 return __builtin_amdgcn_ds_bpermute(__lane << 2, __x);
137}
138
139// Returns a bitmask marking all lanes that have the same value of __x.
140_DEFAULT_FN_ATTRS static __inline__ uint64_t
141__gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x) {
142 return __gpu_match_any_u32_impl(__lane_mask, __x);
143}
144
145// Returns a bitmask marking all lanes that have the same value of __x.
146_DEFAULT_FN_ATTRS static __inline__ uint64_t
147__gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x) {
148 return __gpu_match_any_u64_impl(__lane_mask, __x);
149}
150
151// Returns the current lane mask if every lane contains __x.
152_DEFAULT_FN_ATTRS static __inline__ uint64_t
153__gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x) {
154 return __gpu_match_all_u32_impl(__lane_mask, __x);
155}
156
157// Returns the current lane mask if every lane contains __x.
158_DEFAULT_FN_ATTRS static __inline__ uint64_t
159__gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x) {
160 return __gpu_match_all_u64_impl(__lane_mask, __x);
161}
162
163// Returns true if the flat pointer points to AMDGPU 'shared' memory.
164_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_local(void *ptr) {
165 return __builtin_amdgcn_is_shared((void [[clang::address_space(0)]] *)((
166 void [[clang::opencl_generic]] *)ptr));
167}
168
169// Returns true if the flat pointer points to AMDGPU 'private' memory.
170_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_private(void *ptr) {
171 return __builtin_amdgcn_is_private((void [[clang::address_space(0)]] *)((
172 void [[clang::opencl_generic]] *)ptr));
173}
174
175// Terminates execution of the associated wavefront.
176_DEFAULT_FN_ATTRS [[noreturn]] static __inline__ void __gpu_exit(void) {
177 __builtin_amdgcn_endpgm();
178}
179
180// Suspend the thread briefly to assist the scheduler during busy loops.
181_DEFAULT_FN_ATTRS static __inline__ void __gpu_thread_suspend(void) {
182 __builtin_amdgcn_s_sleep(2);
183}
184
185_Pragma("omp end declare variant");
186_Pragma("omp end declare target");
187
188#endif // __AMDGPUINTRIN_H
static _DEFAULT_FN_ATTRS __inline__ void __gpu_thread_suspend(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_thread_id_x(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_lane_id(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_threads_z(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_lane_mask(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x)
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__ uint32_t __gpu_block_id_y(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_thread_id_z(void)
static _DEFAULT_FN_ATTRS __inline__ void __gpu_sync_lane(uint64_t __lane_mask)
static _DEFAULT_FN_ATTRS __inline__ bool __gpu_is_ptr_private(void *ptr)
static _DEFAULT_FN_ATTRS __inline__ bool __gpu_is_ptr_local(void *ptr)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_blocks_x(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_block_id_z(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_threads_y(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_threads_x(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_lanes(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_thread_id_y(void)
_Pragma("omp begin declare target device_type(nohost)")
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_blocks_z(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_block_id_x(void)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x)
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_ballot(uint64_t __lane_mask, bool __x)
static _DEFAULT_FN_ATTRS __inline__ void __gpu_exit(void)
static _DEFAULT_FN_ATTRS __inline__ void __gpu_sync_threads(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_blocks_y(void)
#define _DEFAULT_FN_ATTRS
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u32_impl(uint64_t __lane_mask, uint32_t __x)
Definition gpuintrin.h:268
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u64_impl(uint64_t __lane_mask, uint64_t __x)
Definition gpuintrin.h:317
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_all_u32_impl(uint64_t __lane_mask, uint32_t __x)
Definition gpuintrin.h:308
static _DEFAULT_FN_ATTRS __inline__ uint64_t __gpu_match_any_u64_impl(uint64_t __lane_mask, uint64_t __x)
Definition gpuintrin.h:288
#define noreturn
Definition stdnoreturn.h:17