1 //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===//
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 // Take a scop created by ScopInfo and map it to GPU code using the ppcg
11 // GPU mapping strategy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "polly/CodeGen/IslNodeBuilder.h"
16 #include "polly/CodeGen/Utils.h"
17 #include "polly/DependenceInfo.h"
18 #include "polly/LinkAllPasses.h"
19 #include "polly/Options.h"
20 #include "polly/ScopDetection.h"
21 #include "polly/ScopInfo.h"
22 #include "polly/Support/SCEVValidator.h"
23 #include "llvm/ADT/PostOrderIterator.h"
24 #include "llvm/Analysis/AliasAnalysis.h"
25 #include "llvm/Analysis/BasicAliasAnalysis.h"
26 #include "llvm/Analysis/GlobalsModRef.h"
27 #include "llvm/Analysis/PostDominators.h"
28 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
29 #include "llvm/Analysis/TargetLibraryInfo.h"
30 #include "llvm/Analysis/TargetTransformInfo.h"
31 #include "llvm/IR/LegacyPassManager.h"
32 #include "llvm/IR/Verifier.h"
33 #include "llvm/Support/TargetRegistry.h"
34 #include "llvm/Support/TargetSelect.h"
35 #include "llvm/Target/TargetMachine.h"
36 #include "llvm/Transforms/IPO/PassManagerBuilder.h"
37 
38 #include "isl/union_map.h"
39 
40 extern "C" {
41 #include "ppcg/cuda.h"
42 #include "ppcg/gpu.h"
43 #include "ppcg/gpu_print.h"
44 #include "ppcg/ppcg.h"
45 #include "ppcg/schedule.h"
46 }
47 
48 #include "llvm/Support/Debug.h"
49 
50 using namespace polly;
51 using namespace llvm;
52 
53 #define DEBUG_TYPE "polly-codegen-ppcg"
54 
55 static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
56                                   cl::desc("Dump the computed GPU Schedule"),
57                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
58                                   cl::cat(PollyCategory));
59 
60 static cl::opt<bool>
61     DumpCode("polly-acc-dump-code",
62              cl::desc("Dump C code describing the GPU mapping"), cl::Hidden,
63              cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
64 
65 static cl::opt<bool> DumpKernelIR("polly-acc-dump-kernel-ir",
66                                   cl::desc("Dump the kernel LLVM-IR"),
67                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
68                                   cl::cat(PollyCategory));
69 
70 static cl::opt<bool> DumpKernelASM("polly-acc-dump-kernel-asm",
71                                    cl::desc("Dump the kernel assembly code"),
72                                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
73                                    cl::cat(PollyCategory));
74 
75 static cl::opt<bool> FastMath("polly-acc-fastmath",
76                               cl::desc("Allow unsafe math optimizations"),
77                               cl::Hidden, cl::init(false), cl::ZeroOrMore,
78                               cl::cat(PollyCategory));
79 static cl::opt<bool> SharedMemory("polly-acc-use-shared",
80                                   cl::desc("Use shared memory"), cl::Hidden,
81                                   cl::init(false), cl::ZeroOrMore,
82                                   cl::cat(PollyCategory));
83 static cl::opt<bool> PrivateMemory("polly-acc-use-private",
84                                    cl::desc("Use private memory"), cl::Hidden,
85                                    cl::init(false), cl::ZeroOrMore,
86                                    cl::cat(PollyCategory));
87 
88 static cl::opt<std::string>
89     CudaVersion("polly-acc-cuda-version",
90                 cl::desc("The CUDA version to compile for"), cl::Hidden,
91                 cl::init("sm_30"), cl::ZeroOrMore, cl::cat(PollyCategory));
92 
93 /// Create the ast expressions for a ScopStmt.
94 ///
95 /// This function is a callback for to generate the ast expressions for each
96 /// of the scheduled ScopStmts.
97 static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
98     void *StmtT, isl_ast_build *Build,
99     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
100                                        isl_id *Id, void *User),
101     void *UserIndex,
102     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
103     void *UserExpr) {
104 
105   ScopStmt *Stmt = (ScopStmt *)StmtT;
106 
107   isl_ctx *Ctx;
108 
109   if (!Stmt || !Build)
110     return NULL;
111 
112   Ctx = isl_ast_build_get_ctx(Build);
113   isl_id_to_ast_expr *RefToExpr = isl_id_to_ast_expr_alloc(Ctx, 0);
114 
115   for (MemoryAccess *Acc : *Stmt) {
116     isl_map *AddrFunc = Acc->getAddressFunction();
117     AddrFunc = isl_map_intersect_domain(AddrFunc, Stmt->getDomain());
118     isl_id *RefId = Acc->getId();
119     isl_pw_multi_aff *PMA = isl_pw_multi_aff_from_map(AddrFunc);
120     isl_multi_pw_aff *MPA = isl_multi_pw_aff_from_pw_multi_aff(PMA);
121     MPA = isl_multi_pw_aff_coalesce(MPA);
122     MPA = FunctionIndex(MPA, RefId, UserIndex);
123     isl_ast_expr *Access = isl_ast_build_access_from_multi_pw_aff(Build, MPA);
124     Access = FunctionExpr(Access, RefId, UserExpr);
125     RefToExpr = isl_id_to_ast_expr_set(RefToExpr, RefId, Access);
126   }
127 
128   return RefToExpr;
129 }
130 
131 /// Generate code for a GPU specific isl AST.
132 ///
133 /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which
134 /// generates code for general-prupose AST nodes, with special functionality
135 /// for generating GPU specific user nodes.
136 ///
137 /// @see GPUNodeBuilder::createUser
138 class GPUNodeBuilder : public IslNodeBuilder {
139 public:
140   GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
141                  const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
142                  DominatorTree &DT, Scop &S, gpu_prog *Prog)
143       : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S), Prog(Prog) {
144     getExprBuilder().setIDToSAI(&IDToSAI);
145   }
146 
147   /// Create after-run-time-check initialization code.
148   void initializeAfterRTH();
149 
150   /// Finalize the generated scop.
151   virtual void finalize();
152 
153 private:
154   /// A vector of array base pointers for which a new ScopArrayInfo was created.
155   ///
156   /// This vector is used to delete the ScopArrayInfo when it is not needed any
157   /// more.
158   std::vector<Value *> LocalArrays;
159 
160   /// A map from ScopArrays to their corresponding device allocations.
161   std::map<ScopArrayInfo *, Value *> DeviceAllocations;
162 
163   /// The current GPU context.
164   Value *GPUContext;
165 
166   /// The set of isl_ids allocated in the kernel
167   std::vector<isl_id *> KernelIds;
168 
169   /// A module containing GPU code.
170   ///
171   /// This pointer is only set in case we are currently generating GPU code.
172   std::unique_ptr<Module> GPUModule;
173 
174   /// The GPU program we generate code for.
175   gpu_prog *Prog;
176 
177   /// Class to free isl_ids.
178   class IslIdDeleter {
179   public:
180     void operator()(__isl_take isl_id *Id) { isl_id_free(Id); };
181   };
182 
183   /// A set containing all isl_ids allocated in a GPU kernel.
184   ///
185   /// By releasing this set all isl_ids will be freed.
186   std::set<std::unique_ptr<isl_id, IslIdDeleter>> KernelIDs;
187 
188   IslExprBuilder::IDToScopArrayInfoTy IDToSAI;
189 
190   /// Create code for user-defined AST nodes.
191   ///
192   /// These AST nodes can be of type:
193   ///
194   ///   - ScopStmt:      A computational statement (TODO)
195   ///   - Kernel:        A GPU kernel call (TODO)
196   ///   - Data-Transfer: A GPU <-> CPU data-transfer
197   ///   - In-kernel synchronization
198   ///   - In-kernel memory copy statement
199   ///
200   /// @param UserStmt The ast node to generate code for.
201   virtual void createUser(__isl_take isl_ast_node *UserStmt);
202 
203   enum DataDirection { HOST_TO_DEVICE, DEVICE_TO_HOST };
204 
205   /// Create code for a data transfer statement
206   ///
207   /// @param TransferStmt The data transfer statement.
208   /// @param Direction The direction in which to transfer data.
209   void createDataTransfer(__isl_take isl_ast_node *TransferStmt,
210                           enum DataDirection Direction);
211 
212   /// Find llvm::Values referenced in GPU kernel.
213   ///
214   /// @param Kernel The kernel to scan for llvm::Values
215   ///
216   /// @returns A set of values referenced by the kernel.
217   SetVector<Value *> getReferencesInKernel(ppcg_kernel *Kernel);
218 
219   /// Compute the sizes of the execution grid for a given kernel.
220   ///
221   /// @param Kernel The kernel to compute grid sizes for.
222   ///
223   /// @returns A tuple with grid sizes for X and Y dimension
224   std::tuple<Value *, Value *> getGridSizes(ppcg_kernel *Kernel);
225 
226   /// Compute the sizes of the thread blocks for a given kernel.
227   ///
228   /// @param Kernel The kernel to compute thread block sizes for.
229   ///
230   /// @returns A tuple with thread block sizes for X, Y, and Z dimensions.
231   std::tuple<Value *, Value *, Value *> getBlockSizes(ppcg_kernel *Kernel);
232 
233   /// Create kernel launch parameters.
234   ///
235   /// @param Kernel        The kernel to create parameters for.
236   /// @param F             The kernel function that has been created.
237   /// @param SubtreeValues The set of llvm::Values referenced by this kernel.
238   ///
239   /// @returns A stack allocated array with pointers to the parameter
240   ///          values that are passed to the kernel.
241   Value *createLaunchParameters(ppcg_kernel *Kernel, Function *F,
242                                 SetVector<Value *> SubtreeValues);
243 
244   /// Create declarations for kernel variable.
245   ///
246   /// This includes shared memory declarations.
247   ///
248   /// @param Kernel        The kernel definition to create variables for.
249   /// @param FN            The function into which to generate the variables.
250   void createKernelVariables(ppcg_kernel *Kernel, Function *FN);
251 
252   /// Add CUDA annotations to module.
253   ///
254   /// Add a set of CUDA annotations that declares the maximal block dimensions
255   /// that will be used to execute the CUDA kernel. This allows the NVIDIA
256   /// PTX compiler to bound the number of allocated registers to ensure the
257   /// resulting kernel is known to run with up to as many block dimensions
258   /// as specified here.
259   ///
260   /// @param M         The module to add the annotations to.
261   /// @param BlockDimX The size of block dimension X.
262   /// @param BlockDimY The size of block dimension Y.
263   /// @param BlockDimZ The size of block dimension Z.
264   void addCUDAAnnotations(Module *M, Value *BlockDimX, Value *BlockDimY,
265                           Value *BlockDimZ);
266 
267   /// Create GPU kernel.
268   ///
269   /// Code generate the kernel described by @p KernelStmt.
270   ///
271   /// @param KernelStmt The ast node to generate kernel code for.
272   void createKernel(__isl_take isl_ast_node *KernelStmt);
273 
274   /// Generate code that computes the size of an array.
275   ///
276   /// @param Array The array for which to compute a size.
277   Value *getArraySize(gpu_array_info *Array);
278 
279   /// Prepare the kernel arguments for kernel code generation
280   ///
281   /// @param Kernel The kernel to generate code for.
282   /// @param FN     The function created for the kernel.
283   void prepareKernelArguments(ppcg_kernel *Kernel, Function *FN);
284 
285   /// Create kernel function.
286   ///
287   /// Create a kernel function located in a newly created module that can serve
288   /// as target for device code generation. Set the Builder to point to the
289   /// start block of this newly created function.
290   ///
291   /// @param Kernel The kernel to generate code for.
292   /// @param SubtreeValues The set of llvm::Values referenced by this kernel.
293   void createKernelFunction(ppcg_kernel *Kernel,
294                             SetVector<Value *> &SubtreeValues);
295 
296   /// Create the declaration of a kernel function.
297   ///
298   /// The kernel function takes as arguments:
299   ///
300   ///   - One i8 pointer for each external array reference used in the kernel.
301   ///   - Host iterators
302   ///   - Parameters
303   ///   - Other LLVM Value references (TODO)
304   ///
305   /// @param Kernel The kernel to generate the function declaration for.
306   /// @param SubtreeValues The set of llvm::Values referenced by this kernel.
307   ///
308   /// @returns The newly declared function.
309   Function *createKernelFunctionDecl(ppcg_kernel *Kernel,
310                                      SetVector<Value *> &SubtreeValues);
311 
312   /// Insert intrinsic functions to obtain thread and block ids.
313   ///
314   /// @param The kernel to generate the intrinsic functions for.
315   void insertKernelIntrinsics(ppcg_kernel *Kernel);
316 
317   /// Create a global-to-shared or shared-to-global copy statement.
318   ///
319   /// @param CopyStmt The copy statement to generate code for
320   void createKernelCopy(ppcg_kernel_stmt *CopyStmt);
321 
322   /// Create code for a ScopStmt called in @p Expr.
323   ///
324   /// @param Expr The expression containing the call.
325   /// @param KernelStmt The kernel statement referenced in the call.
326   void createScopStmt(isl_ast_expr *Expr, ppcg_kernel_stmt *KernelStmt);
327 
328   /// Create an in-kernel synchronization call.
329   void createKernelSync();
330 
331   /// Create a PTX assembly string for the current GPU kernel.
332   ///
333   /// @returns A string containing the corresponding PTX assembly code.
334   std::string createKernelASM();
335 
336   /// Remove references from the dominator tree to the kernel function @p F.
337   ///
338   /// @param F The function to remove references to.
339   void clearDominators(Function *F);
340 
341   /// Remove references from scalar evolution to the kernel function @p F.
342   ///
343   /// @param F The function to remove references to.
344   void clearScalarEvolution(Function *F);
345 
346   /// Remove references from loop info to the kernel function @p F.
347   ///
348   /// @param F The function to remove references to.
349   void clearLoops(Function *F);
350 
351   /// Finalize the generation of the kernel function.
352   ///
353   /// Free the LLVM-IR module corresponding to the kernel and -- if requested --
354   /// dump its IR to stderr.
355   ///
356   /// @returns The Assembly string of the kernel.
357   std::string finalizeKernelFunction();
358 
359   /// Create code that allocates memory to store arrays on device.
360   void allocateDeviceArrays();
361 
362   /// Free all allocated device arrays.
363   void freeDeviceArrays();
364 
365   /// Create a call to initialize the GPU context.
366   ///
367   /// @returns A pointer to the newly initialized context.
368   Value *createCallInitContext();
369 
370   /// Create a call to get the device pointer for a kernel allocation.
371   ///
372   /// @param Allocation The Polly GPU allocation
373   ///
374   /// @returns The device parameter corresponding to this allocation.
375   Value *createCallGetDevicePtr(Value *Allocation);
376 
377   /// Create a call to free the GPU context.
378   ///
379   /// @param Context A pointer to an initialized GPU context.
380   void createCallFreeContext(Value *Context);
381 
382   /// Create a call to allocate memory on the device.
383   ///
384   /// @param Size The size of memory to allocate
385   ///
386   /// @returns A pointer that identifies this allocation.
387   Value *createCallAllocateMemoryForDevice(Value *Size);
388 
389   /// Create a call to free a device array.
390   ///
391   /// @param Array The device array to free.
392   void createCallFreeDeviceMemory(Value *Array);
393 
394   /// Create a call to copy data from host to device.
395   ///
396   /// @param HostPtr A pointer to the host data that should be copied.
397   /// @param DevicePtr A device pointer specifying the location to copy to.
398   void createCallCopyFromHostToDevice(Value *HostPtr, Value *DevicePtr,
399                                       Value *Size);
400 
401   /// Create a call to copy data from device to host.
402   ///
403   /// @param DevicePtr A pointer to the device data that should be copied.
404   /// @param HostPtr A host pointer specifying the location to copy to.
405   void createCallCopyFromDeviceToHost(Value *DevicePtr, Value *HostPtr,
406                                       Value *Size);
407 
408   /// Create a call to get a kernel from an assembly string.
409   ///
410   /// @param Buffer The string describing the kernel.
411   /// @param Entry  The name of the kernel function to call.
412   ///
413   /// @returns A pointer to a kernel object
414   Value *createCallGetKernel(Value *Buffer, Value *Entry);
415 
416   /// Create a call to free a GPU kernel.
417   ///
418   /// @param GPUKernel THe kernel to free.
419   void createCallFreeKernel(Value *GPUKernel);
420 
421   /// Create a call to launch a GPU kernel.
422   ///
423   /// @param GPUKernel  The kernel to launch.
424   /// @param GridDimX   The size of the first grid dimension.
425   /// @param GridDimY   The size of the second grid dimension.
426   /// @param GridBlockX The size of the first block dimension.
427   /// @param GridBlockY The size of the second block dimension.
428   /// @param GridBlockZ The size of the third block dimension.
429   /// @param Paramters  A pointer to an array that contains itself pointers to
430   ///                   the parameter values passed for each kernel argument.
431   void createCallLaunchKernel(Value *GPUKernel, Value *GridDimX,
432                               Value *GridDimY, Value *BlockDimX,
433                               Value *BlockDimY, Value *BlockDimZ,
434                               Value *Parameters);
435 };
436 
437 void GPUNodeBuilder::initializeAfterRTH() {
438   GPUContext = createCallInitContext();
439   allocateDeviceArrays();
440 }
441 
442 void GPUNodeBuilder::finalize() {
443   freeDeviceArrays();
444   createCallFreeContext(GPUContext);
445   IslNodeBuilder::finalize();
446 }
447 
448 void GPUNodeBuilder::allocateDeviceArrays() {
449   isl_ast_build *Build = isl_ast_build_from_context(S.getContext());
450 
451   for (int i = 0; i < Prog->n_array; ++i) {
452     gpu_array_info *Array = &Prog->array[i];
453     auto *ScopArray = (ScopArrayInfo *)Array->user;
454     std::string DevArrayName("p_dev_array_");
455     DevArrayName.append(Array->name);
456 
457     Value *ArraySize = getArraySize(Array);
458     Value *DevArray = createCallAllocateMemoryForDevice(ArraySize);
459     DevArray->setName(DevArrayName);
460     DeviceAllocations[ScopArray] = DevArray;
461   }
462 
463   isl_ast_build_free(Build);
464 }
465 
466 void GPUNodeBuilder::addCUDAAnnotations(Module *M, Value *BlockDimX,
467                                         Value *BlockDimY, Value *BlockDimZ) {
468   auto AnnotationNode = M->getOrInsertNamedMetadata("nvvm.annotations");
469 
470   for (auto &F : *M) {
471     if (F.getCallingConv() != CallingConv::PTX_Kernel)
472       continue;
473 
474     Value *V[] = {BlockDimX, BlockDimY, BlockDimZ};
475 
476     Metadata *Elements[] = {
477         ValueAsMetadata::get(&F),   MDString::get(M->getContext(), "maxntidx"),
478         ValueAsMetadata::get(V[0]), MDString::get(M->getContext(), "maxntidy"),
479         ValueAsMetadata::get(V[1]), MDString::get(M->getContext(), "maxntidz"),
480         ValueAsMetadata::get(V[2]),
481     };
482     MDNode *Node = MDNode::get(M->getContext(), Elements);
483     AnnotationNode->addOperand(Node);
484   }
485 }
486 
487 void GPUNodeBuilder::freeDeviceArrays() {
488   for (auto &Array : DeviceAllocations)
489     createCallFreeDeviceMemory(Array.second);
490 }
491 
492 Value *GPUNodeBuilder::createCallGetKernel(Value *Buffer, Value *Entry) {
493   const char *Name = "polly_getKernel";
494   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
495   Function *F = M->getFunction(Name);
496 
497   // If F is not available, declare it.
498   if (!F) {
499     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
500     std::vector<Type *> Args;
501     Args.push_back(Builder.getInt8PtrTy());
502     Args.push_back(Builder.getInt8PtrTy());
503     FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false);
504     F = Function::Create(Ty, Linkage, Name, M);
505   }
506 
507   return Builder.CreateCall(F, {Buffer, Entry});
508 }
509 
510 Value *GPUNodeBuilder::createCallGetDevicePtr(Value *Allocation) {
511   const char *Name = "polly_getDevicePtr";
512   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
513   Function *F = M->getFunction(Name);
514 
515   // If F is not available, declare it.
516   if (!F) {
517     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
518     std::vector<Type *> Args;
519     Args.push_back(Builder.getInt8PtrTy());
520     FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false);
521     F = Function::Create(Ty, Linkage, Name, M);
522   }
523 
524   return Builder.CreateCall(F, {Allocation});
525 }
526 
527 void GPUNodeBuilder::createCallLaunchKernel(Value *GPUKernel, Value *GridDimX,
528                                             Value *GridDimY, Value *BlockDimX,
529                                             Value *BlockDimY, Value *BlockDimZ,
530                                             Value *Parameters) {
531   const char *Name = "polly_launchKernel";
532   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
533   Function *F = M->getFunction(Name);
534 
535   // If F is not available, declare it.
536   if (!F) {
537     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
538     std::vector<Type *> Args;
539     Args.push_back(Builder.getInt8PtrTy());
540     Args.push_back(Builder.getInt32Ty());
541     Args.push_back(Builder.getInt32Ty());
542     Args.push_back(Builder.getInt32Ty());
543     Args.push_back(Builder.getInt32Ty());
544     Args.push_back(Builder.getInt32Ty());
545     Args.push_back(Builder.getInt8PtrTy());
546     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
547     F = Function::Create(Ty, Linkage, Name, M);
548   }
549 
550   Builder.CreateCall(F, {GPUKernel, GridDimX, GridDimY, BlockDimX, BlockDimY,
551                          BlockDimZ, Parameters});
552 }
553 
554 void GPUNodeBuilder::createCallFreeKernel(Value *GPUKernel) {
555   const char *Name = "polly_freeKernel";
556   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
557   Function *F = M->getFunction(Name);
558 
559   // If F is not available, declare it.
560   if (!F) {
561     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
562     std::vector<Type *> Args;
563     Args.push_back(Builder.getInt8PtrTy());
564     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
565     F = Function::Create(Ty, Linkage, Name, M);
566   }
567 
568   Builder.CreateCall(F, {GPUKernel});
569 }
570 
571 void GPUNodeBuilder::createCallFreeDeviceMemory(Value *Array) {
572   const char *Name = "polly_freeDeviceMemory";
573   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
574   Function *F = M->getFunction(Name);
575 
576   // If F is not available, declare it.
577   if (!F) {
578     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
579     std::vector<Type *> Args;
580     Args.push_back(Builder.getInt8PtrTy());
581     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
582     F = Function::Create(Ty, Linkage, Name, M);
583   }
584 
585   Builder.CreateCall(F, {Array});
586 }
587 
588 Value *GPUNodeBuilder::createCallAllocateMemoryForDevice(Value *Size) {
589   const char *Name = "polly_allocateMemoryForDevice";
590   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
591   Function *F = M->getFunction(Name);
592 
593   // If F is not available, declare it.
594   if (!F) {
595     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
596     std::vector<Type *> Args;
597     Args.push_back(Builder.getInt64Ty());
598     FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false);
599     F = Function::Create(Ty, Linkage, Name, M);
600   }
601 
602   return Builder.CreateCall(F, {Size});
603 }
604 
605 void GPUNodeBuilder::createCallCopyFromHostToDevice(Value *HostData,
606                                                     Value *DeviceData,
607                                                     Value *Size) {
608   const char *Name = "polly_copyFromHostToDevice";
609   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
610   Function *F = M->getFunction(Name);
611 
612   // If F is not available, declare it.
613   if (!F) {
614     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
615     std::vector<Type *> Args;
616     Args.push_back(Builder.getInt8PtrTy());
617     Args.push_back(Builder.getInt8PtrTy());
618     Args.push_back(Builder.getInt64Ty());
619     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
620     F = Function::Create(Ty, Linkage, Name, M);
621   }
622 
623   Builder.CreateCall(F, {HostData, DeviceData, Size});
624 }
625 
626 void GPUNodeBuilder::createCallCopyFromDeviceToHost(Value *DeviceData,
627                                                     Value *HostData,
628                                                     Value *Size) {
629   const char *Name = "polly_copyFromDeviceToHost";
630   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
631   Function *F = M->getFunction(Name);
632 
633   // If F is not available, declare it.
634   if (!F) {
635     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
636     std::vector<Type *> Args;
637     Args.push_back(Builder.getInt8PtrTy());
638     Args.push_back(Builder.getInt8PtrTy());
639     Args.push_back(Builder.getInt64Ty());
640     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
641     F = Function::Create(Ty, Linkage, Name, M);
642   }
643 
644   Builder.CreateCall(F, {DeviceData, HostData, Size});
645 }
646 
647 Value *GPUNodeBuilder::createCallInitContext() {
648   const char *Name = "polly_initContext";
649   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
650   Function *F = M->getFunction(Name);
651 
652   // If F is not available, declare it.
653   if (!F) {
654     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
655     std::vector<Type *> Args;
656     FunctionType *Ty = FunctionType::get(Builder.getInt8PtrTy(), Args, false);
657     F = Function::Create(Ty, Linkage, Name, M);
658   }
659 
660   return Builder.CreateCall(F, {});
661 }
662 
663 void GPUNodeBuilder::createCallFreeContext(Value *Context) {
664   const char *Name = "polly_freeContext";
665   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
666   Function *F = M->getFunction(Name);
667 
668   // If F is not available, declare it.
669   if (!F) {
670     GlobalValue::LinkageTypes Linkage = Function::ExternalLinkage;
671     std::vector<Type *> Args;
672     Args.push_back(Builder.getInt8PtrTy());
673     FunctionType *Ty = FunctionType::get(Builder.getVoidTy(), Args, false);
674     F = Function::Create(Ty, Linkage, Name, M);
675   }
676 
677   Builder.CreateCall(F, {Context});
678 }
679 
680 /// Check if one string is a prefix of another.
681 ///
682 /// @param String The string in which to look for the prefix.
683 /// @param Prefix The prefix to look for.
684 static bool isPrefix(std::string String, std::string Prefix) {
685   return String.find(Prefix) == 0;
686 }
687 
688 Value *GPUNodeBuilder::getArraySize(gpu_array_info *Array) {
689   isl_ast_build *Build = isl_ast_build_from_context(S.getContext());
690   Value *ArraySize = ConstantInt::get(Builder.getInt64Ty(), Array->size);
691 
692   if (!gpu_array_is_scalar(Array)) {
693     auto OffsetDimZero = isl_pw_aff_copy(Array->bound[0]);
694     isl_ast_expr *Res = isl_ast_build_expr_from_pw_aff(Build, OffsetDimZero);
695 
696     for (unsigned int i = 1; i < Array->n_index; i++) {
697       isl_pw_aff *Bound_I = isl_pw_aff_copy(Array->bound[i]);
698       isl_ast_expr *Expr = isl_ast_build_expr_from_pw_aff(Build, Bound_I);
699       Res = isl_ast_expr_mul(Res, Expr);
700     }
701 
702     Value *NumElements = ExprBuilder.create(Res);
703     ArraySize = Builder.CreateMul(ArraySize, NumElements);
704   }
705   isl_ast_build_free(Build);
706   return ArraySize;
707 }
708 
709 void GPUNodeBuilder::createDataTransfer(__isl_take isl_ast_node *TransferStmt,
710                                         enum DataDirection Direction) {
711   isl_ast_expr *Expr = isl_ast_node_user_get_expr(TransferStmt);
712   isl_ast_expr *Arg = isl_ast_expr_get_op_arg(Expr, 0);
713   isl_id *Id = isl_ast_expr_get_id(Arg);
714   auto Array = (gpu_array_info *)isl_id_get_user(Id);
715   auto ScopArray = (ScopArrayInfo *)(Array->user);
716 
717   Value *Size = getArraySize(Array);
718   Value *HostPtr = ScopArray->getBasePtr();
719 
720   Value *DevPtr = DeviceAllocations[ScopArray];
721 
722   if (gpu_array_is_scalar(Array)) {
723     HostPtr = Builder.CreateAlloca(ScopArray->getElementType());
724     Builder.CreateStore(ScopArray->getBasePtr(), HostPtr);
725   }
726 
727   HostPtr = Builder.CreatePointerCast(HostPtr, Builder.getInt8PtrTy());
728 
729   if (Direction == HOST_TO_DEVICE)
730     createCallCopyFromHostToDevice(HostPtr, DevPtr, Size);
731   else
732     createCallCopyFromDeviceToHost(DevPtr, HostPtr, Size);
733 
734   isl_id_free(Id);
735   isl_ast_expr_free(Arg);
736   isl_ast_expr_free(Expr);
737   isl_ast_node_free(TransferStmt);
738 }
739 
740 void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) {
741   isl_ast_expr *Expr = isl_ast_node_user_get_expr(UserStmt);
742   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
743   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
744   isl_id_free(Id);
745   isl_ast_expr_free(StmtExpr);
746 
747   const char *Str = isl_id_get_name(Id);
748   if (!strcmp(Str, "kernel")) {
749     createKernel(UserStmt);
750     isl_ast_expr_free(Expr);
751     return;
752   }
753 
754   if (isPrefix(Str, "to_device")) {
755     createDataTransfer(UserStmt, HOST_TO_DEVICE);
756     isl_ast_expr_free(Expr);
757     return;
758   }
759 
760   if (isPrefix(Str, "from_device")) {
761     createDataTransfer(UserStmt, DEVICE_TO_HOST);
762     isl_ast_expr_free(Expr);
763     return;
764   }
765 
766   isl_id *Anno = isl_ast_node_get_annotation(UserStmt);
767   struct ppcg_kernel_stmt *KernelStmt =
768       (struct ppcg_kernel_stmt *)isl_id_get_user(Anno);
769   isl_id_free(Anno);
770 
771   switch (KernelStmt->type) {
772   case ppcg_kernel_domain:
773     createScopStmt(Expr, KernelStmt);
774     isl_ast_node_free(UserStmt);
775     return;
776   case ppcg_kernel_copy:
777     createKernelCopy(KernelStmt);
778     isl_ast_expr_free(Expr);
779     isl_ast_node_free(UserStmt);
780     return;
781   case ppcg_kernel_sync:
782     createKernelSync();
783     isl_ast_expr_free(Expr);
784     isl_ast_node_free(UserStmt);
785     return;
786   }
787 
788   isl_ast_expr_free(Expr);
789   isl_ast_node_free(UserStmt);
790   return;
791 }
792 void GPUNodeBuilder::createKernelCopy(ppcg_kernel_stmt *KernelStmt) {
793   isl_ast_expr *LocalIndex = isl_ast_expr_copy(KernelStmt->u.c.local_index);
794   LocalIndex = isl_ast_expr_address_of(LocalIndex);
795   Value *LocalAddr = ExprBuilder.create(LocalIndex);
796   isl_ast_expr *Index = isl_ast_expr_copy(KernelStmt->u.c.index);
797   Index = isl_ast_expr_address_of(Index);
798   Value *GlobalAddr = ExprBuilder.create(Index);
799 
800   if (KernelStmt->u.c.read) {
801     LoadInst *Load = Builder.CreateLoad(GlobalAddr, "shared.read");
802     Builder.CreateStore(Load, LocalAddr);
803   } else {
804     LoadInst *Load = Builder.CreateLoad(LocalAddr, "shared.write");
805     Builder.CreateStore(Load, GlobalAddr);
806   }
807 }
808 
809 void GPUNodeBuilder::createScopStmt(isl_ast_expr *Expr,
810                                     ppcg_kernel_stmt *KernelStmt) {
811   auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt;
812   isl_id_to_ast_expr *Indexes = KernelStmt->u.d.ref2expr;
813 
814   LoopToScevMapT LTS;
815   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
816 
817   createSubstitutions(Expr, Stmt, LTS);
818 
819   if (Stmt->isBlockStmt())
820     BlockGen.copyStmt(*Stmt, LTS, Indexes);
821   else
822     assert(0 && "Region statement not supported\n");
823 }
824 
825 void GPUNodeBuilder::createKernelSync() {
826   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
827   auto *Sync = Intrinsic::getDeclaration(M, Intrinsic::nvvm_barrier0);
828   Builder.CreateCall(Sync, {});
829 }
830 
831 /// Collect llvm::Values referenced from @p Node
832 ///
833 /// This function only applies to isl_ast_nodes that are user_nodes referring
834 /// to a ScopStmt. All other node types are ignore.
835 ///
836 /// @param Node The node to collect references for.
837 /// @param User A user pointer used as storage for the data that is collected.
838 ///
839 /// @returns isl_bool_true if data could be collected successfully.
840 isl_bool collectReferencesInGPUStmt(__isl_keep isl_ast_node *Node, void *User) {
841   if (isl_ast_node_get_type(Node) != isl_ast_node_user)
842     return isl_bool_true;
843 
844   isl_ast_expr *Expr = isl_ast_node_user_get_expr(Node);
845   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
846   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
847   const char *Str = isl_id_get_name(Id);
848   isl_id_free(Id);
849   isl_ast_expr_free(StmtExpr);
850   isl_ast_expr_free(Expr);
851 
852   if (!isPrefix(Str, "Stmt"))
853     return isl_bool_true;
854 
855   Id = isl_ast_node_get_annotation(Node);
856   auto *KernelStmt = (ppcg_kernel_stmt *)isl_id_get_user(Id);
857   auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt;
858   isl_id_free(Id);
859 
860   addReferencesFromStmt(Stmt, User, false /* CreateScalarRefs */);
861 
862   return isl_bool_true;
863 }
864 
865 SetVector<Value *> GPUNodeBuilder::getReferencesInKernel(ppcg_kernel *Kernel) {
866   SetVector<Value *> SubtreeValues;
867   SetVector<const SCEV *> SCEVs;
868   SetVector<const Loop *> Loops;
869   SubtreeReferences References = {
870       LI, SE, S, ValueMap, SubtreeValues, SCEVs, getBlockGenerator()};
871 
872   for (const auto &I : IDToValue)
873     SubtreeValues.insert(I.second);
874 
875   isl_ast_node_foreach_descendant_top_down(
876       Kernel->tree, collectReferencesInGPUStmt, &References);
877 
878   for (const SCEV *Expr : SCEVs)
879     findValues(Expr, SE, SubtreeValues);
880 
881   for (auto &SAI : S.arrays())
882     SubtreeValues.remove(SAI->getBasePtr());
883 
884   isl_space *Space = S.getParamSpace();
885   for (long i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
886     isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i);
887     assert(IDToValue.count(Id));
888     Value *Val = IDToValue[Id];
889     SubtreeValues.remove(Val);
890     isl_id_free(Id);
891   }
892   isl_space_free(Space);
893 
894   for (long i = 0; i < isl_space_dim(Kernel->space, isl_dim_set); i++) {
895     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
896     assert(IDToValue.count(Id));
897     Value *Val = IDToValue[Id];
898     SubtreeValues.remove(Val);
899     isl_id_free(Id);
900   }
901 
902   return SubtreeValues;
903 }
904 
905 void GPUNodeBuilder::clearDominators(Function *F) {
906   DomTreeNode *N = DT.getNode(&F->getEntryBlock());
907   std::vector<BasicBlock *> Nodes;
908   for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
909     Nodes.push_back(I->getBlock());
910 
911   for (BasicBlock *BB : Nodes)
912     DT.eraseNode(BB);
913 }
914 
915 void GPUNodeBuilder::clearScalarEvolution(Function *F) {
916   for (BasicBlock &BB : *F) {
917     Loop *L = LI.getLoopFor(&BB);
918     if (L)
919       SE.forgetLoop(L);
920   }
921 }
922 
923 void GPUNodeBuilder::clearLoops(Function *F) {
924   for (BasicBlock &BB : *F) {
925     Loop *L = LI.getLoopFor(&BB);
926     if (L)
927       SE.forgetLoop(L);
928     LI.removeBlock(&BB);
929   }
930 }
931 
932 std::tuple<Value *, Value *> GPUNodeBuilder::getGridSizes(ppcg_kernel *Kernel) {
933   std::vector<Value *> Sizes;
934   isl_ast_build *Context = isl_ast_build_from_context(S.getContext());
935 
936   for (long i = 0; i < Kernel->n_grid; i++) {
937     isl_pw_aff *Size = isl_multi_pw_aff_get_pw_aff(Kernel->grid_size, i);
938     isl_ast_expr *GridSize = isl_ast_build_expr_from_pw_aff(Context, Size);
939     Value *Res = ExprBuilder.create(GridSize);
940     Res = Builder.CreateTrunc(Res, Builder.getInt32Ty());
941     Sizes.push_back(Res);
942   }
943   isl_ast_build_free(Context);
944 
945   for (long i = Kernel->n_grid; i < 3; i++)
946     Sizes.push_back(ConstantInt::get(Builder.getInt32Ty(), 1));
947 
948   return std::make_tuple(Sizes[0], Sizes[1]);
949 }
950 
951 std::tuple<Value *, Value *, Value *>
952 GPUNodeBuilder::getBlockSizes(ppcg_kernel *Kernel) {
953   std::vector<Value *> Sizes;
954 
955   for (long i = 0; i < Kernel->n_block; i++) {
956     Value *Res = ConstantInt::get(Builder.getInt32Ty(), Kernel->block_dim[i]);
957     Sizes.push_back(Res);
958   }
959 
960   for (long i = Kernel->n_block; i < 3; i++)
961     Sizes.push_back(ConstantInt::get(Builder.getInt32Ty(), 1));
962 
963   return std::make_tuple(Sizes[0], Sizes[1], Sizes[2]);
964 }
965 
966 Value *
967 GPUNodeBuilder::createLaunchParameters(ppcg_kernel *Kernel, Function *F,
968                                        SetVector<Value *> SubtreeValues) {
969   Type *ArrayTy = ArrayType::get(Builder.getInt8PtrTy(),
970                                  std::distance(F->arg_begin(), F->arg_end()));
971 
972   BasicBlock *EntryBlock =
973       &Builder.GetInsertBlock()->getParent()->getEntryBlock();
974   std::string Launch = "polly_launch_" + std::to_string(Kernel->id);
975   Instruction *Parameters =
976       new AllocaInst(ArrayTy, Launch + "_params", EntryBlock->getTerminator());
977 
978   int Index = 0;
979   for (long i = 0; i < Prog->n_array; i++) {
980     if (!ppcg_kernel_requires_array_argument(Kernel, i))
981       continue;
982 
983     isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set);
984     const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(Id);
985 
986     Value *DevArray = DeviceAllocations[(ScopArrayInfo *)SAI];
987     DevArray = createCallGetDevicePtr(DevArray);
988     Instruction *Param = new AllocaInst(
989         Builder.getInt8PtrTy(), Launch + "_param_" + std::to_string(Index),
990         EntryBlock->getTerminator());
991     Builder.CreateStore(DevArray, Param);
992     Value *Slot = Builder.CreateGEP(
993         Parameters, {Builder.getInt64(0), Builder.getInt64(Index)});
994     Value *ParamTyped =
995         Builder.CreatePointerCast(Param, Builder.getInt8PtrTy());
996     Builder.CreateStore(ParamTyped, Slot);
997     Index++;
998   }
999 
1000   int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set);
1001 
1002   for (long i = 0; i < NumHostIters; i++) {
1003     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
1004     Value *Val = IDToValue[Id];
1005     isl_id_free(Id);
1006     Instruction *Param = new AllocaInst(
1007         Val->getType(), Launch + "_param_" + std::to_string(Index),
1008         EntryBlock->getTerminator());
1009     Builder.CreateStore(Val, Param);
1010     Value *Slot = Builder.CreateGEP(
1011         Parameters, {Builder.getInt64(0), Builder.getInt64(Index)});
1012     Value *ParamTyped =
1013         Builder.CreatePointerCast(Param, Builder.getInt8PtrTy());
1014     Builder.CreateStore(ParamTyped, Slot);
1015     Index++;
1016   }
1017 
1018   int NumVars = isl_space_dim(Kernel->space, isl_dim_param);
1019 
1020   for (long i = 0; i < NumVars; i++) {
1021     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i);
1022     Value *Val = IDToValue[Id];
1023     isl_id_free(Id);
1024     Instruction *Param = new AllocaInst(
1025         Val->getType(), Launch + "_param_" + std::to_string(Index),
1026         EntryBlock->getTerminator());
1027     Builder.CreateStore(Val, Param);
1028     Value *Slot = Builder.CreateGEP(
1029         Parameters, {Builder.getInt64(0), Builder.getInt64(Index)});
1030     Value *ParamTyped =
1031         Builder.CreatePointerCast(Param, Builder.getInt8PtrTy());
1032     Builder.CreateStore(ParamTyped, Slot);
1033     Index++;
1034   }
1035 
1036   for (auto Val : SubtreeValues) {
1037     Instruction *Param = new AllocaInst(
1038         Val->getType(), Launch + "_param_" + std::to_string(Index),
1039         EntryBlock->getTerminator());
1040     Builder.CreateStore(Val, Param);
1041     Value *Slot = Builder.CreateGEP(
1042         Parameters, {Builder.getInt64(0), Builder.getInt64(Index)});
1043     Value *ParamTyped =
1044         Builder.CreatePointerCast(Param, Builder.getInt8PtrTy());
1045     Builder.CreateStore(ParamTyped, Slot);
1046     Index++;
1047   }
1048 
1049   auto Location = EntryBlock->getTerminator();
1050   return new BitCastInst(Parameters, Builder.getInt8PtrTy(),
1051                          Launch + "_params_i8ptr", Location);
1052 }
1053 
1054 void GPUNodeBuilder::createKernel(__isl_take isl_ast_node *KernelStmt) {
1055   isl_id *Id = isl_ast_node_get_annotation(KernelStmt);
1056   ppcg_kernel *Kernel = (ppcg_kernel *)isl_id_get_user(Id);
1057   isl_id_free(Id);
1058   isl_ast_node_free(KernelStmt);
1059 
1060   Value *BlockDimX, *BlockDimY, *BlockDimZ;
1061   std::tie(BlockDimX, BlockDimY, BlockDimZ) = getBlockSizes(Kernel);
1062 
1063   SetVector<Value *> SubtreeValues = getReferencesInKernel(Kernel);
1064 
1065   assert(Kernel->tree && "Device AST of kernel node is empty");
1066 
1067   Instruction &HostInsertPoint = *Builder.GetInsertPoint();
1068   IslExprBuilder::IDToValueTy HostIDs = IDToValue;
1069   ValueMapT HostValueMap = ValueMap;
1070 
1071   SetVector<const Loop *> Loops;
1072 
1073   // Create for all loops we depend on values that contain the current loop
1074   // iteration. These values are necessary to generate code for SCEVs that
1075   // depend on such loops. As a result we need to pass them to the subfunction.
1076   for (const Loop *L : Loops) {
1077     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
1078                                             SE.getUnknown(Builder.getInt64(1)),
1079                                             L, SCEV::FlagAnyWrap);
1080     Value *V = generateSCEV(OuterLIV);
1081     OutsideLoopIterations[L] = SE.getUnknown(V);
1082     SubtreeValues.insert(V);
1083   }
1084 
1085   createKernelFunction(Kernel, SubtreeValues);
1086 
1087   create(isl_ast_node_copy(Kernel->tree));
1088 
1089   Function *F = Builder.GetInsertBlock()->getParent();
1090   addCUDAAnnotations(F->getParent(), BlockDimX, BlockDimY, BlockDimZ);
1091   clearDominators(F);
1092   clearScalarEvolution(F);
1093   clearLoops(F);
1094 
1095   Builder.SetInsertPoint(&HostInsertPoint);
1096   IDToValue = HostIDs;
1097 
1098   ValueMap = HostValueMap;
1099   ScalarMap.clear();
1100   PHIOpMap.clear();
1101   EscapeMap.clear();
1102   IDToSAI.clear();
1103   Annotator.resetAlternativeAliasBases();
1104   for (auto &BasePtr : LocalArrays)
1105     S.invalidateScopArrayInfo(BasePtr, ScopArrayInfo::MK_Array);
1106   LocalArrays.clear();
1107 
1108   Value *Parameters = createLaunchParameters(Kernel, F, SubtreeValues);
1109 
1110   std::string ASMString = finalizeKernelFunction();
1111   std::string Name = "kernel_" + std::to_string(Kernel->id);
1112   Value *KernelString = Builder.CreateGlobalStringPtr(ASMString, Name);
1113   Value *NameString = Builder.CreateGlobalStringPtr(Name, Name + "_name");
1114   Value *GPUKernel = createCallGetKernel(KernelString, NameString);
1115 
1116   Value *GridDimX, *GridDimY;
1117   std::tie(GridDimX, GridDimY) = getGridSizes(Kernel);
1118 
1119   createCallLaunchKernel(GPUKernel, GridDimX, GridDimY, BlockDimX, BlockDimY,
1120                          BlockDimZ, Parameters);
1121   createCallFreeKernel(GPUKernel);
1122 
1123   for (auto Id : KernelIds)
1124     isl_id_free(Id);
1125 
1126   KernelIds.clear();
1127 }
1128 
1129 /// Compute the DataLayout string for the NVPTX backend.
1130 ///
1131 /// @param is64Bit Are we looking for a 64 bit architecture?
1132 static std::string computeNVPTXDataLayout(bool is64Bit) {
1133   std::string Ret = "e";
1134 
1135   if (!is64Bit)
1136     Ret += "-p:32:32";
1137 
1138   Ret += "-i64:64-v16:16-v32:32-n16:32:64";
1139 
1140   return Ret;
1141 }
1142 
1143 Function *
1144 GPUNodeBuilder::createKernelFunctionDecl(ppcg_kernel *Kernel,
1145                                          SetVector<Value *> &SubtreeValues) {
1146   std::vector<Type *> Args;
1147   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
1148 
1149   for (long i = 0; i < Prog->n_array; i++) {
1150     if (!ppcg_kernel_requires_array_argument(Kernel, i))
1151       continue;
1152 
1153     Args.push_back(Builder.getInt8PtrTy());
1154   }
1155 
1156   int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set);
1157 
1158   for (long i = 0; i < NumHostIters; i++)
1159     Args.push_back(Builder.getInt64Ty());
1160 
1161   int NumVars = isl_space_dim(Kernel->space, isl_dim_param);
1162 
1163   for (long i = 0; i < NumVars; i++)
1164     Args.push_back(Builder.getInt64Ty());
1165 
1166   for (auto *V : SubtreeValues)
1167     Args.push_back(V->getType());
1168 
1169   auto *FT = FunctionType::get(Builder.getVoidTy(), Args, false);
1170   auto *FN = Function::Create(FT, Function::ExternalLinkage, Identifier,
1171                               GPUModule.get());
1172   FN->setCallingConv(CallingConv::PTX_Kernel);
1173 
1174   auto Arg = FN->arg_begin();
1175   for (long i = 0; i < Kernel->n_array; i++) {
1176     if (!ppcg_kernel_requires_array_argument(Kernel, i))
1177       continue;
1178 
1179     Arg->setName(Kernel->array[i].array->name);
1180 
1181     isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set);
1182     const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(isl_id_copy(Id));
1183     Type *EleTy = SAI->getElementType();
1184     Value *Val = &*Arg;
1185     SmallVector<const SCEV *, 4> Sizes;
1186     isl_ast_build *Build =
1187         isl_ast_build_from_context(isl_set_copy(Prog->context));
1188     for (long j = 1; j < Kernel->array[i].array->n_index; j++) {
1189       isl_ast_expr *DimSize = isl_ast_build_expr_from_pw_aff(
1190           Build, isl_pw_aff_copy(Kernel->array[i].array->bound[j]));
1191       auto V = ExprBuilder.create(DimSize);
1192       Sizes.push_back(SE.getSCEV(V));
1193     }
1194     const ScopArrayInfo *SAIRep =
1195         S.getOrCreateScopArrayInfo(Val, EleTy, Sizes, ScopArrayInfo::MK_Array);
1196     LocalArrays.push_back(Val);
1197 
1198     isl_ast_build_free(Build);
1199     KernelIds.push_back(Id);
1200     IDToSAI[Id] = SAIRep;
1201     Arg++;
1202   }
1203 
1204   for (long i = 0; i < NumHostIters; i++) {
1205     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
1206     Arg->setName(isl_id_get_name(Id));
1207     IDToValue[Id] = &*Arg;
1208     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
1209     Arg++;
1210   }
1211 
1212   for (long i = 0; i < NumVars; i++) {
1213     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i);
1214     Arg->setName(isl_id_get_name(Id));
1215     IDToValue[Id] = &*Arg;
1216     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
1217     Arg++;
1218   }
1219 
1220   for (auto *V : SubtreeValues) {
1221     Arg->setName(V->getName());
1222     ValueMap[V] = &*Arg;
1223     Arg++;
1224   }
1225 
1226   return FN;
1227 }
1228 
1229 void GPUNodeBuilder::insertKernelIntrinsics(ppcg_kernel *Kernel) {
1230   Intrinsic::ID IntrinsicsBID[] = {Intrinsic::nvvm_read_ptx_sreg_ctaid_x,
1231                                    Intrinsic::nvvm_read_ptx_sreg_ctaid_y};
1232 
1233   Intrinsic::ID IntrinsicsTID[] = {Intrinsic::nvvm_read_ptx_sreg_tid_x,
1234                                    Intrinsic::nvvm_read_ptx_sreg_tid_y,
1235                                    Intrinsic::nvvm_read_ptx_sreg_tid_z};
1236 
1237   auto addId = [this](__isl_take isl_id *Id, Intrinsic::ID Intr) mutable {
1238     std::string Name = isl_id_get_name(Id);
1239     Module *M = Builder.GetInsertBlock()->getParent()->getParent();
1240     Function *IntrinsicFn = Intrinsic::getDeclaration(M, Intr);
1241     Value *Val = Builder.CreateCall(IntrinsicFn, {});
1242     Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name);
1243     IDToValue[Id] = Val;
1244     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
1245   };
1246 
1247   for (int i = 0; i < Kernel->n_grid; ++i) {
1248     isl_id *Id = isl_id_list_get_id(Kernel->block_ids, i);
1249     addId(Id, IntrinsicsBID[i]);
1250   }
1251 
1252   for (int i = 0; i < Kernel->n_block; ++i) {
1253     isl_id *Id = isl_id_list_get_id(Kernel->thread_ids, i);
1254     addId(Id, IntrinsicsTID[i]);
1255   }
1256 }
1257 
1258 void GPUNodeBuilder::prepareKernelArguments(ppcg_kernel *Kernel, Function *FN) {
1259   auto Arg = FN->arg_begin();
1260   for (long i = 0; i < Kernel->n_array; i++) {
1261     if (!ppcg_kernel_requires_array_argument(Kernel, i))
1262       continue;
1263 
1264     isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set);
1265     const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(isl_id_copy(Id));
1266     isl_id_free(Id);
1267 
1268     if (SAI->getNumberOfDimensions() > 0) {
1269       Arg++;
1270       continue;
1271     }
1272 
1273     Value *Alloca = BlockGen.getOrCreateScalarAlloca(SAI->getBasePtr());
1274     Value *ArgPtr = &*Arg;
1275     Type *TypePtr = SAI->getElementType()->getPointerTo();
1276     Value *TypedArgPtr = Builder.CreatePointerCast(ArgPtr, TypePtr);
1277     Value *Val = Builder.CreateLoad(TypedArgPtr);
1278     Builder.CreateStore(Val, Alloca);
1279 
1280     Arg++;
1281   }
1282 }
1283 
1284 void GPUNodeBuilder::createKernelVariables(ppcg_kernel *Kernel, Function *FN) {
1285   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
1286 
1287   for (int i = 0; i < Kernel->n_var; ++i) {
1288     struct ppcg_kernel_var &Var = Kernel->var[i];
1289     isl_id *Id = isl_space_get_tuple_id(Var.array->space, isl_dim_set);
1290     Type *EleTy = ScopArrayInfo::getFromId(Id)->getElementType();
1291 
1292     Type *ArrayTy = EleTy;
1293     SmallVector<const SCEV *, 4> Sizes;
1294 
1295     for (unsigned int j = 0; j < Var.array->n_index; ++j) {
1296       isl_val *Val = isl_vec_get_element_val(Var.size, j);
1297       long Bound = isl_val_get_num_si(Val);
1298       isl_val_free(Val);
1299       Sizes.push_back(S.getSE()->getConstant(Builder.getInt64Ty(), Bound));
1300       ArrayTy = ArrayType::get(ArrayTy, Bound);
1301     }
1302 
1303     const ScopArrayInfo *SAI;
1304     Value *Allocation;
1305     if (Var.type == ppcg_access_shared) {
1306       auto GlobalVar = new GlobalVariable(
1307           *M, ArrayTy, false, GlobalValue::InternalLinkage, 0, Var.name,
1308           nullptr, GlobalValue::ThreadLocalMode::NotThreadLocal, 3);
1309       GlobalVar->setAlignment(EleTy->getPrimitiveSizeInBits() / 8);
1310       GlobalVar->setInitializer(Constant::getNullValue(ArrayTy));
1311 
1312       Allocation = GlobalVar;
1313     } else if (Var.type == ppcg_access_private) {
1314       Allocation = Builder.CreateAlloca(ArrayTy, 0, "private_array");
1315     } else {
1316       llvm_unreachable("unknown variable type");
1317     }
1318     SAI = S.getOrCreateScopArrayInfo(Allocation, EleTy, Sizes,
1319                                      ScopArrayInfo::MK_Array);
1320     Id = isl_id_alloc(S.getIslCtx(), Var.name, nullptr);
1321     IDToValue[Id] = Allocation;
1322     LocalArrays.push_back(Allocation);
1323     KernelIds.push_back(Id);
1324     IDToSAI[Id] = SAI;
1325   }
1326 }
1327 
1328 void GPUNodeBuilder::createKernelFunction(ppcg_kernel *Kernel,
1329                                           SetVector<Value *> &SubtreeValues) {
1330 
1331   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
1332   GPUModule.reset(new Module(Identifier, Builder.getContext()));
1333   GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda"));
1334   GPUModule->setDataLayout(computeNVPTXDataLayout(true /* is64Bit */));
1335 
1336   Function *FN = createKernelFunctionDecl(Kernel, SubtreeValues);
1337 
1338   BasicBlock *PrevBlock = Builder.GetInsertBlock();
1339   auto EntryBlock = BasicBlock::Create(Builder.getContext(), "entry", FN);
1340 
1341   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
1342   DT.addNewBlock(EntryBlock, PrevBlock);
1343 
1344   Builder.SetInsertPoint(EntryBlock);
1345   Builder.CreateRetVoid();
1346   Builder.SetInsertPoint(EntryBlock, EntryBlock->begin());
1347 
1348   ScopDetection::markFunctionAsInvalid(FN);
1349 
1350   prepareKernelArguments(Kernel, FN);
1351   createKernelVariables(Kernel, FN);
1352   insertKernelIntrinsics(Kernel);
1353 }
1354 
1355 std::string GPUNodeBuilder::createKernelASM() {
1356   llvm::Triple GPUTriple(Triple::normalize("nvptx64-nvidia-cuda"));
1357   std::string ErrMsg;
1358   auto GPUTarget = TargetRegistry::lookupTarget(GPUTriple.getTriple(), ErrMsg);
1359 
1360   if (!GPUTarget) {
1361     errs() << ErrMsg << "\n";
1362     return "";
1363   }
1364 
1365   TargetOptions Options;
1366   Options.UnsafeFPMath = FastMath;
1367   std::unique_ptr<TargetMachine> TargetM(
1368       GPUTarget->createTargetMachine(GPUTriple.getTriple(), CudaVersion, "",
1369                                      Options, Optional<Reloc::Model>()));
1370 
1371   SmallString<0> ASMString;
1372   raw_svector_ostream ASMStream(ASMString);
1373   llvm::legacy::PassManager PM;
1374 
1375   PM.add(createTargetTransformInfoWrapperPass(TargetM->getTargetIRAnalysis()));
1376 
1377   if (TargetM->addPassesToEmitFile(
1378           PM, ASMStream, TargetMachine::CGFT_AssemblyFile, true /* verify */)) {
1379     errs() << "The target does not support generation of this file type!\n";
1380     return "";
1381   }
1382 
1383   PM.run(*GPUModule);
1384 
1385   return ASMStream.str();
1386 }
1387 
1388 std::string GPUNodeBuilder::finalizeKernelFunction() {
1389   // Verify module.
1390   llvm::legacy::PassManager Passes;
1391   Passes.add(createVerifierPass());
1392   Passes.run(*GPUModule);
1393 
1394   if (DumpKernelIR)
1395     outs() << *GPUModule << "\n";
1396 
1397   // Optimize module.
1398   llvm::legacy::PassManager OptPasses;
1399   PassManagerBuilder PassBuilder;
1400   PassBuilder.OptLevel = 3;
1401   PassBuilder.SizeLevel = 0;
1402   PassBuilder.populateModulePassManager(OptPasses);
1403   OptPasses.run(*GPUModule);
1404 
1405   std::string Assembly = createKernelASM();
1406 
1407   if (DumpKernelASM)
1408     outs() << Assembly << "\n";
1409 
1410   GPUModule.release();
1411   KernelIDs.clear();
1412 
1413   return Assembly;
1414 }
1415 
1416 namespace {
1417 class PPCGCodeGeneration : public ScopPass {
1418 public:
1419   static char ID;
1420 
1421   /// The scop that is currently processed.
1422   Scop *S;
1423 
1424   LoopInfo *LI;
1425   DominatorTree *DT;
1426   ScalarEvolution *SE;
1427   const DataLayout *DL;
1428   RegionInfo *RI;
1429 
1430   PPCGCodeGeneration() : ScopPass(ID) {}
1431 
1432   /// Construct compilation options for PPCG.
1433   ///
1434   /// @returns The compilation options.
1435   ppcg_options *createPPCGOptions() {
1436     auto DebugOptions =
1437         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
1438     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
1439 
1440     DebugOptions->dump_schedule_constraints = false;
1441     DebugOptions->dump_schedule = false;
1442     DebugOptions->dump_final_schedule = false;
1443     DebugOptions->dump_sizes = false;
1444     DebugOptions->verbose = false;
1445 
1446     Options->debug = DebugOptions;
1447 
1448     Options->reschedule = true;
1449     Options->scale_tile_loops = false;
1450     Options->wrap = false;
1451 
1452     Options->non_negative_parameters = false;
1453     Options->ctx = nullptr;
1454     Options->sizes = nullptr;
1455 
1456     Options->tile_size = 32;
1457 
1458     Options->use_private_memory = PrivateMemory;
1459     Options->use_shared_memory = SharedMemory;
1460     Options->max_shared_memory = 48 * 1024;
1461 
1462     Options->target = PPCG_TARGET_CUDA;
1463     Options->openmp = false;
1464     Options->linearize_device_arrays = true;
1465     Options->live_range_reordering = false;
1466 
1467     Options->opencl_compiler_options = nullptr;
1468     Options->opencl_use_gpu = false;
1469     Options->opencl_n_include_file = 0;
1470     Options->opencl_include_files = nullptr;
1471     Options->opencl_print_kernel_types = false;
1472     Options->opencl_embed_kernel_code = false;
1473 
1474     Options->save_schedule_file = nullptr;
1475     Options->load_schedule_file = nullptr;
1476 
1477     return Options;
1478   }
1479 
1480   /// Get a tagged access relation containing all accesses of type @p AccessTy.
1481   ///
1482   /// Instead of a normal access of the form:
1483   ///
1484   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
1485   ///
1486   /// a tagged access has the form
1487   ///
1488   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
1489   ///
1490   /// where 'id' is an additional space that references the memory access that
1491   /// triggered the access.
1492   ///
1493   /// @param AccessTy The type of the memory accesses to collect.
1494   ///
1495   /// @return The relation describing all tagged memory accesses.
1496   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
1497     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
1498 
1499     for (auto &Stmt : *S)
1500       for (auto &Acc : Stmt)
1501         if (Acc->getType() == AccessTy) {
1502           isl_map *Relation = Acc->getAccessRelation();
1503           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
1504 
1505           isl_space *Space = isl_map_get_space(Relation);
1506           Space = isl_space_range(Space);
1507           Space = isl_space_from_range(Space);
1508           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
1509           isl_map *Universe = isl_map_universe(Space);
1510           Relation = isl_map_domain_product(Relation, Universe);
1511           Accesses = isl_union_map_add_map(Accesses, Relation);
1512         }
1513 
1514     return Accesses;
1515   }
1516 
1517   /// Get the set of all read accesses, tagged with the access id.
1518   ///
1519   /// @see getTaggedAccesses
1520   isl_union_map *getTaggedReads() {
1521     return getTaggedAccesses(MemoryAccess::READ);
1522   }
1523 
1524   /// Get the set of all may (and must) accesses, tagged with the access id.
1525   ///
1526   /// @see getTaggedAccesses
1527   isl_union_map *getTaggedMayWrites() {
1528     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
1529                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
1530   }
1531 
1532   /// Get the set of all must accesses, tagged with the access id.
1533   ///
1534   /// @see getTaggedAccesses
1535   isl_union_map *getTaggedMustWrites() {
1536     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
1537   }
1538 
1539   /// Collect parameter and array names as isl_ids.
1540   ///
1541   /// To reason about the different parameters and arrays used, ppcg requires
1542   /// a list of all isl_ids in use. As PPCG traditionally performs
1543   /// source-to-source compilation each of these isl_ids is mapped to the
1544   /// expression that represents it. As we do not have a corresponding
1545   /// expression in Polly, we just map each id to a 'zero' expression to match
1546   /// the data format that ppcg expects.
1547   ///
1548   /// @returns Retun a map from collected ids to 'zero' ast expressions.
1549   __isl_give isl_id_to_ast_expr *getNames() {
1550     auto *Names = isl_id_to_ast_expr_alloc(
1551         S->getIslCtx(),
1552         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
1553     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
1554     auto *Space = S->getParamSpace();
1555 
1556     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
1557       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
1558       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
1559     }
1560 
1561     for (auto &Array : S->arrays()) {
1562       auto Id = Array->getBasePtrId();
1563       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
1564     }
1565 
1566     isl_space_free(Space);
1567     isl_ast_expr_free(Zero);
1568 
1569     return Names;
1570   }
1571 
1572   /// Create a new PPCG scop from the current scop.
1573   ///
1574   /// The PPCG scop is initialized with data from the current polly::Scop. From
1575   /// this initial data, the data-dependences in the PPCG scop are initialized.
1576   /// We do not use Polly's dependence analysis for now, to ensure we match
1577   /// the PPCG default behaviour more closely.
1578   ///
1579   /// @returns A new ppcg scop.
1580   ppcg_scop *createPPCGScop() {
1581     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
1582 
1583     PPCGScop->options = createPPCGOptions();
1584 
1585     PPCGScop->start = 0;
1586     PPCGScop->end = 0;
1587 
1588     PPCGScop->context = S->getContext();
1589     PPCGScop->domain = S->getDomains();
1590     PPCGScop->call = nullptr;
1591     PPCGScop->tagged_reads = getTaggedReads();
1592     PPCGScop->reads = S->getReads();
1593     PPCGScop->live_in = nullptr;
1594     PPCGScop->tagged_may_writes = getTaggedMayWrites();
1595     PPCGScop->may_writes = S->getWrites();
1596     PPCGScop->tagged_must_writes = getTaggedMustWrites();
1597     PPCGScop->must_writes = S->getMustWrites();
1598     PPCGScop->live_out = nullptr;
1599     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
1600     PPCGScop->tagger = nullptr;
1601 
1602     PPCGScop->independence = nullptr;
1603     PPCGScop->dep_flow = nullptr;
1604     PPCGScop->tagged_dep_flow = nullptr;
1605     PPCGScop->dep_false = nullptr;
1606     PPCGScop->dep_forced = nullptr;
1607     PPCGScop->dep_order = nullptr;
1608     PPCGScop->tagged_dep_order = nullptr;
1609 
1610     PPCGScop->schedule = S->getScheduleTree();
1611     PPCGScop->names = getNames();
1612 
1613     PPCGScop->pet = nullptr;
1614 
1615     compute_tagger(PPCGScop);
1616     compute_dependences(PPCGScop);
1617 
1618     return PPCGScop;
1619   }
1620 
1621   /// Collect the array acesses in a statement.
1622   ///
1623   /// @param Stmt The statement for which to collect the accesses.
1624   ///
1625   /// @returns A list of array accesses.
1626   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
1627     gpu_stmt_access *Accesses = nullptr;
1628 
1629     for (MemoryAccess *Acc : Stmt) {
1630       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
1631       Access->read = Acc->isRead();
1632       Access->write = Acc->isWrite();
1633       Access->access = Acc->getAccessRelation();
1634       isl_space *Space = isl_map_get_space(Access->access);
1635       Space = isl_space_range(Space);
1636       Space = isl_space_from_range(Space);
1637       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
1638       isl_map *Universe = isl_map_universe(Space);
1639       Access->tagged_access =
1640           isl_map_domain_product(Acc->getAccessRelation(), Universe);
1641       Access->exact_write = !Acc->isMayWrite();
1642       Access->ref_id = Acc->getId();
1643       Access->next = Accesses;
1644       Access->n_index = Acc->getScopArrayInfo()->getNumberOfDimensions();
1645       Accesses = Access;
1646     }
1647 
1648     return Accesses;
1649   }
1650 
1651   /// Collect the list of GPU statements.
1652   ///
1653   /// Each statement has an id, a pointer to the underlying data structure,
1654   /// as well as a list with all memory accesses.
1655   ///
1656   /// TODO: Initialize the list of memory accesses.
1657   ///
1658   /// @returns A linked-list of statements.
1659   gpu_stmt *getStatements() {
1660     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
1661                                        std::distance(S->begin(), S->end()));
1662 
1663     int i = 0;
1664     for (auto &Stmt : *S) {
1665       gpu_stmt *GPUStmt = &Stmts[i];
1666 
1667       GPUStmt->id = Stmt.getDomainId();
1668 
1669       // We use the pet stmt pointer to keep track of the Polly statements.
1670       GPUStmt->stmt = (pet_stmt *)&Stmt;
1671       GPUStmt->accesses = getStmtAccesses(Stmt);
1672       i++;
1673     }
1674 
1675     return Stmts;
1676   }
1677 
1678   /// Derive the extent of an array.
1679   ///
1680   /// The extent of an array is defined by the set of memory locations for
1681   /// which a memory access in the iteration domain exists.
1682   ///
1683   /// @param Array The array to derive the extent for.
1684   ///
1685   /// @returns An isl_set describing the extent of the array.
1686   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
1687     isl_union_map *Accesses = S->getAccesses();
1688     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
1689     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
1690     isl_set *AccessSet =
1691         isl_union_set_extract_set(AccessUSet, Array->getSpace());
1692     isl_union_set_free(AccessUSet);
1693 
1694     return AccessSet;
1695   }
1696 
1697   /// Derive the bounds of an array.
1698   ///
1699   /// For the first dimension we derive the bound of the array from the extent
1700   /// of this dimension. For inner dimensions we obtain their size directly from
1701   /// ScopArrayInfo.
1702   ///
1703   /// @param PPCGArray The array to compute bounds for.
1704   /// @param Array The polly array from which to take the information.
1705   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
1706     if (PPCGArray.n_index > 0) {
1707       isl_set *Dom = isl_set_copy(PPCGArray.extent);
1708       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
1709       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
1710       isl_set_free(Dom);
1711       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
1712       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
1713       isl_aff *One = isl_aff_zero_on_domain(LS);
1714       One = isl_aff_add_constant_si(One, 1);
1715       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
1716       Bound = isl_pw_aff_gist(Bound, S->getContext());
1717       PPCGArray.bound[0] = Bound;
1718     }
1719 
1720     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
1721       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
1722       auto LS = isl_pw_aff_get_domain_space(Bound);
1723       auto Aff = isl_multi_aff_zero(LS);
1724       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
1725       PPCGArray.bound[i] = Bound;
1726     }
1727   }
1728 
1729   /// Create the arrays for @p PPCGProg.
1730   ///
1731   /// @param PPCGProg The program to compute the arrays for.
1732   void createArrays(gpu_prog *PPCGProg) {
1733     int i = 0;
1734     for (auto &Array : S->arrays()) {
1735       std::string TypeName;
1736       raw_string_ostream OS(TypeName);
1737 
1738       OS << *Array->getElementType();
1739       TypeName = OS.str();
1740 
1741       gpu_array_info &PPCGArray = PPCGProg->array[i];
1742 
1743       PPCGArray.space = Array->getSpace();
1744       PPCGArray.type = strdup(TypeName.c_str());
1745       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
1746       PPCGArray.name = strdup(Array->getName().c_str());
1747       PPCGArray.extent = nullptr;
1748       PPCGArray.n_index = Array->getNumberOfDimensions();
1749       PPCGArray.bound =
1750           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
1751       PPCGArray.extent = getExtent(Array);
1752       PPCGArray.n_ref = 0;
1753       PPCGArray.refs = nullptr;
1754       PPCGArray.accessed = true;
1755       PPCGArray.read_only_scalar = false;
1756       PPCGArray.has_compound_element = false;
1757       PPCGArray.local = false;
1758       PPCGArray.declare_local = false;
1759       PPCGArray.global = false;
1760       PPCGArray.linearize = false;
1761       PPCGArray.dep_order = nullptr;
1762       PPCGArray.user = Array;
1763 
1764       setArrayBounds(PPCGArray, Array);
1765       i++;
1766 
1767       collect_references(PPCGProg, &PPCGArray);
1768     }
1769   }
1770 
1771   /// Create an identity map between the arrays in the scop.
1772   ///
1773   /// @returns An identity map between the arrays in the scop.
1774   isl_union_map *getArrayIdentity() {
1775     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
1776 
1777     for (auto &Array : S->arrays()) {
1778       isl_space *Space = Array->getSpace();
1779       Space = isl_space_map_from_set(Space);
1780       isl_map *Identity = isl_map_identity(Space);
1781       Maps = isl_union_map_add_map(Maps, Identity);
1782     }
1783 
1784     return Maps;
1785   }
1786 
1787   /// Create a default-initialized PPCG GPU program.
1788   ///
1789   /// @returns A new gpu grogram description.
1790   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
1791 
1792     if (!PPCGScop)
1793       return nullptr;
1794 
1795     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
1796 
1797     PPCGProg->ctx = S->getIslCtx();
1798     PPCGProg->scop = PPCGScop;
1799     PPCGProg->context = isl_set_copy(PPCGScop->context);
1800     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
1801     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
1802     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
1803     PPCGProg->tagged_must_kill =
1804         isl_union_map_copy(PPCGScop->tagged_must_kills);
1805     PPCGProg->to_inner = getArrayIdentity();
1806     PPCGProg->to_outer = getArrayIdentity();
1807     PPCGProg->may_persist = compute_may_persist(PPCGProg);
1808     PPCGProg->any_to_outer = nullptr;
1809     PPCGProg->array_order = nullptr;
1810     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
1811     PPCGProg->stmts = getStatements();
1812     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
1813     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
1814                                        PPCGProg->n_array);
1815 
1816     createArrays(PPCGProg);
1817 
1818     return PPCGProg;
1819   }
1820 
1821   struct PrintGPUUserData {
1822     struct cuda_info *CudaInfo;
1823     struct gpu_prog *PPCGProg;
1824     std::vector<ppcg_kernel *> Kernels;
1825   };
1826 
1827   /// Print a user statement node in the host code.
1828   ///
1829   /// We use ppcg's printing facilities to print the actual statement and
1830   /// additionally build up a list of all kernels that are encountered in the
1831   /// host ast.
1832   ///
1833   /// @param P The printer to print to
1834   /// @param Options The printing options to use
1835   /// @param Node The node to print
1836   /// @param User A user pointer to carry additional data. This pointer is
1837   ///             expected to be of type PrintGPUUserData.
1838   ///
1839   /// @returns A printer to which the output has been printed.
1840   static __isl_give isl_printer *
1841   printHostUser(__isl_take isl_printer *P,
1842                 __isl_take isl_ast_print_options *Options,
1843                 __isl_take isl_ast_node *Node, void *User) {
1844     auto Data = (struct PrintGPUUserData *)User;
1845     auto Id = isl_ast_node_get_annotation(Node);
1846 
1847     if (Id) {
1848       bool IsUser = !strcmp(isl_id_get_name(Id), "user");
1849 
1850       // If this is a user statement, format it ourselves as ppcg would
1851       // otherwise try to call pet functionality that is not available in
1852       // Polly.
1853       if (IsUser) {
1854         P = isl_printer_start_line(P);
1855         P = isl_printer_print_ast_node(P, Node);
1856         P = isl_printer_end_line(P);
1857         isl_id_free(Id);
1858         isl_ast_print_options_free(Options);
1859         return P;
1860       }
1861 
1862       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
1863       isl_id_free(Id);
1864       Data->Kernels.push_back(Kernel);
1865     }
1866 
1867     return print_host_user(P, Options, Node, User);
1868   }
1869 
1870   /// Print C code corresponding to the control flow in @p Kernel.
1871   ///
1872   /// @param Kernel The kernel to print
1873   void printKernel(ppcg_kernel *Kernel) {
1874     auto *P = isl_printer_to_str(S->getIslCtx());
1875     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
1876     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
1877     P = isl_ast_node_print(Kernel->tree, P, Options);
1878     char *String = isl_printer_get_str(P);
1879     printf("%s\n", String);
1880     free(String);
1881     isl_printer_free(P);
1882   }
1883 
1884   /// Print C code corresponding to the GPU code described by @p Tree.
1885   ///
1886   /// @param Tree An AST describing GPU code
1887   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
1888   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
1889     auto *P = isl_printer_to_str(S->getIslCtx());
1890     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
1891 
1892     PrintGPUUserData Data;
1893     Data.PPCGProg = PPCGProg;
1894 
1895     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
1896     Options =
1897         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
1898     P = isl_ast_node_print(Tree, P, Options);
1899     char *String = isl_printer_get_str(P);
1900     printf("# host\n");
1901     printf("%s\n", String);
1902     free(String);
1903     isl_printer_free(P);
1904 
1905     for (auto Kernel : Data.Kernels) {
1906       printf("# kernel%d\n", Kernel->id);
1907       printKernel(Kernel);
1908     }
1909   }
1910 
1911   // Generate a GPU program using PPCG.
1912   //
1913   // GPU mapping consists of multiple steps:
1914   //
1915   //  1) Compute new schedule for the program.
1916   //  2) Map schedule to GPU (TODO)
1917   //  3) Generate code for new schedule (TODO)
1918   //
1919   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
1920   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
1921   // strategy directly from this pass.
1922   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
1923 
1924     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
1925 
1926     PPCGGen->ctx = S->getIslCtx();
1927     PPCGGen->options = PPCGScop->options;
1928     PPCGGen->print = nullptr;
1929     PPCGGen->print_user = nullptr;
1930     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
1931     PPCGGen->prog = PPCGProg;
1932     PPCGGen->tree = nullptr;
1933     PPCGGen->types.n = 0;
1934     PPCGGen->types.name = nullptr;
1935     PPCGGen->sizes = nullptr;
1936     PPCGGen->used_sizes = nullptr;
1937     PPCGGen->kernel_id = 0;
1938 
1939     // Set scheduling strategy to same strategy PPCG is using.
1940     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
1941     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
1942     isl_options_set_schedule_whole_component(PPCGGen->ctx, false);
1943 
1944     isl_schedule *Schedule = get_schedule(PPCGGen);
1945 
1946     int has_permutable = has_any_permutable_node(Schedule);
1947 
1948     if (!has_permutable || has_permutable < 0) {
1949       Schedule = isl_schedule_free(Schedule);
1950     } else {
1951       Schedule = map_to_device(PPCGGen, Schedule);
1952       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
1953     }
1954 
1955     if (DumpSchedule) {
1956       isl_printer *P = isl_printer_to_str(S->getIslCtx());
1957       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
1958       P = isl_printer_print_str(P, "Schedule\n");
1959       P = isl_printer_print_str(P, "========\n");
1960       if (Schedule)
1961         P = isl_printer_print_schedule(P, Schedule);
1962       else
1963         P = isl_printer_print_str(P, "No schedule found\n");
1964 
1965       printf("%s\n", isl_printer_get_str(P));
1966       isl_printer_free(P);
1967     }
1968 
1969     if (DumpCode) {
1970       printf("Code\n");
1971       printf("====\n");
1972       if (PPCGGen->tree)
1973         printGPUTree(PPCGGen->tree, PPCGProg);
1974       else
1975         printf("No code generated\n");
1976     }
1977 
1978     isl_schedule_free(Schedule);
1979 
1980     return PPCGGen;
1981   }
1982 
1983   /// Free gpu_gen structure.
1984   ///
1985   /// @param PPCGGen The ppcg_gen object to free.
1986   void freePPCGGen(gpu_gen *PPCGGen) {
1987     isl_ast_node_free(PPCGGen->tree);
1988     isl_union_map_free(PPCGGen->sizes);
1989     isl_union_map_free(PPCGGen->used_sizes);
1990     free(PPCGGen);
1991   }
1992 
1993   /// Free the options in the ppcg scop structure.
1994   ///
1995   /// ppcg is not freeing these options for us. To avoid leaks we do this
1996   /// ourselves.
1997   ///
1998   /// @param PPCGScop The scop referencing the options to free.
1999   void freeOptions(ppcg_scop *PPCGScop) {
2000     free(PPCGScop->options->debug);
2001     PPCGScop->options->debug = nullptr;
2002     free(PPCGScop->options);
2003     PPCGScop->options = nullptr;
2004   }
2005 
2006   /// Generate code for a given GPU AST described by @p Root.
2007   ///
2008   /// @param Root An isl_ast_node pointing to the root of the GPU AST.
2009   /// @param Prog The GPU Program to generate code for.
2010   void generateCode(__isl_take isl_ast_node *Root, gpu_prog *Prog) {
2011     ScopAnnotator Annotator;
2012     Annotator.buildAliasScopes(*S);
2013 
2014     Region *R = &S->getRegion();
2015 
2016     simplifyRegion(R, DT, LI, RI);
2017 
2018     BasicBlock *EnteringBB = R->getEnteringBlock();
2019 
2020     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
2021 
2022     GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, *S,
2023                                Prog);
2024 
2025     // Only build the run-time condition and parameters _after_ having
2026     // introduced the conditional branch. This is important as the conditional
2027     // branch will guard the original scop from new induction variables that
2028     // the SCEVExpander may introduce while code generating the parameters and
2029     // which may introduce scalar dependences that prevent us from correctly
2030     // code generating this scop.
2031     BasicBlock *StartBlock =
2032         executeScopConditionally(*S, this, Builder.getTrue());
2033 
2034     // TODO: Handle LICM
2035     // TODO: Verify run-time checks
2036     auto SplitBlock = StartBlock->getSinglePredecessor();
2037     Builder.SetInsertPoint(SplitBlock->getTerminator());
2038     NodeBuilder.addParameters(S->getContext());
2039     Builder.SetInsertPoint(&*StartBlock->begin());
2040 
2041     NodeBuilder.initializeAfterRTH();
2042     NodeBuilder.create(Root);
2043     NodeBuilder.finalize();
2044   }
2045 
2046   bool runOnScop(Scop &CurrentScop) override {
2047     S = &CurrentScop;
2048     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
2049     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
2050     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
2051     DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
2052     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
2053 
2054     // We currently do not support scops with invariant loads.
2055     if (S->hasInvariantAccesses())
2056       return false;
2057 
2058     auto PPCGScop = createPPCGScop();
2059     auto PPCGProg = createPPCGProg(PPCGScop);
2060     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
2061 
2062     if (PPCGGen->tree)
2063       generateCode(isl_ast_node_copy(PPCGGen->tree), PPCGProg);
2064 
2065     freeOptions(PPCGScop);
2066     freePPCGGen(PPCGGen);
2067     gpu_prog_free(PPCGProg);
2068     ppcg_scop_free(PPCGScop);
2069 
2070     return true;
2071   }
2072 
2073   void printScop(raw_ostream &, Scop &) const override {}
2074 
2075   void getAnalysisUsage(AnalysisUsage &AU) const override {
2076     AU.addRequired<DominatorTreeWrapperPass>();
2077     AU.addRequired<RegionInfoPass>();
2078     AU.addRequired<ScalarEvolutionWrapperPass>();
2079     AU.addRequired<ScopDetection>();
2080     AU.addRequired<ScopInfoRegionPass>();
2081     AU.addRequired<LoopInfoWrapperPass>();
2082 
2083     AU.addPreserved<AAResultsWrapperPass>();
2084     AU.addPreserved<BasicAAWrapperPass>();
2085     AU.addPreserved<LoopInfoWrapperPass>();
2086     AU.addPreserved<DominatorTreeWrapperPass>();
2087     AU.addPreserved<GlobalsAAWrapperPass>();
2088     AU.addPreserved<PostDominatorTreeWrapperPass>();
2089     AU.addPreserved<ScopDetection>();
2090     AU.addPreserved<ScalarEvolutionWrapperPass>();
2091     AU.addPreserved<SCEVAAWrapperPass>();
2092 
2093     // FIXME: We do not yet add regions for the newly generated code to the
2094     //        region tree.
2095     AU.addPreserved<RegionInfoPass>();
2096     AU.addPreserved<ScopInfoRegionPass>();
2097   }
2098 };
2099 }
2100 
2101 char PPCGCodeGeneration::ID = 1;
2102 
2103 Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
2104 
2105 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
2106                       "Polly - Apply PPCG translation to SCOP", false, false)
2107 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
2108 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
2109 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
2110 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
2111 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
2112 INITIALIZE_PASS_DEPENDENCY(ScopDetection);
2113 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
2114                     "Polly - Apply PPCG translation to SCOP", false, false)
2115