]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/CodeGen/CGOpenMPRuntimeNVPTX.cpp
Merge llvm, clang, lld, lldb, compiler-rt and libc++ r304149, and update
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / CodeGen / CGOpenMPRuntimeNVPTX.cpp
1 //===---- 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"
16 #include "clang/AST/DeclOpenMP.h"
17 #include "CodeGenFunction.h"
18 #include "clang/AST/StmtOpenMP.h"
19
20 using namespace clang;
21 using namespace CodeGen;
22
23 namespace {
24 enum OpenMPRTLFunctionNVPTX {
25   /// \brief Call to void __kmpc_kernel_init(kmp_int32 thread_limit);
26   OMPRTL_NVPTX__kmpc_kernel_init,
27   /// \brief Call to void __kmpc_kernel_deinit();
28   OMPRTL_NVPTX__kmpc_kernel_deinit,
29   /// \brief Call to void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
30   /// short RequiresOMPRuntime, short RequiresDataSharing);
31   OMPRTL_NVPTX__kmpc_spmd_kernel_init,
32   /// \brief Call to void __kmpc_spmd_kernel_deinit();
33   OMPRTL_NVPTX__kmpc_spmd_kernel_deinit,
34   /// \brief Call to void __kmpc_kernel_prepare_parallel(void
35   /// *outlined_function);
36   OMPRTL_NVPTX__kmpc_kernel_prepare_parallel,
37   /// \brief Call to bool __kmpc_kernel_parallel(void **outlined_function);
38   OMPRTL_NVPTX__kmpc_kernel_parallel,
39   /// \brief Call to void __kmpc_kernel_end_parallel();
40   OMPRTL_NVPTX__kmpc_kernel_end_parallel,
41   /// Call to void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
42   /// global_tid);
43   OMPRTL_NVPTX__kmpc_serialized_parallel,
44   /// Call to void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
45   /// global_tid);
46   OMPRTL_NVPTX__kmpc_end_serialized_parallel,
47   /// \brief Call to int32_t __kmpc_shuffle_int32(int32_t element,
48   /// int16_t lane_offset, int16_t warp_size);
49   OMPRTL_NVPTX__kmpc_shuffle_int32,
50   /// \brief Call to int64_t __kmpc_shuffle_int64(int64_t element,
51   /// int16_t lane_offset, int16_t warp_size);
52   OMPRTL_NVPTX__kmpc_shuffle_int64,
53   /// \brief Call to __kmpc_nvptx_parallel_reduce_nowait(kmp_int32
54   /// global_tid, kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
55   /// void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
56   /// lane_offset, int16_t shortCircuit),
57   /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num));
58   OMPRTL_NVPTX__kmpc_parallel_reduce_nowait,
59   /// \brief Call to __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
60   /// int32_t num_vars, size_t reduce_size, void *reduce_data,
61   /// void (*kmp_ShuffleReductFctPtr)(void *rhs, int16_t lane_id, int16_t
62   /// lane_offset, int16_t shortCircuit),
63   /// void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
64   /// void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
65   /// int32_t index, int32_t width),
66   /// void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad, int32_t
67   /// index, int32_t width, int32_t reduce))
68   OMPRTL_NVPTX__kmpc_teams_reduce_nowait,
69   /// \brief Call to __kmpc_nvptx_end_reduce_nowait(int32_t global_tid);
70   OMPRTL_NVPTX__kmpc_end_reduce_nowait
71 };
72
73 /// Pre(post)-action for different OpenMP constructs specialized for NVPTX.
74 class NVPTXActionTy final : public PrePostActionTy {
75   llvm::Value *EnterCallee;
76   ArrayRef<llvm::Value *> EnterArgs;
77   llvm::Value *ExitCallee;
78   ArrayRef<llvm::Value *> ExitArgs;
79   bool Conditional;
80   llvm::BasicBlock *ContBlock = nullptr;
81
82 public:
83   NVPTXActionTy(llvm::Value *EnterCallee, ArrayRef<llvm::Value *> EnterArgs,
84                 llvm::Value *ExitCallee, ArrayRef<llvm::Value *> ExitArgs,
85                 bool Conditional = false)
86       : EnterCallee(EnterCallee), EnterArgs(EnterArgs), ExitCallee(ExitCallee),
87         ExitArgs(ExitArgs), Conditional(Conditional) {}
88   void Enter(CodeGenFunction &CGF) override {
89     llvm::Value *EnterRes = CGF.EmitRuntimeCall(EnterCallee, EnterArgs);
90     if (Conditional) {
91       llvm::Value *CallBool = CGF.Builder.CreateIsNotNull(EnterRes);
92       auto *ThenBlock = CGF.createBasicBlock("omp_if.then");
93       ContBlock = CGF.createBasicBlock("omp_if.end");
94       // Generate the branch (If-stmt)
95       CGF.Builder.CreateCondBr(CallBool, ThenBlock, ContBlock);
96       CGF.EmitBlock(ThenBlock);
97     }
98   }
99   void Done(CodeGenFunction &CGF) {
100     // Emit the rest of blocks/branches
101     CGF.EmitBranch(ContBlock);
102     CGF.EmitBlock(ContBlock, true);
103   }
104   void Exit(CodeGenFunction &CGF) override {
105     CGF.EmitRuntimeCall(ExitCallee, ExitArgs);
106   }
107 };
108
109 // A class to track the execution mode when codegening directives within
110 // a target region. The appropriate mode (generic/spmd) is set on entry
111 // to the target region and used by containing directives such as 'parallel'
112 // to emit optimized code.
113 class ExecutionModeRAII {
114 private:
115   CGOpenMPRuntimeNVPTX::ExecutionMode SavedMode;
116   CGOpenMPRuntimeNVPTX::ExecutionMode &Mode;
117
118 public:
119   ExecutionModeRAII(CGOpenMPRuntimeNVPTX::ExecutionMode &Mode,
120                     CGOpenMPRuntimeNVPTX::ExecutionMode NewMode)
121       : Mode(Mode) {
122     SavedMode = Mode;
123     Mode = NewMode;
124   }
125   ~ExecutionModeRAII() { Mode = SavedMode; }
126 };
127
128 /// GPU Configuration:  This information can be derived from cuda registers,
129 /// however, providing compile time constants helps generate more efficient
130 /// code.  For all practical purposes this is fine because the configuration
131 /// is the same for all known NVPTX architectures.
132 enum MachineConfiguration : unsigned {
133   WarpSize = 32,
134   /// Number of bits required to represent a lane identifier, which is
135   /// computed as log_2(WarpSize).
136   LaneIDBits = 5,
137   LaneIDMask = WarpSize - 1,
138
139   /// Global memory alignment for performance.
140   GlobalMemoryAlignment = 256,
141 };
142
143 enum NamedBarrier : unsigned {
144   /// Synchronize on this barrier #ID using a named barrier primitive.
145   /// Only the subset of active threads in a parallel region arrive at the
146   /// barrier.
147   NB_Parallel = 1,
148 };
149 } // anonymous namespace
150
151 /// Get the GPU warp size.
152 static llvm::Value *getNVPTXWarpSize(CodeGenFunction &CGF) {
153   CGBuilderTy &Bld = CGF.Builder;
154   return Bld.CreateCall(
155       llvm::Intrinsic::getDeclaration(
156           &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_warpsize),
157       llvm::None, "nvptx_warp_size");
158 }
159
160 /// Get the id of the current thread on the GPU.
161 static llvm::Value *getNVPTXThreadID(CodeGenFunction &CGF) {
162   CGBuilderTy &Bld = CGF.Builder;
163   return Bld.CreateCall(
164       llvm::Intrinsic::getDeclaration(
165           &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_tid_x),
166       llvm::None, "nvptx_tid");
167 }
168
169 /// Get the id of the warp in the block.
170 /// We assume that the warp size is 32, which is always the case
171 /// on the NVPTX device, to generate more efficient code.
172 static llvm::Value *getNVPTXWarpID(CodeGenFunction &CGF) {
173   CGBuilderTy &Bld = CGF.Builder;
174   return Bld.CreateAShr(getNVPTXThreadID(CGF), LaneIDBits, "nvptx_warp_id");
175 }
176
177 /// Get the id of the current lane in the Warp.
178 /// We assume that the warp size is 32, which is always the case
179 /// on the NVPTX device, to generate more efficient code.
180 static llvm::Value *getNVPTXLaneID(CodeGenFunction &CGF) {
181   CGBuilderTy &Bld = CGF.Builder;
182   return Bld.CreateAnd(getNVPTXThreadID(CGF), Bld.getInt32(LaneIDMask),
183                        "nvptx_lane_id");
184 }
185
186 /// Get the maximum number of threads in a block of the GPU.
187 static llvm::Value *getNVPTXNumThreads(CodeGenFunction &CGF) {
188   CGBuilderTy &Bld = CGF.Builder;
189   return Bld.CreateCall(
190       llvm::Intrinsic::getDeclaration(
191           &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_read_ptx_sreg_ntid_x),
192       llvm::None, "nvptx_num_threads");
193 }
194
195 /// Get barrier to synchronize all threads in a block.
196 static void getNVPTXCTABarrier(CodeGenFunction &CGF) {
197   CGBuilderTy &Bld = CGF.Builder;
198   Bld.CreateCall(llvm::Intrinsic::getDeclaration(
199       &CGF.CGM.getModule(), llvm::Intrinsic::nvvm_barrier0));
200 }
201
202 /// Get barrier #ID to synchronize selected (multiple of warp size) threads in
203 /// a CTA.
204 static void getNVPTXBarrier(CodeGenFunction &CGF, int ID,
205                             llvm::Value *NumThreads) {
206   CGBuilderTy &Bld = CGF.Builder;
207   llvm::Value *Args[] = {Bld.getInt32(ID), NumThreads};
208   Bld.CreateCall(llvm::Intrinsic::getDeclaration(&CGF.CGM.getModule(),
209                                                  llvm::Intrinsic::nvvm_barrier),
210                  Args);
211 }
212
213 /// Synchronize all GPU threads in a block.
214 static void syncCTAThreads(CodeGenFunction &CGF) { getNVPTXCTABarrier(CGF); }
215
216 /// Synchronize worker threads in a parallel region.
217 static void syncParallelThreads(CodeGenFunction &CGF, llvm::Value *NumThreads) {
218   return getNVPTXBarrier(CGF, NB_Parallel, NumThreads);
219 }
220
221 /// Get the value of the thread_limit clause in the teams directive.
222 /// For the 'generic' execution mode, the runtime encodes thread_limit in
223 /// the launch parameters, always starting thread_limit+warpSize threads per
224 /// CTA. The threads in the last warp are reserved for master execution.
225 /// For the 'spmd' execution mode, all threads in a CTA are part of the team.
226 static llvm::Value *getThreadLimit(CodeGenFunction &CGF,
227                                    bool IsInSpmdExecutionMode = false) {
228   CGBuilderTy &Bld = CGF.Builder;
229   return IsInSpmdExecutionMode
230              ? getNVPTXNumThreads(CGF)
231              : Bld.CreateSub(getNVPTXNumThreads(CGF), getNVPTXWarpSize(CGF),
232                              "thread_limit");
233 }
234
235 /// Get the thread id of the OMP master thread.
236 /// The master thread id is the first thread (lane) of the last warp in the
237 /// GPU block.  Warp size is assumed to be some power of 2.
238 /// Thread id is 0 indexed.
239 /// E.g: If NumThreads is 33, master id is 32.
240 ///      If NumThreads is 64, master id is 32.
241 ///      If NumThreads is 1024, master id is 992.
242 static llvm::Value *getMasterThreadID(CodeGenFunction &CGF) {
243   CGBuilderTy &Bld = CGF.Builder;
244   llvm::Value *NumThreads = getNVPTXNumThreads(CGF);
245
246   // We assume that the warp size is a power of 2.
247   llvm::Value *Mask = Bld.CreateSub(getNVPTXWarpSize(CGF), Bld.getInt32(1));
248
249   return Bld.CreateAnd(Bld.CreateSub(NumThreads, Bld.getInt32(1)),
250                        Bld.CreateNot(Mask), "master_tid");
251 }
252
253 CGOpenMPRuntimeNVPTX::WorkerFunctionState::WorkerFunctionState(
254     CodeGenModule &CGM)
255     : WorkerFn(nullptr), CGFI(nullptr) {
256   createWorkerFunction(CGM);
257 }
258
259 void CGOpenMPRuntimeNVPTX::WorkerFunctionState::createWorkerFunction(
260     CodeGenModule &CGM) {
261   // Create an worker function with no arguments.
262   CGFI = &CGM.getTypes().arrangeNullaryFunction();
263
264   WorkerFn = llvm::Function::Create(
265       CGM.getTypes().GetFunctionType(*CGFI), llvm::GlobalValue::InternalLinkage,
266       /* placeholder */ "_worker", &CGM.getModule());
267   CGM.SetInternalFunctionAttributes(/*D=*/nullptr, WorkerFn, *CGFI);
268 }
269
270 bool CGOpenMPRuntimeNVPTX::isInSpmdExecutionMode() const {
271   return CurrentExecutionMode == CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd;
272 }
273
274 static CGOpenMPRuntimeNVPTX::ExecutionMode
275 getExecutionModeForDirective(CodeGenModule &CGM,
276                              const OMPExecutableDirective &D) {
277   OpenMPDirectiveKind DirectiveKind = D.getDirectiveKind();
278   switch (DirectiveKind) {
279   case OMPD_target:
280   case OMPD_target_teams:
281     return CGOpenMPRuntimeNVPTX::ExecutionMode::Generic;
282   case OMPD_target_parallel:
283     return CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd;
284   default:
285     llvm_unreachable("Unsupported directive on NVPTX device.");
286   }
287   llvm_unreachable("Unsupported directive on NVPTX device.");
288 }
289
290 void CGOpenMPRuntimeNVPTX::emitGenericKernel(const OMPExecutableDirective &D,
291                                              StringRef ParentName,
292                                              llvm::Function *&OutlinedFn,
293                                              llvm::Constant *&OutlinedFnID,
294                                              bool IsOffloadEntry,
295                                              const RegionCodeGenTy &CodeGen) {
296   ExecutionModeRAII ModeRAII(CurrentExecutionMode,
297                              CGOpenMPRuntimeNVPTX::ExecutionMode::Generic);
298   EntryFunctionState EST;
299   WorkerFunctionState WST(CGM);
300   Work.clear();
301
302   // Emit target region as a standalone region.
303   class NVPTXPrePostActionTy : public PrePostActionTy {
304     CGOpenMPRuntimeNVPTX &RT;
305     CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
306     CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST;
307
308   public:
309     NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX &RT,
310                          CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
311                          CGOpenMPRuntimeNVPTX::WorkerFunctionState &WST)
312         : RT(RT), EST(EST), WST(WST) {}
313     void Enter(CodeGenFunction &CGF) override {
314       RT.emitGenericEntryHeader(CGF, EST, WST);
315     }
316     void Exit(CodeGenFunction &CGF) override {
317       RT.emitGenericEntryFooter(CGF, EST);
318     }
319   } Action(*this, EST, WST);
320   CodeGen.setAction(Action);
321   emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
322                                    IsOffloadEntry, CodeGen);
323
324   // Create the worker function
325   emitWorkerFunction(WST);
326
327   // Now change the name of the worker function to correspond to this target
328   // region's entry function.
329   WST.WorkerFn->setName(OutlinedFn->getName() + "_worker");
330 }
331
332 // Setup NVPTX threads for master-worker OpenMP scheme.
333 void CGOpenMPRuntimeNVPTX::emitGenericEntryHeader(CodeGenFunction &CGF,
334                                                   EntryFunctionState &EST,
335                                                   WorkerFunctionState &WST) {
336   CGBuilderTy &Bld = CGF.Builder;
337
338   llvm::BasicBlock *WorkerBB = CGF.createBasicBlock(".worker");
339   llvm::BasicBlock *MasterCheckBB = CGF.createBasicBlock(".mastercheck");
340   llvm::BasicBlock *MasterBB = CGF.createBasicBlock(".master");
341   EST.ExitBB = CGF.createBasicBlock(".exit");
342
343   auto *IsWorker =
344       Bld.CreateICmpULT(getNVPTXThreadID(CGF), getThreadLimit(CGF));
345   Bld.CreateCondBr(IsWorker, WorkerBB, MasterCheckBB);
346
347   CGF.EmitBlock(WorkerBB);
348   CGF.EmitCallOrInvoke(WST.WorkerFn, llvm::None);
349   CGF.EmitBranch(EST.ExitBB);
350
351   CGF.EmitBlock(MasterCheckBB);
352   auto *IsMaster =
353       Bld.CreateICmpEQ(getNVPTXThreadID(CGF), getMasterThreadID(CGF));
354   Bld.CreateCondBr(IsMaster, MasterBB, EST.ExitBB);
355
356   CGF.EmitBlock(MasterBB);
357   // First action in sequential region:
358   // Initialize the state of the OpenMP runtime library on the GPU.
359   llvm::Value *Args[] = {getThreadLimit(CGF)};
360   CGF.EmitRuntimeCall(
361       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_init), Args);
362 }
363
364 void CGOpenMPRuntimeNVPTX::emitGenericEntryFooter(CodeGenFunction &CGF,
365                                                   EntryFunctionState &EST) {
366   if (!EST.ExitBB)
367     EST.ExitBB = CGF.createBasicBlock(".exit");
368
369   llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".termination.notifier");
370   CGF.EmitBranch(TerminateBB);
371
372   CGF.EmitBlock(TerminateBB);
373   // Signal termination condition.
374   CGF.EmitRuntimeCall(
375       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_deinit), None);
376   // Barrier to terminate worker threads.
377   syncCTAThreads(CGF);
378   // Master thread jumps to exit point.
379   CGF.EmitBranch(EST.ExitBB);
380
381   CGF.EmitBlock(EST.ExitBB);
382   EST.ExitBB = nullptr;
383 }
384
385 void CGOpenMPRuntimeNVPTX::emitSpmdKernel(const OMPExecutableDirective &D,
386                                           StringRef ParentName,
387                                           llvm::Function *&OutlinedFn,
388                                           llvm::Constant *&OutlinedFnID,
389                                           bool IsOffloadEntry,
390                                           const RegionCodeGenTy &CodeGen) {
391   ExecutionModeRAII ModeRAII(CurrentExecutionMode,
392                              CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd);
393   EntryFunctionState EST;
394
395   // Emit target region as a standalone region.
396   class NVPTXPrePostActionTy : public PrePostActionTy {
397     CGOpenMPRuntimeNVPTX &RT;
398     CGOpenMPRuntimeNVPTX::EntryFunctionState &EST;
399     const OMPExecutableDirective &D;
400
401   public:
402     NVPTXPrePostActionTy(CGOpenMPRuntimeNVPTX &RT,
403                          CGOpenMPRuntimeNVPTX::EntryFunctionState &EST,
404                          const OMPExecutableDirective &D)
405         : RT(RT), EST(EST), D(D) {}
406     void Enter(CodeGenFunction &CGF) override {
407       RT.emitSpmdEntryHeader(CGF, EST, D);
408     }
409     void Exit(CodeGenFunction &CGF) override {
410       RT.emitSpmdEntryFooter(CGF, EST);
411     }
412   } Action(*this, EST, D);
413   CodeGen.setAction(Action);
414   emitTargetOutlinedFunctionHelper(D, ParentName, OutlinedFn, OutlinedFnID,
415                                    IsOffloadEntry, CodeGen);
416   return;
417 }
418
419 void CGOpenMPRuntimeNVPTX::emitSpmdEntryHeader(
420     CodeGenFunction &CGF, EntryFunctionState &EST,
421     const OMPExecutableDirective &D) {
422   auto &Bld = CGF.Builder;
423
424   // Setup BBs in entry function.
425   llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute");
426   EST.ExitBB = CGF.createBasicBlock(".exit");
427
428   // Initialize the OMP state in the runtime; called by all active threads.
429   // TODO: Set RequiresOMPRuntime and RequiresDataSharing parameters
430   // based on code analysis of the target region.
431   llvm::Value *Args[] = {getThreadLimit(CGF, /*IsInSpmdExecutionMode=*/true),
432                          /*RequiresOMPRuntime=*/Bld.getInt16(1),
433                          /*RequiresDataSharing=*/Bld.getInt16(1)};
434   CGF.EmitRuntimeCall(
435       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_init), Args);
436   CGF.EmitBranch(ExecuteBB);
437
438   CGF.EmitBlock(ExecuteBB);
439 }
440
441 void CGOpenMPRuntimeNVPTX::emitSpmdEntryFooter(CodeGenFunction &CGF,
442                                                EntryFunctionState &EST) {
443   if (!EST.ExitBB)
444     EST.ExitBB = CGF.createBasicBlock(".exit");
445
446   llvm::BasicBlock *OMPDeInitBB = CGF.createBasicBlock(".omp.deinit");
447   CGF.EmitBranch(OMPDeInitBB);
448
449   CGF.EmitBlock(OMPDeInitBB);
450   // DeInitialize the OMP state in the runtime; called by all active threads.
451   CGF.EmitRuntimeCall(
452       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_spmd_kernel_deinit), None);
453   CGF.EmitBranch(EST.ExitBB);
454
455   CGF.EmitBlock(EST.ExitBB);
456   EST.ExitBB = nullptr;
457 }
458
459 // Create a unique global variable to indicate the execution mode of this target
460 // region. The execution mode is either 'generic', or 'spmd' depending on the
461 // target directive. This variable is picked up by the offload library to setup
462 // the device appropriately before kernel launch. If the execution mode is
463 // 'generic', the runtime reserves one warp for the master, otherwise, all
464 // warps participate in parallel work.
465 static void setPropertyExecutionMode(CodeGenModule &CGM, StringRef Name,
466                                      CGOpenMPRuntimeNVPTX::ExecutionMode Mode) {
467   (void)new llvm::GlobalVariable(
468       CGM.getModule(), CGM.Int8Ty, /*isConstant=*/true,
469       llvm::GlobalValue::WeakAnyLinkage,
470       llvm::ConstantInt::get(CGM.Int8Ty, Mode), Name + Twine("_exec_mode"));
471 }
472
473 void CGOpenMPRuntimeNVPTX::emitWorkerFunction(WorkerFunctionState &WST) {
474   auto &Ctx = CGM.getContext();
475
476   CodeGenFunction CGF(CGM, /*suppressNewContext=*/true);
477   CGF.disableDebugInfo();
478   CGF.StartFunction(GlobalDecl(), Ctx.VoidTy, WST.WorkerFn, *WST.CGFI, {});
479   emitWorkerLoop(CGF, WST);
480   CGF.FinishFunction();
481 }
482
483 void CGOpenMPRuntimeNVPTX::emitWorkerLoop(CodeGenFunction &CGF,
484                                           WorkerFunctionState &WST) {
485   //
486   // The workers enter this loop and wait for parallel work from the master.
487   // When the master encounters a parallel region it sets up the work + variable
488   // arguments, and wakes up the workers.  The workers first check to see if
489   // they are required for the parallel region, i.e., within the # of requested
490   // parallel threads.  The activated workers load the variable arguments and
491   // execute the parallel work.
492   //
493
494   CGBuilderTy &Bld = CGF.Builder;
495
496   llvm::BasicBlock *AwaitBB = CGF.createBasicBlock(".await.work");
497   llvm::BasicBlock *SelectWorkersBB = CGF.createBasicBlock(".select.workers");
498   llvm::BasicBlock *ExecuteBB = CGF.createBasicBlock(".execute.parallel");
499   llvm::BasicBlock *TerminateBB = CGF.createBasicBlock(".terminate.parallel");
500   llvm::BasicBlock *BarrierBB = CGF.createBasicBlock(".barrier.parallel");
501   llvm::BasicBlock *ExitBB = CGF.createBasicBlock(".exit");
502
503   CGF.EmitBranch(AwaitBB);
504
505   // Workers wait for work from master.
506   CGF.EmitBlock(AwaitBB);
507   // Wait for parallel work
508   syncCTAThreads(CGF);
509
510   Address WorkFn =
511       CGF.CreateDefaultAlignTempAlloca(CGF.Int8PtrTy, /*Name=*/"work_fn");
512   Address ExecStatus =
513       CGF.CreateDefaultAlignTempAlloca(CGF.Int8Ty, /*Name=*/"exec_status");
514   CGF.InitTempAlloca(ExecStatus, Bld.getInt8(/*C=*/0));
515   CGF.InitTempAlloca(WorkFn, llvm::Constant::getNullValue(CGF.Int8PtrTy));
516
517   llvm::Value *Args[] = {WorkFn.getPointer()};
518   llvm::Value *Ret = CGF.EmitRuntimeCall(
519       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_parallel), Args);
520   Bld.CreateStore(Bld.CreateZExt(Ret, CGF.Int8Ty), ExecStatus);
521
522   // On termination condition (workid == 0), exit loop.
523   llvm::Value *ShouldTerminate =
524       Bld.CreateIsNull(Bld.CreateLoad(WorkFn), "should_terminate");
525   Bld.CreateCondBr(ShouldTerminate, ExitBB, SelectWorkersBB);
526
527   // Activate requested workers.
528   CGF.EmitBlock(SelectWorkersBB);
529   llvm::Value *IsActive =
530       Bld.CreateIsNotNull(Bld.CreateLoad(ExecStatus), "is_active");
531   Bld.CreateCondBr(IsActive, ExecuteBB, BarrierBB);
532
533   // Signal start of parallel region.
534   CGF.EmitBlock(ExecuteBB);
535
536   // Process work items: outlined parallel functions.
537   for (auto *W : Work) {
538     // Try to match this outlined function.
539     auto *ID = Bld.CreatePointerBitCastOrAddrSpaceCast(W, CGM.Int8PtrTy);
540
541     llvm::Value *WorkFnMatch =
542         Bld.CreateICmpEQ(Bld.CreateLoad(WorkFn), ID, "work_match");
543
544     llvm::BasicBlock *ExecuteFNBB = CGF.createBasicBlock(".execute.fn");
545     llvm::BasicBlock *CheckNextBB = CGF.createBasicBlock(".check.next");
546     Bld.CreateCondBr(WorkFnMatch, ExecuteFNBB, CheckNextBB);
547
548     // Execute this outlined function.
549     CGF.EmitBlock(ExecuteFNBB);
550
551     // Insert call to work function.
552     // FIXME: Pass arguments to outlined function from master thread.
553     auto *Fn = cast<llvm::Function>(W);
554     Address ZeroAddr =
555         CGF.CreateDefaultAlignTempAlloca(CGF.Int32Ty, /*Name=*/".zero.addr");
556     CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C=*/0));
557     llvm::Value *FnArgs[] = {ZeroAddr.getPointer(), ZeroAddr.getPointer()};
558     CGF.EmitCallOrInvoke(Fn, FnArgs);
559
560     // Go to end of parallel region.
561     CGF.EmitBranch(TerminateBB);
562
563     CGF.EmitBlock(CheckNextBB);
564   }
565
566   // Signal end of parallel region.
567   CGF.EmitBlock(TerminateBB);
568   CGF.EmitRuntimeCall(
569       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_end_parallel),
570       llvm::None);
571   CGF.EmitBranch(BarrierBB);
572
573   // All active and inactive workers wait at a barrier after parallel region.
574   CGF.EmitBlock(BarrierBB);
575   // Barrier after parallel region.
576   syncCTAThreads(CGF);
577   CGF.EmitBranch(AwaitBB);
578
579   // Exit target region.
580   CGF.EmitBlock(ExitBB);
581 }
582
583 /// \brief Returns specified OpenMP runtime function for the current OpenMP
584 /// implementation.  Specialized for the NVPTX device.
585 /// \param Function OpenMP runtime function.
586 /// \return Specified function.
587 llvm::Constant *
588 CGOpenMPRuntimeNVPTX::createNVPTXRuntimeFunction(unsigned Function) {
589   llvm::Constant *RTLFn = nullptr;
590   switch (static_cast<OpenMPRTLFunctionNVPTX>(Function)) {
591   case OMPRTL_NVPTX__kmpc_kernel_init: {
592     // Build void __kmpc_kernel_init(kmp_int32 thread_limit);
593     llvm::Type *TypeParams[] = {CGM.Int32Ty};
594     llvm::FunctionType *FnTy =
595         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
596     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_init");
597     break;
598   }
599   case OMPRTL_NVPTX__kmpc_kernel_deinit: {
600     // Build void __kmpc_kernel_deinit();
601     llvm::FunctionType *FnTy =
602         llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
603     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_deinit");
604     break;
605   }
606   case OMPRTL_NVPTX__kmpc_spmd_kernel_init: {
607     // Build void __kmpc_spmd_kernel_init(kmp_int32 thread_limit,
608     // short RequiresOMPRuntime, short RequiresDataSharing);
609     llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
610     llvm::FunctionType *FnTy =
611         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
612     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_init");
613     break;
614   }
615   case OMPRTL_NVPTX__kmpc_spmd_kernel_deinit: {
616     // Build void __kmpc_spmd_kernel_deinit();
617     llvm::FunctionType *FnTy =
618         llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
619     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_spmd_kernel_deinit");
620     break;
621   }
622   case OMPRTL_NVPTX__kmpc_kernel_prepare_parallel: {
623     /// Build void __kmpc_kernel_prepare_parallel(
624     /// void *outlined_function);
625     llvm::Type *TypeParams[] = {CGM.Int8PtrTy};
626     llvm::FunctionType *FnTy =
627         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
628     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_prepare_parallel");
629     break;
630   }
631   case OMPRTL_NVPTX__kmpc_kernel_parallel: {
632     /// Build bool __kmpc_kernel_parallel(void **outlined_function);
633     llvm::Type *TypeParams[] = {CGM.Int8PtrPtrTy};
634     llvm::Type *RetTy = CGM.getTypes().ConvertType(CGM.getContext().BoolTy);
635     llvm::FunctionType *FnTy =
636         llvm::FunctionType::get(RetTy, TypeParams, /*isVarArg*/ false);
637     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_parallel");
638     break;
639   }
640   case OMPRTL_NVPTX__kmpc_kernel_end_parallel: {
641     /// Build void __kmpc_kernel_end_parallel();
642     llvm::FunctionType *FnTy =
643         llvm::FunctionType::get(CGM.VoidTy, llvm::None, /*isVarArg*/ false);
644     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_kernel_end_parallel");
645     break;
646   }
647   case OMPRTL_NVPTX__kmpc_serialized_parallel: {
648     // Build void __kmpc_serialized_parallel(ident_t *loc, kmp_int32
649     // global_tid);
650     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
651     llvm::FunctionType *FnTy =
652         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
653     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_serialized_parallel");
654     break;
655   }
656   case OMPRTL_NVPTX__kmpc_end_serialized_parallel: {
657     // Build void __kmpc_end_serialized_parallel(ident_t *loc, kmp_int32
658     // global_tid);
659     llvm::Type *TypeParams[] = {getIdentTyPointerTy(), CGM.Int32Ty};
660     llvm::FunctionType *FnTy =
661         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg*/ false);
662     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_end_serialized_parallel");
663     break;
664   }
665   case OMPRTL_NVPTX__kmpc_shuffle_int32: {
666     // Build int32_t __kmpc_shuffle_int32(int32_t element,
667     // int16_t lane_offset, int16_t warp_size);
668     llvm::Type *TypeParams[] = {CGM.Int32Ty, CGM.Int16Ty, CGM.Int16Ty};
669     llvm::FunctionType *FnTy =
670         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg*/ false);
671     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int32");
672     break;
673   }
674   case OMPRTL_NVPTX__kmpc_shuffle_int64: {
675     // Build int64_t __kmpc_shuffle_int64(int64_t element,
676     // int16_t lane_offset, int16_t warp_size);
677     llvm::Type *TypeParams[] = {CGM.Int64Ty, CGM.Int16Ty, CGM.Int16Ty};
678     llvm::FunctionType *FnTy =
679         llvm::FunctionType::get(CGM.Int64Ty, TypeParams, /*isVarArg*/ false);
680     RTLFn = CGM.CreateRuntimeFunction(FnTy, "__kmpc_shuffle_int64");
681     break;
682   }
683   case OMPRTL_NVPTX__kmpc_parallel_reduce_nowait: {
684     // Build int32_t kmpc_nvptx_parallel_reduce_nowait(kmp_int32 global_tid,
685     // kmp_int32 num_vars, size_t reduce_size, void* reduce_data,
686     // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
687     // lane_offset, int16_t Algorithm Version),
688     // void (*kmp_InterWarpCopyFctPtr)(void* src, int warp_num));
689     llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
690                                              CGM.Int16Ty, CGM.Int16Ty};
691     auto *ShuffleReduceFnTy =
692         llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
693                                 /*isVarArg=*/false);
694     llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
695     auto *InterWarpCopyFnTy =
696         llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
697                                 /*isVarArg=*/false);
698     llvm::Type *TypeParams[] = {CGM.Int32Ty,
699                                 CGM.Int32Ty,
700                                 CGM.SizeTy,
701                                 CGM.VoidPtrTy,
702                                 ShuffleReduceFnTy->getPointerTo(),
703                                 InterWarpCopyFnTy->getPointerTo()};
704     llvm::FunctionType *FnTy =
705         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
706     RTLFn = CGM.CreateRuntimeFunction(
707         FnTy, /*Name=*/"__kmpc_nvptx_parallel_reduce_nowait");
708     break;
709   }
710   case OMPRTL_NVPTX__kmpc_teams_reduce_nowait: {
711     // Build int32_t __kmpc_nvptx_teams_reduce_nowait(int32_t global_tid,
712     // int32_t num_vars, size_t reduce_size, void *reduce_data,
713     // void (*kmp_ShuffleReductFctPtr)(void *rhsData, int16_t lane_id, int16_t
714     // lane_offset, int16_t shortCircuit),
715     // void (*kmp_InterWarpCopyFctPtr)(void* src, int32_t warp_num),
716     // void (*kmp_CopyToScratchpadFctPtr)(void *reduce_data, void * scratchpad,
717     // int32_t index, int32_t width),
718     // void (*kmp_LoadReduceFctPtr)(void *reduce_data, void * scratchpad,
719     // int32_t index, int32_t width, int32_t reduce))
720     llvm::Type *ShuffleReduceTypeParams[] = {CGM.VoidPtrTy, CGM.Int16Ty,
721                                              CGM.Int16Ty, CGM.Int16Ty};
722     auto *ShuffleReduceFnTy =
723         llvm::FunctionType::get(CGM.VoidTy, ShuffleReduceTypeParams,
724                                 /*isVarArg=*/false);
725     llvm::Type *InterWarpCopyTypeParams[] = {CGM.VoidPtrTy, CGM.Int32Ty};
726     auto *InterWarpCopyFnTy =
727         llvm::FunctionType::get(CGM.VoidTy, InterWarpCopyTypeParams,
728                                 /*isVarArg=*/false);
729     llvm::Type *CopyToScratchpadTypeParams[] = {CGM.VoidPtrTy, CGM.VoidPtrTy,
730                                                 CGM.Int32Ty, CGM.Int32Ty};
731     auto *CopyToScratchpadFnTy =
732         llvm::FunctionType::get(CGM.VoidTy, CopyToScratchpadTypeParams,
733                                 /*isVarArg=*/false);
734     llvm::Type *LoadReduceTypeParams[] = {
735         CGM.VoidPtrTy, CGM.VoidPtrTy, CGM.Int32Ty, CGM.Int32Ty, CGM.Int32Ty};
736     auto *LoadReduceFnTy =
737         llvm::FunctionType::get(CGM.VoidTy, LoadReduceTypeParams,
738                                 /*isVarArg=*/false);
739     llvm::Type *TypeParams[] = {CGM.Int32Ty,
740                                 CGM.Int32Ty,
741                                 CGM.SizeTy,
742                                 CGM.VoidPtrTy,
743                                 ShuffleReduceFnTy->getPointerTo(),
744                                 InterWarpCopyFnTy->getPointerTo(),
745                                 CopyToScratchpadFnTy->getPointerTo(),
746                                 LoadReduceFnTy->getPointerTo()};
747     llvm::FunctionType *FnTy =
748         llvm::FunctionType::get(CGM.Int32Ty, TypeParams, /*isVarArg=*/false);
749     RTLFn = CGM.CreateRuntimeFunction(
750         FnTy, /*Name=*/"__kmpc_nvptx_teams_reduce_nowait");
751     break;
752   }
753   case OMPRTL_NVPTX__kmpc_end_reduce_nowait: {
754     // Build __kmpc_end_reduce_nowait(kmp_int32 global_tid);
755     llvm::Type *TypeParams[] = {CGM.Int32Ty};
756     llvm::FunctionType *FnTy =
757         llvm::FunctionType::get(CGM.VoidTy, TypeParams, /*isVarArg=*/false);
758     RTLFn = CGM.CreateRuntimeFunction(
759         FnTy, /*Name=*/"__kmpc_nvptx_end_reduce_nowait");
760     break;
761   }
762   }
763   return RTLFn;
764 }
765
766 void CGOpenMPRuntimeNVPTX::createOffloadEntry(llvm::Constant *ID,
767                                               llvm::Constant *Addr,
768                                               uint64_t Size, int32_t) {
769   auto *F = dyn_cast<llvm::Function>(Addr);
770   // TODO: Add support for global variables on the device after declare target
771   // support.
772   if (!F)
773     return;
774   llvm::Module *M = F->getParent();
775   llvm::LLVMContext &Ctx = M->getContext();
776
777   // Get "nvvm.annotations" metadata node
778   llvm::NamedMDNode *MD = M->getOrInsertNamedMetadata("nvvm.annotations");
779
780   llvm::Metadata *MDVals[] = {
781       llvm::ConstantAsMetadata::get(F), llvm::MDString::get(Ctx, "kernel"),
782       llvm::ConstantAsMetadata::get(
783           llvm::ConstantInt::get(llvm::Type::getInt32Ty(Ctx), 1))};
784   // Append metadata to nvvm.annotations
785   MD->addOperand(llvm::MDNode::get(Ctx, MDVals));
786 }
787
788 void CGOpenMPRuntimeNVPTX::emitTargetOutlinedFunction(
789     const OMPExecutableDirective &D, StringRef ParentName,
790     llvm::Function *&OutlinedFn, llvm::Constant *&OutlinedFnID,
791     bool IsOffloadEntry, const RegionCodeGenTy &CodeGen) {
792   if (!IsOffloadEntry) // Nothing to do.
793     return;
794
795   assert(!ParentName.empty() && "Invalid target region parent name!");
796
797   CGOpenMPRuntimeNVPTX::ExecutionMode Mode =
798       getExecutionModeForDirective(CGM, D);
799   switch (Mode) {
800   case CGOpenMPRuntimeNVPTX::ExecutionMode::Generic:
801     emitGenericKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
802                       CodeGen);
803     break;
804   case CGOpenMPRuntimeNVPTX::ExecutionMode::Spmd:
805     emitSpmdKernel(D, ParentName, OutlinedFn, OutlinedFnID, IsOffloadEntry,
806                    CodeGen);
807     break;
808   case CGOpenMPRuntimeNVPTX::ExecutionMode::Unknown:
809     llvm_unreachable(
810         "Unknown programming model for OpenMP directive on NVPTX target.");
811   }
812
813   setPropertyExecutionMode(CGM, OutlinedFn->getName(), Mode);
814 }
815
816 CGOpenMPRuntimeNVPTX::CGOpenMPRuntimeNVPTX(CodeGenModule &CGM)
817     : CGOpenMPRuntime(CGM), CurrentExecutionMode(ExecutionMode::Unknown) {
818   if (!CGM.getLangOpts().OpenMPIsDevice)
819     llvm_unreachable("OpenMP NVPTX can only handle device code.");
820 }
821
822 void CGOpenMPRuntimeNVPTX::emitProcBindClause(CodeGenFunction &CGF,
823                                               OpenMPProcBindClauseKind ProcBind,
824                                               SourceLocation Loc) {
825   // Do nothing in case of Spmd mode and L0 parallel.
826   // TODO: If in Spmd mode and L1 parallel emit the clause.
827   if (isInSpmdExecutionMode())
828     return;
829
830   CGOpenMPRuntime::emitProcBindClause(CGF, ProcBind, Loc);
831 }
832
833 void CGOpenMPRuntimeNVPTX::emitNumThreadsClause(CodeGenFunction &CGF,
834                                                 llvm::Value *NumThreads,
835                                                 SourceLocation Loc) {
836   // Do nothing in case of Spmd mode and L0 parallel.
837   // TODO: If in Spmd mode and L1 parallel emit the clause.
838   if (isInSpmdExecutionMode())
839     return;
840
841   CGOpenMPRuntime::emitNumThreadsClause(CGF, NumThreads, Loc);
842 }
843
844 void CGOpenMPRuntimeNVPTX::emitNumTeamsClause(CodeGenFunction &CGF,
845                                               const Expr *NumTeams,
846                                               const Expr *ThreadLimit,
847                                               SourceLocation Loc) {}
848
849 llvm::Value *CGOpenMPRuntimeNVPTX::emitParallelOutlinedFunction(
850     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
851     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
852   return CGOpenMPRuntime::emitParallelOutlinedFunction(D, ThreadIDVar,
853                                                        InnermostKind, CodeGen);
854 }
855
856 llvm::Value *CGOpenMPRuntimeNVPTX::emitTeamsOutlinedFunction(
857     const OMPExecutableDirective &D, const VarDecl *ThreadIDVar,
858     OpenMPDirectiveKind InnermostKind, const RegionCodeGenTy &CodeGen) {
859
860   llvm::Value *OutlinedFunVal = CGOpenMPRuntime::emitTeamsOutlinedFunction(
861       D, ThreadIDVar, InnermostKind, CodeGen);
862   llvm::Function *OutlinedFun = cast<llvm::Function>(OutlinedFunVal);
863   OutlinedFun->removeFnAttr(llvm::Attribute::NoInline);
864   OutlinedFun->removeFnAttr(llvm::Attribute::OptimizeNone);
865   OutlinedFun->addFnAttr(llvm::Attribute::AlwaysInline);
866
867   return OutlinedFun;
868 }
869
870 void CGOpenMPRuntimeNVPTX::emitTeamsCall(CodeGenFunction &CGF,
871                                          const OMPExecutableDirective &D,
872                                          SourceLocation Loc,
873                                          llvm::Value *OutlinedFn,
874                                          ArrayRef<llvm::Value *> CapturedVars) {
875   if (!CGF.HaveInsertPoint())
876     return;
877
878   Address ZeroAddr =
879       CGF.CreateTempAlloca(CGF.Int32Ty, CharUnits::fromQuantity(4),
880                            /*Name*/ ".zero.addr");
881   CGF.InitTempAlloca(ZeroAddr, CGF.Builder.getInt32(/*C*/ 0));
882   llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
883   OutlinedFnArgs.push_back(ZeroAddr.getPointer());
884   OutlinedFnArgs.push_back(ZeroAddr.getPointer());
885   OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
886   CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
887 }
888
889 void CGOpenMPRuntimeNVPTX::emitParallelCall(
890     CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
891     ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
892   if (!CGF.HaveInsertPoint())
893     return;
894
895   if (isInSpmdExecutionMode())
896     emitSpmdParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
897   else
898     emitGenericParallelCall(CGF, Loc, OutlinedFn, CapturedVars, IfCond);
899 }
900
901 void CGOpenMPRuntimeNVPTX::emitGenericParallelCall(
902     CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
903     ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
904   llvm::Function *Fn = cast<llvm::Function>(OutlinedFn);
905
906   auto &&L0ParallelGen = [this, Fn](CodeGenFunction &CGF, PrePostActionTy &) {
907     CGBuilderTy &Bld = CGF.Builder;
908
909     // Prepare for parallel region. Indicate the outlined function.
910     llvm::Value *Args[] = {Bld.CreateBitOrPointerCast(Fn, CGM.Int8PtrTy)};
911     CGF.EmitRuntimeCall(
912         createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_kernel_prepare_parallel),
913         Args);
914
915     // Activate workers. This barrier is used by the master to signal
916     // work for the workers.
917     syncCTAThreads(CGF);
918
919     // OpenMP [2.5, Parallel Construct, p.49]
920     // There is an implied barrier at the end of a parallel region. After the
921     // end of a parallel region, only the master thread of the team resumes
922     // execution of the enclosing task region.
923     //
924     // The master waits at this barrier until all workers are done.
925     syncCTAThreads(CGF);
926
927     // Remember for post-processing in worker loop.
928     Work.push_back(Fn);
929   };
930
931   auto *RTLoc = emitUpdateLocation(CGF, Loc);
932   auto *ThreadID = getThreadID(CGF, Loc);
933   llvm::Value *Args[] = {RTLoc, ThreadID};
934
935   auto &&SeqGen = [this, Fn, &CapturedVars, &Args](CodeGenFunction &CGF,
936                                                    PrePostActionTy &) {
937     auto &&CodeGen = [this, Fn, &CapturedVars](CodeGenFunction &CGF,
938                                                PrePostActionTy &Action) {
939       Action.Enter(CGF);
940
941       llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
942       OutlinedFnArgs.push_back(
943           llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo()));
944       OutlinedFnArgs.push_back(
945           llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo()));
946       OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
947       CGF.EmitCallOrInvoke(Fn, OutlinedFnArgs);
948     };
949
950     RegionCodeGenTy RCG(CodeGen);
951     NVPTXActionTy Action(
952         createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_serialized_parallel),
953         Args,
954         createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_serialized_parallel),
955         Args);
956     RCG.setAction(Action);
957     RCG(CGF);
958   };
959
960   if (IfCond)
961     emitOMPIfClause(CGF, IfCond, L0ParallelGen, SeqGen);
962   else {
963     CodeGenFunction::RunCleanupsScope Scope(CGF);
964     RegionCodeGenTy ThenRCG(L0ParallelGen);
965     ThenRCG(CGF);
966   }
967 }
968
969 void CGOpenMPRuntimeNVPTX::emitSpmdParallelCall(
970     CodeGenFunction &CGF, SourceLocation Loc, llvm::Value *OutlinedFn,
971     ArrayRef<llvm::Value *> CapturedVars, const Expr *IfCond) {
972   // Just call the outlined function to execute the parallel region.
973   // OutlinedFn(&GTid, &zero, CapturedStruct);
974   //
975   // TODO: Do something with IfCond when support for the 'if' clause
976   // is added on Spmd target directives.
977   llvm::SmallVector<llvm::Value *, 16> OutlinedFnArgs;
978   OutlinedFnArgs.push_back(
979       llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo()));
980   OutlinedFnArgs.push_back(
981       llvm::ConstantPointerNull::get(CGM.Int32Ty->getPointerTo()));
982   OutlinedFnArgs.append(CapturedVars.begin(), CapturedVars.end());
983   CGF.EmitCallOrInvoke(OutlinedFn, OutlinedFnArgs);
984 }
985
986 /// This function creates calls to one of two shuffle functions to copy
987 /// variables between lanes in a warp.
988 static llvm::Value *createRuntimeShuffleFunction(CodeGenFunction &CGF,
989                                                  QualType ElemTy,
990                                                  llvm::Value *Elem,
991                                                  llvm::Value *Offset) {
992   auto &CGM = CGF.CGM;
993   auto &C = CGM.getContext();
994   auto &Bld = CGF.Builder;
995   CGOpenMPRuntimeNVPTX &RT =
996       *(static_cast<CGOpenMPRuntimeNVPTX *>(&CGM.getOpenMPRuntime()));
997
998   unsigned Size = CGM.getContext().getTypeSizeInChars(ElemTy).getQuantity();
999   assert(Size <= 8 && "Unsupported bitwidth in shuffle instruction.");
1000
1001   OpenMPRTLFunctionNVPTX ShuffleFn = Size <= 4
1002                                          ? OMPRTL_NVPTX__kmpc_shuffle_int32
1003                                          : OMPRTL_NVPTX__kmpc_shuffle_int64;
1004
1005   // Cast all types to 32- or 64-bit values before calling shuffle routines.
1006   auto CastTy = Size <= 4 ? CGM.Int32Ty : CGM.Int64Ty;
1007   auto *ElemCast = Bld.CreateSExtOrBitCast(Elem, CastTy);
1008   auto *WarpSize = CGF.EmitScalarConversion(
1009       getNVPTXWarpSize(CGF), C.getIntTypeForBitwidth(32, /* Signed */ true),
1010       C.getIntTypeForBitwidth(16, /* Signed */ true), SourceLocation());
1011
1012   auto *ShuffledVal =
1013       CGF.EmitRuntimeCall(RT.createNVPTXRuntimeFunction(ShuffleFn),
1014                           {ElemCast, Offset, WarpSize});
1015
1016   return Bld.CreateTruncOrBitCast(ShuffledVal, CGF.ConvertTypeForMem(ElemTy));
1017 }
1018
1019 namespace {
1020 enum CopyAction : unsigned {
1021   // RemoteLaneToThread: Copy over a Reduce list from a remote lane in
1022   // the warp using shuffle instructions.
1023   RemoteLaneToThread,
1024   // ThreadCopy: Make a copy of a Reduce list on the thread's stack.
1025   ThreadCopy,
1026   // ThreadToScratchpad: Copy a team-reduced array to the scratchpad.
1027   ThreadToScratchpad,
1028   // ScratchpadToThread: Copy from a scratchpad array in global memory
1029   // containing team-reduced data to a thread's stack.
1030   ScratchpadToThread,
1031 };
1032 } // namespace
1033
1034 struct CopyOptionsTy {
1035   llvm::Value *RemoteLaneOffset;
1036   llvm::Value *ScratchpadIndex;
1037   llvm::Value *ScratchpadWidth;
1038 };
1039
1040 /// Emit instructions to copy a Reduce list, which contains partially
1041 /// aggregated values, in the specified direction.
1042 static void emitReductionListCopy(
1043     CopyAction Action, CodeGenFunction &CGF, QualType ReductionArrayTy,
1044     ArrayRef<const Expr *> Privates, Address SrcBase, Address DestBase,
1045     CopyOptionsTy CopyOptions = {nullptr, nullptr, nullptr}) {
1046
1047   auto &CGM = CGF.CGM;
1048   auto &C = CGM.getContext();
1049   auto &Bld = CGF.Builder;
1050
1051   auto *RemoteLaneOffset = CopyOptions.RemoteLaneOffset;
1052   auto *ScratchpadIndex = CopyOptions.ScratchpadIndex;
1053   auto *ScratchpadWidth = CopyOptions.ScratchpadWidth;
1054
1055   // Iterates, element-by-element, through the source Reduce list and
1056   // make a copy.
1057   unsigned Idx = 0;
1058   unsigned Size = Privates.size();
1059   for (auto &Private : Privates) {
1060     Address SrcElementAddr = Address::invalid();
1061     Address DestElementAddr = Address::invalid();
1062     Address DestElementPtrAddr = Address::invalid();
1063     // Should we shuffle in an element from a remote lane?
1064     bool ShuffleInElement = false;
1065     // Set to true to update the pointer in the dest Reduce list to a
1066     // newly created element.
1067     bool UpdateDestListPtr = false;
1068     // Increment the src or dest pointer to the scratchpad, for each
1069     // new element.
1070     bool IncrScratchpadSrc = false;
1071     bool IncrScratchpadDest = false;
1072
1073     switch (Action) {
1074     case RemoteLaneToThread: {
1075       // Step 1.1: Get the address for the src element in the Reduce list.
1076       Address SrcElementPtrAddr =
1077           Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
1078       llvm::Value *SrcElementPtrPtr = CGF.EmitLoadOfScalar(
1079           SrcElementPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1080       SrcElementAddr =
1081           Address(SrcElementPtrPtr, C.getTypeAlignInChars(Private->getType()));
1082
1083       // Step 1.2: Create a temporary to store the element in the destination
1084       // Reduce list.
1085       DestElementPtrAddr =
1086           Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
1087       DestElementAddr =
1088           CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
1089       ShuffleInElement = true;
1090       UpdateDestListPtr = true;
1091       break;
1092     }
1093     case ThreadCopy: {
1094       // Step 1.1: Get the address for the src element in the Reduce list.
1095       Address SrcElementPtrAddr =
1096           Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
1097       llvm::Value *SrcElementPtrPtr = CGF.EmitLoadOfScalar(
1098           SrcElementPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1099       SrcElementAddr =
1100           Address(SrcElementPtrPtr, C.getTypeAlignInChars(Private->getType()));
1101
1102       // Step 1.2: Get the address for dest element.  The destination
1103       // element has already been created on the thread's stack.
1104       DestElementPtrAddr =
1105           Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
1106       llvm::Value *DestElementPtr =
1107           CGF.EmitLoadOfScalar(DestElementPtrAddr, /*Volatile=*/false,
1108                                C.VoidPtrTy, SourceLocation());
1109       Address DestElemAddr =
1110           Address(DestElementPtr, C.getTypeAlignInChars(Private->getType()));
1111       DestElementAddr = Bld.CreateElementBitCast(
1112           DestElemAddr, CGF.ConvertTypeForMem(Private->getType()));
1113       break;
1114     }
1115     case ThreadToScratchpad: {
1116       // Step 1.1: Get the address for the src element in the Reduce list.
1117       Address SrcElementPtrAddr =
1118           Bld.CreateConstArrayGEP(SrcBase, Idx, CGF.getPointerSize());
1119       llvm::Value *SrcElementPtrPtr = CGF.EmitLoadOfScalar(
1120           SrcElementPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1121       SrcElementAddr =
1122           Address(SrcElementPtrPtr, C.getTypeAlignInChars(Private->getType()));
1123
1124       // Step 1.2: Get the address for dest element:
1125       // address = base + index * ElementSizeInChars.
1126       unsigned ElementSizeInChars =
1127           C.getTypeSizeInChars(Private->getType()).getQuantity();
1128       auto *CurrentOffset =
1129           Bld.CreateMul(llvm::ConstantInt::get(CGM.SizeTy, ElementSizeInChars),
1130                         ScratchpadIndex);
1131       auto *ScratchPadElemAbsolutePtrVal =
1132           Bld.CreateAdd(DestBase.getPointer(), CurrentOffset);
1133       ScratchPadElemAbsolutePtrVal =
1134           Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
1135       Address ScratchpadPtr =
1136           Address(ScratchPadElemAbsolutePtrVal,
1137                   C.getTypeAlignInChars(Private->getType()));
1138       DestElementAddr = Bld.CreateElementBitCast(
1139           ScratchpadPtr, CGF.ConvertTypeForMem(Private->getType()));
1140       IncrScratchpadDest = true;
1141       break;
1142     }
1143     case ScratchpadToThread: {
1144       // Step 1.1: Get the address for the src element in the scratchpad.
1145       // address = base + index * ElementSizeInChars.
1146       unsigned ElementSizeInChars =
1147           C.getTypeSizeInChars(Private->getType()).getQuantity();
1148       auto *CurrentOffset =
1149           Bld.CreateMul(llvm::ConstantInt::get(CGM.SizeTy, ElementSizeInChars),
1150                         ScratchpadIndex);
1151       auto *ScratchPadElemAbsolutePtrVal =
1152           Bld.CreateAdd(SrcBase.getPointer(), CurrentOffset);
1153       ScratchPadElemAbsolutePtrVal =
1154           Bld.CreateIntToPtr(ScratchPadElemAbsolutePtrVal, CGF.VoidPtrTy);
1155       SrcElementAddr = Address(ScratchPadElemAbsolutePtrVal,
1156                                C.getTypeAlignInChars(Private->getType()));
1157       IncrScratchpadSrc = true;
1158
1159       // Step 1.2: Create a temporary to store the element in the destination
1160       // Reduce list.
1161       DestElementPtrAddr =
1162           Bld.CreateConstArrayGEP(DestBase, Idx, CGF.getPointerSize());
1163       DestElementAddr =
1164           CGF.CreateMemTemp(Private->getType(), ".omp.reduction.element");
1165       UpdateDestListPtr = true;
1166       break;
1167     }
1168     }
1169
1170     // Regardless of src and dest of copy, we emit the load of src
1171     // element as this is required in all directions
1172     SrcElementAddr = Bld.CreateElementBitCast(
1173         SrcElementAddr, CGF.ConvertTypeForMem(Private->getType()));
1174     llvm::Value *Elem =
1175         CGF.EmitLoadOfScalar(SrcElementAddr, /*Volatile=*/false,
1176                              Private->getType(), SourceLocation());
1177
1178     // Now that all active lanes have read the element in the
1179     // Reduce list, shuffle over the value from the remote lane.
1180     if (ShuffleInElement) {
1181       Elem = createRuntimeShuffleFunction(CGF, Private->getType(), Elem,
1182                                           RemoteLaneOffset);
1183     }
1184
1185     // Store the source element value to the dest element address.
1186     CGF.EmitStoreOfScalar(Elem, DestElementAddr, /*Volatile=*/false,
1187                           Private->getType());
1188
1189     // Step 3.1: Modify reference in dest Reduce list as needed.
1190     // Modifying the reference in Reduce list to point to the newly
1191     // created element.  The element is live in the current function
1192     // scope and that of functions it invokes (i.e., reduce_function).
1193     // RemoteReduceData[i] = (void*)&RemoteElem
1194     if (UpdateDestListPtr) {
1195       CGF.EmitStoreOfScalar(Bld.CreatePointerBitCastOrAddrSpaceCast(
1196                                 DestElementAddr.getPointer(), CGF.VoidPtrTy),
1197                             DestElementPtrAddr, /*Volatile=*/false,
1198                             C.VoidPtrTy);
1199     }
1200
1201     // Step 4.1: Increment SrcBase/DestBase so that it points to the starting
1202     // address of the next element in scratchpad memory, unless we're currently
1203     // processing the last one.  Memory alignment is also taken care of here.
1204     if ((IncrScratchpadDest || IncrScratchpadSrc) && (Idx + 1 < Size)) {
1205       llvm::Value *ScratchpadBasePtr =
1206           IncrScratchpadDest ? DestBase.getPointer() : SrcBase.getPointer();
1207       unsigned ElementSizeInChars =
1208           C.getTypeSizeInChars(Private->getType()).getQuantity();
1209       ScratchpadBasePtr = Bld.CreateAdd(
1210           ScratchpadBasePtr,
1211           Bld.CreateMul(ScratchpadWidth, llvm::ConstantInt::get(
1212                                              CGM.SizeTy, ElementSizeInChars)));
1213
1214       // Take care of global memory alignment for performance
1215       ScratchpadBasePtr = Bld.CreateSub(ScratchpadBasePtr,
1216                                         llvm::ConstantInt::get(CGM.SizeTy, 1));
1217       ScratchpadBasePtr = Bld.CreateSDiv(
1218           ScratchpadBasePtr,
1219           llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
1220       ScratchpadBasePtr = Bld.CreateAdd(ScratchpadBasePtr,
1221                                         llvm::ConstantInt::get(CGM.SizeTy, 1));
1222       ScratchpadBasePtr = Bld.CreateMul(
1223           ScratchpadBasePtr,
1224           llvm::ConstantInt::get(CGM.SizeTy, GlobalMemoryAlignment));
1225
1226       if (IncrScratchpadDest)
1227         DestBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
1228       else /* IncrScratchpadSrc = true */
1229         SrcBase = Address(ScratchpadBasePtr, CGF.getPointerAlign());
1230     }
1231
1232     Idx++;
1233   }
1234 }
1235
1236 /// This function emits a helper that loads data from the scratchpad array
1237 /// and (optionally) reduces it with the input operand.
1238 ///
1239 ///  load_and_reduce(local, scratchpad, index, width, should_reduce)
1240 ///  reduce_data remote;
1241 ///  for elem in remote:
1242 ///    remote.elem = Scratchpad[elem_id][index]
1243 ///  if (should_reduce)
1244 ///    local = local @ remote
1245 ///  else
1246 ///    local = remote
1247 static llvm::Value *
1248 emitReduceScratchpadFunction(CodeGenModule &CGM,
1249                              ArrayRef<const Expr *> Privates,
1250                              QualType ReductionArrayTy, llvm::Value *ReduceFn) {
1251   auto &C = CGM.getContext();
1252   auto Int32Ty = C.getIntTypeForBitwidth(32, /* Signed */ true);
1253
1254   // Destination of the copy.
1255   ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, SourceLocation(),
1256                                   /*Id=*/nullptr, C.VoidPtrTy);
1257   // Base address of the scratchpad array, with each element storing a
1258   // Reduce list per team.
1259   ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, SourceLocation(),
1260                                   /*Id=*/nullptr, C.VoidPtrTy);
1261   // A source index into the scratchpad array.
1262   ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, SourceLocation(),
1263                              /*Id=*/nullptr, Int32Ty);
1264   // Row width of an element in the scratchpad array, typically
1265   // the number of teams.
1266   ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, SourceLocation(),
1267                              /*Id=*/nullptr, Int32Ty);
1268   // If should_reduce == 1, then it's load AND reduce,
1269   // If should_reduce == 0 (or otherwise), then it only loads (+ copy).
1270   // The latter case is used for initialization.
1271   ImplicitParamDecl ShouldReduceArg(C, /*DC=*/nullptr, SourceLocation(),
1272                                     /*Id=*/nullptr, Int32Ty);
1273
1274   FunctionArgList Args;
1275   Args.push_back(&ReduceListArg);
1276   Args.push_back(&ScratchPadArg);
1277   Args.push_back(&IndexArg);
1278   Args.push_back(&WidthArg);
1279   Args.push_back(&ShouldReduceArg);
1280
1281   auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
1282   auto *Fn = llvm::Function::Create(
1283       CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
1284       "_omp_reduction_load_and_reduce", &CGM.getModule());
1285   CGM.SetInternalFunctionAttributes(/*DC=*/nullptr, Fn, CGFI);
1286   CodeGenFunction CGF(CGM);
1287   // We don't need debug information in this function as nothing here refers to
1288   // user code.
1289   CGF.disableDebugInfo();
1290   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
1291
1292   auto &Bld = CGF.Builder;
1293
1294   // Get local Reduce list pointer.
1295   Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
1296   Address ReduceListAddr(
1297       Bld.CreatePointerBitCastOrAddrSpaceCast(
1298           CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
1299                                C.VoidPtrTy, SourceLocation()),
1300           CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
1301       CGF.getPointerAlign());
1302
1303   Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
1304   llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
1305       AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1306
1307   Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
1308   llvm::Value *IndexVal =
1309       Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false,
1310                                              Int32Ty, SourceLocation()),
1311                         CGM.SizeTy, /*isSigned=*/true);
1312
1313   Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
1314   llvm::Value *WidthVal =
1315       Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false,
1316                                              Int32Ty, SourceLocation()),
1317                         CGM.SizeTy, /*isSigned=*/true);
1318
1319   Address AddrShouldReduceArg = CGF.GetAddrOfLocalVar(&ShouldReduceArg);
1320   llvm::Value *ShouldReduceVal = CGF.EmitLoadOfScalar(
1321       AddrShouldReduceArg, /*Volatile=*/false, Int32Ty, SourceLocation());
1322
1323   // The absolute ptr address to the base addr of the next element to copy.
1324   llvm::Value *CumulativeElemBasePtr =
1325       Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
1326   Address SrcDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
1327
1328   // Create a Remote Reduce list to store the elements read from the
1329   // scratchpad array.
1330   Address RemoteReduceList =
1331       CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_red_list");
1332
1333   // Assemble remote Reduce list from scratchpad array.
1334   emitReductionListCopy(ScratchpadToThread, CGF, ReductionArrayTy, Privates,
1335                         SrcDataAddr, RemoteReduceList,
1336                         {/*RemoteLaneOffset=*/nullptr,
1337                          /*ScratchpadIndex=*/IndexVal,
1338                          /*ScratchpadWidth=*/WidthVal});
1339
1340   llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
1341   llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
1342   llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
1343
1344   auto CondReduce = Bld.CreateICmpEQ(ShouldReduceVal, Bld.getInt32(1));
1345   Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
1346
1347   CGF.EmitBlock(ThenBB);
1348   // We should reduce with the local Reduce list.
1349   // reduce_function(LocalReduceList, RemoteReduceList)
1350   llvm::Value *LocalDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1351       ReduceListAddr.getPointer(), CGF.VoidPtrTy);
1352   llvm::Value *RemoteDataPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1353       RemoteReduceList.getPointer(), CGF.VoidPtrTy);
1354   CGF.EmitCallOrInvoke(ReduceFn, {LocalDataPtr, RemoteDataPtr});
1355   Bld.CreateBr(MergeBB);
1356
1357   CGF.EmitBlock(ElseBB);
1358   // No reduction; just copy:
1359   // Local Reduce list = Remote Reduce list.
1360   emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
1361                         RemoteReduceList, ReduceListAddr);
1362   Bld.CreateBr(MergeBB);
1363
1364   CGF.EmitBlock(MergeBB);
1365
1366   CGF.FinishFunction();
1367   return Fn;
1368 }
1369
1370 /// This function emits a helper that stores reduced data from the team
1371 /// master to a scratchpad array in global memory.
1372 ///
1373 ///  for elem in Reduce List:
1374 ///    scratchpad[elem_id][index] = elem
1375 ///
1376 static llvm::Value *emitCopyToScratchpad(CodeGenModule &CGM,
1377                                          ArrayRef<const Expr *> Privates,
1378                                          QualType ReductionArrayTy) {
1379
1380   auto &C = CGM.getContext();
1381   auto Int32Ty = C.getIntTypeForBitwidth(32, /* Signed */ true);
1382
1383   // Source of the copy.
1384   ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, SourceLocation(),
1385                                   /*Id=*/nullptr, C.VoidPtrTy);
1386   // Base address of the scratchpad array, with each element storing a
1387   // Reduce list per team.
1388   ImplicitParamDecl ScratchPadArg(C, /*DC=*/nullptr, SourceLocation(),
1389                                   /*Id=*/nullptr, C.VoidPtrTy);
1390   // A destination index into the scratchpad array, typically the team
1391   // identifier.
1392   ImplicitParamDecl IndexArg(C, /*DC=*/nullptr, SourceLocation(),
1393                              /*Id=*/nullptr, Int32Ty);
1394   // Row width of an element in the scratchpad array, typically
1395   // the number of teams.
1396   ImplicitParamDecl WidthArg(C, /*DC=*/nullptr, SourceLocation(),
1397                              /*Id=*/nullptr, Int32Ty);
1398
1399   FunctionArgList Args;
1400   Args.push_back(&ReduceListArg);
1401   Args.push_back(&ScratchPadArg);
1402   Args.push_back(&IndexArg);
1403   Args.push_back(&WidthArg);
1404
1405   auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
1406   auto *Fn = llvm::Function::Create(
1407       CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
1408       "_omp_reduction_copy_to_scratchpad", &CGM.getModule());
1409   CGM.SetInternalFunctionAttributes(/*DC=*/nullptr, Fn, CGFI);
1410   CodeGenFunction CGF(CGM);
1411   // We don't need debug information in this function as nothing here refers to
1412   // user code.
1413   CGF.disableDebugInfo();
1414   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
1415
1416   auto &Bld = CGF.Builder;
1417
1418   Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
1419   Address SrcDataAddr(
1420       Bld.CreatePointerBitCastOrAddrSpaceCast(
1421           CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
1422                                C.VoidPtrTy, SourceLocation()),
1423           CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
1424       CGF.getPointerAlign());
1425
1426   Address AddrScratchPadArg = CGF.GetAddrOfLocalVar(&ScratchPadArg);
1427   llvm::Value *ScratchPadBase = CGF.EmitLoadOfScalar(
1428       AddrScratchPadArg, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1429
1430   Address AddrIndexArg = CGF.GetAddrOfLocalVar(&IndexArg);
1431   llvm::Value *IndexVal =
1432       Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrIndexArg, /*Volatile=*/false,
1433                                              Int32Ty, SourceLocation()),
1434                         CGF.SizeTy, /*isSigned=*/true);
1435
1436   Address AddrWidthArg = CGF.GetAddrOfLocalVar(&WidthArg);
1437   llvm::Value *WidthVal =
1438       Bld.CreateIntCast(CGF.EmitLoadOfScalar(AddrWidthArg, /*Volatile=*/false,
1439                                              Int32Ty, SourceLocation()),
1440                         CGF.SizeTy, /*isSigned=*/true);
1441
1442   // The absolute ptr address to the base addr of the next element to copy.
1443   llvm::Value *CumulativeElemBasePtr =
1444       Bld.CreatePtrToInt(ScratchPadBase, CGM.SizeTy);
1445   Address DestDataAddr(CumulativeElemBasePtr, CGF.getPointerAlign());
1446
1447   emitReductionListCopy(ThreadToScratchpad, CGF, ReductionArrayTy, Privates,
1448                         SrcDataAddr, DestDataAddr,
1449                         {/*RemoteLaneOffset=*/nullptr,
1450                          /*ScratchpadIndex=*/IndexVal,
1451                          /*ScratchpadWidth=*/WidthVal});
1452
1453   CGF.FinishFunction();
1454   return Fn;
1455 }
1456
1457 /// This function emits a helper that gathers Reduce lists from the first
1458 /// lane of every active warp to lanes in the first warp.
1459 ///
1460 /// void inter_warp_copy_func(void* reduce_data, num_warps)
1461 ///   shared smem[warp_size];
1462 ///   For all data entries D in reduce_data:
1463 ///     If (I am the first lane in each warp)
1464 ///       Copy my local D to smem[warp_id]
1465 ///     sync
1466 ///     if (I am the first warp)
1467 ///       Copy smem[thread_id] to my local D
1468 ///     sync
1469 static llvm::Value *emitInterWarpCopyFunction(CodeGenModule &CGM,
1470                                               ArrayRef<const Expr *> Privates,
1471                                               QualType ReductionArrayTy) {
1472   auto &C = CGM.getContext();
1473   auto &M = CGM.getModule();
1474
1475   // ReduceList: thread local Reduce list.
1476   // At the stage of the computation when this function is called, partially
1477   // aggregated values reside in the first lane of every active warp.
1478   ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, SourceLocation(),
1479                                   /*Id=*/nullptr, C.VoidPtrTy);
1480   // NumWarps: number of warps active in the parallel region.  This could
1481   // be smaller than 32 (max warps in a CTA) for partial block reduction.
1482   ImplicitParamDecl NumWarpsArg(C, /*DC=*/nullptr, SourceLocation(),
1483                                 /*Id=*/nullptr,
1484                                 C.getIntTypeForBitwidth(32, /* Signed */ true));
1485   FunctionArgList Args;
1486   Args.push_back(&ReduceListArg);
1487   Args.push_back(&NumWarpsArg);
1488
1489   auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
1490   auto *Fn = llvm::Function::Create(
1491       CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
1492       "_omp_reduction_inter_warp_copy_func", &CGM.getModule());
1493   CGM.SetInternalFunctionAttributes(/*DC=*/nullptr, Fn, CGFI);
1494   CodeGenFunction CGF(CGM);
1495   // We don't need debug information in this function as nothing here refers to
1496   // user code.
1497   CGF.disableDebugInfo();
1498   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
1499
1500   auto &Bld = CGF.Builder;
1501
1502   // This array is used as a medium to transfer, one reduce element at a time,
1503   // the data from the first lane of every warp to lanes in the first warp
1504   // in order to perform the final step of a reduction in a parallel region
1505   // (reduction across warps).  The array is placed in NVPTX __shared__ memory
1506   // for reduced latency, as well as to have a distinct copy for concurrently
1507   // executing target regions.  The array is declared with common linkage so
1508   // as to be shared across compilation units.
1509   const char *TransferMediumName =
1510       "__openmp_nvptx_data_transfer_temporary_storage";
1511   llvm::GlobalVariable *TransferMedium =
1512       M.getGlobalVariable(TransferMediumName);
1513   if (!TransferMedium) {
1514     auto *Ty = llvm::ArrayType::get(CGM.Int64Ty, WarpSize);
1515     unsigned SharedAddressSpace = C.getTargetAddressSpace(LangAS::cuda_shared);
1516     TransferMedium = new llvm::GlobalVariable(
1517         M, Ty,
1518         /*isConstant=*/false, llvm::GlobalVariable::CommonLinkage,
1519         llvm::Constant::getNullValue(Ty), TransferMediumName,
1520         /*InsertBefore=*/nullptr, llvm::GlobalVariable::NotThreadLocal,
1521         SharedAddressSpace);
1522   }
1523
1524   // Get the CUDA thread id of the current OpenMP thread on the GPU.
1525   auto *ThreadID = getNVPTXThreadID(CGF);
1526   // nvptx_lane_id = nvptx_id % warpsize
1527   auto *LaneID = getNVPTXLaneID(CGF);
1528   // nvptx_warp_id = nvptx_id / warpsize
1529   auto *WarpID = getNVPTXWarpID(CGF);
1530
1531   Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
1532   Address LocalReduceList(
1533       Bld.CreatePointerBitCastOrAddrSpaceCast(
1534           CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
1535                                C.VoidPtrTy, SourceLocation()),
1536           CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
1537       CGF.getPointerAlign());
1538
1539   unsigned Idx = 0;
1540   for (auto &Private : Privates) {
1541     //
1542     // Warp master copies reduce element to transfer medium in __shared__
1543     // memory.
1544     //
1545     llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
1546     llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
1547     llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
1548
1549     // if (lane_id == 0)
1550     auto IsWarpMaster =
1551         Bld.CreateICmpEQ(LaneID, Bld.getInt32(0), "warp_master");
1552     Bld.CreateCondBr(IsWarpMaster, ThenBB, ElseBB);
1553     CGF.EmitBlock(ThenBB);
1554
1555     // Reduce element = LocalReduceList[i]
1556     Address ElemPtrPtrAddr =
1557         Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
1558     llvm::Value *ElemPtrPtr = CGF.EmitLoadOfScalar(
1559         ElemPtrPtrAddr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1560     // elemptr = (type[i]*)(elemptrptr)
1561     Address ElemPtr =
1562         Address(ElemPtrPtr, C.getTypeAlignInChars(Private->getType()));
1563     ElemPtr = Bld.CreateElementBitCast(
1564         ElemPtr, CGF.ConvertTypeForMem(Private->getType()));
1565     // elem = *elemptr
1566     llvm::Value *Elem = CGF.EmitLoadOfScalar(
1567         ElemPtr, /*Volatile=*/false, Private->getType(), SourceLocation());
1568
1569     // Get pointer to location in transfer medium.
1570     // MediumPtr = &medium[warp_id]
1571     llvm::Value *MediumPtrVal = Bld.CreateInBoundsGEP(
1572         TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), WarpID});
1573     Address MediumPtr(MediumPtrVal, C.getTypeAlignInChars(Private->getType()));
1574     // Casting to actual data type.
1575     // MediumPtr = (type[i]*)MediumPtrAddr;
1576     MediumPtr = Bld.CreateElementBitCast(
1577         MediumPtr, CGF.ConvertTypeForMem(Private->getType()));
1578
1579     //*MediumPtr = elem
1580     Bld.CreateStore(Elem, MediumPtr);
1581
1582     Bld.CreateBr(MergeBB);
1583
1584     CGF.EmitBlock(ElseBB);
1585     Bld.CreateBr(MergeBB);
1586
1587     CGF.EmitBlock(MergeBB);
1588
1589     Address AddrNumWarpsArg = CGF.GetAddrOfLocalVar(&NumWarpsArg);
1590     llvm::Value *NumWarpsVal = CGF.EmitLoadOfScalar(
1591         AddrNumWarpsArg, /*Volatile=*/false, C.IntTy, SourceLocation());
1592
1593     auto *NumActiveThreads = Bld.CreateNSWMul(
1594         NumWarpsVal, getNVPTXWarpSize(CGF), "num_active_threads");
1595     // named_barrier_sync(ParallelBarrierID, num_active_threads)
1596     syncParallelThreads(CGF, NumActiveThreads);
1597
1598     //
1599     // Warp 0 copies reduce element from transfer medium.
1600     //
1601     llvm::BasicBlock *W0ThenBB = CGF.createBasicBlock("then");
1602     llvm::BasicBlock *W0ElseBB = CGF.createBasicBlock("else");
1603     llvm::BasicBlock *W0MergeBB = CGF.createBasicBlock("ifcont");
1604
1605     // Up to 32 threads in warp 0 are active.
1606     auto IsActiveThread =
1607         Bld.CreateICmpULT(ThreadID, NumWarpsVal, "is_active_thread");
1608     Bld.CreateCondBr(IsActiveThread, W0ThenBB, W0ElseBB);
1609
1610     CGF.EmitBlock(W0ThenBB);
1611
1612     // SrcMediumPtr = &medium[tid]
1613     llvm::Value *SrcMediumPtrVal = Bld.CreateInBoundsGEP(
1614         TransferMedium, {llvm::Constant::getNullValue(CGM.Int64Ty), ThreadID});
1615     Address SrcMediumPtr(SrcMediumPtrVal,
1616                          C.getTypeAlignInChars(Private->getType()));
1617     // SrcMediumVal = *SrcMediumPtr;
1618     SrcMediumPtr = Bld.CreateElementBitCast(
1619         SrcMediumPtr, CGF.ConvertTypeForMem(Private->getType()));
1620     llvm::Value *SrcMediumValue = CGF.EmitLoadOfScalar(
1621         SrcMediumPtr, /*Volatile=*/false, Private->getType(), SourceLocation());
1622
1623     // TargetElemPtr = (type[i]*)(SrcDataAddr[i])
1624     Address TargetElemPtrPtr =
1625         Bld.CreateConstArrayGEP(LocalReduceList, Idx, CGF.getPointerSize());
1626     llvm::Value *TargetElemPtrVal = CGF.EmitLoadOfScalar(
1627         TargetElemPtrPtr, /*Volatile=*/false, C.VoidPtrTy, SourceLocation());
1628     Address TargetElemPtr =
1629         Address(TargetElemPtrVal, C.getTypeAlignInChars(Private->getType()));
1630     TargetElemPtr = Bld.CreateElementBitCast(
1631         TargetElemPtr, CGF.ConvertTypeForMem(Private->getType()));
1632
1633     // *TargetElemPtr = SrcMediumVal;
1634     CGF.EmitStoreOfScalar(SrcMediumValue, TargetElemPtr, /*Volatile=*/false,
1635                           Private->getType());
1636     Bld.CreateBr(W0MergeBB);
1637
1638     CGF.EmitBlock(W0ElseBB);
1639     Bld.CreateBr(W0MergeBB);
1640
1641     CGF.EmitBlock(W0MergeBB);
1642
1643     // While warp 0 copies values from transfer medium, all other warps must
1644     // wait.
1645     syncParallelThreads(CGF, NumActiveThreads);
1646     Idx++;
1647   }
1648
1649   CGF.FinishFunction();
1650   return Fn;
1651 }
1652
1653 /// Emit a helper that reduces data across two OpenMP threads (lanes)
1654 /// in the same warp.  It uses shuffle instructions to copy over data from
1655 /// a remote lane's stack.  The reduction algorithm performed is specified
1656 /// by the fourth parameter.
1657 ///
1658 /// Algorithm Versions.
1659 /// Full Warp Reduce (argument value 0):
1660 ///   This algorithm assumes that all 32 lanes are active and gathers
1661 ///   data from these 32 lanes, producing a single resultant value.
1662 /// Contiguous Partial Warp Reduce (argument value 1):
1663 ///   This algorithm assumes that only a *contiguous* subset of lanes
1664 ///   are active.  This happens for the last warp in a parallel region
1665 ///   when the user specified num_threads is not an integer multiple of
1666 ///   32.  This contiguous subset always starts with the zeroth lane.
1667 /// Partial Warp Reduce (argument value 2):
1668 ///   This algorithm gathers data from any number of lanes at any position.
1669 /// All reduced values are stored in the lowest possible lane.  The set
1670 /// of problems every algorithm addresses is a super set of those
1671 /// addressable by algorithms with a lower version number.  Overhead
1672 /// increases as algorithm version increases.
1673 ///
1674 /// Terminology
1675 /// Reduce element:
1676 ///   Reduce element refers to the individual data field with primitive
1677 ///   data types to be combined and reduced across threads.
1678 /// Reduce list:
1679 ///   Reduce list refers to a collection of local, thread-private
1680 ///   reduce elements.
1681 /// Remote Reduce list:
1682 ///   Remote Reduce list refers to a collection of remote (relative to
1683 ///   the current thread) reduce elements.
1684 ///
1685 /// We distinguish between three states of threads that are important to
1686 /// the implementation of this function.
1687 /// Alive threads:
1688 ///   Threads in a warp executing the SIMT instruction, as distinguished from
1689 ///   threads that are inactive due to divergent control flow.
1690 /// Active threads:
1691 ///   The minimal set of threads that has to be alive upon entry to this
1692 ///   function.  The computation is correct iff active threads are alive.
1693 ///   Some threads are alive but they are not active because they do not
1694 ///   contribute to the computation in any useful manner.  Turning them off
1695 ///   may introduce control flow overheads without any tangible benefits.
1696 /// Effective threads:
1697 ///   In order to comply with the argument requirements of the shuffle
1698 ///   function, we must keep all lanes holding data alive.  But at most
1699 ///   half of them perform value aggregation; we refer to this half of
1700 ///   threads as effective. The other half is simply handing off their
1701 ///   data.
1702 ///
1703 /// Procedure
1704 /// Value shuffle:
1705 ///   In this step active threads transfer data from higher lane positions
1706 ///   in the warp to lower lane positions, creating Remote Reduce list.
1707 /// Value aggregation:
1708 ///   In this step, effective threads combine their thread local Reduce list
1709 ///   with Remote Reduce list and store the result in the thread local
1710 ///   Reduce list.
1711 /// Value copy:
1712 ///   In this step, we deal with the assumption made by algorithm 2
1713 ///   (i.e. contiguity assumption).  When we have an odd number of lanes
1714 ///   active, say 2k+1, only k threads will be effective and therefore k
1715 ///   new values will be produced.  However, the Reduce list owned by the
1716 ///   (2k+1)th thread is ignored in the value aggregation.  Therefore
1717 ///   we copy the Reduce list from the (2k+1)th lane to (k+1)th lane so
1718 ///   that the contiguity assumption still holds.
1719 static llvm::Value *
1720 emitShuffleAndReduceFunction(CodeGenModule &CGM,
1721                              ArrayRef<const Expr *> Privates,
1722                              QualType ReductionArrayTy, llvm::Value *ReduceFn) {
1723   auto &C = CGM.getContext();
1724
1725   // Thread local Reduce list used to host the values of data to be reduced.
1726   ImplicitParamDecl ReduceListArg(C, /*DC=*/nullptr, SourceLocation(),
1727                                   /*Id=*/nullptr, C.VoidPtrTy);
1728   // Current lane id; could be logical.
1729   ImplicitParamDecl LaneIDArg(C, /*DC=*/nullptr, SourceLocation(),
1730                               /*Id=*/nullptr, C.ShortTy);
1731   // Offset of the remote source lane relative to the current lane.
1732   ImplicitParamDecl RemoteLaneOffsetArg(C, /*DC=*/nullptr, SourceLocation(),
1733                                         /*Id=*/nullptr, C.ShortTy);
1734   // Algorithm version.  This is expected to be known at compile time.
1735   ImplicitParamDecl AlgoVerArg(C, /*DC=*/nullptr, SourceLocation(),
1736                                /*Id=*/nullptr, C.ShortTy);
1737   FunctionArgList Args;
1738   Args.push_back(&ReduceListArg);
1739   Args.push_back(&LaneIDArg);
1740   Args.push_back(&RemoteLaneOffsetArg);
1741   Args.push_back(&AlgoVerArg);
1742
1743   auto &CGFI = CGM.getTypes().arrangeBuiltinFunctionDeclaration(C.VoidTy, Args);
1744   auto *Fn = llvm::Function::Create(
1745       CGM.getTypes().GetFunctionType(CGFI), llvm::GlobalValue::InternalLinkage,
1746       "_omp_reduction_shuffle_and_reduce_func", &CGM.getModule());
1747   CGM.SetInternalFunctionAttributes(/*D=*/nullptr, Fn, CGFI);
1748   CodeGenFunction CGF(CGM);
1749   // We don't need debug information in this function as nothing here refers to
1750   // user code.
1751   CGF.disableDebugInfo();
1752   CGF.StartFunction(GlobalDecl(), C.VoidTy, Fn, CGFI, Args);
1753
1754   auto &Bld = CGF.Builder;
1755
1756   Address AddrReduceListArg = CGF.GetAddrOfLocalVar(&ReduceListArg);
1757   Address LocalReduceList(
1758       Bld.CreatePointerBitCastOrAddrSpaceCast(
1759           CGF.EmitLoadOfScalar(AddrReduceListArg, /*Volatile=*/false,
1760                                C.VoidPtrTy, SourceLocation()),
1761           CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo()),
1762       CGF.getPointerAlign());
1763
1764   Address AddrLaneIDArg = CGF.GetAddrOfLocalVar(&LaneIDArg);
1765   llvm::Value *LaneIDArgVal = CGF.EmitLoadOfScalar(
1766       AddrLaneIDArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
1767
1768   Address AddrRemoteLaneOffsetArg = CGF.GetAddrOfLocalVar(&RemoteLaneOffsetArg);
1769   llvm::Value *RemoteLaneOffsetArgVal = CGF.EmitLoadOfScalar(
1770       AddrRemoteLaneOffsetArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
1771
1772   Address AddrAlgoVerArg = CGF.GetAddrOfLocalVar(&AlgoVerArg);
1773   llvm::Value *AlgoVerArgVal = CGF.EmitLoadOfScalar(
1774       AddrAlgoVerArg, /*Volatile=*/false, C.ShortTy, SourceLocation());
1775
1776   // Create a local thread-private variable to host the Reduce list
1777   // from a remote lane.
1778   Address RemoteReduceList =
1779       CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.remote_reduce_list");
1780
1781   // This loop iterates through the list of reduce elements and copies,
1782   // element by element, from a remote lane in the warp to RemoteReduceList,
1783   // hosted on the thread's stack.
1784   emitReductionListCopy(RemoteLaneToThread, CGF, ReductionArrayTy, Privates,
1785                         LocalReduceList, RemoteReduceList,
1786                         {/*RemoteLaneOffset=*/RemoteLaneOffsetArgVal,
1787                          /*ScratchpadIndex=*/nullptr,
1788                          /*ScratchpadWidth=*/nullptr});
1789
1790   // The actions to be performed on the Remote Reduce list is dependent
1791   // on the algorithm version.
1792   //
1793   //  if (AlgoVer==0) || (AlgoVer==1 && (LaneId < Offset)) || (AlgoVer==2 &&
1794   //  LaneId % 2 == 0 && Offset > 0):
1795   //    do the reduction value aggregation
1796   //
1797   //  The thread local variable Reduce list is mutated in place to host the
1798   //  reduced data, which is the aggregated value produced from local and
1799   //  remote lanes.
1800   //
1801   //  Note that AlgoVer is expected to be a constant integer known at compile
1802   //  time.
1803   //  When AlgoVer==0, the first conjunction evaluates to true, making
1804   //    the entire predicate true during compile time.
1805   //  When AlgoVer==1, the second conjunction has only the second part to be
1806   //    evaluated during runtime.  Other conjunctions evaluates to false
1807   //    during compile time.
1808   //  When AlgoVer==2, the third conjunction has only the second part to be
1809   //    evaluated during runtime.  Other conjunctions evaluates to false
1810   //    during compile time.
1811   auto CondAlgo0 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(0));
1812
1813   auto Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
1814   auto CondAlgo1 = Bld.CreateAnd(
1815       Algo1, Bld.CreateICmpULT(LaneIDArgVal, RemoteLaneOffsetArgVal));
1816
1817   auto Algo2 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(2));
1818   auto CondAlgo2 = Bld.CreateAnd(
1819       Algo2,
1820       Bld.CreateICmpEQ(Bld.CreateAnd(LaneIDArgVal, Bld.getInt16(1)),
1821                        Bld.getInt16(0)));
1822   CondAlgo2 = Bld.CreateAnd(
1823       CondAlgo2, Bld.CreateICmpSGT(RemoteLaneOffsetArgVal, Bld.getInt16(0)));
1824
1825   auto CondReduce = Bld.CreateOr(CondAlgo0, CondAlgo1);
1826   CondReduce = Bld.CreateOr(CondReduce, CondAlgo2);
1827
1828   llvm::BasicBlock *ThenBB = CGF.createBasicBlock("then");
1829   llvm::BasicBlock *ElseBB = CGF.createBasicBlock("else");
1830   llvm::BasicBlock *MergeBB = CGF.createBasicBlock("ifcont");
1831   Bld.CreateCondBr(CondReduce, ThenBB, ElseBB);
1832
1833   CGF.EmitBlock(ThenBB);
1834   // reduce_function(LocalReduceList, RemoteReduceList)
1835   llvm::Value *LocalReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1836       LocalReduceList.getPointer(), CGF.VoidPtrTy);
1837   llvm::Value *RemoteReduceListPtr = Bld.CreatePointerBitCastOrAddrSpaceCast(
1838       RemoteReduceList.getPointer(), CGF.VoidPtrTy);
1839   CGF.EmitCallOrInvoke(ReduceFn, {LocalReduceListPtr, RemoteReduceListPtr});
1840   Bld.CreateBr(MergeBB);
1841
1842   CGF.EmitBlock(ElseBB);
1843   Bld.CreateBr(MergeBB);
1844
1845   CGF.EmitBlock(MergeBB);
1846
1847   // if (AlgoVer==1 && (LaneId >= Offset)) copy Remote Reduce list to local
1848   // Reduce list.
1849   Algo1 = Bld.CreateICmpEQ(AlgoVerArgVal, Bld.getInt16(1));
1850   auto CondCopy = Bld.CreateAnd(
1851       Algo1, Bld.CreateICmpUGE(LaneIDArgVal, RemoteLaneOffsetArgVal));
1852
1853   llvm::BasicBlock *CpyThenBB = CGF.createBasicBlock("then");
1854   llvm::BasicBlock *CpyElseBB = CGF.createBasicBlock("else");
1855   llvm::BasicBlock *CpyMergeBB = CGF.createBasicBlock("ifcont");
1856   Bld.CreateCondBr(CondCopy, CpyThenBB, CpyElseBB);
1857
1858   CGF.EmitBlock(CpyThenBB);
1859   emitReductionListCopy(ThreadCopy, CGF, ReductionArrayTy, Privates,
1860                         RemoteReduceList, LocalReduceList);
1861   Bld.CreateBr(CpyMergeBB);
1862
1863   CGF.EmitBlock(CpyElseBB);
1864   Bld.CreateBr(CpyMergeBB);
1865
1866   CGF.EmitBlock(CpyMergeBB);
1867
1868   CGF.FinishFunction();
1869   return Fn;
1870 }
1871
1872 ///
1873 /// Design of OpenMP reductions on the GPU
1874 ///
1875 /// Consider a typical OpenMP program with one or more reduction
1876 /// clauses:
1877 ///
1878 /// float foo;
1879 /// double bar;
1880 /// #pragma omp target teams distribute parallel for \
1881 ///             reduction(+:foo) reduction(*:bar)
1882 /// for (int i = 0; i < N; i++) {
1883 ///   foo += A[i]; bar *= B[i];
1884 /// }
1885 ///
1886 /// where 'foo' and 'bar' are reduced across all OpenMP threads in
1887 /// all teams.  In our OpenMP implementation on the NVPTX device an
1888 /// OpenMP team is mapped to a CUDA threadblock and OpenMP threads
1889 /// within a team are mapped to CUDA threads within a threadblock.
1890 /// Our goal is to efficiently aggregate values across all OpenMP
1891 /// threads such that:
1892 ///
1893 ///   - the compiler and runtime are logically concise, and
1894 ///   - the reduction is performed efficiently in a hierarchical
1895 ///     manner as follows: within OpenMP threads in the same warp,
1896 ///     across warps in a threadblock, and finally across teams on
1897 ///     the NVPTX device.
1898 ///
1899 /// Introduction to Decoupling
1900 ///
1901 /// We would like to decouple the compiler and the runtime so that the
1902 /// latter is ignorant of the reduction variables (number, data types)
1903 /// and the reduction operators.  This allows a simpler interface
1904 /// and implementation while still attaining good performance.
1905 ///
1906 /// Pseudocode for the aforementioned OpenMP program generated by the
1907 /// compiler is as follows:
1908 ///
1909 /// 1. Create private copies of reduction variables on each OpenMP
1910 ///    thread: 'foo_private', 'bar_private'
1911 /// 2. Each OpenMP thread reduces the chunk of 'A' and 'B' assigned
1912 ///    to it and writes the result in 'foo_private' and 'bar_private'
1913 ///    respectively.
1914 /// 3. Call the OpenMP runtime on the GPU to reduce within a team
1915 ///    and store the result on the team master:
1916 ///
1917 ///     __kmpc_nvptx_parallel_reduce_nowait(...,
1918 ///        reduceData, shuffleReduceFn, interWarpCpyFn)
1919 ///
1920 ///     where:
1921 ///       struct ReduceData {
1922 ///         double *foo;
1923 ///         double *bar;
1924 ///       } reduceData
1925 ///       reduceData.foo = &foo_private
1926 ///       reduceData.bar = &bar_private
1927 ///
1928 ///     'shuffleReduceFn' and 'interWarpCpyFn' are pointers to two
1929 ///     auxiliary functions generated by the compiler that operate on
1930 ///     variables of type 'ReduceData'.  They aid the runtime perform
1931 ///     algorithmic steps in a data agnostic manner.
1932 ///
1933 ///     'shuffleReduceFn' is a pointer to a function that reduces data
1934 ///     of type 'ReduceData' across two OpenMP threads (lanes) in the
1935 ///     same warp.  It takes the following arguments as input:
1936 ///
1937 ///     a. variable of type 'ReduceData' on the calling lane,
1938 ///     b. its lane_id,
1939 ///     c. an offset relative to the current lane_id to generate a
1940 ///        remote_lane_id.  The remote lane contains the second
1941 ///        variable of type 'ReduceData' that is to be reduced.
1942 ///     d. an algorithm version parameter determining which reduction
1943 ///        algorithm to use.
1944 ///
1945 ///     'shuffleReduceFn' retrieves data from the remote lane using
1946 ///     efficient GPU shuffle intrinsics and reduces, using the
1947 ///     algorithm specified by the 4th parameter, the two operands
1948 ///     element-wise.  The result is written to the first operand.
1949 ///
1950 ///     Different reduction algorithms are implemented in different
1951 ///     runtime functions, all calling 'shuffleReduceFn' to perform
1952 ///     the essential reduction step.  Therefore, based on the 4th
1953 ///     parameter, this function behaves slightly differently to
1954 ///     cooperate with the runtime to ensure correctness under
1955 ///     different circumstances.
1956 ///
1957 ///     'InterWarpCpyFn' is a pointer to a function that transfers
1958 ///     reduced variables across warps.  It tunnels, through CUDA
1959 ///     shared memory, the thread-private data of type 'ReduceData'
1960 ///     from lane 0 of each warp to a lane in the first warp.
1961 /// 4. Call the OpenMP runtime on the GPU to reduce across teams.
1962 ///    The last team writes the global reduced value to memory.
1963 ///
1964 ///     ret = __kmpc_nvptx_teams_reduce_nowait(...,
1965 ///             reduceData, shuffleReduceFn, interWarpCpyFn,
1966 ///             scratchpadCopyFn, loadAndReduceFn)
1967 ///
1968 ///     'scratchpadCopyFn' is a helper that stores reduced
1969 ///     data from the team master to a scratchpad array in
1970 ///     global memory.
1971 ///
1972 ///     'loadAndReduceFn' is a helper that loads data from
1973 ///     the scratchpad array and reduces it with the input
1974 ///     operand.
1975 ///
1976 ///     These compiler generated functions hide address
1977 ///     calculation and alignment information from the runtime.
1978 /// 5. if ret == 1:
1979 ///     The team master of the last team stores the reduced
1980 ///     result to the globals in memory.
1981 ///     foo += reduceData.foo; bar *= reduceData.bar
1982 ///
1983 ///
1984 /// Warp Reduction Algorithms
1985 ///
1986 /// On the warp level, we have three algorithms implemented in the
1987 /// OpenMP runtime depending on the number of active lanes:
1988 ///
1989 /// Full Warp Reduction
1990 ///
1991 /// The reduce algorithm within a warp where all lanes are active
1992 /// is implemented in the runtime as follows:
1993 ///
1994 /// full_warp_reduce(void *reduce_data,
1995 ///                  kmp_ShuffleReductFctPtr ShuffleReduceFn) {
1996 ///   for (int offset = WARPSIZE/2; offset > 0; offset /= 2)
1997 ///     ShuffleReduceFn(reduce_data, 0, offset, 0);
1998 /// }
1999 ///
2000 /// The algorithm completes in log(2, WARPSIZE) steps.
2001 ///
2002 /// 'ShuffleReduceFn' is used here with lane_id set to 0 because it is
2003 /// not used therefore we save instructions by not retrieving lane_id
2004 /// from the corresponding special registers.  The 4th parameter, which
2005 /// represents the version of the algorithm being used, is set to 0 to
2006 /// signify full warp reduction.
2007 ///
2008 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
2009 ///
2010 /// #reduce_elem refers to an element in the local lane's data structure
2011 /// #remote_elem is retrieved from a remote lane
2012 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
2013 /// reduce_elem = reduce_elem REDUCE_OP remote_elem;
2014 ///
2015 /// Contiguous Partial Warp Reduction
2016 ///
2017 /// This reduce algorithm is used within a warp where only the first
2018 /// 'n' (n <= WARPSIZE) lanes are active.  It is typically used when the
2019 /// number of OpenMP threads in a parallel region is not a multiple of
2020 /// WARPSIZE.  The algorithm is implemented in the runtime as follows:
2021 ///
2022 /// void
2023 /// contiguous_partial_reduce(void *reduce_data,
2024 ///                           kmp_ShuffleReductFctPtr ShuffleReduceFn,
2025 ///                           int size, int lane_id) {
2026 ///   int curr_size;
2027 ///   int offset;
2028 ///   curr_size = size;
2029 ///   mask = curr_size/2;
2030 ///   while (offset>0) {
2031 ///     ShuffleReduceFn(reduce_data, lane_id, offset, 1);
2032 ///     curr_size = (curr_size+1)/2;
2033 ///     offset = curr_size/2;
2034 ///   }
2035 /// }
2036 ///
2037 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
2038 ///
2039 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
2040 /// if (lane_id < offset)
2041 ///     reduce_elem = reduce_elem REDUCE_OP remote_elem
2042 /// else
2043 ///     reduce_elem = remote_elem
2044 ///
2045 /// This algorithm assumes that the data to be reduced are located in a
2046 /// contiguous subset of lanes starting from the first.  When there is
2047 /// an odd number of active lanes, the data in the last lane is not
2048 /// aggregated with any other lane's dat but is instead copied over.
2049 ///
2050 /// Dispersed Partial Warp Reduction
2051 ///
2052 /// This algorithm is used within a warp when any discontiguous subset of
2053 /// lanes are active.  It is used to implement the reduction operation
2054 /// across lanes in an OpenMP simd region or in a nested parallel region.
2055 ///
2056 /// void
2057 /// dispersed_partial_reduce(void *reduce_data,
2058 ///                          kmp_ShuffleReductFctPtr ShuffleReduceFn) {
2059 ///   int size, remote_id;
2060 ///   int logical_lane_id = number_of_active_lanes_before_me() * 2;
2061 ///   do {
2062 ///       remote_id = next_active_lane_id_right_after_me();
2063 ///       # the above function returns 0 of no active lane
2064 ///       # is present right after the current lane.
2065 ///       size = number_of_active_lanes_in_this_warp();
2066 ///       logical_lane_id /= 2;
2067 ///       ShuffleReduceFn(reduce_data, logical_lane_id,
2068 ///                       remote_id-1-threadIdx.x, 2);
2069 ///   } while (logical_lane_id % 2 == 0 && size > 1);
2070 /// }
2071 ///
2072 /// There is no assumption made about the initial state of the reduction.
2073 /// Any number of lanes (>=1) could be active at any position.  The reduction
2074 /// result is returned in the first active lane.
2075 ///
2076 /// In this version, 'ShuffleReduceFn' behaves, per element, as follows:
2077 ///
2078 /// remote_elem = shuffle_down(reduce_elem, offset, WARPSIZE);
2079 /// if (lane_id % 2 == 0 && offset > 0)
2080 ///     reduce_elem = reduce_elem REDUCE_OP remote_elem
2081 /// else
2082 ///     reduce_elem = remote_elem
2083 ///
2084 ///
2085 /// Intra-Team Reduction
2086 ///
2087 /// This function, as implemented in the runtime call
2088 /// '__kmpc_nvptx_parallel_reduce_nowait', aggregates data across OpenMP
2089 /// threads in a team.  It first reduces within a warp using the
2090 /// aforementioned algorithms.  We then proceed to gather all such
2091 /// reduced values at the first warp.
2092 ///
2093 /// The runtime makes use of the function 'InterWarpCpyFn', which copies
2094 /// data from each of the "warp master" (zeroth lane of each warp, where
2095 /// warp-reduced data is held) to the zeroth warp.  This step reduces (in
2096 /// a mathematical sense) the problem of reduction across warp masters in
2097 /// a block to the problem of warp reduction.
2098 ///
2099 ///
2100 /// Inter-Team Reduction
2101 ///
2102 /// Once a team has reduced its data to a single value, it is stored in
2103 /// a global scratchpad array.  Since each team has a distinct slot, this
2104 /// can be done without locking.
2105 ///
2106 /// The last team to write to the scratchpad array proceeds to reduce the
2107 /// scratchpad array.  One or more workers in the last team use the helper
2108 /// 'loadAndReduceDataFn' to load and reduce values from the array, i.e.,
2109 /// the k'th worker reduces every k'th element.
2110 ///
2111 /// Finally, a call is made to '__kmpc_nvptx_parallel_reduce_nowait' to
2112 /// reduce across workers and compute a globally reduced value.
2113 ///
2114 void CGOpenMPRuntimeNVPTX::emitReduction(
2115     CodeGenFunction &CGF, SourceLocation Loc, ArrayRef<const Expr *> Privates,
2116     ArrayRef<const Expr *> LHSExprs, ArrayRef<const Expr *> RHSExprs,
2117     ArrayRef<const Expr *> ReductionOps, ReductionOptionsTy Options) {
2118   if (!CGF.HaveInsertPoint())
2119     return;
2120
2121   bool ParallelReduction = isOpenMPParallelDirective(Options.ReductionKind);
2122   bool TeamsReduction = isOpenMPTeamsDirective(Options.ReductionKind);
2123   // FIXME: Add support for simd reduction.
2124   assert((TeamsReduction || ParallelReduction) &&
2125          "Invalid reduction selection in emitReduction.");
2126
2127   auto &C = CGM.getContext();
2128
2129   // 1. Build a list of reduction variables.
2130   // void *RedList[<n>] = {<ReductionVars>[0], ..., <ReductionVars>[<n>-1]};
2131   auto Size = RHSExprs.size();
2132   for (auto *E : Privates) {
2133     if (E->getType()->isVariablyModifiedType())
2134       // Reserve place for array size.
2135       ++Size;
2136   }
2137   llvm::APInt ArraySize(/*unsigned int numBits=*/32, Size);
2138   QualType ReductionArrayTy =
2139       C.getConstantArrayType(C.VoidPtrTy, ArraySize, ArrayType::Normal,
2140                              /*IndexTypeQuals=*/0);
2141   Address ReductionList =
2142       CGF.CreateMemTemp(ReductionArrayTy, ".omp.reduction.red_list");
2143   auto IPriv = Privates.begin();
2144   unsigned Idx = 0;
2145   for (unsigned I = 0, E = RHSExprs.size(); I < E; ++I, ++IPriv, ++Idx) {
2146     Address Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
2147                                                    CGF.getPointerSize());
2148     CGF.Builder.CreateStore(
2149         CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2150             CGF.EmitLValue(RHSExprs[I]).getPointer(), CGF.VoidPtrTy),
2151         Elem);
2152     if ((*IPriv)->getType()->isVariablyModifiedType()) {
2153       // Store array size.
2154       ++Idx;
2155       Elem = CGF.Builder.CreateConstArrayGEP(ReductionList, Idx,
2156                                              CGF.getPointerSize());
2157       llvm::Value *Size = CGF.Builder.CreateIntCast(
2158           CGF.getVLASize(
2159                  CGF.getContext().getAsVariableArrayType((*IPriv)->getType()))
2160               .first,
2161           CGF.SizeTy, /*isSigned=*/false);
2162       CGF.Builder.CreateStore(CGF.Builder.CreateIntToPtr(Size, CGF.VoidPtrTy),
2163                               Elem);
2164     }
2165   }
2166
2167   // 2. Emit reduce_func().
2168   auto *ReductionFn = emitReductionFunction(
2169       CGM, CGF.ConvertTypeForMem(ReductionArrayTy)->getPointerTo(), Privates,
2170       LHSExprs, RHSExprs, ReductionOps);
2171
2172   // 4. Build res = __kmpc_reduce{_nowait}(<gtid>, <n>, sizeof(RedList),
2173   // RedList, shuffle_reduce_func, interwarp_copy_func);
2174   auto *ThreadId = getThreadID(CGF, Loc);
2175   auto *ReductionArrayTySize = CGF.getTypeSize(ReductionArrayTy);
2176   auto *RL = CGF.Builder.CreatePointerBitCastOrAddrSpaceCast(
2177       ReductionList.getPointer(), CGF.VoidPtrTy);
2178
2179   auto *ShuffleAndReduceFn = emitShuffleAndReduceFunction(
2180       CGM, Privates, ReductionArrayTy, ReductionFn);
2181   auto *InterWarpCopyFn =
2182       emitInterWarpCopyFunction(CGM, Privates, ReductionArrayTy);
2183
2184   llvm::Value *Res = nullptr;
2185   if (ParallelReduction) {
2186     llvm::Value *Args[] = {ThreadId,
2187                            CGF.Builder.getInt32(RHSExprs.size()),
2188                            ReductionArrayTySize,
2189                            RL,
2190                            ShuffleAndReduceFn,
2191                            InterWarpCopyFn};
2192
2193     Res = CGF.EmitRuntimeCall(
2194         createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_parallel_reduce_nowait),
2195         Args);
2196   }
2197
2198   if (TeamsReduction) {
2199     auto *ScratchPadCopyFn =
2200         emitCopyToScratchpad(CGM, Privates, ReductionArrayTy);
2201     auto *LoadAndReduceFn = emitReduceScratchpadFunction(
2202         CGM, Privates, ReductionArrayTy, ReductionFn);
2203
2204     llvm::Value *Args[] = {ThreadId,
2205                            CGF.Builder.getInt32(RHSExprs.size()),
2206                            ReductionArrayTySize,
2207                            RL,
2208                            ShuffleAndReduceFn,
2209                            InterWarpCopyFn,
2210                            ScratchPadCopyFn,
2211                            LoadAndReduceFn};
2212     Res = CGF.EmitRuntimeCall(
2213         createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_teams_reduce_nowait),
2214         Args);
2215   }
2216
2217   // 5. Build switch(res)
2218   auto *DefaultBB = CGF.createBasicBlock(".omp.reduction.default");
2219   auto *SwInst = CGF.Builder.CreateSwitch(Res, DefaultBB, /*NumCases=*/1);
2220
2221   // 6. Build case 1: where we have reduced values in the master
2222   //    thread in each team.
2223   //    __kmpc_end_reduce{_nowait}(<gtid>);
2224   //    break;
2225   auto *Case1BB = CGF.createBasicBlock(".omp.reduction.case1");
2226   SwInst->addCase(CGF.Builder.getInt32(1), Case1BB);
2227   CGF.EmitBlock(Case1BB);
2228
2229   // Add emission of __kmpc_end_reduce{_nowait}(<gtid>);
2230   llvm::Value *EndArgs[] = {ThreadId};
2231   auto &&CodeGen = [&Privates, &LHSExprs, &RHSExprs, &ReductionOps,
2232                     this](CodeGenFunction &CGF, PrePostActionTy &Action) {
2233     auto IPriv = Privates.begin();
2234     auto ILHS = LHSExprs.begin();
2235     auto IRHS = RHSExprs.begin();
2236     for (auto *E : ReductionOps) {
2237       emitSingleReductionCombiner(CGF, E, *IPriv, cast<DeclRefExpr>(*ILHS),
2238                                   cast<DeclRefExpr>(*IRHS));
2239       ++IPriv;
2240       ++ILHS;
2241       ++IRHS;
2242     }
2243   };
2244   RegionCodeGenTy RCG(CodeGen);
2245   NVPTXActionTy Action(
2246       nullptr, llvm::None,
2247       createNVPTXRuntimeFunction(OMPRTL_NVPTX__kmpc_end_reduce_nowait),
2248       EndArgs);
2249   RCG.setAction(Action);
2250   RCG(CGF);
2251   CGF.EmitBranch(DefaultBB);
2252   CGF.EmitBlock(DefaultBB, /*IsFinished=*/true);
2253 }