|
| 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 SPIRV targets" |
| 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(amdgcn)})"); |
| 22 | + |
| 23 | +// Type aliases to the address spaces used by the SPIRV 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 | +// Attribute to declare a function as a kernel. |
| 31 | +#define __gpu_kernel __attribute__((spir_kernel, visibility("protected"))) |
| 32 | + |
| 33 | +// Returns the number of workgroups in the 'x' dimension of the grid. |
| 34 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_x(void) { |
| 35 | + return __builtin_spirv_get_num_workgroups_x(); |
| 36 | +} |
| 37 | + |
| 38 | +// Returns the number of workgroups in the 'y' dimension of the grid. |
| 39 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_y(void) { |
| 40 | + return __builtin_spirv_get_num_workgroups_y(); |
| 41 | +} |
| 42 | + |
| 43 | +// Returns the number of workgroups in the 'z' dimension of the grid. |
| 44 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_z(void) { |
| 45 | + return __builtin_spirv_get_num_workgroups_z(); |
| 46 | +} |
| 47 | + |
| 48 | +// Returns the 'x' dimension of the current SPIR-V workgroup's id. |
| 49 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_x(void) { |
| 50 | + return __builtin_spirv_get_workgroup_id_x(); |
| 51 | +} |
| 52 | + |
| 53 | +// Returns the 'y' dimension of the current SPIR-V workgroup's id. |
| 54 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_y(void) { |
| 55 | + return __builtin_spirv_get_workgroup_id_y(); |
| 56 | +} |
| 57 | + |
| 58 | +// Returns the 'z' dimension of the current SPIR-V workgroup's id. |
| 59 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_z(void) { |
| 60 | + return __builtin_spirv_get_workgroup_id_z(); |
| 61 | +} |
| 62 | + |
| 63 | +// Returns the number of workitems in the 'x' dimension. |
| 64 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_x(void) { |
| 65 | + return __builtin_spirv_workgroup_size_x(); |
| 66 | +} |
| 67 | + |
| 68 | +// Returns the number of workitems in the 'y' dimension. |
| 69 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_y(void) { |
| 70 | + return __builtin_spirv_workgroup_size_y(); |
| 71 | +} |
| 72 | + |
| 73 | +// Returns the number of workitems in the 'z' dimension. |
| 74 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_z(void) { |
| 75 | + return __builtin_spirv_workgroup_size_z(); |
| 76 | +} |
| 77 | + |
| 78 | +// Returns the 'x' dimension id of the workitem in the current SPIR-V workgroup. |
| 79 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_x(void) { |
| 80 | + return __builtin_spirv_workitem_id_x(); |
| 81 | +} |
| 82 | + |
| 83 | +// Returns the 'y' dimension id of the workitem in the current SPIR-V workgroup. |
| 84 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_y(void) { |
| 85 | + return __builtin_spirv_workitem_id_y(); |
| 86 | +} |
| 87 | + |
| 88 | +// Returns the 'z' dimension id of the workitem in the current SPIR-V workgroup. |
| 89 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_z(void) { |
| 90 | + return __builtin_spirv_workitem_id_z(); |
| 91 | +} |
| 92 | + |
| 93 | +// Returns the size of a wavefront |
| 94 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_lanes(void) { |
| 95 | + __builtin_unreachable(); |
| 96 | +} |
| 97 | + |
| 98 | +// Returns the id of the thread inside of a wavefront executing together. |
| 99 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_id(void) { |
| 100 | + __builtin_unreachable(); |
| 101 | +} |
| 102 | + |
| 103 | +// Returns the bit-mask of active threads in the current wavefront. |
| 104 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_lane_mask(void) { |
| 105 | + __builtin_unreachable(); |
| 106 | +} |
| 107 | + |
| 108 | +// Copies the value from the first active thread in the wavefront to the rest. |
| 109 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t |
| 110 | +__gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x) { |
| 111 | + __builtin_unreachable(); |
| 112 | +} |
| 113 | + |
| 114 | +// Returns a bitmask of threads in the current lane for which \p x is true. |
| 115 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_ballot(uint64_t __lane_mask, |
| 116 | + bool __x) { |
| 117 | + return __lane_mask & __builtin_spirv_ballot(__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_spirv_sync_threads(); |
| 123 | +} |
| 124 | + |
| 125 | +// Wait for all threads in the wavefront to converge. |
| 126 | +_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_lane(uint64_t __lane_mask) { |
| 127 | + __builtin_unreachable(); |
| 128 | +} |
| 129 | + |
| 130 | +// Shuffles the the lanes inside the wavefront according to the given index. |
| 131 | +_DEFAULT_FN_ATTRS static __inline__ uint32_t |
| 132 | +__gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x, |
| 133 | + uint32_t __width) { |
| 134 | + __builtin_unreachable(); |
| 135 | +} |
| 136 | + |
| 137 | +// Returns a bitmask marking all lanes that have the same value of __x. |
| 138 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t |
| 139 | +__gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x) { |
| 140 | + return __gpu_match_any_u32_impl(__lane_mask, __x); |
| 141 | +} |
| 142 | + |
| 143 | +// Returns a bitmask marking all lanes that have the same value of __x. |
| 144 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t |
| 145 | +__gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x) { |
| 146 | + return __gpu_match_any_u64_impl(__lane_mask, __x); |
| 147 | +} |
| 148 | + |
| 149 | +// Returns the current lane mask if every lane contains __x. |
| 150 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t |
| 151 | +__gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x) { |
| 152 | + return __gpu_match_all_u32_impl(__lane_mask, __x); |
| 153 | +} |
| 154 | + |
| 155 | +// Returns the current lane mask if every lane contains __x. |
| 156 | +_DEFAULT_FN_ATTRS static __inline__ uint64_t |
| 157 | +__gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x) { |
| 158 | + return __gpu_match_all_u64_impl(__lane_mask, __x); |
| 159 | +} |
| 160 | + |
| 161 | +// Returns true if the flat pointer points to SPIRV 'shared' memory. |
| 162 | +_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_local(void *ptr) { |
| 163 | + return __builtin_spirv_is_shared((void [[clang::address_space(0)]] *)(( |
| 164 | + void [[clang::opencl_generic]] *)ptr)); |
| 165 | +} |
| 166 | + |
| 167 | +// Returns true if the flat pointer points to SPIRV 'private' memory. |
| 168 | +_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_private(void *ptr) { |
| 169 | + return __builtin_spirv_is_private((void [[clang::address_space(0)]] *)(( |
| 170 | + void [[clang::opencl_generic]] *)ptr)); |
| 171 | +} |
| 172 | + |
| 173 | +// Terminates execution of the associated wavefront. |
| 174 | +_DEFAULT_FN_ATTRS [[noreturn]] static __inline__ void __gpu_exit(void) { |
| 175 | + __builtin_unreachable(); |
| 176 | +} |
| 177 | + |
| 178 | +// Suspend the thread briefly to assist the scheduler during busy loops. |
| 179 | +_DEFAULT_FN_ATTRS static __inline__ void __gpu_thread_suspend(void) { |
| 180 | + // no op |
| 181 | +} |
| 182 | + |
| 183 | +_Pragma("omp end declare variant"); |
| 184 | +_Pragma("omp end declare target"); |
| 185 | + |
| 186 | +#endif // __SPIRVINTRIN_H |
0 commit comments