Path: blob/main/contrib/llvm-project/clang/lib/Headers/amdgpuintrin.h
213766 views
//===-- amdgpuintrin.h - AMDPGU intrinsic functions -----------------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef __AMDGPUINTRIN_H9#define __AMDGPUINTRIN_H1011#ifndef __AMDGPU__12#error "This file is intended for AMDGPU targets or offloading to AMDGPU"13#endif1415#ifndef __GPUINTRIN_H16#error "Never use <amdgpuintrin.h> directly; include <gpuintrin.h> instead"17#endif1819_Pragma("omp begin declare target device_type(nohost)");20_Pragma("omp begin declare variant match(device = {arch(amdgcn)})");2122// Type aliases to the address spaces used by the AMDGPU backend.23#define __gpu_private __attribute__((address_space(5)))24#define __gpu_constant __attribute__((address_space(4)))25#define __gpu_local __attribute__((address_space(3)))26#define __gpu_global __attribute__((address_space(1)))27#define __gpu_generic __attribute__((address_space(0)))2829// Attribute to declare a function as a kernel.30#define __gpu_kernel __attribute__((amdgpu_kernel, visibility("protected")))3132// Returns the number of workgroups in the 'x' dimension of the grid.33_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_x(void) {34return __builtin_amdgcn_grid_size_x() / __builtin_amdgcn_workgroup_size_x();35}3637// Returns the number of workgroups in the 'y' dimension of the grid.38_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_y(void) {39return __builtin_amdgcn_grid_size_y() / __builtin_amdgcn_workgroup_size_y();40}4142// Returns the number of workgroups in the 'z' dimension of the grid.43_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_z(void) {44return __builtin_amdgcn_grid_size_z() / __builtin_amdgcn_workgroup_size_z();45}4647// Returns the 'x' dimension of the current AMD workgroup's id.48_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_x(void) {49return __builtin_amdgcn_workgroup_id_x();50}5152// Returns the 'y' dimension of the current AMD workgroup's id.53_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_y(void) {54return __builtin_amdgcn_workgroup_id_y();55}5657// Returns the 'z' dimension of the current AMD workgroup's id.58_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_z(void) {59return __builtin_amdgcn_workgroup_id_z();60}6162// Returns the number of workitems in the 'x' dimension.63_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_x(void) {64return __builtin_amdgcn_workgroup_size_x();65}6667// Returns the number of workitems in the 'y' dimension.68_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_y(void) {69return __builtin_amdgcn_workgroup_size_y();70}7172// Returns the number of workitems in the 'z' dimension.73_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_z(void) {74return __builtin_amdgcn_workgroup_size_z();75}7677// Returns the 'x' dimension id of the workitem in the current AMD workgroup.78_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_x(void) {79return __builtin_amdgcn_workitem_id_x();80}8182// Returns the 'y' dimension id of the workitem in the current AMD workgroup.83_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_y(void) {84return __builtin_amdgcn_workitem_id_y();85}8687// Returns the 'z' dimension id of the workitem in the current AMD workgroup.88_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_z(void) {89return __builtin_amdgcn_workitem_id_z();90}9192// Returns the size of an AMD wavefront, either 32 or 64 depending on hardware93// and compilation options.94_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_lanes(void) {95return __builtin_amdgcn_wavefrontsize();96}9798// Returns the id of the thread inside of an AMD wavefront executing together.99_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_id(void) {100return __builtin_amdgcn_mbcnt_hi(~0u, __builtin_amdgcn_mbcnt_lo(~0u, 0u));101}102103// Returns the bit-mask of active threads in the current wavefront.104_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_lane_mask(void) {105return __builtin_amdgcn_read_exec();106}107108// Copies the value from the first active thread in the wavefront to the rest.109_DEFAULT_FN_ATTRS static __inline__ uint32_t110__gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x) {111return __builtin_amdgcn_readfirstlane(__x);112}113114// 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,116bool __x) {117// The lane_mask & gives the nvptx semantics when lane_mask is a subset of118// the active threads119return __lane_mask & __builtin_amdgcn_ballot_w64(__x);120}121122// Waits for all the threads in the block to converge and issues a fence.123_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_threads(void) {124__builtin_amdgcn_s_barrier();125__builtin_amdgcn_fence(__ATOMIC_SEQ_CST, "workgroup");126}127128// Wait for all threads in the wavefront to converge, this is a noop on AMDGPU.129_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_lane(uint64_t __lane_mask) {130__builtin_amdgcn_wave_barrier();131}132133// Shuffles the the lanes inside the wavefront according to the given index.134_DEFAULT_FN_ATTRS static __inline__ uint32_t135__gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x,136uint32_t __width) {137uint32_t __lane = __idx + (__gpu_lane_id() & ~(__width - 1));138return __builtin_amdgcn_ds_bpermute(__lane << 2, __x);139}140141// Returns a bitmask marking all lanes that have the same value of __x.142_DEFAULT_FN_ATTRS static __inline__ uint64_t143__gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x) {144return __gpu_match_any_u32_impl(__lane_mask, __x);145}146147// Returns a bitmask marking all lanes that have the same value of __x.148_DEFAULT_FN_ATTRS static __inline__ uint64_t149__gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x) {150return __gpu_match_any_u64_impl(__lane_mask, __x);151}152153// Returns the current lane mask if every lane contains __x.154_DEFAULT_FN_ATTRS static __inline__ uint64_t155__gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x) {156return __gpu_match_all_u32_impl(__lane_mask, __x);157}158159// Returns the current lane mask if every lane contains __x.160_DEFAULT_FN_ATTRS static __inline__ uint64_t161__gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x) {162return __gpu_match_all_u64_impl(__lane_mask, __x);163}164165// Returns true if the flat pointer points to AMDGPU 'shared' memory.166_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_local(void *ptr) {167return __builtin_amdgcn_is_shared((void [[clang::address_space(0)]] *)((168void [[clang::opencl_generic]] *)ptr));169}170171// Returns true if the flat pointer points to AMDGPU 'private' memory.172_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_private(void *ptr) {173return __builtin_amdgcn_is_private((void [[clang::address_space(0)]] *)((174void [[clang::opencl_generic]] *)ptr));175}176177// Terminates execution of the associated wavefront.178_DEFAULT_FN_ATTRS [[noreturn]] static __inline__ void __gpu_exit(void) {179__builtin_amdgcn_endpgm();180}181182// Suspend the thread briefly to assist the scheduler during busy loops.183_DEFAULT_FN_ATTRS static __inline__ void __gpu_thread_suspend(void) {184__builtin_amdgcn_s_sleep(2);185}186187_Pragma("omp end declare variant");188_Pragma("omp end declare target");189190#endif // __AMDGPUINTRIN_H191192193