blob: 4f39d2a299ee6073da92d917c6f660577048f2d1 [file] [log] [blame]
Johannes Doerfert67ab8752021-07-25 18:26:441//===------- Mapping.cpp - OpenMP device runtime mapping helpers -- C++ -*-===//
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//
10//===----------------------------------------------------------------------===//
11
12#include "Mapping.h"
Johannes Doerfert93bebdc2021-11-03 15:06:5713#include "Interface.h"
Johannes Doerfert67ab8752021-07-25 18:26:4414#include "State.h"
15#include "Types.h"
16#include "Utils.h"
17
Joseph Huberb4f84432022-05-09 18:22:5918#pragma omp begin declare target device_type(nohost)
Ron Liebermanb09a5e52022-11-29 21:19:4419
Jon Chesterfield842f8752021-08-23 19:25:2320#include "llvm/Frontend/OpenMP/OMPGridValues.h"
21
Johannes Doerfert2b5a99b2022-12-19 18:54:1522using namespace ompx;
Johannes Doerfert67ab8752021-07-25 18:26:4423
Johannes Doerfert2b5a99b2022-12-19 18:54:1524namespace ompx {
Johannes Doerfert67ab8752021-07-25 18:26:4425namespace impl {
26
Joseph Huberb4f84432022-05-09 18:22:5927// Forward declarations defined to be defined for AMDGCN and NVPTX.
28const llvm::omp::GV &getGridValue();
Joseph Huberb4f84432022-05-09 18:22:5929LaneMaskTy activemask();
30LaneMaskTy lanemaskLT();
31LaneMaskTy lanemaskGT();
32uint32_t getThreadIdInWarp();
Johannes Doerfert1f3a28d2023-07-26 22:20:2033uint32_t getThreadIdInBlock(int32_t Dim);
34uint32_t getNumberOfThreadsInBlock(int32_t Dim);
35uint32_t getNumberOfThreadsInKernel();
36uint32_t getBlockIdInKernel(int32_t Dim);
37uint32_t getNumberOfBlocksInKernel(int32_t Dim);
38uint32_t getWarpIdInBlock();
Joseph Huberb4f84432022-05-09 18:22:5939uint32_t getNumberOfWarpsInBlock();
40
Johannes Doerfert67ab8752021-07-25 18:26:4441/// AMDGCN Implementation
42///
43///{
44#pragma omp begin declare variant match(device = {arch(amdgcn)})
45
Joseph Huberce0caf42022-05-10 21:33:4146const llvm::omp::GV &getGridValue() {
Jon Chesterfield72729822021-10-19 07:05:0547 return llvm::omp::getAMDGPUGridValues<__AMDGCN_WAVEFRONT_SIZE>();
Jon Chesterfield842f8752021-08-23 19:25:2348}
49
Johannes Doerfert1f3a28d2023-07-26 22:20:2050uint32_t getNumberOfThreadsInBlock(int32_t Dim) {
51 switch (Dim) {
52 case 0:
53 return __builtin_amdgcn_workgroup_size_x();
54 case 1:
55 return __builtin_amdgcn_workgroup_size_y();
56 case 2:
57 return __builtin_amdgcn_workgroup_size_z();
58 };
59 UNREACHABLE("Dim outside range!");
Johannes Doerfert93bebdc2021-11-03 15:06:5760}
61
Johannes Doerfert67ab8752021-07-25 18:26:4462LaneMaskTy activemask() { return __builtin_amdgcn_read_exec(); }
63
64LaneMaskTy lanemaskLT() {
65 uint32_t Lane = mapping::getThreadIdInWarp();
66 int64_t Ballot = mapping::activemask();
67 uint64_t Mask = ((uint64_t)1 << Lane) - (uint64_t)1;
68 return Mask & Ballot;
69}
70
71LaneMaskTy lanemaskGT() {
72 uint32_t Lane = mapping::getThreadIdInWarp();
73 if (Lane == (mapping::getWarpSize() - 1))
74 return 0;
75 int64_t Ballot = mapping::activemask();
76 uint64_t Mask = (~((uint64_t)0)) << (Lane + 1);
77 return Mask & Ballot;
78}
79
80uint32_t getThreadIdInWarp() {
81 return __builtin_amdgcn_mbcnt_hi(~0u, __builtin_amdgcn_mbcnt_lo(~0u, 0u));
82}
83
Johannes Doerfert1f3a28d2023-07-26 22:20:2084uint32_t getThreadIdInBlock(int32_t Dim) {
85 switch (Dim) {
86 case 0:
87 return __builtin_amdgcn_workitem_id_x();
88 case 1:
89 return __builtin_amdgcn_workitem_id_y();
90 case 2:
91 return __builtin_amdgcn_workitem_id_z();
92 };
93 UNREACHABLE("Dim outside range!");
Ethan Stewart85c2d92b2022-11-02 16:37:4294}
Johannes Doerfert67ab8752021-07-25 18:26:4495
Johannes Doerfert1f3a28d2023-07-26 22:20:2096uint32_t getNumberOfThreadsInKernel() {
97 return __builtin_amdgcn_grid_size_x() * __builtin_amdgcn_grid_size_y() *
98 __builtin_amdgcn_grid_size_z();
99}
100
101uint32_t getBlockIdInKernel(int32_t Dim) {
102 switch (Dim) {
103 case 0:
104 return __builtin_amdgcn_workgroup_id_x();
105 case 1:
106 return __builtin_amdgcn_workgroup_id_y();
107 case 2:
108 return __builtin_amdgcn_workgroup_id_z();
109 };
110 UNREACHABLE("Dim outside range!");
111}
112
113uint32_t getNumberOfBlocksInKernel(int32_t Dim) {
114 switch (Dim) {
115 case 0:
116 return __builtin_amdgcn_grid_size_x() / __builtin_amdgcn_workgroup_size_x();
117 case 1:
118 return __builtin_amdgcn_grid_size_y() / __builtin_amdgcn_workgroup_size_y();
119 case 2:
120 return __builtin_amdgcn_grid_size_z() / __builtin_amdgcn_workgroup_size_z();
121 };
122 UNREACHABLE("Dim outside range!");
123}
124
125uint32_t getWarpIdInBlock() {
126 return impl::getThreadIdInBlock(mapping::DIM_X) / mapping::getWarpSize();
Johannes Doerfert67ab8752021-07-25 18:26:44127}
128
Johannes Doerfert67ab8752021-07-25 18:26:44129uint32_t getNumberOfWarpsInBlock() {
Johannes Doerfert1f3a28d2023-07-26 22:20:20130 return mapping::getNumberOfThreadsInBlock() / mapping::getWarpSize();
Johannes Doerfert67ab8752021-07-25 18:26:44131}
132
133#pragma omp end declare variant
134///}
135
136/// NVPTX Implementation
137///
138///{
139#pragma omp begin declare variant match( \
Joseph Huber47800a12023-05-23 16:09:16140 device = {arch(nvptx, nvptx64)}, \
141 implementation = {extension(match_any)})
Johannes Doerfert67ab8752021-07-25 18:26:44142
Johannes Doerfert1f3a28d2023-07-26 22:20:20143uint32_t getNumberOfThreadsInBlock(int32_t Dim) {
144 switch (Dim) {
145 case 0:
146 return __nvvm_read_ptx_sreg_ntid_x();
147 case 1:
148 return __nvvm_read_ptx_sreg_ntid_y();
149 case 2:
150 return __nvvm_read_ptx_sreg_ntid_z();
151 };
152 UNREACHABLE("Dim outside range!");
Johannes Doerfert93bebdc2021-11-03 15:06:57153}
154
Joseph Huberce0caf42022-05-10 21:33:41155const llvm::omp::GV &getGridValue() { return llvm::omp::NVPTXGridValues; }
Jon Chesterfield842f8752021-08-23 19:25:23156
Joseph Huber6aed6cc2024-01-30 14:08:51157LaneMaskTy activemask() { return __nvvm_activemask(); }
Johannes Doerfert67ab8752021-07-25 18:26:44158
Joseph Huber6aed6cc2024-01-30 14:08:51159LaneMaskTy lanemaskLT() { return __nvvm_read_ptx_sreg_lanemask_lt(); }
Johannes Doerfert67ab8752021-07-25 18:26:44160
Joseph Huber6aed6cc2024-01-30 14:08:51161LaneMaskTy lanemaskGT() { return __nvvm_read_ptx_sreg_lanemask_gt(); }
Johannes Doerfert67ab8752021-07-25 18:26:44162
Johannes Doerfert1f3a28d2023-07-26 22:20:20163uint32_t getThreadIdInBlock(int32_t Dim) {
164 switch (Dim) {
165 case 0:
166 return __nvvm_read_ptx_sreg_tid_x();
167 case 1:
168 return __nvvm_read_ptx_sreg_tid_y();
169 case 2:
170 return __nvvm_read_ptx_sreg_tid_z();
171 };
172 UNREACHABLE("Dim outside range!");
173}
Johannes Doerfert67ab8752021-07-25 18:26:44174
Joseph Huber9f69d3c2024-03-12 15:39:40175uint32_t getThreadIdInWarp() { return __nvvm_read_ptx_sreg_laneid(); }
Johannes Doerfert67ab8752021-07-25 18:26:44176
Johannes Doerfert1f3a28d2023-07-26 22:20:20177uint32_t getBlockIdInKernel(int32_t Dim) {
178 switch (Dim) {
179 case 0:
180 return __nvvm_read_ptx_sreg_ctaid_x();
181 case 1:
182 return __nvvm_read_ptx_sreg_ctaid_y();
183 case 2:
184 return __nvvm_read_ptx_sreg_ctaid_z();
185 };
186 UNREACHABLE("Dim outside range!");
Johannes Doerfert93bebdc2021-11-03 15:06:57187}
Johannes Doerfert67ab8752021-07-25 18:26:44188
Johannes Doerfert1f3a28d2023-07-26 22:20:20189uint32_t getNumberOfBlocksInKernel(int32_t Dim) {
190 switch (Dim) {
191 case 0:
192 return __nvvm_read_ptx_sreg_nctaid_x();
193 case 1:
194 return __nvvm_read_ptx_sreg_nctaid_y();
195 case 2:
196 return __nvvm_read_ptx_sreg_nctaid_z();
197 };
198 UNREACHABLE("Dim outside range!");
199}
Johannes Doerfert67ab8752021-07-25 18:26:44200
Johannes Doerfert1f3a28d2023-07-26 22:20:20201uint32_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);
208}
Johannes Doerfert67ab8752021-07-25 18:26:44209
Johannes Doerfert1f3a28d2023-07-26 22:20:20210uint32_t getWarpIdInBlock() {
211 return impl::getThreadIdInBlock(mapping::DIM_X) / mapping::getWarpSize();
Johannes Doerfert67ab8752021-07-25 18:26:44212}
213
Johannes Doerfert67ab8752021-07-25 18:26:44214uint32_t getNumberOfWarpsInBlock() {
Johannes Doerfert1f3a28d2023-07-26 22:20:20215 return (mapping::getNumberOfThreadsInBlock() + mapping::getWarpSize() - 1) /
Johannes Doerfert67ab8752021-07-25 18:26:44216 mapping::getWarpSize();
217}
218
219#pragma omp end declare variant
220///}
221
Jon Chesterfield842f8752021-08-23 19:25:23222uint32_t getWarpSize() { return getGridValue().GV_Warp_Size; }
223
Johannes Doerfert67ab8752021-07-25 18:26:44224} // namespace impl
Johannes Doerfert2b5a99b2022-12-19 18:54:15225} // namespace ompx
Johannes Doerfert67ab8752021-07-25 18:26:44226
Johannes Doerfert93bebdc2021-11-03 15:06:57227/// We have to be deliberate about the distinction of `mapping::` and `impl::`
228/// below to avoid repeating assumptions or including irrelevant ones.
229///{
230
Johannes Doerfertccb5d272021-10-30 19:24:25231static bool isInLastWarp() {
Johannes Doerfert1f3a28d2023-07-26 22:20:20232 uint32_t MainTId = (mapping::getNumberOfThreadsInBlock() - 1) &
Johannes Doerfert67ab8752021-07-25 18:26:44233 ~(mapping::getWarpSize() - 1);
234 return mapping::getThreadIdInBlock() == MainTId;
235}
236
Johannes Doerfertccb5d272021-10-30 19:24:25237bool mapping::isMainThreadInGenericMode(bool IsSPMD) {
238 if (IsSPMD || icv::Level)
239 return false;
240
241 // Check if this is the last warp in the block.
242 return isInLastWarp();
243}
244
Joseph Huber85ad5662021-10-09 01:52:54245bool mapping::isMainThreadInGenericMode() {
246 return mapping::isMainThreadInGenericMode(mapping::isSPMDMode());
247}
248
Johannes Doerfertccb5d272021-10-30 19:24:25249bool mapping::isInitialThreadInLevel0(bool IsSPMD) {
250 if (IsSPMD)
251 return mapping::getThreadIdInBlock() == 0;
252 return isInLastWarp();
253}
254
Johannes Doerfert67ab8752021-07-25 18:26:44255bool 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;
259}
260
261LaneMaskTy mapping::activemask() { return impl::activemask(); }
262
263LaneMaskTy mapping::lanemaskLT() { return impl::lanemaskLT(); }
264
265LaneMaskTy mapping::lanemaskGT() { return impl::lanemaskGT(); }
266
Johannes Doerfert93bebdc2021-11-03 15:06:57267uint32_t mapping::getThreadIdInWarp() {
268 uint32_t ThreadIdInWarp = impl::getThreadIdInWarp();
Johannes Doerfert88a68de2023-07-18 23:05:08269 ASSERT(ThreadIdInWarp < impl::getWarpSize(), nullptr);
Johannes Doerfert93bebdc2021-11-03 15:06:57270 return ThreadIdInWarp;
Johannes Doerfert67ab8752021-07-25 18:26:44271}
272
Johannes Doerfert1f3a28d2023-07-26 22:20:20273uint32_t mapping::getThreadIdInBlock(int32_t Dim) {
274 uint32_t ThreadIdInBlock = impl::getThreadIdInBlock(Dim);
Johannes Doerfert93bebdc2021-11-03 15:06:57275 return ThreadIdInBlock;
276}
Johannes Doerfert67ab8752021-07-25 18:26:44277
278uint32_t mapping::getWarpSize() { return impl::getWarpSize(); }
279
Johannes Doerfert1f3a28d2023-07-26 22:20:20280uint32_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());
Johannes Doerfert67ab8752021-07-25 18:26:44284}
Johannes Doerfert1f3a28d2023-07-26 22:20:20285uint32_t mapping::getMaxTeamThreads() {
286 return mapping::getMaxTeamThreads(mapping::isSPMDMode());
Johannes Doerfert57b4c522022-02-14 23:19:33287}
Johannes Doerfert67ab8752021-07-25 18:26:44288
Johannes Doerfert1f3a28d2023-07-26 22:20:20289uint32_t mapping::getNumberOfThreadsInBlock(int32_t Dim) {
290 return impl::getNumberOfThreadsInBlock(Dim);
291}
Johannes Doerfert93bebdc2021-11-03 15:06:57292
Johannes Doerfert1f3a28d2023-07-26 22:20:20293uint32_t mapping::getNumberOfThreadsInKernel() {
294 return impl::getNumberOfThreadsInKernel();
295}
296
297uint32_t mapping::getWarpIdInBlock() {
298 uint32_t WarpID = impl::getWarpIdInBlock();
Johannes Doerfert88a68de2023-07-18 23:05:08299 ASSERT(WarpID < impl::getNumberOfWarpsInBlock(), nullptr);
Johannes Doerfert93bebdc2021-11-03 15:06:57300 return WarpID;
301}
302
Johannes Doerfert1f3a28d2023-07-26 22:20:20303uint32_t mapping::getBlockIdInKernel(int32_t Dim) {
304 uint32_t BlockId = impl::getBlockIdInKernel(Dim);
305 ASSERT(BlockId < impl::getNumberOfBlocksInKernel(Dim), nullptr);
Johannes Doerfert93bebdc2021-11-03 15:06:57306 return BlockId;
307}
308
309uint32_t mapping::getNumberOfWarpsInBlock() {
310 uint32_t NumberOfWarpsInBlocks = impl::getNumberOfWarpsInBlock();
Johannes Doerfert1f3a28d2023-07-26 22:20:20311 ASSERT(impl::getWarpIdInBlock() < NumberOfWarpsInBlocks, nullptr);
Johannes Doerfert93bebdc2021-11-03 15:06:57312 return NumberOfWarpsInBlocks;
313}
314
Johannes Doerfert1f3a28d2023-07-26 22:20:20315uint32_t mapping::getNumberOfBlocksInKernel(int32_t Dim) {
316 uint32_t NumberOfBlocks = impl::getNumberOfBlocksInKernel(Dim);
317 ASSERT(impl::getBlockIdInKernel(Dim) < NumberOfBlocks, nullptr);
Johannes Doerfert93bebdc2021-11-03 15:06:57318 return NumberOfBlocks;
319}
320
Joseph Huber460840c2023-09-06 18:45:05321uint32_t mapping::getNumberOfProcessorElements() {
322 return static_cast<uint32_t>(config::getHardwareParallelism());
323}
Johannes Doerfert93bebdc2021-11-03 15:06:57324
325///}
326
Johannes Doerfert67ab8752021-07-25 18:26:44327/// Execution mode
328///
329///{
Joseph Hubere2dcc222022-03-04 17:07:57330
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.
Joseph Huberb69081e2023-10-18 16:52:43333[[gnu::weak]] int SHARED(IsSPMDMode);
Johannes Doerfert67ab8752021-07-25 18:26:44334
335void mapping::init(bool IsSPMD) {
Johannes Doerfertccb5d272021-10-30 19:24:25336 if (mapping::isInitialThreadInLevel0(IsSPMD))
Johannes Doerfert67ab8752021-07-25 18:26:44337 IsSPMDMode = IsSPMD;
338}
339
340bool mapping::isSPMDMode() { return IsSPMDMode; }
341
342bool mapping::isGenericMode() { return !isSPMDMode(); }
343///}
344
Joseph Hubere95731c2021-09-21 19:32:41345extern "C" {
Joseph Huberb69081e2023-10-18 16:52:43346[[gnu::noinline]] uint32_t __kmpc_get_hardware_thread_id_in_block() {
Joseph Hubere95731c2021-09-21 19:32:41347 return mapping::getThreadIdInBlock();
348}
Joseph Huberbad44d52021-10-09 00:08:28349
Joseph Huberb69081e2023-10-18 16:52:43350[[gnu::noinline]] uint32_t __kmpc_get_hardware_num_threads_in_block() {
Johannes Doerfert1f3a28d2023-07-26 22:20:20351 return impl::getNumberOfThreadsInBlock(mapping::DIM_X);
Joseph Huberbad44d52021-10-09 00:08:28352}
Atmn Patel737c4a22021-11-09 04:16:54353
Joseph Huberb69081e2023-10-18 16:52:43354[[gnu::noinline]] uint32_t __kmpc_get_warp_size() {
Atmn Patel737c4a22021-11-09 04:16:54355 return impl::getWarpSize();
356}
Joseph Hubere95731c2021-09-21 19:32:41357}
Johannes Doerfert1f3a28d2023-07-26 22:20:20358
Johannes Doerfertdaef6d32023-07-27 00:25:39359#define _TGT_KERNEL_LANGUAGE(NAME, MAPPER_NAME) \
360 extern "C" int ompx_##NAME(int Dim) { return mapping::MAPPER_NAME(Dim); }
361
362_TGT_KERNEL_LANGUAGE(thread_id, getThreadIdInBlock)
Johannes Doerfertdaef6d32023-07-27 00:25:39363_TGT_KERNEL_LANGUAGE(block_id, getBlockIdInKernel)
Shilei Tianfcf1a102023-08-04 01:50:58364_TGT_KERNEL_LANGUAGE(block_dim, getNumberOfThreadsInBlock)
365_TGT_KERNEL_LANGUAGE(grid_dim, getNumberOfBlocksInKernel)
Johannes Doerfertdaef6d32023-07-27 00:25:39366
Shilei Tian7eeec8e2024-05-24 13:54:54367extern "C" uint64_t ompx_ballot_sync(uint64_t mask, int pred) {
368 return utils::ballotSync(mask, pred);
369}
370
Johannes Doerfert67ab8752021-07-25 18:26:44371#pragma omp end declare target