1 //===------- Mapping.cpp - OpenMP device runtime mapping helpers -- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 //===----------------------------------------------------------------------===//
13 #include "DeviceTypes.h"
14 #include "DeviceUtils.h"
15 #include "Interface.h"
18 #pragma omp begin declare target device_type(nohost)
20 #include "llvm/Frontend/OpenMP/OMPGridValues.h"
27 // Forward declarations defined to be defined for AMDGCN and NVPTX.
28 const llvm::omp::GV
&getGridValue();
29 LaneMaskTy
activemask();
30 LaneMaskTy
lanemaskLT();
31 LaneMaskTy
lanemaskGT();
32 uint32_t getThreadIdInWarp();
33 uint32_t getThreadIdInBlock(int32_t Dim
);
34 uint32_t getNumberOfThreadsInBlock(int32_t Dim
);
35 uint32_t getNumberOfThreadsInKernel();
36 uint32_t getBlockIdInKernel(int32_t Dim
);
37 uint32_t getNumberOfBlocksInKernel(int32_t Dim
);
38 uint32_t getWarpIdInBlock();
39 uint32_t getNumberOfWarpsInBlock();
41 /// AMDGCN Implementation
44 #pragma omp begin declare variant match(device = {arch(amdgcn)})
46 const llvm::omp::GV
&getGridValue() {
47 return llvm::omp::getAMDGPUGridValues
<__AMDGCN_WAVEFRONT_SIZE
>();
50 uint32_t getNumberOfThreadsInBlock(int32_t Dim
) {
53 return __builtin_amdgcn_workgroup_size_x();
55 return __builtin_amdgcn_workgroup_size_y();
57 return __builtin_amdgcn_workgroup_size_z();
59 UNREACHABLE("Dim outside range!");
62 LaneMaskTy
activemask() { return __builtin_amdgcn_read_exec(); }
64 LaneMaskTy
lanemaskLT() {
65 uint32_t Lane
= mapping::getThreadIdInWarp();
66 int64_t Ballot
= mapping::activemask();
67 uint64_t Mask
= ((uint64_t)1 << Lane
) - (uint64_t)1;
71 LaneMaskTy
lanemaskGT() {
72 uint32_t Lane
= mapping::getThreadIdInWarp();
73 if (Lane
== (mapping::getWarpSize() - 1))
75 int64_t Ballot
= mapping::activemask();
76 uint64_t Mask
= (~((uint64_t)0)) << (Lane
+ 1);
80 uint32_t getThreadIdInWarp() {
81 return __builtin_amdgcn_mbcnt_hi(~0u, __builtin_amdgcn_mbcnt_lo(~0u, 0u));
84 uint32_t getThreadIdInBlock(int32_t Dim
) {
87 return __builtin_amdgcn_workitem_id_x();
89 return __builtin_amdgcn_workitem_id_y();
91 return __builtin_amdgcn_workitem_id_z();
93 UNREACHABLE("Dim outside range!");
96 uint32_t getNumberOfThreadsInKernel() {
97 return __builtin_amdgcn_grid_size_x() * __builtin_amdgcn_grid_size_y() *
98 __builtin_amdgcn_grid_size_z();
101 uint32_t getBlockIdInKernel(int32_t Dim
) {
104 return __builtin_amdgcn_workgroup_id_x();
106 return __builtin_amdgcn_workgroup_id_y();
108 return __builtin_amdgcn_workgroup_id_z();
110 UNREACHABLE("Dim outside range!");
113 uint32_t getNumberOfBlocksInKernel(int32_t Dim
) {
116 return __builtin_amdgcn_grid_size_x() / __builtin_amdgcn_workgroup_size_x();
118 return __builtin_amdgcn_grid_size_y() / __builtin_amdgcn_workgroup_size_y();
120 return __builtin_amdgcn_grid_size_z() / __builtin_amdgcn_workgroup_size_z();
122 UNREACHABLE("Dim outside range!");
125 uint32_t getWarpIdInBlock() {
126 return impl::getThreadIdInBlock(mapping::DIM_X
) / mapping::getWarpSize();
129 uint32_t getNumberOfWarpsInBlock() {
130 return mapping::getNumberOfThreadsInBlock() / mapping::getWarpSize();
133 #pragma omp end declare variant
136 /// NVPTX Implementation
139 #pragma omp begin declare variant match( \
140 device = {arch(nvptx, nvptx64)}, \
141 implementation = {extension(match_any)})
143 uint32_t getNumberOfThreadsInBlock(int32_t Dim
) {
146 return __nvvm_read_ptx_sreg_ntid_x();
148 return __nvvm_read_ptx_sreg_ntid_y();
150 return __nvvm_read_ptx_sreg_ntid_z();
152 UNREACHABLE("Dim outside range!");
155 const llvm::omp::GV
&getGridValue() { return llvm::omp::NVPTXGridValues
; }
157 LaneMaskTy
activemask() { return __nvvm_activemask(); }
159 LaneMaskTy
lanemaskLT() { return __nvvm_read_ptx_sreg_lanemask_lt(); }
161 LaneMaskTy
lanemaskGT() { return __nvvm_read_ptx_sreg_lanemask_gt(); }
163 uint32_t getThreadIdInBlock(int32_t Dim
) {
166 return __nvvm_read_ptx_sreg_tid_x();
168 return __nvvm_read_ptx_sreg_tid_y();
170 return __nvvm_read_ptx_sreg_tid_z();
172 UNREACHABLE("Dim outside range!");
175 uint32_t getThreadIdInWarp() { return __nvvm_read_ptx_sreg_laneid(); }
177 uint32_t getBlockIdInKernel(int32_t Dim
) {
180 return __nvvm_read_ptx_sreg_ctaid_x();
182 return __nvvm_read_ptx_sreg_ctaid_y();
184 return __nvvm_read_ptx_sreg_ctaid_z();
186 UNREACHABLE("Dim outside range!");
189 uint32_t getNumberOfBlocksInKernel(int32_t Dim
) {
192 return __nvvm_read_ptx_sreg_nctaid_x();
194 return __nvvm_read_ptx_sreg_nctaid_y();
196 return __nvvm_read_ptx_sreg_nctaid_z();
198 UNREACHABLE("Dim outside range!");
201 uint32_t getNumberOfThreadsInKernel() {
202 return impl::getNumberOfThreadsInBlock(0) *
203 impl::getNumberOfBlocksInKernel(0) *
204 impl::getNumberOfThreadsInBlock(1) *
205 impl::getNumberOfBlocksInKernel(1) *
206 impl::getNumberOfThreadsInBlock(2) *
207 impl::getNumberOfBlocksInKernel(2);
210 uint32_t getWarpIdInBlock() {
211 return impl::getThreadIdInBlock(mapping::DIM_X
) / mapping::getWarpSize();
214 uint32_t getNumberOfWarpsInBlock() {
215 return (mapping::getNumberOfThreadsInBlock() + mapping::getWarpSize() - 1) /
216 mapping::getWarpSize();
219 #pragma omp end declare variant
222 uint32_t getWarpSize() { return getGridValue().GV_Warp_Size
; }
227 /// We have to be deliberate about the distinction of `mapping::` and `impl::`
228 /// below to avoid repeating assumptions or including irrelevant ones.
231 static bool isInLastWarp() {
232 uint32_t MainTId
= (mapping::getNumberOfThreadsInBlock() - 1) &
233 ~(mapping::getWarpSize() - 1);
234 return mapping::getThreadIdInBlock() == MainTId
;
237 bool mapping::isMainThreadInGenericMode(bool IsSPMD
) {
238 if (IsSPMD
|| icv::Level
)
241 // Check if this is the last warp in the block.
242 return isInLastWarp();
245 bool mapping::isMainThreadInGenericMode() {
246 return mapping::isMainThreadInGenericMode(mapping::isSPMDMode());
249 bool mapping::isInitialThreadInLevel0(bool IsSPMD
) {
251 return mapping::getThreadIdInBlock() == 0;
252 return isInLastWarp();
255 bool mapping::isLeaderInWarp() {
256 __kmpc_impl_lanemask_t Active
= mapping::activemask();
257 __kmpc_impl_lanemask_t LaneMaskLT
= mapping::lanemaskLT();
258 return utils::popc(Active
& LaneMaskLT
) == 0;
261 LaneMaskTy
mapping::activemask() { return impl::activemask(); }
263 LaneMaskTy
mapping::lanemaskLT() { return impl::lanemaskLT(); }
265 LaneMaskTy
mapping::lanemaskGT() { return impl::lanemaskGT(); }
267 uint32_t mapping::getThreadIdInWarp() {
268 uint32_t ThreadIdInWarp
= impl::getThreadIdInWarp();
269 ASSERT(ThreadIdInWarp
< impl::getWarpSize(), nullptr);
270 return ThreadIdInWarp
;
273 uint32_t mapping::getThreadIdInBlock(int32_t Dim
) {
274 uint32_t ThreadIdInBlock
= impl::getThreadIdInBlock(Dim
);
275 return ThreadIdInBlock
;
278 uint32_t mapping::getWarpSize() { return impl::getWarpSize(); }
280 uint32_t mapping::getMaxTeamThreads(bool IsSPMD
) {
281 uint32_t BlockSize
= mapping::getNumberOfThreadsInBlock();
282 // If we are in SPMD mode, remove one warp.
283 return BlockSize
- (!IsSPMD
* impl::getWarpSize());
285 uint32_t mapping::getMaxTeamThreads() {
286 return mapping::getMaxTeamThreads(mapping::isSPMDMode());
289 uint32_t mapping::getNumberOfThreadsInBlock(int32_t Dim
) {
290 return impl::getNumberOfThreadsInBlock(Dim
);
293 uint32_t mapping::getNumberOfThreadsInKernel() {
294 return impl::getNumberOfThreadsInKernel();
297 uint32_t mapping::getWarpIdInBlock() {
298 uint32_t WarpID
= impl::getWarpIdInBlock();
299 ASSERT(WarpID
< impl::getNumberOfWarpsInBlock(), nullptr);
303 uint32_t mapping::getBlockIdInKernel(int32_t Dim
) {
304 uint32_t BlockId
= impl::getBlockIdInKernel(Dim
);
305 ASSERT(BlockId
< impl::getNumberOfBlocksInKernel(Dim
), nullptr);
309 uint32_t mapping::getNumberOfWarpsInBlock() {
310 uint32_t NumberOfWarpsInBlocks
= impl::getNumberOfWarpsInBlock();
311 ASSERT(impl::getWarpIdInBlock() < NumberOfWarpsInBlocks
, nullptr);
312 return NumberOfWarpsInBlocks
;
315 uint32_t mapping::getNumberOfBlocksInKernel(int32_t Dim
) {
316 uint32_t NumberOfBlocks
= impl::getNumberOfBlocksInKernel(Dim
);
317 ASSERT(impl::getBlockIdInKernel(Dim
) < NumberOfBlocks
, nullptr);
318 return NumberOfBlocks
;
321 uint32_t mapping::getNumberOfProcessorElements() {
322 return static_cast<uint32_t>(config::getHardwareParallelism());
331 // TODO: This is a workaround for initialization coming from kernels outside of
332 // the TU. We will need to solve this more correctly in the future.
333 [[gnu::weak
]] int SHARED(IsSPMDMode
);
335 void mapping::init(bool IsSPMD
) {
336 if (mapping::isInitialThreadInLevel0(IsSPMD
))
340 bool mapping::isSPMDMode() { return IsSPMDMode
; }
342 bool mapping::isGenericMode() { return !isSPMDMode(); }
346 [[gnu::noinline
]] uint32_t __kmpc_get_hardware_thread_id_in_block() {
347 return mapping::getThreadIdInBlock();
350 [[gnu::noinline
]] uint32_t __kmpc_get_hardware_num_threads_in_block() {
351 return impl::getNumberOfThreadsInBlock(mapping::DIM_X
);
354 [[gnu::noinline
]] uint32_t __kmpc_get_warp_size() {
355 return impl::getWarpSize();
359 #define _TGT_KERNEL_LANGUAGE(NAME, MAPPER_NAME) \
360 extern "C" int ompx_##NAME(int Dim) { return mapping::MAPPER_NAME(Dim); }
362 _TGT_KERNEL_LANGUAGE(thread_id
, getThreadIdInBlock
)
363 _TGT_KERNEL_LANGUAGE(block_id
, getBlockIdInKernel
)
364 _TGT_KERNEL_LANGUAGE(block_dim
, getNumberOfThreadsInBlock
)
365 _TGT_KERNEL_LANGUAGE(grid_dim
, getNumberOfBlocksInKernel
)
368 uint64_t ompx_ballot_sync(uint64_t mask
, int pred
) {
369 return utils::ballotSync(mask
, pred
);
372 int ompx_shfl_down_sync_i(uint64_t mask
, int var
, unsigned delta
, int width
) {
373 return utils::shuffleDown(mask
, var
, delta
, width
);
376 float ompx_shfl_down_sync_f(uint64_t mask
, float var
, unsigned delta
,
378 return utils::convertViaPun
<float>(utils::shuffleDown(
379 mask
, utils::convertViaPun
<int32_t>(var
), delta
, width
));
382 long ompx_shfl_down_sync_l(uint64_t mask
, long var
, unsigned delta
, int width
) {
383 return utils::shuffleDown(mask
, var
, delta
, width
);
386 double ompx_shfl_down_sync_d(uint64_t mask
, double var
, unsigned delta
,
388 return utils::convertViaPun
<double>(utils::shuffleDown(
389 mask
, utils::convertViaPun
<int64_t>(var
), delta
, width
));
393 #pragma omp end declare target