blob: 0fd261150f273ea5d729bc0910bb82892ff8ed3f [file] [log] [blame]
Samuel Antao45bfe4c2016-02-08 15:59:20 +00001//===---- CGOpenMPRuntimeNVPTX.cpp - Interface to OpenMP NVPTX Runtimes ---===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a class for OpenMP runtime code generation specialized to NVPTX
11// targets.
12//
13//===----------------------------------------------------------------------===//
14
15#include "CGOpenMPRuntimeNVPTX.h"
Carlo Bertollic6872252016-04-04 15:55:02 +000016#include "CodeGenFunction.h"
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000017#include "clang/AST/DeclOpenMP.h"
Carlo Bertollic6872252016-04-04 15:55:02 +000018#include "clang/AST/StmtOpenMP.h"
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000019#include "clang/AST/StmtVisitor.h"
20#include "llvm/ADT/SmallPtrSet.h"
Samuel Antao45bfe4c2016-02-08 15:59:20 +000021
22using namespace clang;
23using namespace CodeGen;
24
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +000025namespace {
26enum OpenMPRTLFunctionNVPTX {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000027 /// Call to void __kmpc_kernel_init(kmp_int32 thread_limit,
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +000028 /// int16_t RequiresOMPRuntime);
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +000029 OMPRTL_NVPTX__kmpc_kernel_init,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000030 /// Call to void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +000031 OMPRTL_NVPTX__kmpc_kernel_deinit,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000032 /// Call to void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +000033 /// int16_t RequiresOMPRuntime, int16_t RequiresDataSharing);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +000034 OMPRTL_NVPTX__kmpc_spmd_kernel_init,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000035 /// Call to void __kmpc_spmd_kernel_deinit();
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +000036 OMPRTL_NVPTX__kmpc_spmd_kernel_deinit,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000037 /// Call to void __kmpc_kernel_prepare_parallel(void
Gheorghe-Teodor Bercea7d80da12018-03-07 21:59:50 +000038 /// *outlined_function, int16_t
Jonas Hahnfeldfa059ba2017-12-27 10:39:56 +000039 /// IsOMPRuntimeInitialized);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +000040 OMPRTL_NVPTX__kmpc_kernel_prepare_parallel,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000041 /// Call to bool __kmpc_kernel_parallel(void **outlined_function,
Gheorghe-Teodor Bercea7d80da12018-03-07 21:59:50 +000042 /// int16_t IsOMPRuntimeInitialized);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +000043 OMPRTL_NVPTX__kmpc_kernel_parallel,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000044 /// Call to void __kmpc_kernel_end_parallel();
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +000045 OMPRTL_NVPTX__kmpc_kernel_end_parallel,
46 /// Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
47 /// global_tid);
48 OMPRTL_NVPTX__kmpc_serialized_parallel,
49 /// Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
50 /// global_tid);
51 OMPRTL_NVPTX__kmpc_end_serialized_parallel,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000052 /// Call to int32_t __kmpc_shuffle_int32(int32_t element,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +000053 /// int16_t lane_offset, int16_t warp_size);
54 OMPRTL_NVPTX__kmpc_shuffle_int32,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000055 /// Call to int64_t __kmpc_shuffle_int64(int64_t element,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +000056 /// int16_t lane_offset, int16_t warp_size);
57 OMPRTL_NVPTX__kmpc_shuffle_int64,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000058 /// Call to __kmpc_nvptx_parallel_reduce_nowait(kmp_int32
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +000059 /// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
60 /// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
61 /// lane_offset, int16_t shortCircuit),
62 /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num));
63 OMPRTL_NVPTX__kmpc_parallel_reduce_nowait,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000064 /// Call to __kmpc_nvptx_simd_reduce_nowait(kmp_int32
Alexey Bataevfac26cf2018-05-02 20:03:27 +000065 /// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
66 /// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
67 /// lane_offset, int16_t shortCircuit),
68 /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num));
69 OMPRTL_NVPTX__kmpc_simd_reduce_nowait,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000070 /// Call to __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +000071 /// int32_t num_vars, size_t reduce_size, void *reduce_data,
72 /// void (*kmp_ShuffleReductFctPtr)(void *rhs, int16_t lane_id, int16_t
73 /// lane_offset, int16_t shortCircuit),
74 /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
75 /// void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
76 /// int32_t index, int32_t width),
77 /// void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad, int32_t
78 /// index, int32_t width, int32_t reduce))
79 OMPRTL_NVPTX__kmpc_teams_reduce_nowait,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000080 /// Call to __kmpc_nvptx_end_reduce_nowait(int32_t global_tid);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000081 OMPRTL_NVPTX__kmpc_end_reduce_nowait,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000082 /// Call to void __kmpc_data_sharing_init_stack();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000083 OMPRTL_NVPTX__kmpc_data_sharing_init_stack,
Gheorghe-Teodor Berceaad4e5792018-07-13 16:18:24 +000084 /// Call to void __kmpc_data_sharing_init_stack_spmd();
85 OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000086 /// Call to void* __kmpc_data_sharing_push_stack(size_t size,
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000087 /// int16_t UseSharedMemory);
88 OMPRTL_NVPTX__kmpc_data_sharing_push_stack,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000089 /// Call to void __kmpc_data_sharing_pop_stack(void *a);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000090 OMPRTL_NVPTX__kmpc_data_sharing_pop_stack,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000091 /// Call to void __kmpc_begin_sharing_variables(void ***args,
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000092 /// size_t n_args);
93 OMPRTL_NVPTX__kmpc_begin_sharing_variables,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000094 /// Call to void __kmpc_end_sharing_variables();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000095 OMPRTL_NVPTX__kmpc_end_sharing_variables,
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000096 /// Call to void __kmpc_get_shared_variables(void ***GlobalArgs)
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +000097 OMPRTL_NVPTX__kmpc_get_shared_variables,
Alexey Bataevd7ff6d62018-05-07 14:50:05 +000098 /// Call to uint16_t __kmpc_parallel_level(ident_t *loc, kmp_int32
99 /// global_tid);
100 OMPRTL_NVPTX__kmpc_parallel_level,
Alexey Bataev673110d2018-05-16 13:36:30 +0000101 /// Call to int8_t __kmpc_is_spmd_exec_mode();
102 OMPRTL_NVPTX__kmpc_is_spmd_exec_mode,
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000103};
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000104
105/// Pre(post)-action for different OpenMP constructs specialized for NVPTX.
106class NVPTXActionTy final : public PrePostActionTy {
Alexey Bataev9ff80832018-04-16 20:16:21 +0000107 llvm::Value *EnterCallee = nullptr;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000108 ArrayRef<llvm::Value *> EnterArgs;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000109 llvm::Value *ExitCallee = nullptr;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000110 ArrayRef<llvm::Value *> ExitArgs;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000111 bool Conditional = false;
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000112 llvm::BasicBlock *ContBlock = nullptr;
113
114public:
115 NVPTXActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs,
116 llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs,
117 bool Conditional = false)
118 : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
119 ExitArgs(ExitArgs), Conditional(Conditional) {}
120 void Enter(CodeGenFunction &CGF) override {
121 llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
122 if (Conditional) {
123 llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
124 auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
125 ContBlock = CGF.createBasicBlock("omp_if.end");
126 // Generate the branch (If-stmt)
127 CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
128 CGF.EmitBlock(ThenBlock);
129 }
130 }
131 void Done(CodeGenFunction &CGF) {
132 // Emit the rest of blocks/branches
133 CGF.EmitBranch(ContBlock);
134 CGF.EmitBlock(ContBlock, true);
135 }
136 void Exit(CodeGenFunction &CGF) override {
137 CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
138 }
139};
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000140
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000141/// A class to track the execution mode when codegening directives within
142/// a target region. The appropriate mode (SPMD|NON-SPMD) is set on entry
143/// to the target region and used by containing directives such as 'parallel'
144/// to emit optimized code.
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000145class ExecutionModeRAII {
146private:
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000147 CGOpenMPRuntimeNVPTX::ExecutionMode SavedMode;
148 CGOpenMPRuntimeNVPTX::ExecutionMode &Mode;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000149
150public:
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000151 ExecutionModeRAII(CGOpenMPRuntimeNVPTX::ExecutionMode &Mode, bool IsSPMD)
152 : Mode(Mode) {
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000153 SavedMode = Mode;
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000154 Mode = IsSPMD ? CGOpenMPRuntimeNVPTX::EM_SPMD
155 : CGOpenMPRuntimeNVPTX::EM_NonSPMD;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000156 }
157 ~ExecutionModeRAII() { Mode = SavedMode; }
158};
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000159
160/// GPU Configuration: This information can be derived from cuda registers,
161/// however, providing compile time constants helps generate more efficient
162/// code. For all practical purposes this is fine because the configuration
163/// is the same for all known NVPTX architectures.
164enum MachineConfiguration : unsigned {
165 WarpSize = 32,
166 /// Number of bits required to represent a lane identifier, which is
167 /// computed as log_2(WarpSize).
168 LaneIDBits = 5,
169 LaneIDMask = WarpSize - 1,
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +0000170
171 /// Global memory alignment for performance.
172 GlobalMemoryAlignment = 256,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000173};
174
175enum NamedBarrier : unsigned {
176 /// Synchronize on this barrier #ID using a named barrier primitive.
177 /// Only the subset of active threads in a parallel region arrive at the
178 /// barrier.
179 NB_Parallel = 1,
180};
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000181
182/// Get the list of variables that can escape their declaration context.
183class CheckVarsEscapingDeclContext final
184 : public ConstStmtVisitor<CheckVarsEscapingDeclContext> {
185 CodeGenFunction &CGF;
186 llvm::SetVector<const ValueDecl *> EscapedDecls;
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000187 llvm::SetVector<const ValueDecl *> EscapedVariableLengthDecls;
Alexey Bataevc99042b2018-03-15 18:10:54 +0000188 llvm::SmallPtrSet<const Decl *, 4> EscapedParameters;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000189 RecordDecl *GlobalizedRD = nullptr;
190 llvm::SmallDenseMap<const ValueDecl *, const FieldDecl *> MappedDeclsFields;
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000191 bool AllEscaped = false;
Alexey Bataev91433f62018-06-26 17:24:03 +0000192 bool IsForCombinedParallelRegion = false;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000193
194 void markAsEscaped(const ValueDecl *VD) {
Alexey Bataev03f270c2018-03-30 18:31:07 +0000195 // Do not globalize declare target variables.
Alexey Bataev97b72212018-08-14 18:31:20 +0000196 if (!isa<VarDecl>(VD) ||
197 OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(VD))
Alexey Bataev03f270c2018-03-30 18:31:07 +0000198 return;
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000199 VD = cast<ValueDecl>(VD->getCanonicalDecl());
Alexey Bataevc99042b2018-03-15 18:10:54 +0000200 // Variables captured by value must be globalized.
201 if (auto *CSI = CGF.CapturedStmtInfo) {
Mikael Holmen9f373a32018-03-16 07:27:57 +0000202 if (const FieldDecl *FD = CSI->lookup(cast<VarDecl>(VD))) {
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000203 // Check if need to capture the variable that was already captured by
204 // value in the outer region.
Alexey Bataev91433f62018-06-26 17:24:03 +0000205 if (!IsForCombinedParallelRegion) {
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000206 if (!FD->hasAttrs())
207 return;
208 const auto *Attr = FD->getAttr<OMPCaptureKindAttr>();
209 if (!Attr)
210 return;
211 if (!isOpenMPPrivate(
212 static_cast<OpenMPClauseKind>(Attr->getCaptureKind())) ||
213 Attr->getCaptureKind() == OMPC_map)
214 return;
215 }
216 if (!FD->getType()->isReferenceType()) {
217 assert(!VD->getType()->isVariablyModifiedType() &&
218 "Parameter captured by value with variably modified type");
219 EscapedParameters.insert(VD);
Alexey Bataev91433f62018-06-26 17:24:03 +0000220 } else if (!IsForCombinedParallelRegion) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000221 return;
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000222 }
Alexey Bataevc99042b2018-03-15 18:10:54 +0000223 }
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000224 }
225 if ((!CGF.CapturedStmtInfo ||
Alexey Bataev91433f62018-06-26 17:24:03 +0000226 (IsForCombinedParallelRegion && CGF.CapturedStmtInfo)) &&
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000227 VD->getType()->isReferenceType())
228 // Do not globalize variables with reference type.
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000229 return;
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000230 if (VD->getType()->isVariablyModifiedType())
231 EscapedVariableLengthDecls.insert(VD);
232 else
233 EscapedDecls.insert(VD);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000234 }
235
236 void VisitValueDecl(const ValueDecl *VD) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000237 if (VD->getType()->isLValueReferenceType())
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000238 markAsEscaped(VD);
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000239 if (const auto *VarD = dyn_cast<VarDecl>(VD)) {
240 if (!isa<ParmVarDecl>(VarD) && VarD->hasInit()) {
241 const bool SavedAllEscaped = AllEscaped;
242 AllEscaped = VD->getType()->isLValueReferenceType();
243 Visit(VarD->getInit());
244 AllEscaped = SavedAllEscaped;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000245 }
246 }
247 }
Alexey Bataev91433f62018-06-26 17:24:03 +0000248 void VisitOpenMPCapturedStmt(const CapturedStmt *S,
249 ArrayRef<OMPClause *> Clauses,
250 bool IsCombinedParallelRegion) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000251 if (!S)
252 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000253 for (const CapturedStmt::Capture &C : S->captures()) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000254 if (C.capturesVariable() && !C.capturesVariableByCopy()) {
255 const ValueDecl *VD = C.getCapturedVar();
Alexey Bataev91433f62018-06-26 17:24:03 +0000256 bool SavedIsForCombinedParallelRegion = IsForCombinedParallelRegion;
257 if (IsCombinedParallelRegion) {
258 // Check if the variable is privatized in the combined construct and
259 // those private copies must be shared in the inner parallel
260 // directive.
261 IsForCombinedParallelRegion = false;
262 for (const OMPClause *C : Clauses) {
263 if (!isOpenMPPrivate(C->getClauseKind()) ||
264 C->getClauseKind() == OMPC_reduction ||
265 C->getClauseKind() == OMPC_linear ||
266 C->getClauseKind() == OMPC_private)
267 continue;
268 ArrayRef<const Expr *> Vars;
269 if (const auto *PC = dyn_cast<OMPFirstprivateClause>(C))
270 Vars = PC->getVarRefs();
271 else if (const auto *PC = dyn_cast<OMPLastprivateClause>(C))
272 Vars = PC->getVarRefs();
273 else
274 llvm_unreachable("Unexpected clause.");
275 for (const auto *E : Vars) {
276 const Decl *D =
277 cast<DeclRefExpr>(E)->getDecl()->getCanonicalDecl();
278 if (D == VD->getCanonicalDecl()) {
279 IsForCombinedParallelRegion = true;
280 break;
281 }
282 }
283 if (IsForCombinedParallelRegion)
284 break;
285 }
286 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000287 markAsEscaped(VD);
288 if (isa<OMPCapturedExprDecl>(VD))
289 VisitValueDecl(VD);
Alexey Bataev91433f62018-06-26 17:24:03 +0000290 IsForCombinedParallelRegion = SavedIsForCombinedParallelRegion;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000291 }
292 }
293 }
294
295 typedef std::pair<CharUnits /*Align*/, const ValueDecl *> VarsDataTy;
296 static bool stable_sort_comparator(const VarsDataTy P1, const VarsDataTy P2) {
297 return P1.first > P2.first;
298 }
299
300 void buildRecordForGlobalizedVars() {
301 assert(!GlobalizedRD &&
302 "Record for globalized variables is built already.");
303 if (EscapedDecls.empty())
304 return;
305 ASTContext &C = CGF.getContext();
306 SmallVector<VarsDataTy, 4> GlobalizedVars;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000307 for (const ValueDecl *D : EscapedDecls)
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000308 GlobalizedVars.emplace_back(C.getDeclAlign(D), D);
309 std::stable_sort(GlobalizedVars.begin(), GlobalizedVars.end(),
310 stable_sort_comparator);
311 // Build struct _globalized_locals_ty {
312 // /* globalized vars */
313 // };
314 GlobalizedRD = C.buildImplicitRecord("_globalized_locals_ty");
315 GlobalizedRD->startDefinition();
316 for (const auto &Pair : GlobalizedVars) {
317 const ValueDecl *VD = Pair.second;
318 QualType Type = VD->getType();
319 if (Type->isLValueReferenceType())
320 Type = C.getPointerType(Type.getNonReferenceType());
321 else
322 Type = Type.getNonReferenceType();
323 SourceLocation Loc = VD->getLocation();
324 auto *Field = FieldDecl::Create(
325 C, GlobalizedRD, Loc, Loc, VD->getIdentifier(), Type,
326 C.getTrivialTypeSourceInfo(Type, SourceLocation()),
327 /*BW=*/nullptr, /*Mutable=*/false,
328 /*InitStyle=*/ICIS_NoInit);
329 Field->setAccess(AS_public);
330 GlobalizedRD->addDecl(Field);
331 if (VD->hasAttrs()) {
332 for (specific_attr_iterator<AlignedAttr> I(VD->getAttrs().begin()),
333 E(VD->getAttrs().end());
334 I != E; ++I)
335 Field->addAttr(*I);
336 }
337 MappedDeclsFields.try_emplace(VD, Field);
338 }
339 GlobalizedRD->completeDefinition();
340 }
341
342public:
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000343 CheckVarsEscapingDeclContext(CodeGenFunction &CGF) : CGF(CGF) {}
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000344 virtual ~CheckVarsEscapingDeclContext() = default;
345 void VisitDeclStmt(const DeclStmt *S) {
346 if (!S)
347 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000348 for (const Decl *D : S->decls())
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000349 if (const auto *VD = dyn_cast_or_null<ValueDecl>(D))
350 VisitValueDecl(VD);
351 }
352 void VisitOMPExecutableDirective(const OMPExecutableDirective *D) {
353 if (!D)
354 return;
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000355 if (!D->hasAssociatedStmt())
356 return;
357 if (const auto *S =
358 dyn_cast_or_null<CapturedStmt>(D->getAssociatedStmt())) {
359 // Do not analyze directives that do not actually require capturing,
360 // like `omp for` or `omp simd` directives.
361 llvm::SmallVector<OpenMPDirectiveKind, 4> CaptureRegions;
362 getOpenMPCaptureRegions(CaptureRegions, D->getDirectiveKind());
363 if (CaptureRegions.size() == 1 && CaptureRegions.back() == OMPD_unknown) {
364 VisitStmt(S->getCapturedStmt());
365 return;
Alexey Bataev673110d2018-05-16 13:36:30 +0000366 }
Alexey Bataev91433f62018-06-26 17:24:03 +0000367 VisitOpenMPCapturedStmt(
368 S, D->clauses(),
369 CaptureRegions.back() == OMPD_parallel &&
370 isOpenMPDistributeDirective(D->getDirectiveKind()));
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000371 }
372 }
373 void VisitCapturedStmt(const CapturedStmt *S) {
374 if (!S)
375 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000376 for (const CapturedStmt::Capture &C : S->captures()) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000377 if (C.capturesVariable() && !C.capturesVariableByCopy()) {
378 const ValueDecl *VD = C.getCapturedVar();
379 markAsEscaped(VD);
380 if (isa<OMPCapturedExprDecl>(VD))
381 VisitValueDecl(VD);
382 }
383 }
384 }
385 void VisitLambdaExpr(const LambdaExpr *E) {
386 if (!E)
387 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000388 for (const LambdaCapture &C : E->captures()) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000389 if (C.capturesVariable()) {
390 if (C.getCaptureKind() == LCK_ByRef) {
391 const ValueDecl *VD = C.getCapturedVar();
392 markAsEscaped(VD);
393 if (E->isInitCapture(&C) || isa<OMPCapturedExprDecl>(VD))
394 VisitValueDecl(VD);
395 }
396 }
397 }
398 }
399 void VisitBlockExpr(const BlockExpr *E) {
400 if (!E)
401 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000402 for (const BlockDecl::Capture &C : E->getBlockDecl()->captures()) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000403 if (C.isByRef()) {
404 const VarDecl *VD = C.getVariable();
405 markAsEscaped(VD);
406 if (isa<OMPCapturedExprDecl>(VD) || VD->isInitCapture())
407 VisitValueDecl(VD);
408 }
409 }
410 }
411 void VisitCallExpr(const CallExpr *E) {
412 if (!E)
413 return;
414 for (const Expr *Arg : E->arguments()) {
415 if (!Arg)
416 continue;
417 if (Arg->isLValue()) {
418 const bool SavedAllEscaped = AllEscaped;
419 AllEscaped = true;
420 Visit(Arg);
421 AllEscaped = SavedAllEscaped;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000422 } else {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000423 Visit(Arg);
Alexey Bataev9ff80832018-04-16 20:16:21 +0000424 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000425 }
426 Visit(E->getCallee());
427 }
428 void VisitDeclRefExpr(const DeclRefExpr *E) {
429 if (!E)
430 return;
431 const ValueDecl *VD = E->getDecl();
432 if (AllEscaped)
433 markAsEscaped(VD);
434 if (isa<OMPCapturedExprDecl>(VD))
435 VisitValueDecl(VD);
436 else if (const auto *VarD = dyn_cast<VarDecl>(VD))
437 if (VarD->isInitCapture())
438 VisitValueDecl(VD);
439 }
440 void VisitUnaryOperator(const UnaryOperator *E) {
441 if (!E)
442 return;
443 if (E->getOpcode() == UO_AddrOf) {
444 const bool SavedAllEscaped = AllEscaped;
445 AllEscaped = true;
446 Visit(E->getSubExpr());
447 AllEscaped = SavedAllEscaped;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000448 } else {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000449 Visit(E->getSubExpr());
Alexey Bataev9ff80832018-04-16 20:16:21 +0000450 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000451 }
452 void VisitImplicitCastExpr(const ImplicitCastExpr *E) {
453 if (!E)
454 return;
455 if (E->getCastKind() == CK_ArrayToPointerDecay) {
456 const bool SavedAllEscaped = AllEscaped;
457 AllEscaped = true;
458 Visit(E->getSubExpr());
459 AllEscaped = SavedAllEscaped;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000460 } else {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000461 Visit(E->getSubExpr());
Alexey Bataev9ff80832018-04-16 20:16:21 +0000462 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000463 }
464 void VisitExpr(const Expr *E) {
465 if (!E)
466 return;
467 bool SavedAllEscaped = AllEscaped;
468 if (!E->isLValue())
469 AllEscaped = false;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000470 for (const Stmt *Child : E->children())
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000471 if (Child)
472 Visit(Child);
473 AllEscaped = SavedAllEscaped;
474 }
475 void VisitStmt(const Stmt *S) {
476 if (!S)
477 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +0000478 for (const Stmt *Child : S->children())
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000479 if (Child)
480 Visit(Child);
481 }
482
Alexey Bataevc99042b2018-03-15 18:10:54 +0000483 /// Returns the record that handles all the escaped local variables and used
484 /// instead of their original storage.
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000485 const RecordDecl *getGlobalizedRecord() {
486 if (!GlobalizedRD)
487 buildRecordForGlobalizedVars();
488 return GlobalizedRD;
489 }
490
Alexey Bataevc99042b2018-03-15 18:10:54 +0000491 /// Returns the field in the globalized record for the escaped variable.
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000492 const FieldDecl *getFieldForGlobalizedVar(const ValueDecl *VD) const {
493 assert(GlobalizedRD &&
494 "Record for globalized variables must be generated already.");
495 auto I = MappedDeclsFields.find(VD);
496 if (I == MappedDeclsFields.end())
497 return nullptr;
498 return I->getSecond();
499 }
500
Alexey Bataevc99042b2018-03-15 18:10:54 +0000501 /// Returns the list of the escaped local variables/parameters.
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000502 ArrayRef<const ValueDecl *> getEscapedDecls() const {
503 return EscapedDecls.getArrayRef();
504 }
Alexey Bataevc99042b2018-03-15 18:10:54 +0000505
506 /// Checks if the escaped local variable is actually a parameter passed by
507 /// value.
508 const llvm::SmallPtrSetImpl<const Decl *> &getEscapedParameters() const {
509 return EscapedParameters;
510 }
Alexey Bataev63cc8e92018-03-20 14:45:59 +0000511
512 /// Returns the list of the escaped variables with the variably modified
513 /// types.
514 ArrayRef<const ValueDecl *> getEscapedVariableLengthDecls() const {
515 return EscapedVariableLengthDecls.getArrayRef();
516 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000517};
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000518} // anonymous namespace
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000519
520/// Get the GPU warp size.
521static llvm::Value *getNVPTXWarpSize(CodeGenFunction &CGF) {
Alexey Bataev3c595a62017-08-14 15:01:03 +0000522 return CGF.EmitRuntimeCall(
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000523 llvm::Intrinsic::getDeclaration(
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000524 &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize),
Alexey Bataev3c595a62017-08-14 15:01:03 +0000525 "nvptx_warp_size");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000526}
527
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000528/// Get the id of the current thread on the GPU.
529static llvm::Value *getNVPTXThreadID(CodeGenFunction &CGF) {
Alexey Bataev3c595a62017-08-14 15:01:03 +0000530 return CGF.EmitRuntimeCall(
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000531 llvm::Intrinsic::getDeclaration(
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000532 &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x),
Alexey Bataev3c595a62017-08-14 15:01:03 +0000533 "nvptx_tid");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000534}
535
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000536/// Get the id of the warp in the block.
537/// We assume that the warp size is 32, which is always the case
538/// on the NVPTX device, to generate more efficient code.
539static llvm::Value *getNVPTXWarpID(CodeGenFunction &CGF) {
540 CGBuilderTy &Bld = CGF.Builder;
541 return Bld.CreateAShr(getNVPTXThreadID(CGF), LaneIDBits, "nvptx_warp_id");
542}
543
544/// Get the id of the current lane in the Warp.
545/// We assume that the warp size is 32, which is always the case
546/// on the NVPTX device, to generate more efficient code.
547static llvm::Value *getNVPTXLaneID(CodeGenFunction &CGF) {
548 CGBuilderTy &Bld = CGF.Builder;
549 return Bld.CreateAnd(getNVPTXThreadID(CGF), Bld.getInt32(LaneIDMask),
550 "nvptx_lane_id");
551}
552
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000553/// Get the maximum number of threads in a block of the GPU.
554static llvm::Value *getNVPTXNumThreads(CodeGenFunction &CGF) {
Alexey Bataev3c595a62017-08-14 15:01:03 +0000555 return CGF.EmitRuntimeCall(
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000556 llvm::Intrinsic::getDeclaration(
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000557 &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x),
Alexey Bataev3c595a62017-08-14 15:01:03 +0000558 "nvptx_num_threads");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000559}
560
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000561/// Get barrier to synchronize all threads in a block.
562static void getNVPTXCTABarrier(CodeGenFunction &CGF) {
Alexey Bataev3c595a62017-08-14 15:01:03 +0000563 CGF.EmitRuntimeCall(llvm::Intrinsic::getDeclaration(
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000564 &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier0));
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000565}
566
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000567/// Get barrier #ID to synchronize selected (multiple of warp size) threads in
568/// a CTA.
569static void getNVPTXBarrier(CodeGenFunction &CGF, int ID,
570 llvm::Value *NumThreads) {
571 CGBuilderTy &Bld = CGF.Builder;
572 llvm::Value *Args[] = {Bld.getInt32(ID), NumThreads};
Alexey Bataev3c595a62017-08-14 15:01:03 +0000573 CGF.EmitRuntimeCall(llvm::Intrinsic::getDeclaration(
574 &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier),
575 Args);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000576}
577
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000578/// Synchronize all GPU threads in a block.
579static void syncCTAThreads(CodeGenFunction &CGF) { getNVPTXCTABarrier(CGF); }
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000580
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +0000581/// Synchronize worker threads in a parallel region.
582static void syncParallelThreads(CodeGenFunction &CGF, llvm::Value *NumThreads) {
583 return getNVPTXBarrier(CGF, NB_Parallel, NumThreads);
584}
585
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000586/// Get the value of the thread_limit clause in the teams directive.
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000587/// For the 'generic' execution mode, the runtime encodes thread_limit in
588/// the launch parameters, always starting thread_limit+warpSize threads per
589/// CTA. The threads in the last warp are reserved for master execution.
590/// For the 'spmd' execution mode, all threads in a CTA are part of the team.
591static llvm::Value *getThreadLimit(CodeGenFunction &CGF,
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000592 bool IsInSPMDExecutionMode = false) {
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000593 CGBuilderTy &Bld = CGF.Builder;
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000594 return IsInSPMDExecutionMode
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000595 ? getNVPTXNumThreads(CGF)
Alexey Bataeve290ec02018-04-06 16:03:36 +0000596 : Bld.CreateNUWSub(getNVPTXNumThreads(CGF), getNVPTXWarpSize(CGF),
597 "thread_limit");
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000598}
599
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000600/// Get the thread id of the OMP master thread.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000601/// The master thread id is the first thread (lane) of the last warp in the
602/// GPU block. Warp size is assumed to be some power of 2.
603/// Thread id is 0 indexed.
604/// E.g: If NumThreads is 33, master id is 32.
605/// If NumThreads is 64, master id is 32.
606/// If NumThreads is 1024, master id is 992.
Arpith Chacko Jacobccf2f732017-01-03 20:19:56 +0000607static llvm::Value *getMasterThreadID(CodeGenFunction &CGF) {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000608 CGBuilderTy &Bld = CGF.Builder;
609 llvm::Value *NumThreads = getNVPTXNumThreads(CGF);
610
611 // We assume that the warp size is a power of 2.
Alexey Bataeve290ec02018-04-06 16:03:36 +0000612 llvm::Value *Mask = Bld.CreateNUWSub(getNVPTXWarpSize(CGF), Bld.getInt32(1));
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000613
Alexey Bataeve290ec02018-04-06 16:03:36 +0000614 return Bld.CreateAnd(Bld.CreateNUWSub(NumThreads, Bld.getInt32(1)),
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000615 Bld.CreateNot(Mask), "master_tid");
616}
617
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000618CGOpenMPRuntimeNVPTX::WorkerFunctionState::WorkerFunctionState(
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000619 CodeGenModule &CGM, SourceLocation Loc)
Alexey Bataev9ff80832018-04-16 20:16:21 +0000620 : WorkerFn(nullptr), CGFI(CGM.getTypes().arrangeNullaryFunction()),
621 Loc(Loc) {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000622 createWorkerFunction(CGM);
Vasileios Kalintirise5c09592016-03-22 10:41:20 +0000623}
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000624
625void CGOpenMPRuntimeNVPTX::WorkerFunctionState::createWorkerFunction(
626 CodeGenModule &CGM) {
627 // Create an worker function with no arguments.
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000628
629 WorkerFn = llvm::Function::Create(
Alexey Bataev9ff80832018-04-16 20:16:21 +0000630 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
Alexey Bataevaee93892018-01-08 20:09:47 +0000631 /*placeholder=*/"_worker", &CGM.getModule());
Alexey Bataev9ff80832018-04-16 20:16:21 +0000632 CGM.SetInternalFunctionAttributes(GlobalDecl(), WorkerFn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +0000633 WorkerFn->setDoesNotRecurse();
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000634}
635
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000636CGOpenMPRuntimeNVPTX::ExecutionMode
637CGOpenMPRuntimeNVPTX::getExecutionMode() const {
638 return CurrentExecutionMode;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000639}
640
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000641static CGOpenMPRuntimeNVPTX::DataSharingMode
642getDataSharingMode(CodeGenModule &CGM) {
643 return CGM.getLangOpts().OpenMPCUDAMode ? CGOpenMPRuntimeNVPTX::CUDA
644 : CGOpenMPRuntimeNVPTX::Generic;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000645}
646
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000647/// Checks if the \p Body is the \a CompoundStmt and returns its child statement
648/// iff there is only one.
649static const Stmt *getSingleCompoundChild(const Stmt *Body) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000650 if (const auto *C = dyn_cast<CompoundStmt>(Body))
651 if (C->size() == 1)
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000652 return C->body_front();
653 return Body;
654}
655
656/// Check if the parallel directive has an 'if' clause with non-constant or
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000657/// false condition. Also, check if the number of threads is strictly specified
658/// and run those directives in non-SPMD mode.
659static bool hasParallelIfNumThreadsClause(ASTContext &Ctx,
660 const OMPExecutableDirective &D) {
661 if (D.hasClausesOfKind<OMPNumThreadsClause>())
662 return true;
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000663 for (const auto *C : D.getClausesOfKind<OMPIfClause>()) {
664 OpenMPDirectiveKind NameModifier = C->getNameModifier();
665 if (NameModifier != OMPD_parallel && NameModifier != OMPD_unknown)
666 continue;
667 const Expr *Cond = C->getCondition();
668 bool Result;
669 if (!Cond->EvaluateAsBooleanCondition(Result, Ctx) || !Result)
670 return true;
671 }
672 return false;
673}
674
675/// Check for inner (nested) SPMD construct, if any
676static bool hasNestedSPMDDirective(ASTContext &Ctx,
677 const OMPExecutableDirective &D) {
678 const auto *CS = D.getInnermostCapturedStmt();
679 const auto *Body = CS->getCapturedStmt()->IgnoreContainers();
680 const Stmt *ChildStmt = getSingleCompoundChild(Body);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000681
682 if (const auto *NestedDir = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
683 OpenMPDirectiveKind DKind = NestedDir->getDirectiveKind();
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000684 switch (D.getDirectiveKind()) {
685 case OMPD_target:
Alexey Bataevdf093e72018-05-11 19:45:14 +0000686 if (isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000687 !hasParallelIfNumThreadsClause(Ctx, *NestedDir))
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000688 return true;
689 if (DKind == OMPD_teams || DKind == OMPD_teams_distribute) {
690 Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers();
691 if (!Body)
692 return false;
693 ChildStmt = getSingleCompoundChild(Body);
694 if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
695 DKind = NND->getDirectiveKind();
Alexey Bataevdf093e72018-05-11 19:45:14 +0000696 if (isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000697 !hasParallelIfNumThreadsClause(Ctx, *NND))
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000698 return true;
699 if (DKind == OMPD_distribute) {
700 Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers();
701 if (!Body)
702 return false;
703 ChildStmt = getSingleCompoundChild(Body);
704 if (!ChildStmt)
705 return false;
706 if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
707 DKind = NND->getDirectiveKind();
Alexey Bataevdf093e72018-05-11 19:45:14 +0000708 return isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000709 !hasParallelIfNumThreadsClause(Ctx, *NND);
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000710 }
711 }
712 }
713 }
714 return false;
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000715 case OMPD_target_teams:
Alexey Bataevdf093e72018-05-11 19:45:14 +0000716 if (isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000717 !hasParallelIfNumThreadsClause(Ctx, *NestedDir))
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000718 return true;
719 if (DKind == OMPD_distribute) {
720 Body = NestedDir->getInnermostCapturedStmt()->IgnoreContainers();
721 if (!Body)
722 return false;
723 ChildStmt = getSingleCompoundChild(Body);
724 if (const auto *NND = dyn_cast<OMPExecutableDirective>(ChildStmt)) {
725 DKind = NND->getDirectiveKind();
Alexey Bataevdf093e72018-05-11 19:45:14 +0000726 return isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000727 !hasParallelIfNumThreadsClause(Ctx, *NND);
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000728 }
729 }
730 return false;
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000731 case OMPD_target_teams_distribute:
Alexey Bataevdf093e72018-05-11 19:45:14 +0000732 return isOpenMPParallelDirective(DKind) &&
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000733 !hasParallelIfNumThreadsClause(Ctx, *NestedDir);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000734 case OMPD_target_simd:
735 case OMPD_target_parallel:
736 case OMPD_target_parallel_for:
737 case OMPD_target_parallel_for_simd:
738 case OMPD_target_teams_distribute_simd:
739 case OMPD_target_teams_distribute_parallel_for:
740 case OMPD_target_teams_distribute_parallel_for_simd:
741 case OMPD_parallel:
742 case OMPD_for:
743 case OMPD_parallel_for:
744 case OMPD_parallel_sections:
745 case OMPD_for_simd:
746 case OMPD_parallel_for_simd:
747 case OMPD_cancel:
748 case OMPD_cancellation_point:
749 case OMPD_ordered:
750 case OMPD_threadprivate:
751 case OMPD_task:
752 case OMPD_simd:
753 case OMPD_sections:
754 case OMPD_section:
755 case OMPD_single:
756 case OMPD_master:
757 case OMPD_critical:
758 case OMPD_taskyield:
759 case OMPD_barrier:
760 case OMPD_taskwait:
761 case OMPD_taskgroup:
762 case OMPD_atomic:
763 case OMPD_flush:
764 case OMPD_teams:
765 case OMPD_target_data:
766 case OMPD_target_exit_data:
767 case OMPD_target_enter_data:
768 case OMPD_distribute:
769 case OMPD_distribute_simd:
770 case OMPD_distribute_parallel_for:
771 case OMPD_distribute_parallel_for_simd:
772 case OMPD_teams_distribute:
773 case OMPD_teams_distribute_simd:
774 case OMPD_teams_distribute_parallel_for:
775 case OMPD_teams_distribute_parallel_for_simd:
776 case OMPD_target_update:
777 case OMPD_declare_simd:
778 case OMPD_declare_target:
779 case OMPD_end_declare_target:
780 case OMPD_declare_reduction:
781 case OMPD_taskloop:
782 case OMPD_taskloop_simd:
783 case OMPD_unknown:
784 llvm_unreachable("Unexpected directive.");
785 }
786 }
787
788 return false;
789}
790
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000791static bool supportsSPMDExecutionMode(ASTContext &Ctx,
792 const OMPExecutableDirective &D) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000793 OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind();
794 switch (DirectiveKind) {
795 case OMPD_target:
796 case OMPD_target_teams:
797 case OMPD_target_teams_distribute:
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000798 return hasNestedSPMDDirective(Ctx, D);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000799 case OMPD_target_parallel:
800 case OMPD_target_parallel_for:
801 case OMPD_target_parallel_for_simd:
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000802 case OMPD_target_teams_distribute_parallel_for:
803 case OMPD_target_teams_distribute_parallel_for_simd:
Alexey Bataev2a3320a2018-05-15 18:01:01 +0000804 return !hasParallelIfNumThreadsClause(Ctx, D);
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000805 case OMPD_target_simd:
806 case OMPD_target_teams_distribute_simd:
Alexey Bataevdf093e72018-05-11 19:45:14 +0000807 return false;
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000808 case OMPD_parallel:
809 case OMPD_for:
810 case OMPD_parallel_for:
811 case OMPD_parallel_sections:
812 case OMPD_for_simd:
813 case OMPD_parallel_for_simd:
814 case OMPD_cancel:
815 case OMPD_cancellation_point:
816 case OMPD_ordered:
817 case OMPD_threadprivate:
818 case OMPD_task:
819 case OMPD_simd:
820 case OMPD_sections:
821 case OMPD_section:
822 case OMPD_single:
823 case OMPD_master:
824 case OMPD_critical:
825 case OMPD_taskyield:
826 case OMPD_barrier:
827 case OMPD_taskwait:
828 case OMPD_taskgroup:
829 case OMPD_atomic:
830 case OMPD_flush:
831 case OMPD_teams:
832 case OMPD_target_data:
833 case OMPD_target_exit_data:
834 case OMPD_target_enter_data:
835 case OMPD_distribute:
836 case OMPD_distribute_simd:
837 case OMPD_distribute_parallel_for:
838 case OMPD_distribute_parallel_for_simd:
839 case OMPD_teams_distribute:
840 case OMPD_teams_distribute_simd:
841 case OMPD_teams_distribute_parallel_for:
842 case OMPD_teams_distribute_parallel_for_simd:
843 case OMPD_target_update:
844 case OMPD_declare_simd:
845 case OMPD_declare_target:
846 case OMPD_end_declare_target:
847 case OMPD_declare_reduction:
848 case OMPD_taskloop:
849 case OMPD_taskloop_simd:
850 case OMPD_unknown:
851 break;
852 }
853 llvm_unreachable(
854 "Unknown programming model for OpenMP directive on NVPTX target.");
855}
856
857void CGOpenMPRuntimeNVPTX::emitNonSPMDKernel(const OMPExecutableDirective &D,
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000858 StringRef ParentName,
859 llvm::Function *&OutlinedFn,
860 llvm::Constant *&OutlinedFnID,
861 bool IsOffloadEntry,
862 const RegionCodeGenTy &CodeGen) {
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000863 ExecutionModeRAII ModeRAII(CurrentExecutionMode, /*IsSPMD=*/false);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000864 EntryFunctionState EST;
Stephen Kellyf2ceec42018-08-09 21:08:08 +0000865 WorkerFunctionState WST(CGM, D.getBeginLoc());
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +0000866 Work.clear();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000867 WrapperFunctionsMap.clear();
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000868
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000869 // Emit target region as a standalone region.
870 class NVPTXPrePostActionTy : public PrePostActionTy {
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000871 CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
872 CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000873
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000874 public:
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000875 NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000876 CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST)
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000877 : EST(EST), WST(WST) {}
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000878 void Enter(CodeGenFunction &CGF) override {
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000879 static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000880 .emitNonSPMDEntryHeader(CGF, EST, WST);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000881 }
882 void Exit(CodeGenFunction &CGF) override {
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000883 static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000884 .emitNonSPMDEntryFooter(CGF, EST);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000885 }
Alexey Bataev7cae94e2018-01-04 19:45:16 +0000886 } Action(EST, WST);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000887 CodeGen.setAction(Action);
888 emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
889 IsOffloadEntry, CodeGen);
890
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000891 // Now change the name of the worker function to correspond to this target
892 // region's entry function.
Alexey Bataev9ff80832018-04-16 20:16:21 +0000893 WST.WorkerFn->setName(Twine(OutlinedFn->getName(), "_worker"));
Alexey Bataevaee93892018-01-08 20:09:47 +0000894
895 // Create the worker function
896 emitWorkerFunction(WST);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000897}
898
899// Setup NVPTX threads for master-worker OpenMP scheme.
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000900void CGOpenMPRuntimeNVPTX::emitNonSPMDEntryHeader(CodeGenFunction &CGF,
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000901 EntryFunctionState &EST,
902 WorkerFunctionState &WST) {
903 CGBuilderTy &Bld = CGF.Builder;
904
905 llvm::BasicBlock *WorkerBB = CGF.createBasicBlock(".worker");
906 llvm::BasicBlock *MasterCheckBB = CGF.createBasicBlock(".mastercheck");
907 llvm::BasicBlock *MasterBB = CGF.createBasicBlock(".master");
908 EST.ExitBB = CGF.createBasicBlock(".exit");
909
Alexey Bataev9ff80832018-04-16 20:16:21 +0000910 llvm::Value *IsWorker =
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000911 Bld.CreateICmpULT(getNVPTXThreadID(CGF), getThreadLimit(CGF));
912 Bld.CreateCondBr(IsWorker, WorkerBB, MasterCheckBB);
913
914 CGF.EmitBlock(WorkerBB);
Alexey Bataevb7f3cba2018-03-19 17:04:07 +0000915 emitCall(CGF, WST.Loc, WST.WorkerFn);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000916 CGF.EmitBranch(EST.ExitBB);
917
918 CGF.EmitBlock(MasterCheckBB);
Alexey Bataev9ff80832018-04-16 20:16:21 +0000919 llvm::Value *IsMaster =
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000920 Bld.CreateICmpEQ(getNVPTXThreadID(CGF), getMasterThreadID(CGF));
921 Bld.CreateCondBr(IsMaster, MasterBB, EST.ExitBB);
922
923 CGF.EmitBlock(MasterBB);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000924 IsInTargetMasterThreadRegion = true;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000925 // SEQUENTIAL (MASTER) REGION START
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000926 // First action in sequential region:
927 // Initialize the state of the OpenMP runtime library on the GPU.
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +0000928 // TODO: Optimize runtime initialization and pass in correct value.
929 llvm::Value *Args[] = {getThreadLimit(CGF),
930 Bld.getInt16(/*RequiresOMPRuntime=*/1)};
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000931 CGF.EmitRuntimeCall(
932 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_init), Args);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000933
934 // For data sharing, we need to initialize the stack.
935 CGF.EmitRuntimeCall(
936 createNVPTXRuntimeFunction(
937 OMPRTL_NVPTX__kmpc_data_sharing_init_stack));
938
Alexey Bataevc99042b2018-03-15 18:10:54 +0000939 emitGenericVarsProlog(CGF, WST.Loc);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000940}
941
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000942void CGOpenMPRuntimeNVPTX::emitNonSPMDEntryFooter(CodeGenFunction &CGF,
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000943 EntryFunctionState &EST) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000944 IsInTargetMasterThreadRegion = false;
Alexey Bataevc99042b2018-03-15 18:10:54 +0000945 if (!CGF.HaveInsertPoint())
946 return;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +0000947
Alexey Bataevd7ff6d62018-05-07 14:50:05 +0000948 emitGenericVarsEpilog(CGF);
949
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000950 if (!EST.ExitBB)
951 EST.ExitBB = CGF.createBasicBlock(".exit");
952
953 llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".termination.notifier");
954 CGF.EmitBranch(TerminateBB);
955
956 CGF.EmitBlock(TerminateBB);
957 // Signal termination condition.
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +0000958 // TODO: Optimize runtime initialization and pass in correct value.
959 llvm::Value *Args[] = {CGF.Builder.getInt16(/*IsOMPRuntimeInitialized=*/1)};
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000960 CGF.EmitRuntimeCall(
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +0000961 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_deinit), Args);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +0000962 // Barrier to terminate worker threads.
963 syncCTAThreads(CGF);
964 // Master thread jumps to exit point.
965 CGF.EmitBranch(EST.ExitBB);
966
967 CGF.EmitBlock(EST.ExitBB);
968 EST.ExitBB = nullptr;
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +0000969}
970
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000971void CGOpenMPRuntimeNVPTX::emitSPMDKernel(const OMPExecutableDirective &D,
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000972 StringRef ParentName,
973 llvm::Function *&OutlinedFn,
974 llvm::Constant *&OutlinedFnID,
975 bool IsOffloadEntry,
976 const RegionCodeGenTy &CodeGen) {
Alexey Bataevbf5c8482018-05-10 18:32:08 +0000977 ExecutionModeRAII ModeRAII(CurrentExecutionMode, /*IsSPMD=*/true);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000978 EntryFunctionState EST;
979
980 // Emit target region as a standalone region.
981 class NVPTXPrePostActionTy : public PrePostActionTy {
982 CGOpenMPRuntimeNVPTX &RT;
983 CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
984 const OMPExecutableDirective &D;
985
986 public:
987 NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX &RT,
988 CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
989 const OMPExecutableDirective &D)
990 : RT(RT), EST(EST), D(D) {}
991 void Enter(CodeGenFunction &CGF) override {
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000992 RT.emitSPMDEntryHeader(CGF, EST, D);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000993 }
994 void Exit(CodeGenFunction &CGF) override {
Alexey Bataev4065b9a2018-06-21 20:26:33 +0000995 RT.emitSPMDEntryFooter(CGF, EST);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +0000996 }
997 } Action(*this, EST, D);
998 CodeGen.setAction(Action);
999 emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
1000 IsOffloadEntry, CodeGen);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001001}
1002
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001003void CGOpenMPRuntimeNVPTX::emitSPMDEntryHeader(
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001004 CodeGenFunction &CGF, EntryFunctionState &EST,
1005 const OMPExecutableDirective &D) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00001006 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001007
1008 // Setup BBs in entry function.
1009 llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute");
1010 EST.ExitBB = CGF.createBasicBlock(".exit");
1011
1012 // Initialize the OMP state in the runtime; called by all active threads.
1013 // TODO: Set RequiresOMPRuntime and RequiresDataSharing parameters
1014 // based on code analysis of the target region.
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001015 llvm::Value *Args[] = {getThreadLimit(CGF, /*IsInSPMDExecutionMode=*/true),
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001016 /*RequiresOMPRuntime=*/Bld.getInt16(1),
1017 /*RequiresDataSharing=*/Bld.getInt16(1)};
1018 CGF.EmitRuntimeCall(
1019 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_init), Args);
Gheorghe-Teodor Berceaad4e5792018-07-13 16:18:24 +00001020
1021 // For data sharing, we need to initialize the stack.
1022 CGF.EmitRuntimeCall(
1023 createNVPTXRuntimeFunction(
1024 OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd));
1025
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001026 CGF.EmitBranch(ExecuteBB);
1027
1028 CGF.EmitBlock(ExecuteBB);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001029
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001030 IsInTargetMasterThreadRegion = true;
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001031}
1032
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001033void CGOpenMPRuntimeNVPTX::emitSPMDEntryFooter(CodeGenFunction &CGF,
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001034 EntryFunctionState &EST) {
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001035 IsInTargetMasterThreadRegion = false;
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001036 if (!CGF.HaveInsertPoint())
1037 return;
1038
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001039 if (!EST.ExitBB)
1040 EST.ExitBB = CGF.createBasicBlock(".exit");
1041
1042 llvm::BasicBlock *OMPDeInitBB = CGF.createBasicBlock(".omp.deinit");
1043 CGF.EmitBranch(OMPDeInitBB);
1044
1045 CGF.EmitBlock(OMPDeInitBB);
1046 // DeInitialize the OMP state in the runtime; called by all active threads.
1047 CGF.EmitRuntimeCall(
1048 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_deinit), None);
1049 CGF.EmitBranch(EST.ExitBB);
1050
1051 CGF.EmitBlock(EST.ExitBB);
1052 EST.ExitBB = nullptr;
1053}
1054
1055// Create a unique global variable to indicate the execution mode of this target
1056// region. The execution mode is either 'generic', or 'spmd' depending on the
1057// target directive. This variable is picked up by the offload library to setup
1058// the device appropriately before kernel launch. If the execution mode is
1059// 'generic', the runtime reserves one warp for the master, otherwise, all
1060// warps participate in parallel work.
1061static void setPropertyExecutionMode(CodeGenModule &CGM, StringRef Name,
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001062 bool Mode) {
1063 auto *GVMode =
1064 new llvm::GlobalVariable(CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
1065 llvm::GlobalValue::WeakAnyLinkage,
1066 llvm::ConstantInt::get(CGM.Int8Ty, Mode ? 0 : 1),
1067 Twine(Name, "_exec_mode"));
Alexey Bataev9ff80832018-04-16 20:16:21 +00001068 CGM.addCompilerUsedGlobal(GVMode);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001069}
1070
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001071void CGOpenMPRuntimeNVPTX::emitWorkerFunction(WorkerFunctionState &WST) {
Gheorghe-Teodor Berceaeb89b1d2017-11-21 15:54:54 +00001072 ASTContext &Ctx = CGM.getContext();
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001073
1074 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
Alexey Bataev9ff80832018-04-16 20:16:21 +00001075 CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, WST.WorkerFn, WST.CGFI, {},
Alexey Bataev7cae94e2018-01-04 19:45:16 +00001076 WST.Loc, WST.Loc);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001077 emitWorkerLoop(CGF, WST);
1078 CGF.FinishFunction();
1079}
1080
1081void CGOpenMPRuntimeNVPTX::emitWorkerLoop(CodeGenFunction &CGF,
1082 WorkerFunctionState &WST) {
1083 //
1084 // The workers enter this loop and wait for parallel work from the master.
1085 // When the master encounters a parallel region it sets up the work + variable
1086 // arguments, and wakes up the workers. The workers first check to see if
1087 // they are required for the parallel region, i.e., within the # of requested
1088 // parallel threads. The activated workers load the variable arguments and
1089 // execute the parallel work.
1090 //
1091
1092 CGBuilderTy &Bld = CGF.Builder;
1093
1094 llvm::BasicBlock *AwaitBB = CGF.createBasicBlock(".await.work");
1095 llvm::BasicBlock *SelectWorkersBB = CGF.createBasicBlock(".select.workers");
1096 llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute.parallel");
1097 llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".terminate.parallel");
1098 llvm::BasicBlock *BarrierBB = CGF.createBasicBlock(".barrier.parallel");
1099 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit");
1100
1101 CGF.EmitBranch(AwaitBB);
1102
1103 // Workers wait for work from master.
1104 CGF.EmitBlock(AwaitBB);
1105 // Wait for parallel work
1106 syncCTAThreads(CGF);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +00001107
1108 Address WorkFn =
1109 CGF.CreateDefaultAlignTempAlloca(CGF.Int8PtrTy, /*Name=*/"work_fn");
1110 Address ExecStatus =
1111 CGF.CreateDefaultAlignTempAlloca(CGF.Int8Ty, /*Name=*/"exec_status");
1112 CGF.InitTempAlloca(ExecStatus, Bld.getInt8(/*C=*/0));
1113 CGF.InitTempAlloca(WorkFn, llvm::Constant::getNullValue(CGF.Int8PtrTy));
1114
Jonas Hahnfeldfa059ba2017-12-27 10:39:56 +00001115 // TODO: Optimize runtime initialization and pass in correct value.
Gheorghe-Teodor Bercea7d80da12018-03-07 21:59:50 +00001116 llvm::Value *Args[] = {WorkFn.getPointer(),
Jonas Hahnfeldfa059ba2017-12-27 10:39:56 +00001117 /*RequiresOMPRuntime=*/Bld.getInt16(1)};
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001118 llvm::Value *Ret = CGF.EmitRuntimeCall(
1119 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_parallel), Args);
1120 Bld.CreateStore(Bld.CreateZExt(Ret, CGF.Int8Ty), ExecStatus);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +00001121
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001122 // On termination condition (workid == 0), exit loop.
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001123 llvm::Value *WorkID = Bld.CreateLoad(WorkFn);
1124 llvm::Value *ShouldTerminate = Bld.CreateIsNull(WorkID, "should_terminate");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001125 Bld.CreateCondBr(ShouldTerminate, ExitBB, SelectWorkersBB);
1126
1127 // Activate requested workers.
1128 CGF.EmitBlock(SelectWorkersBB);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +00001129 llvm::Value *IsActive =
1130 Bld.CreateIsNotNull(Bld.CreateLoad(ExecStatus), "is_active");
1131 Bld.CreateCondBr(IsActive, ExecuteBB, BarrierBB);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001132
1133 // Signal start of parallel region.
1134 CGF.EmitBlock(ExecuteBB);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001135
1136 // Process work items: outlined parallel functions.
Alexey Bataev9ff80832018-04-16 20:16:21 +00001137 for (llvm::Function *W : Work) {
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001138 // Try to match this outlined function.
Alexey Bataev9ff80832018-04-16 20:16:21 +00001139 llvm::Value *ID = Bld.CreatePointerBitCastOrAddrSpaceCast(W, CGM.Int8PtrTy);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001140
1141 llvm::Value *WorkFnMatch =
1142 Bld.CreateICmpEQ(Bld.CreateLoad(WorkFn), ID, "work_match");
1143
1144 llvm::BasicBlock *ExecuteFNBB = CGF.createBasicBlock(".execute.fn");
1145 llvm::BasicBlock *CheckNextBB = CGF.createBasicBlock(".check.next");
1146 Bld.CreateCondBr(WorkFnMatch, ExecuteFNBB, CheckNextBB);
1147
1148 // Execute this outlined function.
1149 CGF.EmitBlock(ExecuteFNBB);
1150
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001151 // Insert call to work function via shared wrapper. The shared
1152 // wrapper takes two arguments:
1153 // - the parallelism level;
Alexey Bataevb7f3cba2018-03-19 17:04:07 +00001154 // - the thread ID;
1155 emitCall(CGF, WST.Loc, W,
1156 {Bld.getInt16(/*ParallelLevel=*/0), getThreadID(CGF, WST.Loc)});
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001157
1158 // Go to end of parallel region.
1159 CGF.EmitBranch(TerminateBB);
1160
1161 CGF.EmitBlock(CheckNextBB);
1162 }
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001163 // Default case: call to outlined function through pointer if the target
1164 // region makes a declare target call that may contain an orphaned parallel
1165 // directive.
1166 auto *ParallelFnTy =
1167 llvm::FunctionType::get(CGM.VoidTy, {CGM.Int16Ty, CGM.Int32Ty},
1168 /*isVarArg=*/false)
1169 ->getPointerTo();
1170 llvm::Value *WorkFnCast = Bld.CreateBitCast(WorkID, ParallelFnTy);
1171 // Insert call to work function via shared wrapper. The shared
1172 // wrapper takes two arguments:
1173 // - the parallelism level;
1174 // - the thread ID;
1175 emitCall(CGF, WST.Loc, WorkFnCast,
1176 {Bld.getInt16(/*ParallelLevel=*/0), getThreadID(CGF, WST.Loc)});
1177 // Go to end of parallel region.
1178 CGF.EmitBranch(TerminateBB);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001179
1180 // Signal end of parallel region.
1181 CGF.EmitBlock(TerminateBB);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001182 CGF.EmitRuntimeCall(
1183 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_end_parallel),
1184 llvm::None);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001185 CGF.EmitBranch(BarrierBB);
1186
1187 // All active and inactive workers wait at a barrier after parallel region.
1188 CGF.EmitBlock(BarrierBB);
1189 // Barrier after parallel region.
1190 syncCTAThreads(CGF);
1191 CGF.EmitBranch(AwaitBB);
1192
1193 // Exit target region.
1194 CGF.EmitBlock(ExitBB);
1195}
1196
Adrian Prantl9fc8faf2018-05-09 01:00:01 +00001197/// Returns specified OpenMP runtime function for the current OpenMP
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001198/// implementation. Specialized for the NVPTX device.
1199/// \param Function OpenMP runtime function.
1200/// \return Specified function.
1201llvm::Constant *
1202CGOpenMPRuntimeNVPTX::createNVPTXRuntimeFunction(unsigned Function) {
1203 llvm::Constant *RTLFn = nullptr;
1204 switch (static_cast<OpenMPRTLFunctionNVPTX>(Function)) {
1205 case OMPRTL_NVPTX__kmpc_kernel_init: {
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +00001206 // Build void __kmpc_kernel_init(kmp_int32 thread_limit, int16_t
1207 // RequiresOMPRuntime);
1208 llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001209 auto *FnTy =
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001210 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1211 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_init");
1212 break;
1213 }
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +00001214 case OMPRTL_NVPTX__kmpc_kernel_deinit: {
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +00001215 // Build void __kmpc_kernel_deinit(int16_t IsOMPRuntimeInitialized);
1216 llvm::Type *TypeParams[] = {CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001217 auto *FnTy =
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +00001218 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
Arpith Chacko Jacob406acdb2017-01-05 15:24:05 +00001219 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_deinit");
1220 break;
1221 }
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001222 case OMPRTL_NVPTX__kmpc_spmd_kernel_init: {
1223 // Build void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
Jonas Hahnfeld891c7fb2017-11-22 14:46:49 +00001224 // int16_t RequiresOMPRuntime, int16_t RequiresDataSharing);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001225 llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001226 auto *FnTy =
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001227 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1228 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_init");
1229 break;
1230 }
1231 case OMPRTL_NVPTX__kmpc_spmd_kernel_deinit: {
1232 // Build void __kmpc_spmd_kernel_deinit();
Alexey Bataev9ff80832018-04-16 20:16:21 +00001233 auto *FnTy =
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001234 llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
1235 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_deinit");
1236 break;
1237 }
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001238 case OMPRTL_NVPTX__kmpc_kernel_prepare_parallel: {
1239 /// Build void __kmpc_kernel_prepare_parallel(
Gheorghe-Teodor Bercea7d80da12018-03-07 21:59:50 +00001240 /// void *outlined_function, int16_t IsOMPRuntimeInitialized);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001241 llvm::Type *TypeParams[] = {CGM.Int8PtrTy, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001242 auto *FnTy =
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001243 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1244 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_prepare_parallel");
1245 break;
1246 }
1247 case OMPRTL_NVPTX__kmpc_kernel_parallel: {
Gheorghe-Teodor Bercea7d80da12018-03-07 21:59:50 +00001248 /// Build bool __kmpc_kernel_parallel(void **outlined_function,
1249 /// int16_t IsOMPRuntimeInitialized);
1250 llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy, CGM.Int16Ty};
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001251 llvm::Type *RetTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
Alexey Bataev9ff80832018-04-16 20:16:21 +00001252 auto *FnTy =
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001253 llvm::FunctionType::get(RetTy, TypeParams, /*isVarArg*/ false);
1254 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_parallel");
1255 break;
1256 }
1257 case OMPRTL_NVPTX__kmpc_kernel_end_parallel: {
1258 /// Build void __kmpc_kernel_end_parallel();
Alexey Bataev9ff80832018-04-16 20:16:21 +00001259 auto *FnTy =
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001260 llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
1261 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_end_parallel");
1262 break;
1263 }
1264 case OMPRTL_NVPTX__kmpc_serialized_parallel: {
1265 // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
1266 // global_tid);
1267 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001268 auto *FnTy =
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001269 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1270 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
1271 break;
1272 }
1273 case OMPRTL_NVPTX__kmpc_end_serialized_parallel: {
1274 // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
1275 // global_tid);
1276 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001277 auto *FnTy =
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001278 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1279 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
1280 break;
1281 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001282 case OMPRTL_NVPTX__kmpc_shuffle_int32: {
1283 // Build int32_t __kmpc_shuffle_int32(int32_t element,
1284 // int16_t lane_offset, int16_t warp_size);
1285 llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001286 auto *FnTy =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001287 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
1288 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int32");
1289 break;
1290 }
1291 case OMPRTL_NVPTX__kmpc_shuffle_int64: {
1292 // Build int64_t __kmpc_shuffle_int64(int64_t element,
1293 // int16_t lane_offset, int16_t warp_size);
1294 llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int16Ty, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001295 auto *FnTy =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001296 llvm::FunctionType::get(CGM.Int64Ty, TypeParams, /*isVarArg*/ false);
1297 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int64");
1298 break;
1299 }
1300 case OMPRTL_NVPTX__kmpc_parallel_reduce_nowait: {
1301 // Build int32_t kmpc_nvptx_parallel_reduce_nowait(kmp_int32 global_tid,
1302 // kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
1303 // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
1304 // lane_offset, int16_t Algorithm Version),
1305 // void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num));
1306 llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
1307 CGM.Int16Ty, CGM.Int16Ty};
1308 auto *ShuffleReduceFnTy =
1309 llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
1310 /*isVarArg=*/false);
1311 llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
1312 auto *InterWarpCopyFnTy =
1313 llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
1314 /*isVarArg=*/false);
1315 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1316 CGM.Int32Ty,
1317 CGM.SizeTy,
1318 CGM.VoidPtrTy,
1319 ShuffleReduceFnTy->getPointerTo(),
1320 InterWarpCopyFnTy->getPointerTo()};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001321 auto *FnTy =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001322 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1323 RTLFn = CGM.CreateRuntimeFunction(
1324 FnTy, /*Name=*/"__kmpc_nvptx_parallel_reduce_nowait");
1325 break;
1326 }
Alexey Bataevfac26cf2018-05-02 20:03:27 +00001327 case OMPRTL_NVPTX__kmpc_simd_reduce_nowait: {
1328 // Build int32_t kmpc_nvptx_simd_reduce_nowait(kmp_int32 global_tid,
1329 // kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
1330 // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
1331 // lane_offset, int16_t Algorithm Version),
1332 // void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num));
1333 llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
1334 CGM.Int16Ty, CGM.Int16Ty};
1335 auto *ShuffleReduceFnTy =
1336 llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
1337 /*isVarArg=*/false);
1338 llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
1339 auto *InterWarpCopyFnTy =
1340 llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
1341 /*isVarArg=*/false);
1342 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1343 CGM.Int32Ty,
1344 CGM.SizeTy,
1345 CGM.VoidPtrTy,
1346 ShuffleReduceFnTy->getPointerTo(),
1347 InterWarpCopyFnTy->getPointerTo()};
1348 auto *FnTy =
1349 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1350 RTLFn = CGM.CreateRuntimeFunction(
1351 FnTy, /*Name=*/"__kmpc_nvptx_simd_reduce_nowait");
1352 break;
1353 }
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00001354 case OMPRTL_NVPTX__kmpc_teams_reduce_nowait: {
1355 // Build int32_t __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
1356 // int32_t num_vars, size_t reduce_size, void *reduce_data,
1357 // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
1358 // lane_offset, int16_t shortCircuit),
1359 // void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
1360 // void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
1361 // int32_t index, int32_t width),
1362 // void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad,
1363 // int32_t index, int32_t width, int32_t reduce))
1364 llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
1365 CGM.Int16Ty, CGM.Int16Ty};
1366 auto *ShuffleReduceFnTy =
1367 llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
1368 /*isVarArg=*/false);
1369 llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
1370 auto *InterWarpCopyFnTy =
1371 llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
1372 /*isVarArg=*/false);
1373 llvm::Type *CopyToScratchpadTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy,
1374 CGM.Int32Ty, CGM.Int32Ty};
1375 auto *CopyToScratchpadFnTy =
1376 llvm::FunctionType::get(CGM.VoidTy, CopyToScratchpadTypeParams,
1377 /*isVarArg=*/false);
1378 llvm::Type *LoadReduceTypeParams[] = {
1379 CGM.VoidPtrTy, CGM.VoidPtrTy, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty};
1380 auto *LoadReduceFnTy =
1381 llvm::FunctionType::get(CGM.VoidTy, LoadReduceTypeParams,
1382 /*isVarArg=*/false);
1383 llvm::Type *TypeParams[] = {CGM.Int32Ty,
1384 CGM.Int32Ty,
1385 CGM.SizeTy,
1386 CGM.VoidPtrTy,
1387 ShuffleReduceFnTy->getPointerTo(),
1388 InterWarpCopyFnTy->getPointerTo(),
1389 CopyToScratchpadFnTy->getPointerTo(),
1390 LoadReduceFnTy->getPointerTo()};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001391 auto *FnTy =
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00001392 llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
1393 RTLFn = CGM.CreateRuntimeFunction(
1394 FnTy, /*Name=*/"__kmpc_nvptx_teams_reduce_nowait");
1395 break;
1396 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001397 case OMPRTL_NVPTX__kmpc_end_reduce_nowait: {
1398 // Build __kmpc_end_reduce_nowait(kmp_int32 global_tid);
1399 llvm::Type *TypeParams[] = {CGM.Int32Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001400 auto *FnTy =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00001401 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1402 RTLFn = CGM.CreateRuntimeFunction(
1403 FnTy, /*Name=*/"__kmpc_nvptx_end_reduce_nowait");
1404 break;
1405 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001406 case OMPRTL_NVPTX__kmpc_data_sharing_init_stack: {
1407 /// Build void __kmpc_data_sharing_init_stack();
Alexey Bataev9ff80832018-04-16 20:16:21 +00001408 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001409 llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
1410 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_data_sharing_init_stack");
1411 break;
1412 }
Gheorghe-Teodor Berceaad4e5792018-07-13 16:18:24 +00001413 case OMPRTL_NVPTX__kmpc_data_sharing_init_stack_spmd: {
1414 /// Build void __kmpc_data_sharing_init_stack_spmd();
1415 auto *FnTy =
1416 llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
1417 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_data_sharing_init_stack_spmd");
1418 break;
1419 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001420 case OMPRTL_NVPTX__kmpc_data_sharing_push_stack: {
1421 // Build void *__kmpc_data_sharing_push_stack(size_t size,
1422 // int16_t UseSharedMemory);
1423 llvm::Type *TypeParams[] = {CGM.SizeTy, CGM.Int16Ty};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001424 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001425 llvm::FunctionType::get(CGM.VoidPtrTy, TypeParams, /*isVarArg=*/false);
1426 RTLFn = CGM.CreateRuntimeFunction(
1427 FnTy, /*Name=*/"__kmpc_data_sharing_push_stack");
1428 break;
1429 }
1430 case OMPRTL_NVPTX__kmpc_data_sharing_pop_stack: {
1431 // Build void __kmpc_data_sharing_pop_stack(void *a);
1432 llvm::Type *TypeParams[] = {CGM.VoidPtrTy};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001433 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001434 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
1435 RTLFn = CGM.CreateRuntimeFunction(FnTy,
1436 /*Name=*/"__kmpc_data_sharing_pop_stack");
1437 break;
1438 }
1439 case OMPRTL_NVPTX__kmpc_begin_sharing_variables: {
1440 /// Build void __kmpc_begin_sharing_variables(void ***args,
1441 /// size_t n_args);
1442 llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo(), CGM.SizeTy};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001443 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001444 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1445 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_begin_sharing_variables");
1446 break;
1447 }
1448 case OMPRTL_NVPTX__kmpc_end_sharing_variables: {
1449 /// Build void __kmpc_end_sharing_variables();
Alexey Bataev9ff80832018-04-16 20:16:21 +00001450 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001451 llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
1452 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_sharing_variables");
1453 break;
1454 }
1455 case OMPRTL_NVPTX__kmpc_get_shared_variables: {
1456 /// Build void __kmpc_get_shared_variables(void ***GlobalArgs);
1457 llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy->getPointerTo()};
Alexey Bataev9ff80832018-04-16 20:16:21 +00001458 auto *FnTy =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001459 llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
1460 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_get_shared_variables");
1461 break;
1462 }
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001463 case OMPRTL_NVPTX__kmpc_parallel_level: {
1464 // Build uint16_t __kmpc_parallel_level(ident_t *loc, kmp_int32 global_tid);
1465 llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
1466 auto *FnTy =
1467 llvm::FunctionType::get(CGM.Int16Ty, TypeParams, /*isVarArg*/ false);
1468 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_parallel_level");
1469 break;
1470 }
Alexey Bataev673110d2018-05-16 13:36:30 +00001471 case OMPRTL_NVPTX__kmpc_is_spmd_exec_mode: {
1472 // Build int8_t __kmpc_is_spmd_exec_mode();
1473 auto *FnTy = llvm::FunctionType::get(CGM.Int8Ty, /*isVarArg=*/false);
1474 RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_is_spmd_exec_mode");
1475 break;
1476 }
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001477 }
1478 return RTLFn;
1479}
1480
1481void CGOpenMPRuntimeNVPTX::createOffloadEntry(llvm::Constant *ID,
1482 llvm::Constant *Addr,
Alexey Bataev03f270c2018-03-30 18:31:07 +00001483 uint64_t Size, int32_t,
1484 llvm::GlobalValue::LinkageTypes) {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001485 // TODO: Add support for global variables on the device after declare target
1486 // support.
Alexey Bataev9ff80832018-04-16 20:16:21 +00001487 if (!isa<llvm::Function>(Addr))
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001488 return;
Alexey Bataev9ff80832018-04-16 20:16:21 +00001489 llvm::Module &M = CGM.getModule();
1490 llvm::LLVMContext &Ctx = CGM.getLLVMContext();
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001491
1492 // Get "nvvm.annotations" metadata node
Alexey Bataev9ff80832018-04-16 20:16:21 +00001493 llvm::NamedMDNode *MD = M.getOrInsertNamedMetadata("nvvm.annotations");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001494
1495 llvm::Metadata *MDVals[] = {
Alexey Bataev9ff80832018-04-16 20:16:21 +00001496 llvm::ConstantAsMetadata::get(Addr), llvm::MDString::get(Ctx, "kernel"),
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001497 llvm::ConstantAsMetadata::get(
1498 llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1))};
1499 // Append metadata to nvvm.annotations
1500 MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
1501}
1502
1503void CGOpenMPRuntimeNVPTX::emitTargetOutlinedFunction(
1504 const OMPExecutableDirective &D, StringRef ParentName,
1505 llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
Alexey Bataev14fa1c62016-03-29 05:34:15 +00001506 bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001507 if (!IsOffloadEntry) // Nothing to do.
1508 return;
1509
1510 assert(!ParentName.empty() && "Invalid target region parent name!");
1511
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001512 bool Mode = supportsSPMDExecutionMode(CGM.getContext(), D);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001513 if (Mode)
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001514 emitSPMDKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001515 CodeGen);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001516 else
1517 emitNonSPMDKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
1518 CodeGen);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001519
1520 setPropertyExecutionMode(CGM, OutlinedFn->getName(), Mode);
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001521}
1522
Samuel Antao45bfe4c2016-02-08 15:59:20 +00001523CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001524 : CGOpenMPRuntime(CGM, "_", "$") {
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001525 if (!CGM.getLangOpts().OpenMPIsDevice)
1526 llvm_unreachable("OpenMP NVPTX can only handle device code.");
Arpith Chacko Jacob5c309e42016-03-22 01:48:56 +00001527}
Carlo Bertollic6872252016-04-04 15:55:02 +00001528
Arpith Chacko Jacob2cd6eea2017-01-25 16:55:10 +00001529void CGOpenMPRuntimeNVPTX::emitProcBindClause(CodeGenFunction &CGF,
1530 OpenMPProcBindClauseKind ProcBind,
1531 SourceLocation Loc) {
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001532 // Do nothing in case of SPMD mode and L0 parallel.
Alexey Bataev2a3320a2018-05-15 18:01:01 +00001533 if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD)
Arpith Chacko Jacob2cd6eea2017-01-25 16:55:10 +00001534 return;
1535
1536 CGOpenMPRuntime::emitProcBindClause(CGF, ProcBind, Loc);
1537}
1538
Arpith Chacko Jacobe04da5d2017-01-25 01:18:34 +00001539void CGOpenMPRuntimeNVPTX::emitNumThreadsClause(CodeGenFunction &CGF,
1540 llvm::Value *NumThreads,
1541 SourceLocation Loc) {
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001542 // Do nothing in case of SPMD mode and L0 parallel.
Alexey Bataev2a3320a2018-05-15 18:01:01 +00001543 if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD)
Arpith Chacko Jacobe04da5d2017-01-25 01:18:34 +00001544 return;
1545
1546 CGOpenMPRuntime::emitNumThreadsClause(CGF, NumThreads, Loc);
1547}
1548
Carlo Bertollic6872252016-04-04 15:55:02 +00001549void CGOpenMPRuntimeNVPTX::emitNumTeamsClause(CodeGenFunction &CGF,
1550 const Expr *NumTeams,
1551 const Expr *ThreadLimit,
1552 SourceLocation Loc) {}
1553
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001554llvm::Value *CGOpenMPRuntimeNVPTX::emitParallelOutlinedFunction(
1555 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1556 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
Alexey Bataevc99042b2018-03-15 18:10:54 +00001557 // Emit target region as a standalone region.
1558 class NVPTXPrePostActionTy : public PrePostActionTy {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001559 bool &IsInParallelRegion;
1560 bool PrevIsInParallelRegion;
Alexey Bataevc99042b2018-03-15 18:10:54 +00001561
1562 public:
Alexey Bataevb99dcb52018-07-09 17:43:58 +00001563 NVPTXPrePostActionTy(bool &IsInParallelRegion)
1564 : IsInParallelRegion(IsInParallelRegion) {}
Alexey Bataevc99042b2018-03-15 18:10:54 +00001565 void Enter(CodeGenFunction &CGF) override {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001566 PrevIsInParallelRegion = IsInParallelRegion;
1567 IsInParallelRegion = true;
Alexey Bataevc99042b2018-03-15 18:10:54 +00001568 }
1569 void Exit(CodeGenFunction &CGF) override {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001570 IsInParallelRegion = PrevIsInParallelRegion;
Alexey Bataevc99042b2018-03-15 18:10:54 +00001571 }
Alexey Bataevb99dcb52018-07-09 17:43:58 +00001572 } Action(IsInParallelRegion);
Alexey Bataevc99042b2018-03-15 18:10:54 +00001573 CodeGen.setAction(Action);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001574 bool PrevIsInTargetMasterThreadRegion = IsInTargetMasterThreadRegion;
1575 IsInTargetMasterThreadRegion = false;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001576 auto *OutlinedFun =
1577 cast<llvm::Function>(CGOpenMPRuntime::emitParallelOutlinedFunction(
1578 D, ThreadIDVar, InnermostKind, CodeGen));
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001579 IsInTargetMasterThreadRegion = PrevIsInTargetMasterThreadRegion;
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001580 if (getExecutionMode() != CGOpenMPRuntimeNVPTX::EM_SPMD &&
1581 !IsInParallelRegion) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001582 llvm::Function *WrapperFun =
1583 createParallelDataSharingWrapper(OutlinedFun, D);
1584 WrapperFunctionsMap[OutlinedFun] = WrapperFun;
1585 }
1586
1587 return OutlinedFun;
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001588}
1589
1590llvm::Value *CGOpenMPRuntimeNVPTX::emitTeamsOutlinedFunction(
Carlo Bertollic6872252016-04-04 15:55:02 +00001591 const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
1592 OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00001593 SourceLocation Loc = D.getBeginLoc();
Carlo Bertollic6872252016-04-04 15:55:02 +00001594
Alexey Bataevc99042b2018-03-15 18:10:54 +00001595 // Emit target region as a standalone region.
1596 class NVPTXPrePostActionTy : public PrePostActionTy {
1597 SourceLocation &Loc;
1598
1599 public:
1600 NVPTXPrePostActionTy(SourceLocation &Loc) : Loc(Loc) {}
1601 void Enter(CodeGenFunction &CGF) override {
1602 static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
1603 .emitGenericVarsProlog(CGF, Loc);
1604 }
1605 void Exit(CodeGenFunction &CGF) override {
1606 static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
1607 .emitGenericVarsEpilog(CGF);
1608 }
1609 } Action(Loc);
1610 CodeGen.setAction(Action);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001611 llvm::Value *OutlinedFunVal = CGOpenMPRuntime::emitTeamsOutlinedFunction(
1612 D, ThreadIDVar, InnermostKind, CodeGen);
1613 llvm::Function *OutlinedFun = cast<llvm::Function>(OutlinedFunVal);
1614 OutlinedFun->removeFnAttr(llvm::Attribute::NoInline);
Mehdi Amini6aa9e9b2017-05-29 05:38:20 +00001615 OutlinedFun->removeFnAttr(llvm::Attribute::OptimizeNone);
Arpith Chacko Jacob19b911c2017-01-18 18:18:53 +00001616 OutlinedFun->addFnAttr(llvm::Attribute::AlwaysInline);
Carlo Bertollic6872252016-04-04 15:55:02 +00001617
1618 return OutlinedFun;
1619}
1620
Alexey Bataevc99042b2018-03-15 18:10:54 +00001621void CGOpenMPRuntimeNVPTX::emitGenericVarsProlog(CodeGenFunction &CGF,
1622 SourceLocation Loc) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001623 if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic)
1624 return;
1625
Alexey Bataevc99042b2018-03-15 18:10:54 +00001626 CGBuilderTy &Bld = CGF.Builder;
1627
1628 const auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
1629 if (I == FunctionGlobalizedDecls.end())
1630 return;
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001631 if (const RecordDecl *GlobalizedVarsRecord = I->getSecond().GlobalRecord) {
1632 QualType RecTy = CGM.getContext().getRecordType(GlobalizedVarsRecord);
Alexey Bataevc99042b2018-03-15 18:10:54 +00001633
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001634 // Recover pointer to this function's global record. The runtime will
1635 // handle the specifics of the allocation of the memory.
1636 // Use actual memory size of the record including the padding
1637 // for alignment purposes.
1638 unsigned Alignment =
1639 CGM.getContext().getTypeAlignInChars(RecTy).getQuantity();
1640 unsigned GlobalRecordSize =
1641 CGM.getContext().getTypeSizeInChars(RecTy).getQuantity();
1642 GlobalRecordSize = llvm::alignTo(GlobalRecordSize, Alignment);
1643 // TODO: allow the usage of shared memory to be controlled by
1644 // the user, for now, default to global.
1645 llvm::Value *GlobalRecordSizeArg[] = {
1646 llvm::ConstantInt::get(CGM.SizeTy, GlobalRecordSize),
1647 CGF.Builder.getInt16(/*UseSharedMemory=*/0)};
1648 llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall(
1649 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_push_stack),
1650 GlobalRecordSizeArg);
1651 llvm::Value *GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1652 GlobalRecValue, CGF.ConvertTypeForMem(RecTy)->getPointerTo());
1653 LValue Base =
1654 CGF.MakeNaturalAlignPointeeAddrLValue(GlobalRecCastAddr, RecTy);
1655 I->getSecond().GlobalRecordAddr = GlobalRecValue;
Alexey Bataevc99042b2018-03-15 18:10:54 +00001656
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001657 // Emit the "global alloca" which is a GEP from the global declaration
1658 // record using the pointer returned by the runtime.
1659 for (auto &Rec : I->getSecond().LocalVarData) {
1660 bool EscapedParam = I->getSecond().EscapedParameters.count(Rec.first);
1661 llvm::Value *ParValue;
1662 if (EscapedParam) {
1663 const auto *VD = cast<VarDecl>(Rec.first);
1664 LValue ParLVal =
1665 CGF.MakeAddrLValue(CGF.GetAddrOfLocalVar(VD), VD->getType());
1666 ParValue = CGF.EmitLoadOfScalar(ParLVal, Loc);
1667 }
1668 const FieldDecl *FD = Rec.second.first;
1669 LValue VarAddr = CGF.EmitLValueForField(Base, FD);
1670 Rec.second.second = VarAddr.getAddress();
1671 if (EscapedParam) {
1672 const auto *VD = cast<VarDecl>(Rec.first);
1673 CGF.EmitStoreOfScalar(ParValue, VarAddr);
1674 I->getSecond().MappedParams->setVarAddr(CGF, VD, VarAddr.getAddress());
1675 }
Alexey Bataevc99042b2018-03-15 18:10:54 +00001676 }
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001677 }
1678 for (const ValueDecl *VD : I->getSecond().EscapedVariableLengthDecls) {
1679 // Recover pointer to this function's global record. The runtime will
1680 // handle the specifics of the allocation of the memory.
1681 // Use actual memory size of the record including the padding
1682 // for alignment purposes.
Alexey Bataev9ff80832018-04-16 20:16:21 +00001683 CGBuilderTy &Bld = CGF.Builder;
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001684 llvm::Value *Size = CGF.getTypeSize(VD->getType());
1685 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1686 Size = Bld.CreateNUWAdd(
1687 Size, llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity() - 1));
1688 llvm::Value *AlignVal =
1689 llvm::ConstantInt::get(CGF.SizeTy, Align.getQuantity());
1690 Size = Bld.CreateUDiv(Size, AlignVal);
1691 Size = Bld.CreateNUWMul(Size, AlignVal);
1692 // TODO: allow the usage of shared memory to be controlled by
1693 // the user, for now, default to global.
1694 llvm::Value *GlobalRecordSizeArg[] = {
1695 Size, CGF.Builder.getInt16(/*UseSharedMemory=*/0)};
1696 llvm::Value *GlobalRecValue = CGF.EmitRuntimeCall(
1697 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_push_stack),
1698 GlobalRecordSizeArg);
1699 llvm::Value *GlobalRecCastAddr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1700 GlobalRecValue, CGF.ConvertTypeForMem(VD->getType())->getPointerTo());
1701 LValue Base = CGF.MakeAddrLValue(GlobalRecCastAddr, VD->getType(),
1702 CGM.getContext().getDeclAlign(VD),
1703 AlignmentSource::Decl);
1704 I->getSecond().MappedParams->setVarAddr(CGF, cast<VarDecl>(VD),
1705 Base.getAddress());
1706 I->getSecond().EscapedVariableLengthDeclsAddrs.emplace_back(GlobalRecValue);
Alexey Bataevc99042b2018-03-15 18:10:54 +00001707 }
1708 I->getSecond().MappedParams->apply(CGF);
1709}
1710
1711void CGOpenMPRuntimeNVPTX::emitGenericVarsEpilog(CodeGenFunction &CGF) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001712 if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic)
1713 return;
1714
Alexey Bataevc99042b2018-03-15 18:10:54 +00001715 const auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001716 if (I != FunctionGlobalizedDecls.end()) {
Alexey Bataevc99042b2018-03-15 18:10:54 +00001717 I->getSecond().MappedParams->restore(CGF);
1718 if (!CGF.HaveInsertPoint())
1719 return;
Alexey Bataev63cc8e92018-03-20 14:45:59 +00001720 for (llvm::Value *Addr :
1721 llvm::reverse(I->getSecond().EscapedVariableLengthDeclsAddrs)) {
1722 CGF.EmitRuntimeCall(
1723 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_pop_stack),
1724 Addr);
1725 }
1726 if (I->getSecond().GlobalRecordAddr) {
1727 CGF.EmitRuntimeCall(
1728 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_data_sharing_pop_stack),
1729 I->getSecond().GlobalRecordAddr);
1730 }
Alexey Bataevc99042b2018-03-15 18:10:54 +00001731 }
1732}
1733
Carlo Bertollic6872252016-04-04 15:55:02 +00001734void CGOpenMPRuntimeNVPTX::emitTeamsCall(CodeGenFunction &CGF,
1735 const OMPExecutableDirective &D,
1736 SourceLocation Loc,
1737 llvm::Value *OutlinedFn,
1738 ArrayRef<llvm::Value *> CapturedVars) {
1739 if (!CGF.HaveInsertPoint())
1740 return;
1741
Alexey Bataevb7f3cba2018-03-19 17:04:07 +00001742 Address ZeroAddr = CGF.CreateMemTemp(
1743 CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
1744 /*Name*/ ".zero.addr");
Carlo Bertollic6872252016-04-04 15:55:02 +00001745 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
1746 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
Alexey Bataevb7f3cba2018-03-19 17:04:07 +00001747 OutlinedFnArgs.push_back(emitThreadIDAddress(CGF, Loc).getPointer());
Carlo Bertollic6872252016-04-04 15:55:02 +00001748 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
1749 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
Alexey Bataev3c595a62017-08-14 15:01:03 +00001750 emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
Carlo Bertollic6872252016-04-04 15:55:02 +00001751}
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001752
1753void CGOpenMPRuntimeNVPTX::emitParallelCall(
1754 CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
1755 ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
1756 if (!CGF.HaveInsertPoint())
1757 return;
1758
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001759 if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_SPMD)
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001760 emitSPMDParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001761 else
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001762 emitNonSPMDParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001763}
1764
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001765void CGOpenMPRuntimeNVPTX::emitNonSPMDParallelCall(
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001766 CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
1767 ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
1768 llvm::Function *Fn = cast<llvm::Function>(OutlinedFn);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001769
1770 // Force inline this outlined function at its call site.
1771 Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
1772
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001773 Address ZeroAddr = CGF.CreateMemTemp(CGF.getContext().getIntTypeForBitwidth(
1774 /*DestWidth=*/32, /*Signed=*/1),
1775 ".zero.addr");
1776 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Alexey Bataev8521ff62018-07-25 20:03:01 +00001777 // ThreadId for serialized parallels is 0.
1778 Address ThreadIDAddr = ZeroAddr;
1779 auto &&CodeGen = [this, Fn, CapturedVars, Loc, ZeroAddr, &ThreadIDAddr](
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001780 CodeGenFunction &CGF, PrePostActionTy &Action) {
1781 Action.Enter(CGF);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001782
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001783 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
1784 OutlinedFnArgs.push_back(ThreadIDAddr.getPointer());
1785 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
1786 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
1787 emitOutlinedFunctionCall(CGF, Loc, Fn, OutlinedFnArgs);
1788 };
1789 auto &&SeqGen = [this, &CodeGen, Loc](CodeGenFunction &CGF,
1790 PrePostActionTy &) {
1791
1792 RegionCodeGenTy RCG(CodeGen);
1793 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
1794 llvm::Value *ThreadID = getThreadID(CGF, Loc);
1795 llvm::Value *Args[] = {RTLoc, ThreadID};
1796
1797 NVPTXActionTy Action(
1798 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel),
1799 Args,
1800 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel),
1801 Args);
1802 RCG.setAction(Action);
1803 RCG(CGF);
1804 };
1805
1806 auto &&L0ParallelGen = [this, CapturedVars, Fn](CodeGenFunction &CGF,
1807 PrePostActionTy &Action) {
1808 CGBuilderTy &Bld = CGF.Builder;
1809 llvm::Function *WFn = WrapperFunctionsMap[Fn];
1810 assert(WFn && "Wrapper function does not exist!");
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001811 llvm::Value *ID = Bld.CreateBitOrPointerCast(WFn, CGM.Int8PtrTy);
1812
1813 // Prepare for parallel region. Indicate the outlined function.
1814 llvm::Value *Args[] = {ID, /*RequiresOMPRuntime=*/Bld.getInt16(1)};
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001815 CGF.EmitRuntimeCall(
1816 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_prepare_parallel),
1817 Args);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001818
1819 // Create a private scope that will globalize the arguments
1820 // passed from the outside of the target region.
1821 CodeGenFunction::OMPPrivateScope PrivateArgScope(CGF);
1822
1823 // There's somehting to share.
1824 if (!CapturedVars.empty()) {
1825 // Prepare for parallel region. Indicate the outlined function.
1826 Address SharedArgs =
1827 CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "shared_arg_refs");
1828 llvm::Value *SharedArgsPtr = SharedArgs.getPointer();
1829
1830 llvm::Value *DataSharingArgs[] = {
1831 SharedArgsPtr,
1832 llvm::ConstantInt::get(CGM.SizeTy, CapturedVars.size())};
1833 CGF.EmitRuntimeCall(createNVPTXRuntimeFunction(
1834 OMPRTL_NVPTX__kmpc_begin_sharing_variables),
1835 DataSharingArgs);
1836
1837 // Store variable address in a list of references to pass to workers.
1838 unsigned Idx = 0;
1839 ASTContext &Ctx = CGF.getContext();
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001840 Address SharedArgListAddress = CGF.EmitLoadOfPointer(
1841 SharedArgs, Ctx.getPointerType(Ctx.getPointerType(Ctx.VoidPtrTy))
1842 .castAs<PointerType>());
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001843 for (llvm::Value *V : CapturedVars) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001844 Address Dst = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx,
1845 CGF.getPointerSize());
1846 llvm::Value *PtrV;
Alexey Bataev17314212018-03-20 15:41:05 +00001847 if (V->getType()->isIntegerTy())
1848 PtrV = Bld.CreateIntToPtr(V, CGF.VoidPtrTy);
1849 else
1850 PtrV = Bld.CreatePointerBitCastOrAddrSpaceCast(V, CGF.VoidPtrTy);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001851 CGF.EmitStoreOfScalar(PtrV, Dst, /*Volatile=*/false,
1852 Ctx.getPointerType(Ctx.VoidPtrTy));
Alexey Bataevc99042b2018-03-15 18:10:54 +00001853 ++Idx;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001854 }
1855 }
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001856
1857 // Activate workers. This barrier is used by the master to signal
1858 // work for the workers.
1859 syncCTAThreads(CGF);
1860
1861 // OpenMP [2.5, Parallel Construct, p.49]
1862 // There is an implied barrier at the end of a parallel region. After the
1863 // end of a parallel region, only the master thread of the team resumes
1864 // execution of the enclosing task region.
1865 //
1866 // The master waits at this barrier until all workers are done.
1867 syncCTAThreads(CGF);
1868
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001869 if (!CapturedVars.empty())
1870 CGF.EmitRuntimeCall(
1871 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_sharing_variables));
1872
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001873 // Remember for post-processing in worker loop.
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00001874 Work.emplace_back(WFn);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001875 };
1876
Alexey Bataev8521ff62018-07-25 20:03:01 +00001877 auto &&LNParallelGen = [this, Loc, &SeqGen, &L0ParallelGen, &CodeGen,
1878 &ThreadIDAddr](CodeGenFunction &CGF,
1879 PrePostActionTy &Action) {
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001880 RegionCodeGenTy RCG(CodeGen);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001881 if (IsInParallelRegion) {
1882 SeqGen(CGF, Action);
1883 } else if (IsInTargetMasterThreadRegion) {
1884 L0ParallelGen(CGF, Action);
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001885 } else if (getExecutionMode() == CGOpenMPRuntimeNVPTX::EM_NonSPMD) {
1886 RCG(CGF);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001887 } else {
1888 // Check for master and then parallelism:
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001889 // if (__kmpc_is_spmd_exec_mode() || __kmpc_parallel_level(loc, gtid)) {
Alexey Bataev673110d2018-05-16 13:36:30 +00001890 // Serialized execution.
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001891 // } else if (master) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001892 // Worker call.
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001893 // } else {
1894 // Outlined function call.
1895 // }
1896 CGBuilderTy &Bld = CGF.Builder;
1897 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit");
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001898 llvm::BasicBlock *SeqBB = CGF.createBasicBlock(".sequential");
1899 llvm::BasicBlock *ParallelCheckBB = CGF.createBasicBlock(".parcheck");
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001900 llvm::BasicBlock *MasterCheckBB = CGF.createBasicBlock(".mastercheck");
Alexey Bataev673110d2018-05-16 13:36:30 +00001901 llvm::Value *IsSPMD = Bld.CreateIsNotNull(CGF.EmitNounwindRuntimeCall(
1902 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_is_spmd_exec_mode)));
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001903 Bld.CreateCondBr(IsSPMD, SeqBB, ParallelCheckBB);
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001904 // There is no need to emit line number for unconditional branch.
1905 (void)ApplyDebugLocation::CreateEmpty(CGF);
1906 CGF.EmitBlock(ParallelCheckBB);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001907 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
1908 llvm::Value *ThreadID = getThreadID(CGF, Loc);
1909 llvm::Value *PL = CGF.EmitRuntimeCall(
1910 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_level),
1911 {RTLoc, ThreadID});
1912 llvm::Value *Res = Bld.CreateIsNotNull(PL);
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001913 Bld.CreateCondBr(Res, SeqBB, MasterCheckBB);
1914 CGF.EmitBlock(SeqBB);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001915 SeqGen(CGF, Action);
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001916 CGF.EmitBranch(ExitBB);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001917 // There is no need to emit line number for unconditional branch.
1918 (void)ApplyDebugLocation::CreateEmpty(CGF);
Alexey Bataev0baba9e2018-05-25 20:16:03 +00001919 CGF.EmitBlock(MasterCheckBB);
1920 llvm::BasicBlock *MasterThenBB = CGF.createBasicBlock("master.then");
1921 llvm::BasicBlock *ElseBlock = CGF.createBasicBlock("omp_if.else");
1922 llvm::Value *IsMaster =
1923 Bld.CreateICmpEQ(getNVPTXThreadID(CGF), getMasterThreadID(CGF));
1924 Bld.CreateCondBr(IsMaster, MasterThenBB, ElseBlock);
1925 CGF.EmitBlock(MasterThenBB);
1926 L0ParallelGen(CGF, Action);
1927 CGF.EmitBranch(ExitBB);
1928 // There is no need to emit line number for unconditional branch.
1929 (void)ApplyDebugLocation::CreateEmpty(CGF);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001930 CGF.EmitBlock(ElseBlock);
Alexey Bataev8521ff62018-07-25 20:03:01 +00001931 // In the worker need to use the real thread id.
1932 ThreadIDAddr = emitThreadIDAddress(CGF, Loc);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001933 RCG(CGF);
1934 // There is no need to emit line number for unconditional branch.
1935 (void)ApplyDebugLocation::CreateEmpty(CGF);
1936 // Emit the continuation block for code after the if.
1937 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
1938 }
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001939 };
1940
Alexey Bataev9ff80832018-04-16 20:16:21 +00001941 if (IfCond) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001942 emitOMPIfClause(CGF, IfCond, LNParallelGen, SeqGen);
Alexey Bataev9ff80832018-04-16 20:16:21 +00001943 } else {
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001944 CodeGenFunction::RunCleanupsScope Scope(CGF);
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00001945 RegionCodeGenTy ThenRCG(LNParallelGen);
Arpith Chacko Jacobbb36fe82017-01-10 15:42:51 +00001946 ThenRCG(CGF);
1947 }
1948}
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001949
Alexey Bataev4065b9a2018-06-21 20:26:33 +00001950void CGOpenMPRuntimeNVPTX::emitSPMDParallelCall(
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001951 CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
1952 ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
1953 // Just call the outlined function to execute the parallel region.
1954 // OutlinedFn(&GTid, &zero, CapturedStruct);
1955 //
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00001956 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
Carlo Bertolli79712092018-02-28 20:48:35 +00001957
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001958 Address ZeroAddr = CGF.CreateMemTemp(CGF.getContext().getIntTypeForBitwidth(
1959 /*DestWidth=*/32, /*Signed=*/1),
1960 ".zero.addr");
Carlo Bertolli79712092018-02-28 20:48:35 +00001961 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Alexey Bataev8521ff62018-07-25 20:03:01 +00001962 // ThreadId for serialized parallels is 0.
1963 Address ThreadIDAddr = ZeroAddr;
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001964 auto &&CodeGen = [this, OutlinedFn, CapturedVars, Loc, ZeroAddr,
Alexey Bataev8521ff62018-07-25 20:03:01 +00001965 &ThreadIDAddr](CodeGenFunction &CGF,
1966 PrePostActionTy &Action) {
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001967 Action.Enter(CGF);
1968
1969 llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
1970 OutlinedFnArgs.push_back(ThreadIDAddr.getPointer());
1971 OutlinedFnArgs.push_back(ZeroAddr.getPointer());
1972 OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
1973 emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, OutlinedFnArgs);
1974 };
1975 auto &&SeqGen = [this, &CodeGen, Loc](CodeGenFunction &CGF,
1976 PrePostActionTy &) {
1977
1978 RegionCodeGenTy RCG(CodeGen);
1979 llvm::Value *RTLoc = emitUpdateLocation(CGF, Loc);
1980 llvm::Value *ThreadID = getThreadID(CGF, Loc);
1981 llvm::Value *Args[] = {RTLoc, ThreadID};
1982
1983 NVPTXActionTy Action(
1984 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel),
1985 Args,
1986 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel),
1987 Args);
1988 RCG.setAction(Action);
1989 RCG(CGF);
1990 };
1991
1992 if (IsInTargetMasterThreadRegion) {
Alexey Bataev8521ff62018-07-25 20:03:01 +00001993 // In the worker need to use the real thread id.
1994 ThreadIDAddr = emitThreadIDAddress(CGF, Loc);
Alexey Bataevbf5c8482018-05-10 18:32:08 +00001995 RegionCodeGenTy RCG(CodeGen);
1996 RCG(CGF);
1997 } else {
1998 // If we are not in the target region, it is definitely L2 parallelism or
1999 // more, because for SPMD mode we always has L1 parallel level, sowe don't
2000 // need to check for orphaned directives.
2001 RegionCodeGenTy RCG(SeqGen);
2002 RCG(CGF);
2003 }
Arpith Chacko Jacob44a87c92017-01-18 19:35:00 +00002004}
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002005
Alexey Bataev504fc2d2018-05-07 17:23:05 +00002006void CGOpenMPRuntimeNVPTX::emitCriticalRegion(
2007 CodeGenFunction &CGF, StringRef CriticalName,
2008 const RegionCodeGenTy &CriticalOpGen, SourceLocation Loc,
2009 const Expr *Hint) {
2010 llvm::BasicBlock *LoopBB = CGF.createBasicBlock("omp.critical.loop");
2011 llvm::BasicBlock *TestBB = CGF.createBasicBlock("omp.critical.test");
2012 llvm::BasicBlock *SyncBB = CGF.createBasicBlock("omp.critical.sync");
2013 llvm::BasicBlock *BodyBB = CGF.createBasicBlock("omp.critical.body");
2014 llvm::BasicBlock *ExitBB = CGF.createBasicBlock("omp.critical.exit");
2015
2016 // Fetch team-local id of the thread.
2017 llvm::Value *ThreadID = getNVPTXThreadID(CGF);
2018
2019 // Get the width of the team.
2020 llvm::Value *TeamWidth = getNVPTXNumThreads(CGF);
2021
2022 // Initialize the counter variable for the loop.
2023 QualType Int32Ty =
2024 CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/0);
2025 Address Counter = CGF.CreateMemTemp(Int32Ty, "critical_counter");
2026 LValue CounterLVal = CGF.MakeAddrLValue(Counter, Int32Ty);
2027 CGF.EmitStoreOfScalar(llvm::Constant::getNullValue(CGM.Int32Ty), CounterLVal,
2028 /*isInit=*/true);
2029
2030 // Block checks if loop counter exceeds upper bound.
2031 CGF.EmitBlock(LoopBB);
2032 llvm::Value *CounterVal = CGF.EmitLoadOfScalar(CounterLVal, Loc);
2033 llvm::Value *CmpLoopBound = CGF.Builder.CreateICmpSLT(CounterVal, TeamWidth);
2034 CGF.Builder.CreateCondBr(CmpLoopBound, TestBB, ExitBB);
2035
2036 // Block tests which single thread should execute region, and which threads
2037 // should go straight to synchronisation point.
2038 CGF.EmitBlock(TestBB);
2039 CounterVal = CGF.EmitLoadOfScalar(CounterLVal, Loc);
2040 llvm::Value *CmpThreadToCounter =
2041 CGF.Builder.CreateICmpEQ(ThreadID, CounterVal);
2042 CGF.Builder.CreateCondBr(CmpThreadToCounter, BodyBB, SyncBB);
2043
2044 // Block emits the body of the critical region.
2045 CGF.EmitBlock(BodyBB);
2046
2047 // Output the critical statement.
2048 CriticalOpGen(CGF);
2049
2050 // After the body surrounded by the critical region, the single executing
2051 // thread will jump to the synchronisation point.
2052 // Block waits for all threads in current team to finish then increments the
2053 // counter variable and returns to the loop.
2054 CGF.EmitBlock(SyncBB);
2055 getNVPTXCTABarrier(CGF);
2056
2057 llvm::Value *IncCounterVal =
2058 CGF.Builder.CreateNSWAdd(CounterVal, CGF.Builder.getInt32(1));
2059 CGF.EmitStoreOfScalar(IncCounterVal, CounterLVal);
2060 CGF.EmitBranch(LoopBB);
2061
2062 // Block that is reached when all threads in the team complete the region.
2063 CGF.EmitBlock(ExitBB, /*IsFinished=*/true);
2064}
2065
Alexey Bataevb2575932018-01-04 20:18:55 +00002066/// Cast value to the specified type.
Alexey Bataeva453f362018-03-19 17:53:56 +00002067static llvm::Value *castValueToType(CodeGenFunction &CGF, llvm::Value *Val,
2068 QualType ValTy, QualType CastTy,
2069 SourceLocation Loc) {
2070 assert(!CGF.getContext().getTypeSizeInChars(CastTy).isZero() &&
2071 "Cast type must sized.");
2072 assert(!CGF.getContext().getTypeSizeInChars(ValTy).isZero() &&
2073 "Val type must sized.");
2074 llvm::Type *LLVMCastTy = CGF.ConvertTypeForMem(CastTy);
2075 if (ValTy == CastTy)
Alexey Bataevb2575932018-01-04 20:18:55 +00002076 return Val;
Alexey Bataeva453f362018-03-19 17:53:56 +00002077 if (CGF.getContext().getTypeSizeInChars(ValTy) ==
2078 CGF.getContext().getTypeSizeInChars(CastTy))
2079 return CGF.Builder.CreateBitCast(Val, LLVMCastTy);
2080 if (CastTy->isIntegerType() && ValTy->isIntegerType())
2081 return CGF.Builder.CreateIntCast(Val, LLVMCastTy,
2082 CastTy->hasSignedIntegerRepresentation());
2083 Address CastItem = CGF.CreateMemTemp(CastTy);
Alexey Bataevb2575932018-01-04 20:18:55 +00002084 Address ValCastItem = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2085 CastItem, Val->getType()->getPointerTo(CastItem.getAddressSpace()));
Alexey Bataeva453f362018-03-19 17:53:56 +00002086 CGF.EmitStoreOfScalar(Val, ValCastItem, /*Volatile=*/false, ValTy);
2087 return CGF.EmitLoadOfScalar(CastItem, /*Volatile=*/false, CastTy, Loc);
Alexey Bataevb2575932018-01-04 20:18:55 +00002088}
2089
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002090/// This function creates calls to one of two shuffle functions to copy
2091/// variables between lanes in a warp.
2092static llvm::Value *createRuntimeShuffleFunction(CodeGenFunction &CGF,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002093 llvm::Value *Elem,
Alexey Bataeva453f362018-03-19 17:53:56 +00002094 QualType ElemType,
2095 llvm::Value *Offset,
2096 SourceLocation Loc) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00002097 CodeGenModule &CGM = CGF.CGM;
2098 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002099 CGOpenMPRuntimeNVPTX &RT =
2100 *(static_cast<CGOpenMPRuntimeNVPTX *>(&CGM.getOpenMPRuntime()));
2101
Alexey Bataeva453f362018-03-19 17:53:56 +00002102 CharUnits Size = CGF.getContext().getTypeSizeInChars(ElemType);
2103 assert(Size.getQuantity() <= 8 &&
2104 "Unsupported bitwidth in shuffle instruction.");
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002105
Alexey Bataeva453f362018-03-19 17:53:56 +00002106 OpenMPRTLFunctionNVPTX ShuffleFn = Size.getQuantity() <= 4
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002107 ? OMPRTL_NVPTX__kmpc_shuffle_int32
2108 : OMPRTL_NVPTX__kmpc_shuffle_int64;
2109
2110 // Cast all types to 32- or 64-bit values before calling shuffle routines.
Alexey Bataeva453f362018-03-19 17:53:56 +00002111 QualType CastTy = CGF.getContext().getIntTypeForBitwidth(
2112 Size.getQuantity() <= 4 ? 32 : 64, /*Signed=*/1);
2113 llvm::Value *ElemCast = castValueToType(CGF, Elem, ElemType, CastTy, Loc);
Alexey Bataev9ff80832018-04-16 20:16:21 +00002114 llvm::Value *WarpSize =
Alexey Bataevb2575932018-01-04 20:18:55 +00002115 Bld.CreateIntCast(getNVPTXWarpSize(CGF), CGM.Int16Ty, /*isSigned=*/true);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002116
Alexey Bataev9ff80832018-04-16 20:16:21 +00002117 llvm::Value *ShuffledVal = CGF.EmitRuntimeCall(
2118 RT.createNVPTXRuntimeFunction(ShuffleFn), {ElemCast, Offset, WarpSize});
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002119
Alexey Bataeva453f362018-03-19 17:53:56 +00002120 return castValueToType(CGF, ShuffledVal, CastTy, ElemType, Loc);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002121}
2122
Alexey Bataev12c62902018-06-22 19:10:38 +00002123static void shuffleAndStore(CodeGenFunction &CGF, Address SrcAddr,
2124 Address DestAddr, QualType ElemType,
2125 llvm::Value *Offset, SourceLocation Loc) {
2126 CGBuilderTy &Bld = CGF.Builder;
2127
2128 CharUnits Size = CGF.getContext().getTypeSizeInChars(ElemType);
2129 // Create the loop over the big sized data.
2130 // ptr = (void*)Elem;
2131 // ptrEnd = (void*) Elem + 1;
2132 // Step = 8;
2133 // while (ptr + Step < ptrEnd)
2134 // shuffle((int64_t)*ptr);
2135 // Step = 4;
2136 // while (ptr + Step < ptrEnd)
2137 // shuffle((int32_t)*ptr);
2138 // ...
2139 Address ElemPtr = DestAddr;
2140 Address Ptr = SrcAddr;
2141 Address PtrEnd = Bld.CreatePointerBitCastOrAddrSpaceCast(
2142 Bld.CreateConstGEP(SrcAddr, 1, Size), CGF.VoidPtrTy);
2143 for (int IntSize = 8; IntSize >= 1; IntSize /= 2) {
2144 if (Size < CharUnits::fromQuantity(IntSize))
2145 continue;
2146 QualType IntType = CGF.getContext().getIntTypeForBitwidth(
2147 CGF.getContext().toBits(CharUnits::fromQuantity(IntSize)),
2148 /*Signed=*/1);
2149 llvm::Type *IntTy = CGF.ConvertTypeForMem(IntType);
2150 Ptr = Bld.CreatePointerBitCastOrAddrSpaceCast(Ptr, IntTy->getPointerTo());
2151 ElemPtr =
2152 Bld.CreatePointerBitCastOrAddrSpaceCast(ElemPtr, IntTy->getPointerTo());
2153 if (Size.getQuantity() / IntSize > 1) {
2154 llvm::BasicBlock *PreCondBB = CGF.createBasicBlock(".shuffle.pre_cond");
2155 llvm::BasicBlock *ThenBB = CGF.createBasicBlock(".shuffle.then");
2156 llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".shuffle.exit");
2157 llvm::BasicBlock *CurrentBB = Bld.GetInsertBlock();
2158 CGF.EmitBlock(PreCondBB);
2159 llvm::PHINode *PhiSrc =
2160 Bld.CreatePHI(Ptr.getType(), /*NumReservedValues=*/2);
2161 PhiSrc->addIncoming(Ptr.getPointer(), CurrentBB);
2162 llvm::PHINode *PhiDest =
2163 Bld.CreatePHI(ElemPtr.getType(), /*NumReservedValues=*/2);
2164 PhiDest->addIncoming(ElemPtr.getPointer(), CurrentBB);
2165 Ptr = Address(PhiSrc, Ptr.getAlignment());
2166 ElemPtr = Address(PhiDest, ElemPtr.getAlignment());
2167 llvm::Value *PtrDiff = Bld.CreatePtrDiff(
2168 PtrEnd.getPointer(), Bld.CreatePointerBitCastOrAddrSpaceCast(
2169 Ptr.getPointer(), CGF.VoidPtrTy));
2170 Bld.CreateCondBr(Bld.CreateICmpSGT(PtrDiff, Bld.getInt64(IntSize - 1)),
2171 ThenBB, ExitBB);
2172 CGF.EmitBlock(ThenBB);
2173 llvm::Value *Res = createRuntimeShuffleFunction(
2174 CGF, CGF.EmitLoadOfScalar(Ptr, /*Volatile=*/false, IntType, Loc),
2175 IntType, Offset, Loc);
2176 CGF.EmitStoreOfScalar(Res, ElemPtr, /*Volatile=*/false, IntType);
2177 Ptr = Bld.CreateConstGEP(Ptr, 1, CharUnits::fromQuantity(IntSize));
2178 ElemPtr =
2179 Bld.CreateConstGEP(ElemPtr, 1, CharUnits::fromQuantity(IntSize));
2180 PhiSrc->addIncoming(Ptr.getPointer(), ThenBB);
2181 PhiDest->addIncoming(ElemPtr.getPointer(), ThenBB);
2182 CGF.EmitBranch(PreCondBB);
2183 CGF.EmitBlock(ExitBB);
2184 } else {
2185 llvm::Value *Res = createRuntimeShuffleFunction(
2186 CGF, CGF.EmitLoadOfScalar(Ptr, /*Volatile=*/false, IntType, Loc),
2187 IntType, Offset, Loc);
2188 CGF.EmitStoreOfScalar(Res, ElemPtr, /*Volatile=*/false, IntType);
2189 Ptr = Bld.CreateConstGEP(Ptr, 1, CharUnits::fromQuantity(IntSize));
2190 ElemPtr =
2191 Bld.CreateConstGEP(ElemPtr, 1, CharUnits::fromQuantity(IntSize));
2192 }
2193 Size = Size % IntSize;
2194 }
2195}
2196
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002197namespace {
2198enum CopyAction : unsigned {
2199 // RemoteLaneToThread: Copy over a Reduce list from a remote lane in
2200 // the warp using shuffle instructions.
2201 RemoteLaneToThread,
2202 // ThreadCopy: Make a copy of a Reduce list on the thread's stack.
2203 ThreadCopy,
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002204 // ThreadToScratchpad: Copy a team-reduced array to the scratchpad.
2205 ThreadToScratchpad,
2206 // ScratchpadToThread: Copy from a scratchpad array in global memory
2207 // containing team-reduced data to a thread's stack.
2208 ScratchpadToThread,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002209};
2210} // namespace
2211
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002212struct CopyOptionsTy {
2213 llvm::Value *RemoteLaneOffset;
2214 llvm::Value *ScratchpadIndex;
2215 llvm::Value *ScratchpadWidth;
2216};
2217
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002218/// Emit instructions to copy a Reduce list, which contains partially
2219/// aggregated values, in the specified direction.
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002220static void emitReductionListCopy(
2221 CopyAction Action, CodeGenFunction &CGF, QualType ReductionArrayTy,
2222 ArrayRef<const Expr *> Privates, Address SrcBase, Address DestBase,
2223 CopyOptionsTy CopyOptions = {nullptr, nullptr, nullptr}) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002224
Alexey Bataev9ff80832018-04-16 20:16:21 +00002225 CodeGenModule &CGM = CGF.CGM;
2226 ASTContext &C = CGM.getContext();
2227 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002228
Alexey Bataev9ff80832018-04-16 20:16:21 +00002229 llvm::Value *RemoteLaneOffset = CopyOptions.RemoteLaneOffset;
2230 llvm::Value *ScratchpadIndex = CopyOptions.ScratchpadIndex;
2231 llvm::Value *ScratchpadWidth = CopyOptions.ScratchpadWidth;
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002232
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002233 // Iterates, element-by-element, through the source Reduce list and
2234 // make a copy.
2235 unsigned Idx = 0;
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002236 unsigned Size = Privates.size();
Alexey Bataev9ff80832018-04-16 20:16:21 +00002237 for (const Expr *Private : Privates) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002238 Address SrcElementAddr = Address::invalid();
2239 Address DestElementAddr = Address::invalid();
2240 Address DestElementPtrAddr = Address::invalid();
2241 // Should we shuffle in an element from a remote lane?
2242 bool ShuffleInElement = false;
2243 // Set to true to update the pointer in the dest Reduce list to a
2244 // newly created element.
2245 bool UpdateDestListPtr = false;
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002246 // Increment the src or dest pointer to the scratchpad, for each
2247 // new element.
2248 bool IncrScratchpadSrc = false;
2249 bool IncrScratchpadDest = false;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002250
2251 switch (Action) {
2252 case RemoteLaneToThread: {
2253 // Step 1.1: Get the address for the src element in the Reduce list.
2254 Address SrcElementPtrAddr =
2255 Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
Alexey Bataevb2575932018-01-04 20:18:55 +00002256 SrcElementAddr = CGF.EmitLoadOfPointer(
2257 SrcElementPtrAddr,
2258 C.getPointerType(Private->getType())->castAs<PointerType>());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002259
2260 // Step 1.2: Create a temporary to store the element in the destination
2261 // Reduce list.
2262 DestElementPtrAddr =
2263 Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
2264 DestElementAddr =
2265 CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
2266 ShuffleInElement = true;
2267 UpdateDestListPtr = true;
2268 break;
2269 }
2270 case ThreadCopy: {
2271 // Step 1.1: Get the address for the src element in the Reduce list.
2272 Address SrcElementPtrAddr =
2273 Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
Alexey Bataevb2575932018-01-04 20:18:55 +00002274 SrcElementAddr = CGF.EmitLoadOfPointer(
2275 SrcElementPtrAddr,
2276 C.getPointerType(Private->getType())->castAs<PointerType>());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002277
2278 // Step 1.2: Get the address for dest element. The destination
2279 // element has already been created on the thread's stack.
2280 DestElementPtrAddr =
2281 Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
Alexey Bataevb2575932018-01-04 20:18:55 +00002282 DestElementAddr = CGF.EmitLoadOfPointer(
2283 DestElementPtrAddr,
2284 C.getPointerType(Private->getType())->castAs<PointerType>());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002285 break;
2286 }
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002287 case ThreadToScratchpad: {
2288 // Step 1.1: Get the address for the src element in the Reduce list.
2289 Address SrcElementPtrAddr =
2290 Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
Alexey Bataevb2575932018-01-04 20:18:55 +00002291 SrcElementAddr = CGF.EmitLoadOfPointer(
2292 SrcElementPtrAddr,
2293 C.getPointerType(Private->getType())->castAs<PointerType>());
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002294
2295 // Step 1.2: Get the address for dest element:
2296 // address = base + index * ElementSizeInChars.
Alexey Bataeve290ec02018-04-06 16:03:36 +00002297 llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
Alexey Bataev9ff80832018-04-16 20:16:21 +00002298 llvm::Value *CurrentOffset =
Alexey Bataeve290ec02018-04-06 16:03:36 +00002299 Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex);
Alexey Bataev9ff80832018-04-16 20:16:21 +00002300 llvm::Value *ScratchPadElemAbsolutePtrVal =
Alexey Bataeve290ec02018-04-06 16:03:36 +00002301 Bld.CreateNUWAdd(DestBase.getPointer(), CurrentOffset);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002302 ScratchPadElemAbsolutePtrVal =
2303 Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
Alexey Bataevb2575932018-01-04 20:18:55 +00002304 DestElementAddr = Address(ScratchPadElemAbsolutePtrVal,
2305 C.getTypeAlignInChars(Private->getType()));
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002306 IncrScratchpadDest = true;
2307 break;
2308 }
2309 case ScratchpadToThread: {
2310 // Step 1.1: Get the address for the src element in the scratchpad.
2311 // address = base + index * ElementSizeInChars.
Alexey Bataeve290ec02018-04-06 16:03:36 +00002312 llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
Alexey Bataev9ff80832018-04-16 20:16:21 +00002313 llvm::Value *CurrentOffset =
Alexey Bataeve290ec02018-04-06 16:03:36 +00002314 Bld.CreateNUWMul(ElementSizeInChars, ScratchpadIndex);
Alexey Bataev9ff80832018-04-16 20:16:21 +00002315 llvm::Value *ScratchPadElemAbsolutePtrVal =
Alexey Bataeve290ec02018-04-06 16:03:36 +00002316 Bld.CreateNUWAdd(SrcBase.getPointer(), CurrentOffset);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002317 ScratchPadElemAbsolutePtrVal =
2318 Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
2319 SrcElementAddr = Address(ScratchPadElemAbsolutePtrVal,
2320 C.getTypeAlignInChars(Private->getType()));
2321 IncrScratchpadSrc = true;
2322
2323 // Step 1.2: Create a temporary to store the element in the destination
2324 // Reduce list.
2325 DestElementPtrAddr =
2326 Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
2327 DestElementAddr =
2328 CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
2329 UpdateDestListPtr = true;
2330 break;
2331 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002332 }
2333
2334 // Regardless of src and dest of copy, we emit the load of src
2335 // element as this is required in all directions
2336 SrcElementAddr = Bld.CreateElementBitCast(
2337 SrcElementAddr, CGF.ConvertTypeForMem(Private->getType()));
Alexey Bataev12c62902018-06-22 19:10:38 +00002338 DestElementAddr = Bld.CreateElementBitCast(DestElementAddr,
2339 SrcElementAddr.getElementType());
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002340
2341 // Now that all active lanes have read the element in the
2342 // Reduce list, shuffle over the value from the remote lane.
Alexey Bataeva453f362018-03-19 17:53:56 +00002343 if (ShuffleInElement) {
Alexey Bataev12c62902018-06-22 19:10:38 +00002344 shuffleAndStore(CGF, SrcElementAddr, DestElementAddr, Private->getType(),
2345 RemoteLaneOffset, Private->getExprLoc());
2346 } else {
2347 if (Private->getType()->isScalarType()) {
2348 llvm::Value *Elem =
2349 CGF.EmitLoadOfScalar(SrcElementAddr, /*Volatile=*/false,
2350 Private->getType(), Private->getExprLoc());
2351 // Store the source element value to the dest element address.
2352 CGF.EmitStoreOfScalar(Elem, DestElementAddr, /*Volatile=*/false,
2353 Private->getType());
2354 } else {
2355 CGF.EmitAggregateCopy(
2356 CGF.MakeAddrLValue(DestElementAddr, Private->getType()),
2357 CGF.MakeAddrLValue(SrcElementAddr, Private->getType()),
2358 Private->getType(), AggValueSlot::DoesNotOverlap);
2359 }
Alexey Bataeva453f362018-03-19 17:53:56 +00002360 }
Alexey Bataevb2575932018-01-04 20:18:55 +00002361
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002362 // Step 3.1: Modify reference in dest Reduce list as needed.
2363 // Modifying the reference in Reduce list to point to the newly
2364 // created element. The element is live in the current function
2365 // scope and that of functions it invokes (i.e., reduce_function).
2366 // RemoteReduceData[i] = (void*)&RemoteElem
2367 if (UpdateDestListPtr) {
2368 CGF.EmitStoreOfScalar(Bld.CreatePointerBitCastOrAddrSpaceCast(
2369 DestElementAddr.getPointer(), CGF.VoidPtrTy),
2370 DestElementPtrAddr, /*Volatile=*/false,
2371 C.VoidPtrTy);
2372 }
2373
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002374 // Step 4.1: Increment SrcBase/DestBase so that it points to the starting
2375 // address of the next element in scratchpad memory, unless we're currently
2376 // processing the last one. Memory alignment is also taken care of here.
2377 if ((IncrScratchpadDest || IncrScratchpadSrc) && (Idx + 1 < Size)) {
2378 llvm::Value *ScratchpadBasePtr =
2379 IncrScratchpadDest ? DestBase.getPointer() : SrcBase.getPointer();
Alexey Bataeve290ec02018-04-06 16:03:36 +00002380 llvm::Value *ElementSizeInChars = CGF.getTypeSize(Private->getType());
2381 ScratchpadBasePtr = Bld.CreateNUWAdd(
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002382 ScratchpadBasePtr,
Alexey Bataeve290ec02018-04-06 16:03:36 +00002383 Bld.CreateNUWMul(ScratchpadWidth, ElementSizeInChars));
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002384
2385 // Take care of global memory alignment for performance
Alexey Bataeve290ec02018-04-06 16:03:36 +00002386 ScratchpadBasePtr = Bld.CreateNUWSub(
2387 ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1));
2388 ScratchpadBasePtr = Bld.CreateUDiv(
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002389 ScratchpadBasePtr,
2390 llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
Alexey Bataeve290ec02018-04-06 16:03:36 +00002391 ScratchpadBasePtr = Bld.CreateNUWAdd(
2392 ScratchpadBasePtr, llvm::ConstantInt::get(CGM.SizeTy, 1));
2393 ScratchpadBasePtr = Bld.CreateNUWMul(
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002394 ScratchpadBasePtr,
2395 llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
2396
2397 if (IncrScratchpadDest)
2398 DestBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
2399 else /* IncrScratchpadSrc = true */
2400 SrcBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
2401 }
2402
Alexey Bataev9ff80832018-04-16 20:16:21 +00002403 ++Idx;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002404 }
2405}
2406
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002407/// This function emits a helper that loads data from the scratchpad array
2408/// and (optionally) reduces it with the input operand.
2409///
2410/// load_and_reduce(local, scratchpad, index, width, should_reduce)
2411/// reduce_data remote;
2412/// for elem in remote:
2413/// remote.elem = Scratchpad[elem_id][index]
2414/// if (should_reduce)
2415/// local = local @ remote
2416/// else
2417/// local = remote
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002418static llvm::Value *emitReduceScratchpadFunction(
2419 CodeGenModule &CGM, ArrayRef<const Expr *> Privates,
2420 QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00002421 ASTContext &C = CGM.getContext();
2422 QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002423
2424 // Destination of the copy.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002425 ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2426 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002427 // Base address of the scratchpad array, with each element storing a
2428 // Reduce list per team.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002429 ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2430 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002431 // A source index into the scratchpad array.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002432 ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
2433 ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002434 // Row width of an element in the scratchpad array, typically
2435 // the number of teams.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002436 ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
2437 ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002438 // If should_reduce == 1, then it's load AND reduce,
2439 // If should_reduce == 0 (or otherwise), then it only loads (+ copy).
2440 // The latter case is used for initialization.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002441 ImplicitParamDecl ShouldReduceArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2442 Int32Ty, ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002443
2444 FunctionArgList Args;
2445 Args.push_back(&ReduceListArg);
2446 Args.push_back(&ScratchPadArg);
2447 Args.push_back(&IndexArg);
2448 Args.push_back(&WidthArg);
2449 Args.push_back(&ShouldReduceArg);
2450
Alexey Bataev9ff80832018-04-16 20:16:21 +00002451 const CGFunctionInfo &CGFI =
2452 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002453 auto *Fn = llvm::Function::Create(
2454 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2455 "_omp_reduction_load_and_reduce", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00002456 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002457 Fn->setDoesNotRecurse();
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002458 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002459 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002460
Alexey Bataev9ff80832018-04-16 20:16:21 +00002461 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002462
2463 // Get local Reduce list pointer.
2464 Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
2465 Address ReduceListAddr(
2466 Bld.CreatePointerBitCastOrAddrSpaceCast(
2467 CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002468 C.VoidPtrTy, Loc),
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002469 CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
2470 CGF.getPointerAlign());
2471
2472 Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
2473 llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002474 AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002475
2476 Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002477 llvm::Value *IndexVal = Bld.CreateIntCast(
2478 CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc),
2479 CGM.SizeTy, /*isSigned=*/true);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002480
2481 Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002482 llvm::Value *WidthVal = Bld.CreateIntCast(
2483 CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false, Int32Ty, Loc),
2484 CGM.SizeTy, /*isSigned=*/true);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002485
2486 Address AddrShouldReduceArg = CGF.GetAddrOfLocalVar(&ShouldReduceArg);
2487 llvm::Value *ShouldReduceVal = CGF.EmitLoadOfScalar(
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002488 AddrShouldReduceArg, /*Volatile=*/false, Int32Ty, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002489
2490 // The absolute ptr address to the base addr of the next element to copy.
2491 llvm::Value *CumulativeElemBasePtr =
2492 Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
2493 Address SrcDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
2494
2495 // Create a Remote Reduce list to store the elements read from the
2496 // scratchpad array.
2497 Address RemoteReduceList =
2498 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_red_list");
2499
2500 // Assemble remote Reduce list from scratchpad array.
2501 emitReductionListCopy(ScratchpadToThread, CGF, ReductionArrayTy, Privates,
2502 SrcDataAddr, RemoteReduceList,
2503 {/*RemoteLaneOffset=*/nullptr,
2504 /*ScratchpadIndex=*/IndexVal,
2505 /*ScratchpadWidth=*/WidthVal});
2506
2507 llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
2508 llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
2509 llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
2510
Alexey Bataev9ff80832018-04-16 20:16:21 +00002511 llvm::Value *CondReduce = Bld.CreateIsNotNull(ShouldReduceVal);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002512 Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
2513
2514 CGF.EmitBlock(ThenBB);
2515 // We should reduce with the local Reduce list.
2516 // reduce_function(LocalReduceList, RemoteReduceList)
2517 llvm::Value *LocalDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
2518 ReduceListAddr.getPointer(), CGF.VoidPtrTy);
2519 llvm::Value *RemoteDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
2520 RemoteReduceList.getPointer(), CGF.VoidPtrTy);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002521 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
2522 CGF, Loc, ReduceFn, {LocalDataPtr, RemoteDataPtr});
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002523 Bld.CreateBr(MergeBB);
2524
2525 CGF.EmitBlock(ElseBB);
2526 // No reduction; just copy:
2527 // Local Reduce list = Remote Reduce list.
2528 emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
2529 RemoteReduceList, ReduceListAddr);
2530 Bld.CreateBr(MergeBB);
2531
2532 CGF.EmitBlock(MergeBB);
2533
2534 CGF.FinishFunction();
2535 return Fn;
2536}
2537
2538/// This function emits a helper that stores reduced data from the team
2539/// master to a scratchpad array in global memory.
2540///
2541/// for elem in Reduce List:
2542/// scratchpad[elem_id][index] = elem
2543///
Benjamin Kramer674d5792017-05-26 20:08:24 +00002544static llvm::Value *emitCopyToScratchpad(CodeGenModule &CGM,
2545 ArrayRef<const Expr *> Privates,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002546 QualType ReductionArrayTy,
2547 SourceLocation Loc) {
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002548
Alexey Bataev9ff80832018-04-16 20:16:21 +00002549 ASTContext &C = CGM.getContext();
2550 QualType Int32Ty = C.getIntTypeForBitwidth(32, /*Signed=*/1);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002551
2552 // Source of the copy.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002553 ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2554 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002555 // Base address of the scratchpad array, with each element storing a
2556 // Reduce list per team.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002557 ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2558 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002559 // A destination index into the scratchpad array, typically the team
2560 // identifier.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002561 ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
2562 ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002563 // Row width of an element in the scratchpad array, typically
2564 // the number of teams.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002565 ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, Int32Ty,
2566 ImplicitParamDecl::Other);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002567
2568 FunctionArgList Args;
2569 Args.push_back(&ReduceListArg);
2570 Args.push_back(&ScratchPadArg);
2571 Args.push_back(&IndexArg);
2572 Args.push_back(&WidthArg);
2573
Alexey Bataev9ff80832018-04-16 20:16:21 +00002574 const CGFunctionInfo &CGFI =
2575 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002576 auto *Fn = llvm::Function::Create(
2577 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2578 "_omp_reduction_copy_to_scratchpad", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00002579 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002580 Fn->setDoesNotRecurse();
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002581 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002582 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002583
Alexey Bataev9ff80832018-04-16 20:16:21 +00002584 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002585
2586 Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
2587 Address SrcDataAddr(
2588 Bld.CreatePointerBitCastOrAddrSpaceCast(
2589 CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002590 C.VoidPtrTy, Loc),
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002591 CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
2592 CGF.getPointerAlign());
2593
2594 Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
2595 llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002596 AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002597
2598 Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
Alexey Bataeva9b9cc02018-01-23 18:12:38 +00002599 llvm::Value *IndexVal = Bld.CreateIntCast(
2600 CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false, Int32Ty, Loc),
2601 CGF.SizeTy, /*isSigned=*/true);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002602
2603 Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
2604 llvm::Value *WidthVal =
2605 Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false,
2606 Int32Ty, SourceLocation()),
2607 CGF.SizeTy, /*isSigned=*/true);
2608
2609 // The absolute ptr address to the base addr of the next element to copy.
2610 llvm::Value *CumulativeElemBasePtr =
2611 Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
2612 Address DestDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
2613
2614 emitReductionListCopy(ThreadToScratchpad, CGF, ReductionArrayTy, Privates,
2615 SrcDataAddr, DestDataAddr,
2616 {/*RemoteLaneOffset=*/nullptr,
2617 /*ScratchpadIndex=*/IndexVal,
2618 /*ScratchpadWidth=*/WidthVal});
2619
2620 CGF.FinishFunction();
2621 return Fn;
2622}
2623
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002624/// This function emits a helper that gathers Reduce lists from the first
2625/// lane of every active warp to lanes in the first warp.
2626///
2627/// void inter_warp_copy_func(void* reduce_data, num_warps)
2628/// shared smem[warp_size];
2629/// For all data entries D in reduce_data:
2630/// If (I am the first lane in each warp)
2631/// Copy my local D to smem[warp_id]
2632/// sync
2633/// if (I am the first warp)
2634/// Copy smem[thread_id] to my local D
2635/// sync
2636static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM,
2637 ArrayRef<const Expr *> Privates,
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002638 QualType ReductionArrayTy,
2639 SourceLocation Loc) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00002640 ASTContext &C = CGM.getContext();
2641 llvm::Module &M = CGM.getModule();
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002642
2643 // ReduceList: thread local Reduce list.
2644 // At the stage of the computation when this function is called, partially
2645 // aggregated values reside in the first lane of every active warp.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002646 ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2647 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002648 // NumWarps: number of warps active in the parallel region. This could
2649 // be smaller than 32 (max warps in a CTA) for partial block reduction.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002650 ImplicitParamDecl NumWarpsArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
Alexey Bataev56223232017-06-09 13:40:18 +00002651 C.getIntTypeForBitwidth(32, /* Signed */ true),
2652 ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002653 FunctionArgList Args;
2654 Args.push_back(&ReduceListArg);
2655 Args.push_back(&NumWarpsArg);
2656
Alexey Bataev9ff80832018-04-16 20:16:21 +00002657 const CGFunctionInfo &CGFI =
2658 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002659 auto *Fn = llvm::Function::Create(
2660 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2661 "_omp_reduction_inter_warp_copy_func", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00002662 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002663 Fn->setDoesNotRecurse();
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002664 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002665 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002666
Alexey Bataev9ff80832018-04-16 20:16:21 +00002667 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002668
2669 // This array is used as a medium to transfer, one reduce element at a time,
2670 // the data from the first lane of every warp to lanes in the first warp
2671 // in order to perform the final step of a reduction in a parallel region
2672 // (reduction across warps). The array is placed in NVPTX __shared__ memory
2673 // for reduced latency, as well as to have a distinct copy for concurrently
2674 // executing target regions. The array is declared with common linkage so
2675 // as to be shared across compilation units.
Alexey Bataev9ff80832018-04-16 20:16:21 +00002676 StringRef TransferMediumName =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002677 "__openmp_nvptx_data_transfer_temporary_storage";
2678 llvm::GlobalVariable *TransferMedium =
2679 M.getGlobalVariable(TransferMediumName);
2680 if (!TransferMedium) {
2681 auto *Ty = llvm::ArrayType::get(CGM.Int64Ty, WarpSize);
2682 unsigned SharedAddressSpace = C.getTargetAddressSpace(LangAS::cuda_shared);
2683 TransferMedium = new llvm::GlobalVariable(
2684 M, Ty,
2685 /*isConstant=*/false, llvm::GlobalVariable::CommonLinkage,
2686 llvm::Constant::getNullValue(Ty), TransferMediumName,
2687 /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
2688 SharedAddressSpace);
Alexey Bataev9ff80832018-04-16 20:16:21 +00002689 CGM.addCompilerUsedGlobal(TransferMedium);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002690 }
2691
2692 // Get the CUDA thread id of the current OpenMP thread on the GPU.
Alexey Bataev9ff80832018-04-16 20:16:21 +00002693 llvm::Value *ThreadID = getNVPTXThreadID(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002694 // nvptx_lane_id = nvptx_id % warpsize
Alexey Bataev9ff80832018-04-16 20:16:21 +00002695 llvm::Value *LaneID = getNVPTXLaneID(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002696 // nvptx_warp_id = nvptx_id / warpsize
Alexey Bataev9ff80832018-04-16 20:16:21 +00002697 llvm::Value *WarpID = getNVPTXWarpID(CGF);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002698
2699 Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
2700 Address LocalReduceList(
2701 Bld.CreatePointerBitCastOrAddrSpaceCast(
2702 CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
2703 C.VoidPtrTy, SourceLocation()),
2704 CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
2705 CGF.getPointerAlign());
2706
2707 unsigned Idx = 0;
Alexey Bataev9ff80832018-04-16 20:16:21 +00002708 for (const Expr *Private : Privates) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002709 //
2710 // Warp master copies reduce element to transfer medium in __shared__
2711 // memory.
2712 //
2713 llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
2714 llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
2715 llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
2716
2717 // if (lane_id == 0)
Alexey Bataev9ff80832018-04-16 20:16:21 +00002718 llvm::Value *IsWarpMaster = Bld.CreateIsNull(LaneID, "warp_master");
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002719 Bld.CreateCondBr(IsWarpMaster, ThenBB, ElseBB);
2720 CGF.EmitBlock(ThenBB);
2721
2722 // Reduce element = LocalReduceList[i]
2723 Address ElemPtrPtrAddr =
2724 Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
2725 llvm::Value *ElemPtrPtr = CGF.EmitLoadOfScalar(
2726 ElemPtrPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
2727 // elemptr = (type[i]*)(elemptrptr)
2728 Address ElemPtr =
2729 Address(ElemPtrPtr, C.getTypeAlignInChars(Private->getType()));
2730 ElemPtr = Bld.CreateElementBitCast(
2731 ElemPtr, CGF.ConvertTypeForMem(Private->getType()));
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002732
2733 // Get pointer to location in transfer medium.
2734 // MediumPtr = &medium[warp_id]
2735 llvm::Value *MediumPtrVal = Bld.CreateInBoundsGEP(
2736 TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), WarpID});
2737 Address MediumPtr(MediumPtrVal, C.getTypeAlignInChars(Private->getType()));
2738 // Casting to actual data type.
2739 // MediumPtr = (type[i]*)MediumPtrAddr;
2740 MediumPtr = Bld.CreateElementBitCast(
2741 MediumPtr, CGF.ConvertTypeForMem(Private->getType()));
2742
Alexey Bataev12c62902018-06-22 19:10:38 +00002743 // elem = *elemptr
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002744 //*MediumPtr = elem
Alexey Bataev12c62902018-06-22 19:10:38 +00002745 if (Private->getType()->isScalarType()) {
2746 llvm::Value *Elem = CGF.EmitLoadOfScalar(ElemPtr, /*Volatile=*/false,
2747 Private->getType(), Loc);
2748 // Store the source element value to the dest element address.
2749 CGF.EmitStoreOfScalar(Elem, MediumPtr, /*Volatile=*/false,
2750 Private->getType());
2751 } else {
2752 CGF.EmitAggregateCopy(CGF.MakeAddrLValue(ElemPtr, Private->getType()),
2753 CGF.MakeAddrLValue(MediumPtr, Private->getType()),
2754 Private->getType(), AggValueSlot::DoesNotOverlap);
2755 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002756
2757 Bld.CreateBr(MergeBB);
2758
2759 CGF.EmitBlock(ElseBB);
2760 Bld.CreateBr(MergeBB);
2761
2762 CGF.EmitBlock(MergeBB);
2763
2764 Address AddrNumWarpsArg = CGF.GetAddrOfLocalVar(&NumWarpsArg);
2765 llvm::Value *NumWarpsVal = CGF.EmitLoadOfScalar(
2766 AddrNumWarpsArg, /*Volatile=*/false, C.IntTy, SourceLocation());
2767
Alexey Bataev9ff80832018-04-16 20:16:21 +00002768 llvm::Value *NumActiveThreads = Bld.CreateNSWMul(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002769 NumWarpsVal, getNVPTXWarpSize(CGF), "num_active_threads");
2770 // named_barrier_sync(ParallelBarrierID, num_active_threads)
2771 syncParallelThreads(CGF, NumActiveThreads);
2772
2773 //
2774 // Warp 0 copies reduce element from transfer medium.
2775 //
2776 llvm::BasicBlock *W0ThenBB = CGF.createBasicBlock("then");
2777 llvm::BasicBlock *W0ElseBB = CGF.createBasicBlock("else");
2778 llvm::BasicBlock *W0MergeBB = CGF.createBasicBlock("ifcont");
2779
2780 // Up to 32 threads in warp 0 are active.
Alexey Bataev9ff80832018-04-16 20:16:21 +00002781 llvm::Value *IsActiveThread =
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002782 Bld.CreateICmpULT(ThreadID, NumWarpsVal, "is_active_thread");
2783 Bld.CreateCondBr(IsActiveThread, W0ThenBB, W0ElseBB);
2784
2785 CGF.EmitBlock(W0ThenBB);
2786
2787 // SrcMediumPtr = &medium[tid]
2788 llvm::Value *SrcMediumPtrVal = Bld.CreateInBoundsGEP(
2789 TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), ThreadID});
2790 Address SrcMediumPtr(SrcMediumPtrVal,
2791 C.getTypeAlignInChars(Private->getType()));
2792 // SrcMediumVal = *SrcMediumPtr;
2793 SrcMediumPtr = Bld.CreateElementBitCast(
2794 SrcMediumPtr, CGF.ConvertTypeForMem(Private->getType()));
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002795
2796 // TargetElemPtr = (type[i]*)(SrcDataAddr[i])
2797 Address TargetElemPtrPtr =
2798 Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
2799 llvm::Value *TargetElemPtrVal = CGF.EmitLoadOfScalar(
2800 TargetElemPtrPtr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
2801 Address TargetElemPtr =
2802 Address(TargetElemPtrVal, C.getTypeAlignInChars(Private->getType()));
2803 TargetElemPtr = Bld.CreateElementBitCast(
2804 TargetElemPtr, CGF.ConvertTypeForMem(Private->getType()));
2805
2806 // *TargetElemPtr = SrcMediumVal;
Alexey Bataev12c62902018-06-22 19:10:38 +00002807 if (Private->getType()->isScalarType()) {
2808 llvm::Value *SrcMediumValue = CGF.EmitLoadOfScalar(
2809 SrcMediumPtr, /*Volatile=*/false, Private->getType(), Loc);
2810 CGF.EmitStoreOfScalar(SrcMediumValue, TargetElemPtr, /*Volatile=*/false,
2811 Private->getType());
2812 } else {
2813 CGF.EmitAggregateCopy(
2814 CGF.MakeAddrLValue(SrcMediumPtr, Private->getType()),
2815 CGF.MakeAddrLValue(TargetElemPtr, Private->getType()),
2816 Private->getType(), AggValueSlot::DoesNotOverlap);
2817 }
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002818 Bld.CreateBr(W0MergeBB);
2819
2820 CGF.EmitBlock(W0ElseBB);
2821 Bld.CreateBr(W0MergeBB);
2822
2823 CGF.EmitBlock(W0MergeBB);
2824
2825 // While warp 0 copies values from transfer medium, all other warps must
2826 // wait.
2827 syncParallelThreads(CGF, NumActiveThreads);
Alexey Bataev9ff80832018-04-16 20:16:21 +00002828 ++Idx;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002829 }
2830
2831 CGF.FinishFunction();
2832 return Fn;
2833}
2834
2835/// Emit a helper that reduces data across two OpenMP threads (lanes)
2836/// in the same warp. It uses shuffle instructions to copy over data from
2837/// a remote lane's stack. The reduction algorithm performed is specified
2838/// by the fourth parameter.
2839///
2840/// Algorithm Versions.
2841/// Full Warp Reduce (argument value 0):
2842/// This algorithm assumes that all 32 lanes are active and gathers
2843/// data from these 32 lanes, producing a single resultant value.
2844/// Contiguous Partial Warp Reduce (argument value 1):
2845/// This algorithm assumes that only a *contiguous* subset of lanes
2846/// are active. This happens for the last warp in a parallel region
2847/// when the user specified num_threads is not an integer multiple of
2848/// 32. This contiguous subset always starts with the zeroth lane.
2849/// Partial Warp Reduce (argument value 2):
2850/// This algorithm gathers data from any number of lanes at any position.
2851/// All reduced values are stored in the lowest possible lane. The set
2852/// of problems every algorithm addresses is a super set of those
2853/// addressable by algorithms with a lower version number. Overhead
2854/// increases as algorithm version increases.
2855///
2856/// Terminology
2857/// Reduce element:
2858/// Reduce element refers to the individual data field with primitive
2859/// data types to be combined and reduced across threads.
2860/// Reduce list:
2861/// Reduce list refers to a collection of local, thread-private
2862/// reduce elements.
2863/// Remote Reduce list:
2864/// Remote Reduce list refers to a collection of remote (relative to
2865/// the current thread) reduce elements.
2866///
2867/// We distinguish between three states of threads that are important to
2868/// the implementation of this function.
2869/// Alive threads:
2870/// Threads in a warp executing the SIMT instruction, as distinguished from
2871/// threads that are inactive due to divergent control flow.
2872/// Active threads:
2873/// The minimal set of threads that has to be alive upon entry to this
2874/// function. The computation is correct iff active threads are alive.
2875/// Some threads are alive but they are not active because they do not
2876/// contribute to the computation in any useful manner. Turning them off
2877/// may introduce control flow overheads without any tangible benefits.
2878/// Effective threads:
2879/// In order to comply with the argument requirements of the shuffle
2880/// function, we must keep all lanes holding data alive. But at most
2881/// half of them perform value aggregation; we refer to this half of
2882/// threads as effective. The other half is simply handing off their
2883/// data.
2884///
2885/// Procedure
2886/// Value shuffle:
2887/// In this step active threads transfer data from higher lane positions
2888/// in the warp to lower lane positions, creating Remote Reduce list.
2889/// Value aggregation:
2890/// In this step, effective threads combine their thread local Reduce list
2891/// with Remote Reduce list and store the result in the thread local
2892/// Reduce list.
2893/// Value copy:
2894/// In this step, we deal with the assumption made by algorithm 2
2895/// (i.e. contiguity assumption). When we have an odd number of lanes
2896/// active, say 2k+1, only k threads will be effective and therefore k
2897/// new values will be produced. However, the Reduce list owned by the
2898/// (2k+1)th thread is ignored in the value aggregation. Therefore
2899/// we copy the Reduce list from the (2k+1)th lane to (k+1)th lane so
2900/// that the contiguity assumption still holds.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002901static llvm::Value *emitShuffleAndReduceFunction(
2902 CodeGenModule &CGM, ArrayRef<const Expr *> Privates,
2903 QualType ReductionArrayTy, llvm::Value *ReduceFn, SourceLocation Loc) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00002904 ASTContext &C = CGM.getContext();
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002905
2906 // Thread local Reduce list used to host the values of data to be reduced.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002907 ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2908 C.VoidPtrTy, ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002909 // Current lane id; could be logical.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002910 ImplicitParamDecl LaneIDArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr, C.ShortTy,
2911 ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002912 // Offset of the remote source lane relative to the current lane.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002913 ImplicitParamDecl RemoteLaneOffsetArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2914 C.ShortTy, ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002915 // Algorithm version. This is expected to be known at compile time.
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002916 ImplicitParamDecl AlgoVerArg(C, /*DC=*/nullptr, Loc, /*Id=*/nullptr,
2917 C.ShortTy, ImplicitParamDecl::Other);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002918 FunctionArgList Args;
2919 Args.push_back(&ReduceListArg);
2920 Args.push_back(&LaneIDArg);
2921 Args.push_back(&RemoteLaneOffsetArg);
2922 Args.push_back(&AlgoVerArg);
2923
Alexey Bataev9ff80832018-04-16 20:16:21 +00002924 const CGFunctionInfo &CGFI =
2925 CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002926 auto *Fn = llvm::Function::Create(
2927 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
2928 "_omp_reduction_shuffle_and_reduce_func", &CGM.getModule());
Rafael Espindola51ec5a92018-02-28 23:46:35 +00002929 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00002930 Fn->setDoesNotRecurse();
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002931 CodeGenFunction CGF(CGM);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00002932 CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args, Loc, Loc);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002933
Alexey Bataev9ff80832018-04-16 20:16:21 +00002934 CGBuilderTy &Bld = CGF.Builder;
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002935
2936 Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
2937 Address LocalReduceList(
2938 Bld.CreatePointerBitCastOrAddrSpaceCast(
2939 CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
2940 C.VoidPtrTy, SourceLocation()),
2941 CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
2942 CGF.getPointerAlign());
2943
2944 Address AddrLaneIDArg = CGF.GetAddrOfLocalVar(&LaneIDArg);
2945 llvm::Value *LaneIDArgVal = CGF.EmitLoadOfScalar(
2946 AddrLaneIDArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
2947
2948 Address AddrRemoteLaneOffsetArg = CGF.GetAddrOfLocalVar(&RemoteLaneOffsetArg);
2949 llvm::Value *RemoteLaneOffsetArgVal = CGF.EmitLoadOfScalar(
2950 AddrRemoteLaneOffsetArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
2951
2952 Address AddrAlgoVerArg = CGF.GetAddrOfLocalVar(&AlgoVerArg);
2953 llvm::Value *AlgoVerArgVal = CGF.EmitLoadOfScalar(
2954 AddrAlgoVerArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
2955
2956 // Create a local thread-private variable to host the Reduce list
2957 // from a remote lane.
2958 Address RemoteReduceList =
2959 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_reduce_list");
2960
2961 // This loop iterates through the list of reduce elements and copies,
2962 // element by element, from a remote lane in the warp to RemoteReduceList,
2963 // hosted on the thread's stack.
2964 emitReductionListCopy(RemoteLaneToThread, CGF, ReductionArrayTy, Privates,
2965 LocalReduceList, RemoteReduceList,
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00002966 {/*RemoteLaneOffset=*/RemoteLaneOffsetArgVal,
2967 /*ScratchpadIndex=*/nullptr,
2968 /*ScratchpadWidth=*/nullptr});
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002969
2970 // The actions to be performed on the Remote Reduce list is dependent
2971 // on the algorithm version.
2972 //
2973 // if (AlgoVer==0) || (AlgoVer==1 && (LaneId < Offset)) || (AlgoVer==2 &&
2974 // LaneId % 2 == 0 && Offset > 0):
2975 // do the reduction value aggregation
2976 //
2977 // The thread local variable Reduce list is mutated in place to host the
2978 // reduced data, which is the aggregated value produced from local and
2979 // remote lanes.
2980 //
2981 // Note that AlgoVer is expected to be a constant integer known at compile
2982 // time.
2983 // When AlgoVer==0, the first conjunction evaluates to true, making
2984 // the entire predicate true during compile time.
2985 // When AlgoVer==1, the second conjunction has only the second part to be
2986 // evaluated during runtime. Other conjunctions evaluates to false
2987 // during compile time.
2988 // When AlgoVer==2, the third conjunction has only the second part to be
2989 // evaluated during runtime. Other conjunctions evaluates to false
2990 // during compile time.
Alexey Bataev9ff80832018-04-16 20:16:21 +00002991 llvm::Value *CondAlgo0 = Bld.CreateIsNull(AlgoVerArgVal);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002992
Alexey Bataev9ff80832018-04-16 20:16:21 +00002993 llvm::Value *Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
2994 llvm::Value *CondAlgo1 = Bld.CreateAnd(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00002995 Algo1, Bld.CreateICmpULT(LaneIDArgVal, RemoteLaneOffsetArgVal));
2996
Alexey Bataev9ff80832018-04-16 20:16:21 +00002997 llvm::Value *Algo2 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(2));
2998 llvm::Value *CondAlgo2 = Bld.CreateAnd(
2999 Algo2, Bld.CreateIsNull(Bld.CreateAnd(LaneIDArgVal, Bld.getInt16(1))));
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003000 CondAlgo2 = Bld.CreateAnd(
3001 CondAlgo2, Bld.CreateICmpSGT(RemoteLaneOffsetArgVal, Bld.getInt16(0)));
3002
Alexey Bataev9ff80832018-04-16 20:16:21 +00003003 llvm::Value *CondReduce = Bld.CreateOr(CondAlgo0, CondAlgo1);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003004 CondReduce = Bld.CreateOr(CondReduce, CondAlgo2);
3005
3006 llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
3007 llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
3008 llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
3009 Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
3010
3011 CGF.EmitBlock(ThenBB);
3012 // reduce_function(LocalReduceList, RemoteReduceList)
3013 llvm::Value *LocalReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
3014 LocalReduceList.getPointer(), CGF.VoidPtrTy);
3015 llvm::Value *RemoteReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
3016 RemoteReduceList.getPointer(), CGF.VoidPtrTy);
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003017 CGM.getOpenMPRuntime().emitOutlinedFunctionCall(
3018 CGF, Loc, ReduceFn, {LocalReduceListPtr, RemoteReduceListPtr});
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003019 Bld.CreateBr(MergeBB);
3020
3021 CGF.EmitBlock(ElseBB);
3022 Bld.CreateBr(MergeBB);
3023
3024 CGF.EmitBlock(MergeBB);
3025
3026 // if (AlgoVer==1 && (LaneId >= Offset)) copy Remote Reduce list to local
3027 // Reduce list.
3028 Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
Alexey Bataev9ff80832018-04-16 20:16:21 +00003029 llvm::Value *CondCopy = Bld.CreateAnd(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003030 Algo1, Bld.CreateICmpUGE(LaneIDArgVal, RemoteLaneOffsetArgVal));
3031
3032 llvm::BasicBlock *CpyThenBB = CGF.createBasicBlock("then");
3033 llvm::BasicBlock *CpyElseBB = CGF.createBasicBlock("else");
3034 llvm::BasicBlock *CpyMergeBB = CGF.createBasicBlock("ifcont");
3035 Bld.CreateCondBr(CondCopy, CpyThenBB, CpyElseBB);
3036
3037 CGF.EmitBlock(CpyThenBB);
3038 emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
3039 RemoteReduceList, LocalReduceList);
3040 Bld.CreateBr(CpyMergeBB);
3041
3042 CGF.EmitBlock(CpyElseBB);
3043 Bld.CreateBr(CpyMergeBB);
3044
3045 CGF.EmitBlock(CpyMergeBB);
3046
3047 CGF.FinishFunction();
3048 return Fn;
3049}
3050
3051///
3052/// Design of OpenMP reductions on the GPU
3053///
3054/// Consider a typical OpenMP program with one or more reduction
3055/// clauses:
3056///
3057/// float foo;
3058/// double bar;
3059/// #pragma omp target teams distribute parallel for \
3060/// reduction(+:foo) reduction(*:bar)
3061/// for (int i = 0; i < N; i++) {
3062/// foo += A[i]; bar *= B[i];
3063/// }
3064///
3065/// where 'foo' and 'bar' are reduced across all OpenMP threads in
3066/// all teams. In our OpenMP implementation on the NVPTX device an
3067/// OpenMP team is mapped to a CUDA threadblock and OpenMP threads
3068/// within a team are mapped to CUDA threads within a threadblock.
3069/// Our goal is to efficiently aggregate values across all OpenMP
3070/// threads such that:
3071///
3072/// - the compiler and runtime are logically concise, and
3073/// - the reduction is performed efficiently in a hierarchical
3074/// manner as follows: within OpenMP threads in the same warp,
3075/// across warps in a threadblock, and finally across teams on
3076/// the NVPTX device.
3077///
3078/// Introduction to Decoupling
3079///
3080/// We would like to decouple the compiler and the runtime so that the
3081/// latter is ignorant of the reduction variables (number, data types)
3082/// and the reduction operators. This allows a simpler interface
3083/// and implementation while still attaining good performance.
3084///
3085/// Pseudocode for the aforementioned OpenMP program generated by the
3086/// compiler is as follows:
3087///
3088/// 1. Create private copies of reduction variables on each OpenMP
3089/// thread: 'foo_private', 'bar_private'
3090/// 2. Each OpenMP thread reduces the chunk of 'A' and 'B' assigned
3091/// to it and writes the result in 'foo_private' and 'bar_private'
3092/// respectively.
3093/// 3. Call the OpenMP runtime on the GPU to reduce within a team
3094/// and store the result on the team master:
3095///
3096/// __kmpc_nvptx_parallel_reduce_nowait(...,
3097/// reduceData, shuffleReduceFn, interWarpCpyFn)
3098///
3099/// where:
3100/// struct ReduceData {
3101/// double *foo;
3102/// double *bar;
3103/// } reduceData
3104/// reduceData.foo = &foo_private
3105/// reduceData.bar = &bar_private
3106///
3107/// 'shuffleReduceFn' and 'interWarpCpyFn' are pointers to two
3108/// auxiliary functions generated by the compiler that operate on
3109/// variables of type 'ReduceData'. They aid the runtime perform
3110/// algorithmic steps in a data agnostic manner.
3111///
3112/// 'shuffleReduceFn' is a pointer to a function that reduces data
3113/// of type 'ReduceData' across two OpenMP threads (lanes) in the
3114/// same warp. It takes the following arguments as input:
3115///
3116/// a. variable of type 'ReduceData' on the calling lane,
3117/// b. its lane_id,
3118/// c. an offset relative to the current lane_id to generate a
3119/// remote_lane_id. The remote lane contains the second
3120/// variable of type 'ReduceData' that is to be reduced.
3121/// d. an algorithm version parameter determining which reduction
3122/// algorithm to use.
3123///
3124/// 'shuffleReduceFn' retrieves data from the remote lane using
3125/// efficient GPU shuffle intrinsics and reduces, using the
3126/// algorithm specified by the 4th parameter, the two operands
3127/// element-wise. The result is written to the first operand.
3128///
3129/// Different reduction algorithms are implemented in different
3130/// runtime functions, all calling 'shuffleReduceFn' to perform
3131/// the essential reduction step. Therefore, based on the 4th
3132/// parameter, this function behaves slightly differently to
3133/// cooperate with the runtime to ensure correctness under
3134/// different circumstances.
3135///
3136/// 'InterWarpCpyFn' is a pointer to a function that transfers
3137/// reduced variables across warps. It tunnels, through CUDA
3138/// shared memory, the thread-private data of type 'ReduceData'
3139/// from lane 0 of each warp to a lane in the first warp.
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003140/// 4. Call the OpenMP runtime on the GPU to reduce across teams.
3141/// The last team writes the global reduced value to memory.
3142///
3143/// ret = __kmpc_nvptx_teams_reduce_nowait(...,
3144/// reduceData, shuffleReduceFn, interWarpCpyFn,
3145/// scratchpadCopyFn, loadAndReduceFn)
3146///
3147/// 'scratchpadCopyFn' is a helper that stores reduced
3148/// data from the team master to a scratchpad array in
3149/// global memory.
3150///
3151/// 'loadAndReduceFn' is a helper that loads data from
3152/// the scratchpad array and reduces it with the input
3153/// operand.
3154///
3155/// These compiler generated functions hide address
3156/// calculation and alignment information from the runtime.
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003157/// 5. if ret == 1:
3158/// The team master of the last team stores the reduced
3159/// result to the globals in memory.
3160/// foo += reduceData.foo; bar *= reduceData.bar
3161///
3162///
3163/// Warp Reduction Algorithms
3164///
3165/// On the warp level, we have three algorithms implemented in the
3166/// OpenMP runtime depending on the number of active lanes:
3167///
3168/// Full Warp Reduction
3169///
3170/// The reduce algorithm within a warp where all lanes are active
3171/// is implemented in the runtime as follows:
3172///
3173/// full_warp_reduce(void *reduce_data,
3174/// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
3175/// for (int offset = WARPSIZE/2; offset > 0; offset /= 2)
3176/// ShuffleReduceFn(reduce_data, 0, offset, 0);
3177/// }
3178///
3179/// The algorithm completes in log(2, WARPSIZE) steps.
3180///
3181/// 'ShuffleReduceFn' is used here with lane_id set to 0 because it is
3182/// not used therefore we save instructions by not retrieving lane_id
3183/// from the corresponding special registers. The 4th parameter, which
3184/// represents the version of the algorithm being used, is set to 0 to
3185/// signify full warp reduction.
3186///
3187/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
3188///
3189/// #reduce_elem refers to an element in the local lane's data structure
3190/// #remote_elem is retrieved from a remote lane
3191/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
3192/// reduce_elem = reduce_elem REDUCE_OP remote_elem;
3193///
3194/// Contiguous Partial Warp Reduction
3195///
3196/// This reduce algorithm is used within a warp where only the first
3197/// 'n' (n <= WARPSIZE) lanes are active. It is typically used when the
3198/// number of OpenMP threads in a parallel region is not a multiple of
3199/// WARPSIZE. The algorithm is implemented in the runtime as follows:
3200///
3201/// void
3202/// contiguous_partial_reduce(void *reduce_data,
3203/// kmp_ShuffleReductFctPtr ShuffleReduceFn,
3204/// int size, int lane_id) {
3205/// int curr_size;
3206/// int offset;
3207/// curr_size = size;
3208/// mask = curr_size/2;
3209/// while (offset>0) {
3210/// ShuffleReduceFn(reduce_data, lane_id, offset, 1);
3211/// curr_size = (curr_size+1)/2;
3212/// offset = curr_size/2;
3213/// }
3214/// }
3215///
3216/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
3217///
3218/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
3219/// if (lane_id < offset)
3220/// reduce_elem = reduce_elem REDUCE_OP remote_elem
3221/// else
3222/// reduce_elem = remote_elem
3223///
3224/// This algorithm assumes that the data to be reduced are located in a
3225/// contiguous subset of lanes starting from the first. When there is
3226/// an odd number of active lanes, the data in the last lane is not
3227/// aggregated with any other lane's dat but is instead copied over.
3228///
3229/// Dispersed Partial Warp Reduction
3230///
3231/// This algorithm is used within a warp when any discontiguous subset of
3232/// lanes are active. It is used to implement the reduction operation
3233/// across lanes in an OpenMP simd region or in a nested parallel region.
3234///
3235/// void
3236/// dispersed_partial_reduce(void *reduce_data,
3237/// kmp_ShuffleReductFctPtr ShuffleReduceFn) {
3238/// int size, remote_id;
3239/// int logical_lane_id = number_of_active_lanes_before_me() * 2;
3240/// do {
3241/// remote_id = next_active_lane_id_right_after_me();
3242/// # the above function returns 0 of no active lane
3243/// # is present right after the current lane.
3244/// size = number_of_active_lanes_in_this_warp();
3245/// logical_lane_id /= 2;
3246/// ShuffleReduceFn(reduce_data, logical_lane_id,
3247/// remote_id-1-threadIdx.x, 2);
3248/// } while (logical_lane_id % 2 == 0 && size > 1);
3249/// }
3250///
3251/// There is no assumption made about the initial state of the reduction.
3252/// Any number of lanes (>=1) could be active at any position. The reduction
3253/// result is returned in the first active lane.
3254///
3255/// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
3256///
3257/// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
3258/// if (lane_id % 2 == 0 && offset > 0)
3259/// reduce_elem = reduce_elem REDUCE_OP remote_elem
3260/// else
3261/// reduce_elem = remote_elem
3262///
3263///
3264/// Intra-Team Reduction
3265///
3266/// This function, as implemented in the runtime call
3267/// '__kmpc_nvptx_parallel_reduce_nowait', aggregates data across OpenMP
3268/// threads in a team. It first reduces within a warp using the
3269/// aforementioned algorithms. We then proceed to gather all such
3270/// reduced values at the first warp.
3271///
3272/// The runtime makes use of the function 'InterWarpCpyFn', which copies
3273/// data from each of the "warp master" (zeroth lane of each warp, where
3274/// warp-reduced data is held) to the zeroth warp. This step reduces (in
3275/// a mathematical sense) the problem of reduction across warp masters in
3276/// a block to the problem of warp reduction.
3277///
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003278///
3279/// Inter-Team Reduction
3280///
3281/// Once a team has reduced its data to a single value, it is stored in
3282/// a global scratchpad array. Since each team has a distinct slot, this
3283/// can be done without locking.
3284///
3285/// The last team to write to the scratchpad array proceeds to reduce the
3286/// scratchpad array. One or more workers in the last team use the helper
3287/// 'loadAndReduceDataFn' to load and reduce values from the array, i.e.,
3288/// the k'th worker reduces every k'th element.
3289///
3290/// Finally, a call is made to '__kmpc_nvptx_parallel_reduce_nowait' to
3291/// reduce across workers and compute a globally reduced value.
3292///
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003293void CGOpenMPRuntimeNVPTX::emitReduction(
3294 CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
3295 ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
3296 ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
3297 if (!CGF.HaveInsertPoint())
3298 return;
3299
3300 bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003301 bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
Alexey Bataevfac26cf2018-05-02 20:03:27 +00003302 bool SimdReduction = isOpenMPSimdDirective(Options.ReductionKind);
3303 assert((TeamsReduction || ParallelReduction || SimdReduction) &&
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003304 "Invalid reduction selection in emitReduction.");
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003305
Alexey Bataev7b55d2d2018-06-18 17:11:45 +00003306 if (Options.SimpleReduction) {
3307 CGOpenMPRuntime::emitReduction(CGF, Loc, Privates, LHSExprs, RHSExprs,
3308 ReductionOps, Options);
3309 return;
3310 }
3311
Alexey Bataev9ff80832018-04-16 20:16:21 +00003312 ASTContext &C = CGM.getContext();
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003313
3314 // 1. Build a list of reduction variables.
3315 // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
3316 auto Size = RHSExprs.size();
Alexey Bataev9ff80832018-04-16 20:16:21 +00003317 for (const Expr *E : Privates) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003318 if (E->getType()->isVariablyModifiedType())
3319 // Reserve place for array size.
3320 ++Size;
3321 }
3322 llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
3323 QualType ReductionArrayTy =
3324 C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
3325 /*IndexTypeQuals=*/0);
3326 Address ReductionList =
3327 CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
3328 auto IPriv = Privates.begin();
3329 unsigned Idx = 0;
3330 for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
3331 Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
3332 CGF.getPointerSize());
3333 CGF.Builder.CreateStore(
3334 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3335 CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
3336 Elem);
3337 if ((*IPriv)->getType()->isVariablyModifiedType()) {
3338 // Store array size.
3339 ++Idx;
3340 Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
3341 CGF.getPointerSize());
3342 llvm::Value *Size = CGF.Builder.CreateIntCast(
3343 CGF.getVLASize(
3344 CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
Sander de Smalen891af03a2018-02-03 13:55:59 +00003345 .NumElts,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003346 CGF.SizeTy, /*isSigned=*/false);
3347 CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
3348 Elem);
3349 }
3350 }
3351
3352 // 2. Emit reduce_func().
Alexey Bataev9ff80832018-04-16 20:16:21 +00003353 llvm::Value *ReductionFn = emitReductionFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003354 CGM, Loc, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(),
3355 Privates, LHSExprs, RHSExprs, ReductionOps);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003356
3357 // 4. Build res = __kmpc_reduce{_nowait}(<gtid>, <n>, sizeof(RedList),
3358 // RedList, shuffle_reduce_func, interwarp_copy_func);
Alexey Bataev9ff80832018-04-16 20:16:21 +00003359 llvm::Value *ThreadId = getThreadID(CGF, Loc);
3360 llvm::Value *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
3361 llvm::Value *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003362 ReductionList.getPointer(), CGF.VoidPtrTy);
3363
Alexey Bataev9ff80832018-04-16 20:16:21 +00003364 llvm::Value *ShuffleAndReduceFn = emitShuffleAndReduceFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003365 CGM, Privates, ReductionArrayTy, ReductionFn, Loc);
Alexey Bataev9ff80832018-04-16 20:16:21 +00003366 llvm::Value *InterWarpCopyFn =
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003367 emitInterWarpCopyFunction(CGM, Privates, ReductionArrayTy, Loc);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003368
Alexey Bataevfac26cf2018-05-02 20:03:27 +00003369 llvm::Value *Args[] = {ThreadId,
3370 CGF.Builder.getInt32(RHSExprs.size()),
3371 ReductionArrayTySize,
3372 RL,
3373 ShuffleAndReduceFn,
3374 InterWarpCopyFn};
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003375
Alexey Bataevfac26cf2018-05-02 20:03:27 +00003376 llvm::Value *Res = nullptr;
3377 if (ParallelReduction)
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003378 Res = CGF.EmitRuntimeCall(
3379 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_reduce_nowait),
3380 Args);
Alexey Bataevfac26cf2018-05-02 20:03:27 +00003381 else if (SimdReduction)
3382 Res = CGF.EmitRuntimeCall(
3383 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_simd_reduce_nowait),
3384 Args);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003385
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003386 if (TeamsReduction) {
Alexey Bataev9ff80832018-04-16 20:16:21 +00003387 llvm::Value *ScratchPadCopyFn =
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003388 emitCopyToScratchpad(CGM, Privates, ReductionArrayTy, Loc);
Alexey Bataev9ff80832018-04-16 20:16:21 +00003389 llvm::Value *LoadAndReduceFn = emitReduceScratchpadFunction(
Alexey Bataev7cae94e2018-01-04 19:45:16 +00003390 CGM, Privates, ReductionArrayTy, ReductionFn, Loc);
Arpith Chacko Jacobfc711b12017-02-16 16:48:49 +00003391
3392 llvm::Value *Args[] = {ThreadId,
3393 CGF.Builder.getInt32(RHSExprs.size()),
3394 ReductionArrayTySize,
3395 RL,
3396 ShuffleAndReduceFn,
3397 InterWarpCopyFn,
3398 ScratchPadCopyFn,
3399 LoadAndReduceFn};
3400 Res = CGF.EmitRuntimeCall(
3401 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_teams_reduce_nowait),
3402 Args);
3403 }
3404
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003405 // 5. Build switch(res)
Alexey Bataev9ff80832018-04-16 20:16:21 +00003406 llvm::BasicBlock *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
3407 llvm::SwitchInst *SwInst =
3408 CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/1);
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003409
3410 // 6. Build case 1: where we have reduced values in the master
3411 // thread in each team.
3412 // __kmpc_end_reduce{_nowait}(<gtid>);
3413 // break;
Alexey Bataev9ff80832018-04-16 20:16:21 +00003414 llvm::BasicBlock *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003415 SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
3416 CGF.EmitBlock(Case1BB);
3417
3418 // Add emission of __kmpc_end_reduce{_nowait}(<gtid>);
3419 llvm::Value *EndArgs[] = {ThreadId};
Alexey Bataev9ff80832018-04-16 20:16:21 +00003420 auto &&CodeGen = [Privates, LHSExprs, RHSExprs, ReductionOps,
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003421 this](CodeGenFunction &CGF, PrePostActionTy &Action) {
3422 auto IPriv = Privates.begin();
3423 auto ILHS = LHSExprs.begin();
3424 auto IRHS = RHSExprs.begin();
Alexey Bataev9ff80832018-04-16 20:16:21 +00003425 for (const Expr *E : ReductionOps) {
Arpith Chacko Jacob101e8fb2017-02-16 16:20:16 +00003426 emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
3427 cast<DeclRefExpr>(*IRHS));
3428 ++IPriv;
3429 ++ILHS;
3430 ++IRHS;
3431 }
3432 };
3433 RegionCodeGenTy RCG(CodeGen);
3434 NVPTXActionTy Action(
3435 nullptr, llvm::None,
3436 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_reduce_nowait),
3437 EndArgs);
3438 RCG.setAction(Action);
3439 RCG(CGF);
3440 CGF.EmitBranch(DefaultBB);
3441 CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
3442}
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003443
3444const VarDecl *
3445CGOpenMPRuntimeNVPTX::translateParameter(const FieldDecl *FD,
3446 const VarDecl *NativeParam) const {
3447 if (!NativeParam->getType()->isReferenceType())
3448 return NativeParam;
3449 QualType ArgType = NativeParam->getType();
3450 QualifierCollector QC;
3451 const Type *NonQualTy = QC.strip(ArgType);
3452 QualType PointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType();
3453 if (const auto *Attr = FD->getAttr<OMPCaptureKindAttr>()) {
3454 if (Attr->getCaptureKind() == OMPC_map) {
3455 PointeeTy = CGM.getContext().getAddrSpaceQualType(PointeeTy,
3456 LangAS::opencl_global);
3457 }
3458 }
3459 ArgType = CGM.getContext().getPointerType(PointeeTy);
3460 QC.addRestrict();
3461 enum { NVPTX_local_addr = 5 };
Alexander Richardson6d989432017-10-15 18:48:14 +00003462 QC.addAddressSpace(getLangASFromTargetAS(NVPTX_local_addr));
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003463 ArgType = QC.apply(CGM.getContext(), ArgType);
Alexey Bataev9ff80832018-04-16 20:16:21 +00003464 if (isa<ImplicitParamDecl>(NativeParam))
Alexey Bataevb45d43c2017-11-22 16:02:03 +00003465 return ImplicitParamDecl::Create(
3466 CGM.getContext(), /*DC=*/nullptr, NativeParam->getLocation(),
3467 NativeParam->getIdentifier(), ArgType, ImplicitParamDecl::Other);
Alexey Bataevb45d43c2017-11-22 16:02:03 +00003468 return ParmVarDecl::Create(
3469 CGM.getContext(),
3470 const_cast<DeclContext *>(NativeParam->getDeclContext()),
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003471 NativeParam->getBeginLoc(), NativeParam->getLocation(),
Alexey Bataevb45d43c2017-11-22 16:02:03 +00003472 NativeParam->getIdentifier(), ArgType,
3473 /*TInfo=*/nullptr, SC_None, /*DefArg=*/nullptr);
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003474}
3475
3476Address
3477CGOpenMPRuntimeNVPTX::getParameterAddress(CodeGenFunction &CGF,
3478 const VarDecl *NativeParam,
3479 const VarDecl *TargetParam) const {
3480 assert(NativeParam != TargetParam &&
3481 NativeParam->getType()->isReferenceType() &&
3482 "Native arg must not be the same as target arg.");
3483 Address LocalAddr = CGF.GetAddrOfLocalVar(TargetParam);
3484 QualType NativeParamType = NativeParam->getType();
3485 QualifierCollector QC;
3486 const Type *NonQualTy = QC.strip(NativeParamType);
3487 QualType NativePointeeTy = cast<ReferenceType>(NonQualTy)->getPointeeType();
3488 unsigned NativePointeeAddrSpace =
Alexander Richardson6d989432017-10-15 18:48:14 +00003489 CGF.getContext().getTargetAddressSpace(NativePointeeTy);
Alexey Bataev36f2c4d2017-09-13 20:20:59 +00003490 QualType TargetTy = TargetParam->getType();
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003491 llvm::Value *TargetAddr = CGF.EmitLoadOfScalar(
Alexey Bataev36f2c4d2017-09-13 20:20:59 +00003492 LocalAddr, /*Volatile=*/false, TargetTy, SourceLocation());
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003493 // First cast to generic.
3494 TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3495 TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo(
3496 /*AddrSpace=*/0));
3497 // Cast from generic to native address space.
3498 TargetAddr = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
3499 TargetAddr, TargetAddr->getType()->getPointerElementType()->getPointerTo(
3500 NativePointeeAddrSpace));
3501 Address NativeParamAddr = CGF.CreateMemTemp(NativeParamType);
3502 CGF.EmitStoreOfScalar(TargetAddr, NativeParamAddr, /*Volatile=*/false,
Alexey Bataev36f2c4d2017-09-13 20:20:59 +00003503 NativeParamType);
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003504 return NativeParamAddr;
3505}
3506
3507void CGOpenMPRuntimeNVPTX::emitOutlinedFunctionCall(
Alexey Bataev3c595a62017-08-14 15:01:03 +00003508 CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003509 ArrayRef<llvm::Value *> Args) const {
3510 SmallVector<llvm::Value *, 4> TargetArgs;
Alexey Bataev07ed94a2017-08-15 14:34:04 +00003511 TargetArgs.reserve(Args.size());
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003512 auto *FnType =
3513 cast<llvm::FunctionType>(OutlinedFn->getType()->getPointerElementType());
3514 for (unsigned I = 0, E = Args.size(); I < E; ++I) {
Alexey Bataev07ed94a2017-08-15 14:34:04 +00003515 if (FnType->isVarArg() && FnType->getNumParams() <= I) {
3516 TargetArgs.append(std::next(Args.begin(), I), Args.end());
3517 break;
3518 }
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003519 llvm::Type *TargetType = FnType->getParamType(I);
3520 llvm::Value *NativeArg = Args[I];
3521 if (!TargetType->isPointerTy()) {
3522 TargetArgs.emplace_back(NativeArg);
3523 continue;
3524 }
3525 llvm::Value *TargetArg = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
Alexey Bataevc99042b2018-03-15 18:10:54 +00003526 NativeArg,
3527 NativeArg->getType()->getPointerElementType()->getPointerTo());
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003528 TargetArgs.emplace_back(
3529 CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(TargetArg, TargetType));
3530 }
Alexey Bataev3c595a62017-08-14 15:01:03 +00003531 CGOpenMPRuntime::emitOutlinedFunctionCall(CGF, Loc, OutlinedFn, TargetArgs);
Alexey Bataev3b8d5582017-08-08 18:04:06 +00003532}
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003533
3534/// Emit function which wraps the outline parallel region
3535/// and controls the arguments which are passed to this function.
3536/// The wrapper ensures that the outlined function is called
3537/// with the correct arguments when data is shared.
3538llvm::Function *CGOpenMPRuntimeNVPTX::createParallelDataSharingWrapper(
3539 llvm::Function *OutlinedParallelFn, const OMPExecutableDirective &D) {
3540 ASTContext &Ctx = CGM.getContext();
3541 const auto &CS = *D.getCapturedStmt(OMPD_parallel);
3542
3543 // Create a function that takes as argument the source thread.
3544 FunctionArgList WrapperArgs;
3545 QualType Int16QTy =
3546 Ctx.getIntTypeForBitwidth(/*DestWidth=*/16, /*Signed=*/false);
3547 QualType Int32QTy =
3548 Ctx.getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/false);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003549 ImplicitParamDecl ParallelLevelArg(Ctx, /*DC=*/nullptr, D.getBeginLoc(),
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003550 /*Id=*/nullptr, Int16QTy,
3551 ImplicitParamDecl::Other);
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003552 ImplicitParamDecl WrapperArg(Ctx, /*DC=*/nullptr, D.getBeginLoc(),
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003553 /*Id=*/nullptr, Int32QTy,
3554 ImplicitParamDecl::Other);
3555 WrapperArgs.emplace_back(&ParallelLevelArg);
3556 WrapperArgs.emplace_back(&WrapperArg);
3557
Alexey Bataev9ff80832018-04-16 20:16:21 +00003558 const CGFunctionInfo &CGFI =
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003559 CGM.getTypes().arrangeBuiltinFunctionDeclaration(Ctx.VoidTy, WrapperArgs);
3560
3561 auto *Fn = llvm::Function::Create(
3562 CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
Alexey Bataev9ff80832018-04-16 20:16:21 +00003563 Twine(OutlinedParallelFn->getName(), "_wrapper"), &CGM.getModule());
Alexey Bataevc99042b2018-03-15 18:10:54 +00003564 CGM.SetInternalFunctionAttributes(GlobalDecl(), Fn, CGFI);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003565 Fn->setLinkage(llvm::GlobalValue::InternalLinkage);
Alexey Bataevc0f879b2018-04-10 20:10:53 +00003566 Fn->setDoesNotRecurse();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003567
3568 CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
3569 CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, Fn, CGFI, WrapperArgs,
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003570 D.getBeginLoc(), D.getBeginLoc());
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003571
3572 const auto *RD = CS.getCapturedRecordDecl();
3573 auto CurField = RD->field_begin();
3574
Alexey Bataevb7f3cba2018-03-19 17:04:07 +00003575 Address ZeroAddr = CGF.CreateMemTemp(
3576 CGF.getContext().getIntTypeForBitwidth(/*DestWidth=*/32, /*Signed=*/1),
3577 /*Name*/ ".zero.addr");
3578 CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003579 // Get the array of arguments.
3580 SmallVector<llvm::Value *, 8> Args;
3581
Alexey Bataevb7f3cba2018-03-19 17:04:07 +00003582 Args.emplace_back(CGF.GetAddrOfLocalVar(&WrapperArg).getPointer());
3583 Args.emplace_back(ZeroAddr.getPointer());
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003584
3585 CGBuilderTy &Bld = CGF.Builder;
3586 auto CI = CS.capture_begin();
3587
3588 // Use global memory for data sharing.
3589 // Handle passing of global args to workers.
3590 Address GlobalArgs =
3591 CGF.CreateDefaultAlignTempAlloca(CGF.VoidPtrPtrTy, "global_args");
3592 llvm::Value *GlobalArgsPtr = GlobalArgs.getPointer();
3593 llvm::Value *DataSharingArgs[] = {GlobalArgsPtr};
3594 CGF.EmitRuntimeCall(
3595 createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_get_shared_variables),
3596 DataSharingArgs);
3597
3598 // Retrieve the shared variables from the list of references returned
3599 // by the runtime. Pass the variables to the outlined function.
Alexey Bataev17314212018-03-20 15:41:05 +00003600 Address SharedArgListAddress = Address::invalid();
3601 if (CS.capture_size() > 0 ||
3602 isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) {
3603 SharedArgListAddress = CGF.EmitLoadOfPointer(
3604 GlobalArgs, CGF.getContext()
3605 .getPointerType(CGF.getContext().getPointerType(
3606 CGF.getContext().VoidPtrTy))
3607 .castAs<PointerType>());
3608 }
3609 unsigned Idx = 0;
3610 if (isOpenMPLoopBoundSharingDirective(D.getDirectiveKind())) {
3611 Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx,
3612 CGF.getPointerSize());
3613 Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
3614 Src, CGF.SizeTy->getPointerTo());
3615 llvm::Value *LB = CGF.EmitLoadOfScalar(
3616 TypedAddress,
3617 /*Volatile=*/false,
3618 CGF.getContext().getPointerType(CGF.getContext().getSizeType()),
3619 cast<OMPLoopDirective>(D).getLowerBoundVariable()->getExprLoc());
3620 Args.emplace_back(LB);
3621 ++Idx;
3622 Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, Idx,
3623 CGF.getPointerSize());
3624 TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
3625 Src, CGF.SizeTy->getPointerTo());
3626 llvm::Value *UB = CGF.EmitLoadOfScalar(
3627 TypedAddress,
3628 /*Volatile=*/false,
3629 CGF.getContext().getPointerType(CGF.getContext().getSizeType()),
3630 cast<OMPLoopDirective>(D).getUpperBoundVariable()->getExprLoc());
3631 Args.emplace_back(UB);
3632 ++Idx;
3633 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003634 if (CS.capture_size() > 0) {
3635 ASTContext &CGFContext = CGF.getContext();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003636 for (unsigned I = 0, E = CS.capture_size(); I < E; ++I, ++CI, ++CurField) {
3637 QualType ElemTy = CurField->getType();
Alexey Bataev17314212018-03-20 15:41:05 +00003638 Address Src = Bld.CreateConstInBoundsGEP(SharedArgListAddress, I + Idx,
3639 CGF.getPointerSize());
3640 Address TypedAddress = Bld.CreatePointerBitCastOrAddrSpaceCast(
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003641 Src, CGF.ConvertTypeForMem(CGFContext.getPointerType(ElemTy)));
3642 llvm::Value *Arg = CGF.EmitLoadOfScalar(TypedAddress,
3643 /*Volatile=*/false,
3644 CGFContext.getPointerType(ElemTy),
3645 CI->getLocation());
Alexey Bataev2091ca62018-04-23 17:33:41 +00003646 if (CI->capturesVariableByCopy() &&
3647 !CI->getCapturedVar()->getType()->isAnyPointerType()) {
Alexey Bataev17314212018-03-20 15:41:05 +00003648 Arg = castValueToType(CGF, Arg, ElemTy, CGFContext.getUIntPtrType(),
3649 CI->getLocation());
3650 }
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003651 Args.emplace_back(Arg);
3652 }
3653 }
3654
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003655 emitOutlinedFunctionCall(CGF, D.getBeginLoc(), OutlinedParallelFn, Args);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003656 CGF.FinishFunction();
3657 return Fn;
3658}
3659
3660void CGOpenMPRuntimeNVPTX::emitFunctionProlog(CodeGenFunction &CGF,
3661 const Decl *D) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00003662 if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic)
3663 return;
3664
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003665 assert(D && "Expected function or captured|block decl.");
3666 assert(FunctionGlobalizedDecls.count(CGF.CurFn) == 0 &&
3667 "Function is registered already.");
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003668 const Stmt *Body = nullptr;
Alexey Bataevc99042b2018-03-15 18:10:54 +00003669 bool NeedToDelayGlobalization = false;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003670 if (const auto *FD = dyn_cast<FunctionDecl>(D)) {
3671 Body = FD->getBody();
3672 } else if (const auto *BD = dyn_cast<BlockDecl>(D)) {
3673 Body = BD->getBody();
3674 } else if (const auto *CD = dyn_cast<CapturedDecl>(D)) {
3675 Body = CD->getBody();
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003676 NeedToDelayGlobalization = CGF.CapturedStmtInfo->getKind() == CR_OpenMP;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003677 }
3678 if (!Body)
3679 return;
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003680 CheckVarsEscapingDeclContext VarChecker(CGF);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003681 VarChecker.Visit(Body);
3682 const RecordDecl *GlobalizedVarsRecord = VarChecker.getGlobalizedRecord();
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003683 ArrayRef<const ValueDecl *> EscapedVariableLengthDecls =
3684 VarChecker.getEscapedVariableLengthDecls();
3685 if (!GlobalizedVarsRecord && EscapedVariableLengthDecls.empty())
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003686 return;
Alexey Bataevc99042b2018-03-15 18:10:54 +00003687 auto I = FunctionGlobalizedDecls.try_emplace(CGF.CurFn).first;
3688 I->getSecond().MappedParams =
3689 llvm::make_unique<CodeGenFunction::OMPMapVars>();
3690 I->getSecond().GlobalRecord = GlobalizedVarsRecord;
3691 I->getSecond().EscapedParameters.insert(
3692 VarChecker.getEscapedParameters().begin(),
3693 VarChecker.getEscapedParameters().end());
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003694 I->getSecond().EscapedVariableLengthDecls.append(
3695 EscapedVariableLengthDecls.begin(), EscapedVariableLengthDecls.end());
Alexey Bataevc99042b2018-03-15 18:10:54 +00003696 DeclToAddrMapTy &Data = I->getSecond().LocalVarData;
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003697 for (const ValueDecl *VD : VarChecker.getEscapedDecls()) {
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003698 assert(VD->isCanonicalDecl() && "Expected canonical declaration");
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003699 const FieldDecl *FD = VarChecker.getFieldForGlobalizedVar(VD);
Alexey Bataevc99042b2018-03-15 18:10:54 +00003700 Data.insert(std::make_pair(VD, std::make_pair(FD, Address::invalid())));
3701 }
3702 if (!NeedToDelayGlobalization) {
Stephen Kellyf2ceec42018-08-09 21:08:08 +00003703 emitGenericVarsProlog(CGF, D->getBeginLoc());
Alexey Bataevc99042b2018-03-15 18:10:54 +00003704 struct GlobalizationScope final : EHScopeStack::Cleanup {
3705 GlobalizationScope() = default;
3706
3707 void Emit(CodeGenFunction &CGF, Flags flags) override {
3708 static_cast<CGOpenMPRuntimeNVPTX &>(CGF.CGM.getOpenMPRuntime())
3709 .emitGenericVarsEpilog(CGF);
3710 }
3711 };
3712 CGF.EHStack.pushCleanup<GlobalizationScope>(NormalAndEHCleanup);
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003713 }
3714}
3715
3716Address CGOpenMPRuntimeNVPTX::getAddressOfLocalVariable(CodeGenFunction &CGF,
3717 const VarDecl *VD) {
Alexey Bataevd7ff6d62018-05-07 14:50:05 +00003718 if (getDataSharingMode(CGM) != CGOpenMPRuntimeNVPTX::Generic)
3719 return Address::invalid();
3720
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003721 VD = VD->getCanonicalDecl();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003722 auto I = FunctionGlobalizedDecls.find(CGF.CurFn);
3723 if (I == FunctionGlobalizedDecls.end())
3724 return Address::invalid();
Alexey Bataevc99042b2018-03-15 18:10:54 +00003725 auto VDI = I->getSecond().LocalVarData.find(VD);
Alexey Bataev63cc8e92018-03-20 14:45:59 +00003726 if (VDI != I->getSecond().LocalVarData.end())
3727 return VDI->second.second;
3728 if (VD->hasAttrs()) {
3729 for (specific_attr_iterator<OMPReferencedVarAttr> IT(VD->attr_begin()),
3730 E(VD->attr_end());
3731 IT != E; ++IT) {
3732 auto VDI = I->getSecond().LocalVarData.find(
3733 cast<VarDecl>(cast<DeclRefExpr>(IT->getRef())->getDecl())
3734 ->getCanonicalDecl());
3735 if (VDI != I->getSecond().LocalVarData.end())
3736 return VDI->second.second;
3737 }
3738 }
3739 return Address::invalid();
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003740}
3741
3742void CGOpenMPRuntimeNVPTX::functionFinished(CodeGenFunction &CGF) {
Gheorghe-Teodor Bercead3dcf2f2018-03-14 14:17:45 +00003743 FunctionGlobalizedDecls.erase(CGF.CurFn);
3744 CGOpenMPRuntime::functionFinished(CGF);
3745}