clang 23.0.0git
spirvintrin.h
Go to the documentation of this file.
1//===-- spirvintrin.h - SPIR-V 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 __SPIRVINTRIN_H
10#define __SPIRVINTRIN_H
11
12#ifndef __SPIRV__
13#error "This file is intended for SPIR-V targets or offloading to SPIR-V"
14#endif
15
16#ifndef __GPUINTRIN_H
17#error "Never use <spirvintrin.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(spirv64)})");
22
23// Type aliases to the address spaces used by the SPIR-V backend.
24#define __gpu_private __attribute__((address_space(0)))
25#define __gpu_constant __attribute__((address_space(2)))
26#define __gpu_local __attribute__((address_space(3)))
27#define __gpu_global __attribute__((address_space(1)))
28#define __gpu_generic __attribute__((address_space(4)))
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_spirv_num_workgroups(0);
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_spirv_num_workgroups(1);
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_spirv_num_workgroups(2);
43}
44
45// Returns the 'x' dimension of the current workgroup's id.
46_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_x(void) {
47 return __builtin_spirv_workgroup_id(0);
48}
49
50// Returns the 'y' dimension of the current workgroup's id.
51_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_y(void) {
52 return __builtin_spirv_workgroup_id(1);
53}
54
55// Returns the 'z' dimension of the current workgroup's id.
56_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_z(void) {
57 return __builtin_spirv_workgroup_id(2);
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_spirv_workgroup_size(0);
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_spirv_workgroup_size(1);
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_spirv_workgroup_size(2);
73}
74
75// Returns the 'x' dimension id of the workitem in the current workgroup.
76_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_x(void) {
77 return __builtin_spirv_local_invocation_id(0);
78}
79
80// Returns the 'y' dimension id of the workitem in the current workgroup.
81_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_y(void) {
82 return __builtin_spirv_local_invocation_id(1);
83}
84
85// Returns the 'z' dimension id of the workitem in the current workgroup.
86_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_z(void) {
87 return __builtin_spirv_local_invocation_id(2);
88}
89
90// Returns the size of an 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_spirv_subgroup_size();
94}
95
96// Returns the id of the thread inside of an wavefront executing together.
97_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_id(void) {
98 return __builtin_spirv_subgroup_local_invocation_id();
99}
100
101// Returns the bit-mask of active threads in the current wavefront. This
102// implementation is incorrect if the target uses more than 64 lanes.
103_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_lane_mask(void) {
104 uint32_t [[clang::ext_vector_type(4)]] __mask =
105 __builtin_spirv_subgroup_ballot(1);
106 return __builtin_bit_cast(uint64_t,
107 __builtin_shufflevector(__mask, __mask, 0, 1));
108}
109
110// Copies the value from the first active thread in the wavefront to the rest.
111_DEFAULT_FN_ATTRS static __inline__ uint32_t
112__gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x) {
113 return __builtin_spirv_subgroup_shuffle(__x,
114 __builtin_ctzg(__gpu_lane_mask()));
115}
116
117// Returns a bitmask of threads in the current lane for which \p x is true. This
118// implementation is incorrect if the target uses more than 64 lanes.
119_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_ballot(uint64_t __lane_mask,
120 bool __x) {
121 // The lane_mask & gives the nvptx semantics when lane_mask is a subset of
122 // the active threads.
123 uint32_t [[clang::ext_vector_type(4)]] __mask =
124 __builtin_spirv_subgroup_ballot(__x);
125 return __lane_mask & __builtin_bit_cast(uint64_t, __builtin_shufflevector(
126 __mask, __mask, 0, 1));
127}
128
129// Waits for all the threads in the block to converge and issues a fence.
130_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_threads(void) {
131 __builtin_spirv_group_barrier();
132}
133
134// Wait for all threads in the wavefront to converge, this is a noop on SPIR-V.
135_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_lane(uint64_t __lane_mask) {
136}
137
138// Shuffles the the lanes inside the wavefront according to the given index.
139_DEFAULT_FN_ATTRS static __inline__ uint32_t
140__gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x,
141 uint32_t __width) {
142 uint32_t __lane = __idx + (__gpu_lane_id() & ~(__width - 1));
143 return __builtin_spirv_subgroup_shuffle(__x, __lane);
144}
145
146// Returns a bitmask marking all lanes that have the same value of __x.
147_DEFAULT_FN_ATTRS static __inline__ uint64_t
148__gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x) {
149 return __gpu_match_any_u32_impl(__lane_mask, __x);
150}
151
152// Returns a bitmask marking all lanes that have the same value of __x.
153_DEFAULT_FN_ATTRS static __inline__ uint64_t
154__gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x) {
155 return __gpu_match_any_u64_impl(__lane_mask, __x);
156}
157
158// Returns the current lane mask if every lane contains __x.
159_DEFAULT_FN_ATTRS static __inline__ uint64_t
160__gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x) {
161 return __gpu_match_all_u32_impl(__lane_mask, __x);
162}
163
164// Returns the current lane mask if every lane contains __x.
165_DEFAULT_FN_ATTRS static __inline__ uint64_t
166__gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x) {
167 return __gpu_match_all_u64_impl(__lane_mask, __x);
168}
169
170// SPIR-V does not expose this, always return false.
171_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_local(void *ptr) {
172 return 0;
173}
174
175// SPIR-V does not expose this, always return false.
176_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_private(void *ptr) {
177 return 0;
178}
179
180// SPIR-V only supports 'OpTerminateInvocation' in fragment shaders.
181_DEFAULT_FN_ATTRS [[noreturn]] static __inline__ void __gpu_exit(void) {
182 __builtin_trap();
183}
184
185// This is a no-op as SPIR-V does not support it.
186_DEFAULT_FN_ATTRS static __inline__ void __gpu_thread_suspend(void) {}
187
188_Pragma("omp end declare variant");
189_Pragma("omp end declare target");
190
191#endif // __SPIRVINTRIN_H
#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
static _DEFAULT_FN_ATTRS __inline__ void __gpu_thread_suspend(void)
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_thread_id_x(void)
Definition spirvintrin.h:76
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_lane_id(void)
Definition spirvintrin.h:97
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)
Definition spirvintrin.h:71
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)
Definition spirvintrin.h:51
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_thread_id_z(void)
Definition spirvintrin.h:86
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)
Definition spirvintrin.h:31
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_block_id_z(void)
Definition spirvintrin.h:56
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)
Definition spirvintrin.h:66
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_threads_x(void)
Definition spirvintrin.h:61
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_lanes(void)
Definition spirvintrin.h:92
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)
Definition spirvintrin.h:81
_Pragma("omp begin declare target device_type(nohost)")
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_num_blocks_z(void)
Definition spirvintrin.h:41
static _DEFAULT_FN_ATTRS __inline__ uint32_t __gpu_block_id_x(void)
Definition spirvintrin.h:46
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)
Definition spirvintrin.h:36
#define noreturn
Definition stdnoreturn.h:17