1 //===------- Utils.cpp - OpenMP device runtime utility functions -- 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 "Utils.h"
13 
14 #include "Interface.h"
15 #include "Mapping.h"
16 
17 #pragma omp declare target
18 
19 using namespace _OMP;
20 
21 namespace _OMP {
22 /// Helper to keep code alive without introducing a performance penalty.
23 __attribute__((used, weak, optnone)) void keepAlive() {
24   __kmpc_get_hardware_thread_id_in_block();
25   __kmpc_get_hardware_num_threads_in_block();
26   __kmpc_barrier_simple_spmd(nullptr, 0);
27 }
28 } // namespace _OMP
29 
30 namespace impl {
31 
32 /// AMDGCN Implementation
33 ///
34 ///{
35 #pragma omp begin declare variant match(device = {arch(amdgcn)})
36 
37 void Unpack(uint64_t Val, uint32_t *LowBits, uint32_t *HighBits) {
38   *LowBits = (uint32_t)(Val & UINT64_C(0x00000000FFFFFFFF));
39   *HighBits = (uint32_t)((Val & UINT64_C(0xFFFFFFFF00000000)) >> 32);
40 }
41 
42 uint64_t Pack(uint32_t LowBits, uint32_t HighBits) {
43   return (((uint64_t)HighBits) << 32) | (uint64_t)LowBits;
44 }
45 
46 #pragma omp end declare variant
47 
48 /// NVPTX Implementation
49 ///
50 ///{
51 #pragma omp begin declare variant match(                                       \
52     device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})
53 
54 void Unpack(uint64_t Val, uint32_t *LowBits, uint32_t *HighBits) {
55   uint32_t LowBitsLocal, HighBitsLocal;
56   asm("mov.b64 {%0,%1}, %2;"
57       : "=r"(LowBitsLocal), "=r"(HighBitsLocal)
58       : "l"(Val));
59   *LowBits = LowBitsLocal;
60   *HighBits = HighBitsLocal;
61 }
62 
63 uint64_t Pack(uint32_t LowBits, uint32_t HighBits) {
64   uint64_t Val;
65   asm("mov.b64 %0, {%1,%2};" : "=l"(Val) : "r"(LowBits), "r"(HighBits));
66   return Val;
67 }
68 
69 #pragma omp end declare variant
70 
71 /// AMDGCN Implementation
72 ///
73 ///{
74 #pragma omp begin declare variant match(device = {arch(amdgcn)})
75 
76 int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
77   int Width = mapping::getWarpSize();
78   int Self = mapping::getgetThreadIdInWarp();
79   int Index = SrcLane + (Self & ~(Width - 1));
80   return __builtin_amdgcn_ds_bpermute(Index << 2, Var);
81 }
82 
83 int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t LaneDelta,
84                     int32_t Width) {
85   int Self = mapping::getThreadIdInWarp();
86   int Index = Self + LaneDelta;
87   Index = (int)(LaneDelta + (Self & (Width - 1))) >= Width ? Self : Index;
88   return __builtin_amdgcn_ds_bpermute(Index << 2, Var);
89 }
90 
91 #pragma omp end declare variant
92 ///}
93 
94 /// NVPTX Implementation
95 ///
96 ///{
97 #pragma omp begin declare variant match(                                       \
98     device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})
99 
100 int32_t shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
101   return __nvvm_shfl_sync_idx_i32(Mask, Var, SrcLane, 0x1f);
102 }
103 
104 int32_t shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta, int32_t Width) {
105   int32_t T = ((mapping::getWarpSize() - Width) << 8) | 0x1f;
106   return __nvvm_shfl_sync_down_i32(Mask, Var, Delta, T);
107 }
108 
109 #pragma omp end declare variant
110 } // namespace impl
111 
112 uint64_t utils::pack(uint32_t LowBits, uint32_t HighBits) {
113   return impl::Pack(LowBits, HighBits);
114 }
115 
116 void utils::unpack(uint64_t Val, uint32_t &LowBits, uint32_t &HighBits) {
117   impl::Unpack(Val, &LowBits, &HighBits);
118 }
119 
120 int32_t utils::shuffle(uint64_t Mask, int32_t Var, int32_t SrcLane) {
121   return impl::shuffle(Mask, Var, SrcLane);
122 }
123 
124 int32_t utils::shuffleDown(uint64_t Mask, int32_t Var, uint32_t Delta,
125                            int32_t Width) {
126   return impl::shuffleDown(Mask, Var, Delta, Width);
127 }
128 
129 extern "C" {
130 int32_t __kmpc_shuffle_int32(int32_t Val, int16_t Delta, int16_t SrcLane) {
131   return impl::shuffleDown(lanes::All, Val, Delta, SrcLane);
132 }
133 
134 int64_t __kmpc_shuffle_int64(int64_t Val, int16_t Delta, int16_t Width) {
135   uint32_t lo, hi;
136   utils::unpack(Val, lo, hi);
137   hi = impl::shuffleDown(lanes::All, hi, Delta, Width);
138   lo = impl::shuffleDown(lanes::All, lo, Delta, Width);
139   return utils::pack(lo, hi);
140 }
141 }
142 
143 #pragma omp end declare target
144