19dfe4e7cSTobias Grosser //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===//
29dfe4e7cSTobias Grosser //
39dfe4e7cSTobias Grosser //                     The LLVM Compiler Infrastructure
49dfe4e7cSTobias Grosser //
59dfe4e7cSTobias Grosser // This file is distributed under the University of Illinois Open Source
69dfe4e7cSTobias Grosser // License. See LICENSE.TXT for details.
79dfe4e7cSTobias Grosser //
89dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===//
99dfe4e7cSTobias Grosser //
109dfe4e7cSTobias Grosser // Take a scop created by ScopInfo and map it to GPU code using the ppcg
119dfe4e7cSTobias Grosser // GPU mapping strategy.
129dfe4e7cSTobias Grosser //
139dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===//
149dfe4e7cSTobias Grosser 
159dfe4e7cSTobias Grosser #include "polly/CodeGen/IslNodeBuilder.h"
1638fc0aedSTobias Grosser #include "polly/CodeGen/Utils.h"
179dfe4e7cSTobias Grosser #include "polly/DependenceInfo.h"
189dfe4e7cSTobias Grosser #include "polly/LinkAllPasses.h"
19f384594dSTobias Grosser #include "polly/Options.h"
209dfe4e7cSTobias Grosser #include "polly/ScopInfo.h"
219dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h"
229dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h"
239dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h"
249dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h"
259dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
269dfe4e7cSTobias Grosser 
27f384594dSTobias Grosser #include "isl/union_map.h"
28f384594dSTobias Grosser 
29e938517eSTobias Grosser extern "C" {
30a56f8f8eSTobias Grosser #include "ppcg/cuda.h"
31a56f8f8eSTobias Grosser #include "ppcg/gpu.h"
32a56f8f8eSTobias Grosser #include "ppcg/gpu_print.h"
33a56f8f8eSTobias Grosser #include "ppcg/ppcg.h"
34a56f8f8eSTobias Grosser #include "ppcg/schedule.h"
35e938517eSTobias Grosser }
36e938517eSTobias Grosser 
379dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h"
389dfe4e7cSTobias Grosser 
399dfe4e7cSTobias Grosser using namespace polly;
409dfe4e7cSTobias Grosser using namespace llvm;
419dfe4e7cSTobias Grosser 
429dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg"
439dfe4e7cSTobias Grosser 
44f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
45f384594dSTobias Grosser                                   cl::desc("Dump the computed GPU Schedule"),
46681bd568STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
47f384594dSTobias Grosser                                   cl::cat(PollyCategory));
4869b46751STobias Grosser 
4969b46751STobias Grosser static cl::opt<bool>
5069b46751STobias Grosser     DumpCode("polly-acc-dump-code",
5169b46751STobias Grosser              cl::desc("Dump C code describing the GPU mapping"), cl::Hidden,
5269b46751STobias Grosser              cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
5369b46751STobias Grosser 
5432837fe3STobias Grosser static cl::opt<bool> DumpKernelIR("polly-acc-dump-kernel-ir",
5532837fe3STobias Grosser                                   cl::desc("Dump the kernel LLVM-IR"),
5632837fe3STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
5732837fe3STobias Grosser                                   cl::cat(PollyCategory));
5832837fe3STobias Grosser 
5960c60025STobias Grosser /// Create the ast expressions for a ScopStmt.
6060c60025STobias Grosser ///
6160c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each
6260c60025STobias Grosser /// of the scheduled ScopStmts.
6360c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
6460c60025STobias Grosser     void *Stmt, isl_ast_build *Build,
6560c60025STobias Grosser     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
6660c60025STobias Grosser                                        isl_id *Id, void *User),
6760c60025STobias Grosser     void *UserIndex,
6860c60025STobias Grosser     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
6960c60025STobias Grosser     void *User_expr) {
7060c60025STobias Grosser 
7160c60025STobias Grosser   // TODO: Implement the AST expression generation. For now we just return a
7260c60025STobias Grosser   // nullptr to ensure that we do not free uninitialized pointers.
7360c60025STobias Grosser 
7460c60025STobias Grosser   return nullptr;
7560c60025STobias Grosser }
76f384594dSTobias Grosser 
7738fc0aedSTobias Grosser /// Generate code for a GPU specific isl AST.
7838fc0aedSTobias Grosser ///
7938fc0aedSTobias Grosser /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which
8038fc0aedSTobias Grosser /// generates code for general-prupose AST nodes, with special functionality
8138fc0aedSTobias Grosser /// for generating GPU specific user nodes.
8238fc0aedSTobias Grosser ///
8338fc0aedSTobias Grosser /// @see GPUNodeBuilder::createUser
8438fc0aedSTobias Grosser class GPUNodeBuilder : public IslNodeBuilder {
8538fc0aedSTobias Grosser public:
8638fc0aedSTobias Grosser   GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
8738fc0aedSTobias Grosser                  const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
8832837fe3STobias Grosser                  DominatorTree &DT, Scop &S, gpu_prog *Prog)
8932837fe3STobias Grosser       : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S), Prog(Prog) {}
9038fc0aedSTobias Grosser 
9138fc0aedSTobias Grosser private:
9232837fe3STobias Grosser   /// A module containing GPU code.
9332837fe3STobias Grosser   ///
9432837fe3STobias Grosser   /// This pointer is only set in case we are currently generating GPU code.
9532837fe3STobias Grosser   std::unique_ptr<Module> GPUModule;
9632837fe3STobias Grosser 
9732837fe3STobias Grosser   /// The GPU program we generate code for.
9832837fe3STobias Grosser   gpu_prog *Prog;
9932837fe3STobias Grosser 
100472f9654STobias Grosser   /// Class to free isl_ids.
101472f9654STobias Grosser   class IslIdDeleter {
102472f9654STobias Grosser   public:
103472f9654STobias Grosser     void operator()(__isl_take isl_id *Id) { isl_id_free(Id); };
104472f9654STobias Grosser   };
105472f9654STobias Grosser 
106472f9654STobias Grosser   /// A set containing all isl_ids allocated in a GPU kernel.
107472f9654STobias Grosser   ///
108472f9654STobias Grosser   /// By releasing this set all isl_ids will be freed.
109472f9654STobias Grosser   std::set<std::unique_ptr<isl_id, IslIdDeleter>> KernelIDs;
110472f9654STobias Grosser 
11138fc0aedSTobias Grosser   /// Create code for user-defined AST nodes.
11238fc0aedSTobias Grosser   ///
11338fc0aedSTobias Grosser   /// These AST nodes can be of type:
11438fc0aedSTobias Grosser   ///
11538fc0aedSTobias Grosser   ///   - ScopStmt:      A computational statement (TODO)
11638fc0aedSTobias Grosser   ///   - Kernel:        A GPU kernel call (TODO)
11738fc0aedSTobias Grosser   ///   - Data-Transfer: A GPU <-> CPU data-transfer (TODO)
1185260c041STobias Grosser   ///   - In-kernel synchronization
1195260c041STobias Grosser   ///   - In-kernel memory copy statement
12038fc0aedSTobias Grosser   ///
1211fb9b64dSTobias Grosser   /// @param UserStmt The ast node to generate code for.
1221fb9b64dSTobias Grosser   virtual void createUser(__isl_take isl_ast_node *UserStmt);
12332837fe3STobias Grosser 
12432837fe3STobias Grosser   /// Create GPU kernel.
12532837fe3STobias Grosser   ///
12632837fe3STobias Grosser   /// Code generate the kernel described by @p KernelStmt.
12732837fe3STobias Grosser   ///
12832837fe3STobias Grosser   /// @param KernelStmt The ast node to generate kernel code for.
12932837fe3STobias Grosser   void createKernel(__isl_take isl_ast_node *KernelStmt);
13032837fe3STobias Grosser 
13132837fe3STobias Grosser   /// Create kernel function.
13232837fe3STobias Grosser   ///
13332837fe3STobias Grosser   /// Create a kernel function located in a newly created module that can serve
13432837fe3STobias Grosser   /// as target for device code generation. Set the Builder to point to the
13532837fe3STobias Grosser   /// start block of this newly created function.
13632837fe3STobias Grosser   ///
13732837fe3STobias Grosser   /// @param Kernel The kernel to generate code for.
13832837fe3STobias Grosser   void createKernelFunction(ppcg_kernel *Kernel);
13932837fe3STobias Grosser 
14032837fe3STobias Grosser   /// Create the declaration of a kernel function.
14132837fe3STobias Grosser   ///
14232837fe3STobias Grosser   /// The kernel function takes as arguments:
14332837fe3STobias Grosser   ///
14432837fe3STobias Grosser   ///   - One i8 pointer for each external array reference used in the kernel.
145f6044bd0STobias Grosser   ///   - Host iterators
146c84a1995STobias Grosser   ///   - Parameters
14732837fe3STobias Grosser   ///   - Other LLVM Value references (TODO)
14832837fe3STobias Grosser   ///
14932837fe3STobias Grosser   /// @param Kernel The kernel to generate the function declaration for.
15032837fe3STobias Grosser   /// @returns The newly declared function.
15132837fe3STobias Grosser   Function *createKernelFunctionDecl(ppcg_kernel *Kernel);
15232837fe3STobias Grosser 
153472f9654STobias Grosser   /// Insert intrinsic functions to obtain thread and block ids.
154472f9654STobias Grosser   ///
155472f9654STobias Grosser   /// @param The kernel to generate the intrinsic functions for.
156472f9654STobias Grosser   void insertKernelIntrinsics(ppcg_kernel *Kernel);
157472f9654STobias Grosser 
1585260c041STobias Grosser   /// Create an in-kernel synchronization call.
1595260c041STobias Grosser   void createKernelSync();
1605260c041STobias Grosser 
16132837fe3STobias Grosser   /// Finalize the generation of the kernel function.
16232837fe3STobias Grosser   ///
16332837fe3STobias Grosser   /// Free the LLVM-IR module corresponding to the kernel and -- if requested --
16432837fe3STobias Grosser   /// dump its IR to stderr.
16532837fe3STobias Grosser   void finalizeKernelFunction();
1661fb9b64dSTobias Grosser };
1671fb9b64dSTobias Grosser 
1685260c041STobias Grosser /// Check if one string is a prefix of another.
1695260c041STobias Grosser ///
1705260c041STobias Grosser /// @param String The string in which to look for the prefix.
1715260c041STobias Grosser /// @param Prefix The prefix to look for.
1725260c041STobias Grosser static bool isPrefix(std::string String, std::string Prefix) {
1735260c041STobias Grosser   return String.find(Prefix) == 0;
1745260c041STobias Grosser }
1755260c041STobias Grosser 
1761fb9b64dSTobias Grosser void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) {
17732837fe3STobias Grosser   isl_ast_expr *Expr = isl_ast_node_user_get_expr(UserStmt);
17832837fe3STobias Grosser   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
17932837fe3STobias Grosser   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
18032837fe3STobias Grosser   isl_id_free(Id);
18132837fe3STobias Grosser   isl_ast_expr_free(StmtExpr);
18232837fe3STobias Grosser 
18332837fe3STobias Grosser   const char *Str = isl_id_get_name(Id);
18432837fe3STobias Grosser   if (!strcmp(Str, "kernel")) {
18532837fe3STobias Grosser     createKernel(UserStmt);
18632837fe3STobias Grosser     isl_ast_expr_free(Expr);
18732837fe3STobias Grosser     return;
18832837fe3STobias Grosser   }
18932837fe3STobias Grosser 
1905260c041STobias Grosser   if (isPrefix(Str, "to_device") || isPrefix(Str, "from_device")) {
1915260c041STobias Grosser     // TODO: Insert memory copies
19232837fe3STobias Grosser     isl_ast_expr_free(Expr);
1931fb9b64dSTobias Grosser     isl_ast_node_free(UserStmt);
19438fc0aedSTobias Grosser     return;
19538fc0aedSTobias Grosser   }
19638fc0aedSTobias Grosser 
1975260c041STobias Grosser   isl_id *Anno = isl_ast_node_get_annotation(UserStmt);
1985260c041STobias Grosser   struct ppcg_kernel_stmt *KernelStmt =
1995260c041STobias Grosser       (struct ppcg_kernel_stmt *)isl_id_get_user(Anno);
2005260c041STobias Grosser   isl_id_free(Anno);
2015260c041STobias Grosser 
2025260c041STobias Grosser   switch (KernelStmt->type) {
2035260c041STobias Grosser   case ppcg_kernel_domain:
2045260c041STobias Grosser     // TODO Create kernel user stmt
2055260c041STobias Grosser     isl_ast_expr_free(Expr);
2065260c041STobias Grosser     isl_ast_node_free(UserStmt);
2075260c041STobias Grosser     return;
2085260c041STobias Grosser   case ppcg_kernel_copy:
2095260c041STobias Grosser     // TODO: Create kernel copy stmt
2105260c041STobias Grosser     isl_ast_expr_free(Expr);
2115260c041STobias Grosser     isl_ast_node_free(UserStmt);
2125260c041STobias Grosser     return;
2135260c041STobias Grosser   case ppcg_kernel_sync:
2145260c041STobias Grosser     createKernelSync();
2155260c041STobias Grosser     isl_ast_expr_free(Expr);
2165260c041STobias Grosser     isl_ast_node_free(UserStmt);
2175260c041STobias Grosser     return;
2185260c041STobias Grosser   }
2195260c041STobias Grosser 
2205260c041STobias Grosser   isl_ast_expr_free(Expr);
2215260c041STobias Grosser   isl_ast_node_free(UserStmt);
2225260c041STobias Grosser   return;
2235260c041STobias Grosser }
2245260c041STobias Grosser 
2255260c041STobias Grosser void GPUNodeBuilder::createKernelSync() {
2265260c041STobias Grosser   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
2275260c041STobias Grosser   auto *Sync = Intrinsic::getDeclaration(M, Intrinsic::nvvm_barrier0);
2285260c041STobias Grosser   Builder.CreateCall(Sync, {});
2295260c041STobias Grosser }
2305260c041STobias Grosser 
23132837fe3STobias Grosser void GPUNodeBuilder::createKernel(__isl_take isl_ast_node *KernelStmt) {
23232837fe3STobias Grosser   isl_id *Id = isl_ast_node_get_annotation(KernelStmt);
23332837fe3STobias Grosser   ppcg_kernel *Kernel = (ppcg_kernel *)isl_id_get_user(Id);
23432837fe3STobias Grosser   isl_id_free(Id);
23532837fe3STobias Grosser   isl_ast_node_free(KernelStmt);
23632837fe3STobias Grosser 
23732837fe3STobias Grosser   assert(Kernel->tree && "Device AST of kernel node is empty");
23832837fe3STobias Grosser 
23932837fe3STobias Grosser   Instruction &HostInsertPoint = *Builder.GetInsertPoint();
240472f9654STobias Grosser   IslExprBuilder::IDToValueTy HostIDs = IDToValue;
24132837fe3STobias Grosser 
24232837fe3STobias Grosser   createKernelFunction(Kernel);
24332837fe3STobias Grosser 
24459ab0705STobias Grosser   create(isl_ast_node_copy(Kernel->tree));
24559ab0705STobias Grosser 
24632837fe3STobias Grosser   Builder.SetInsertPoint(&HostInsertPoint);
247472f9654STobias Grosser   IDToValue = HostIDs;
24832837fe3STobias Grosser 
24932837fe3STobias Grosser   finalizeKernelFunction();
25032837fe3STobias Grosser }
25132837fe3STobias Grosser 
25232837fe3STobias Grosser /// Compute the DataLayout string for the NVPTX backend.
25332837fe3STobias Grosser ///
25432837fe3STobias Grosser /// @param is64Bit Are we looking for a 64 bit architecture?
25532837fe3STobias Grosser static std::string computeNVPTXDataLayout(bool is64Bit) {
25632837fe3STobias Grosser   std::string Ret = "e";
25732837fe3STobias Grosser 
25832837fe3STobias Grosser   if (!is64Bit)
25932837fe3STobias Grosser     Ret += "-p:32:32";
26032837fe3STobias Grosser 
26132837fe3STobias Grosser   Ret += "-i64:64-v16:16-v32:32-n16:32:64";
26232837fe3STobias Grosser 
26332837fe3STobias Grosser   return Ret;
26432837fe3STobias Grosser }
26532837fe3STobias Grosser 
26632837fe3STobias Grosser Function *GPUNodeBuilder::createKernelFunctionDecl(ppcg_kernel *Kernel) {
26732837fe3STobias Grosser   std::vector<Type *> Args;
26832837fe3STobias Grosser   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
26932837fe3STobias Grosser 
27032837fe3STobias Grosser   for (long i = 0; i < Prog->n_array; i++) {
27132837fe3STobias Grosser     if (!ppcg_kernel_requires_array_argument(Kernel, i))
27232837fe3STobias Grosser       continue;
27332837fe3STobias Grosser 
27432837fe3STobias Grosser     Args.push_back(Builder.getInt8PtrTy());
27532837fe3STobias Grosser   }
27632837fe3STobias Grosser 
277f6044bd0STobias Grosser   int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set);
278f6044bd0STobias Grosser 
279f6044bd0STobias Grosser   for (long i = 0; i < NumHostIters; i++)
280f6044bd0STobias Grosser     Args.push_back(Builder.getInt64Ty());
281f6044bd0STobias Grosser 
282c84a1995STobias Grosser   int NumVars = isl_space_dim(Kernel->space, isl_dim_param);
283c84a1995STobias Grosser 
284c84a1995STobias Grosser   for (long i = 0; i < NumVars; i++)
285c84a1995STobias Grosser     Args.push_back(Builder.getInt64Ty());
286c84a1995STobias Grosser 
28732837fe3STobias Grosser   auto *FT = FunctionType::get(Builder.getVoidTy(), Args, false);
28832837fe3STobias Grosser   auto *FN = Function::Create(FT, Function::ExternalLinkage, Identifier,
28932837fe3STobias Grosser                               GPUModule.get());
29032837fe3STobias Grosser   FN->setCallingConv(CallingConv::PTX_Kernel);
29132837fe3STobias Grosser 
29232837fe3STobias Grosser   auto Arg = FN->arg_begin();
29332837fe3STobias Grosser   for (long i = 0; i < Kernel->n_array; i++) {
29432837fe3STobias Grosser     if (!ppcg_kernel_requires_array_argument(Kernel, i))
29532837fe3STobias Grosser       continue;
29632837fe3STobias Grosser 
29732837fe3STobias Grosser     Arg->setName(Prog->array[i].name);
29832837fe3STobias Grosser     Arg++;
29932837fe3STobias Grosser   }
30032837fe3STobias Grosser 
301f6044bd0STobias Grosser   for (long i = 0; i < NumHostIters; i++) {
302f6044bd0STobias Grosser     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
303f6044bd0STobias Grosser     Arg->setName(isl_id_get_name(Id));
304f6044bd0STobias Grosser     IDToValue[Id] = &*Arg;
305f6044bd0STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
306f6044bd0STobias Grosser     Arg++;
307f6044bd0STobias Grosser   }
308f6044bd0STobias Grosser 
309c84a1995STobias Grosser   for (long i = 0; i < NumVars; i++) {
310c84a1995STobias Grosser     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i);
311c84a1995STobias Grosser     Arg->setName(isl_id_get_name(Id));
312c84a1995STobias Grosser     IDToValue[Id] = &*Arg;
313c84a1995STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
314c84a1995STobias Grosser     Arg++;
315c84a1995STobias Grosser   }
316c84a1995STobias Grosser 
31732837fe3STobias Grosser   return FN;
31832837fe3STobias Grosser }
31932837fe3STobias Grosser 
320472f9654STobias Grosser void GPUNodeBuilder::insertKernelIntrinsics(ppcg_kernel *Kernel) {
321472f9654STobias Grosser   Intrinsic::ID IntrinsicsBID[] = {Intrinsic::nvvm_read_ptx_sreg_ctaid_x,
322472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_ctaid_y};
323472f9654STobias Grosser 
324472f9654STobias Grosser   Intrinsic::ID IntrinsicsTID[] = {Intrinsic::nvvm_read_ptx_sreg_tid_x,
325472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_tid_y,
326472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_tid_z};
327472f9654STobias Grosser 
328472f9654STobias Grosser   auto addId = [this](__isl_take isl_id *Id, Intrinsic::ID Intr) mutable {
329472f9654STobias Grosser     std::string Name = isl_id_get_name(Id);
330472f9654STobias Grosser     Module *M = Builder.GetInsertBlock()->getParent()->getParent();
331472f9654STobias Grosser     Function *IntrinsicFn = Intrinsic::getDeclaration(M, Intr);
332472f9654STobias Grosser     Value *Val = Builder.CreateCall(IntrinsicFn, {});
333472f9654STobias Grosser     Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name);
334472f9654STobias Grosser     IDToValue[Id] = Val;
335472f9654STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
336472f9654STobias Grosser   };
337472f9654STobias Grosser 
338472f9654STobias Grosser   for (int i = 0; i < Kernel->n_grid; ++i) {
339472f9654STobias Grosser     isl_id *Id = isl_id_list_get_id(Kernel->block_ids, i);
340472f9654STobias Grosser     addId(Id, IntrinsicsBID[i]);
341472f9654STobias Grosser   }
342472f9654STobias Grosser 
343472f9654STobias Grosser   for (int i = 0; i < Kernel->n_block; ++i) {
344472f9654STobias Grosser     isl_id *Id = isl_id_list_get_id(Kernel->thread_ids, i);
345472f9654STobias Grosser     addId(Id, IntrinsicsTID[i]);
346472f9654STobias Grosser   }
347472f9654STobias Grosser }
348472f9654STobias Grosser 
34932837fe3STobias Grosser void GPUNodeBuilder::createKernelFunction(ppcg_kernel *Kernel) {
35032837fe3STobias Grosser 
35132837fe3STobias Grosser   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
35232837fe3STobias Grosser   GPUModule.reset(new Module(Identifier, Builder.getContext()));
35332837fe3STobias Grosser   GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda"));
35432837fe3STobias Grosser   GPUModule->setDataLayout(computeNVPTXDataLayout(true /* is64Bit */));
35532837fe3STobias Grosser 
35632837fe3STobias Grosser   Function *FN = createKernelFunctionDecl(Kernel);
35732837fe3STobias Grosser 
35859ab0705STobias Grosser   BasicBlock *PrevBlock = Builder.GetInsertBlock();
35932837fe3STobias Grosser   auto EntryBlock = BasicBlock::Create(Builder.getContext(), "entry", FN);
36032837fe3STobias Grosser 
36159ab0705STobias Grosser   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
36259ab0705STobias Grosser   DT.addNewBlock(EntryBlock, PrevBlock);
36359ab0705STobias Grosser 
36432837fe3STobias Grosser   Builder.SetInsertPoint(EntryBlock);
36532837fe3STobias Grosser   Builder.CreateRetVoid();
36632837fe3STobias Grosser   Builder.SetInsertPoint(EntryBlock, EntryBlock->begin());
367472f9654STobias Grosser 
368472f9654STobias Grosser   insertKernelIntrinsics(Kernel);
36932837fe3STobias Grosser }
37032837fe3STobias Grosser 
37132837fe3STobias Grosser void GPUNodeBuilder::finalizeKernelFunction() {
37232837fe3STobias Grosser 
37332837fe3STobias Grosser   if (DumpKernelIR)
37432837fe3STobias Grosser     outs() << *GPUModule << "\n";
37532837fe3STobias Grosser 
37632837fe3STobias Grosser   GPUModule.release();
377472f9654STobias Grosser   KernelIDs.clear();
37832837fe3STobias Grosser }
37932837fe3STobias Grosser 
3809dfe4e7cSTobias Grosser namespace {
3819dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass {
3829dfe4e7cSTobias Grosser public:
3839dfe4e7cSTobias Grosser   static char ID;
3849dfe4e7cSTobias Grosser 
385e938517eSTobias Grosser   /// The scop that is currently processed.
386e938517eSTobias Grosser   Scop *S;
387e938517eSTobias Grosser 
38838fc0aedSTobias Grosser   LoopInfo *LI;
38938fc0aedSTobias Grosser   DominatorTree *DT;
39038fc0aedSTobias Grosser   ScalarEvolution *SE;
39138fc0aedSTobias Grosser   const DataLayout *DL;
39238fc0aedSTobias Grosser   RegionInfo *RI;
39338fc0aedSTobias Grosser 
3949dfe4e7cSTobias Grosser   PPCGCodeGeneration() : ScopPass(ID) {}
3959dfe4e7cSTobias Grosser 
396e938517eSTobias Grosser   /// Construct compilation options for PPCG.
397e938517eSTobias Grosser   ///
398e938517eSTobias Grosser   /// @returns The compilation options.
399e938517eSTobias Grosser   ppcg_options *createPPCGOptions() {
400e938517eSTobias Grosser     auto DebugOptions =
401e938517eSTobias Grosser         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
402e938517eSTobias Grosser     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
403e938517eSTobias Grosser 
404e938517eSTobias Grosser     DebugOptions->dump_schedule_constraints = false;
405e938517eSTobias Grosser     DebugOptions->dump_schedule = false;
406e938517eSTobias Grosser     DebugOptions->dump_final_schedule = false;
407e938517eSTobias Grosser     DebugOptions->dump_sizes = false;
408e938517eSTobias Grosser 
409e938517eSTobias Grosser     Options->debug = DebugOptions;
410e938517eSTobias Grosser 
411e938517eSTobias Grosser     Options->reschedule = true;
412e938517eSTobias Grosser     Options->scale_tile_loops = false;
413e938517eSTobias Grosser     Options->wrap = false;
414e938517eSTobias Grosser 
415e938517eSTobias Grosser     Options->non_negative_parameters = false;
416e938517eSTobias Grosser     Options->ctx = nullptr;
417e938517eSTobias Grosser     Options->sizes = nullptr;
418e938517eSTobias Grosser 
4194eaedde5STobias Grosser     Options->tile_size = 32;
4204eaedde5STobias Grosser 
421e938517eSTobias Grosser     Options->use_private_memory = false;
422e938517eSTobias Grosser     Options->use_shared_memory = false;
423e938517eSTobias Grosser     Options->max_shared_memory = 0;
424e938517eSTobias Grosser 
425e938517eSTobias Grosser     Options->target = PPCG_TARGET_CUDA;
426e938517eSTobias Grosser     Options->openmp = false;
427e938517eSTobias Grosser     Options->linearize_device_arrays = true;
428e938517eSTobias Grosser     Options->live_range_reordering = false;
429e938517eSTobias Grosser 
430e938517eSTobias Grosser     Options->opencl_compiler_options = nullptr;
431e938517eSTobias Grosser     Options->opencl_use_gpu = false;
432e938517eSTobias Grosser     Options->opencl_n_include_file = 0;
433e938517eSTobias Grosser     Options->opencl_include_files = nullptr;
434e938517eSTobias Grosser     Options->opencl_print_kernel_types = false;
435e938517eSTobias Grosser     Options->opencl_embed_kernel_code = false;
436e938517eSTobias Grosser 
437e938517eSTobias Grosser     Options->save_schedule_file = nullptr;
438e938517eSTobias Grosser     Options->load_schedule_file = nullptr;
439e938517eSTobias Grosser 
440e938517eSTobias Grosser     return Options;
441e938517eSTobias Grosser   }
442e938517eSTobias Grosser 
443f384594dSTobias Grosser   /// Get a tagged access relation containing all accesses of type @p AccessTy.
444f384594dSTobias Grosser   ///
445f384594dSTobias Grosser   /// Instead of a normal access of the form:
446f384594dSTobias Grosser   ///
447f384594dSTobias Grosser   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
448f384594dSTobias Grosser   ///
449f384594dSTobias Grosser   /// a tagged access has the form
450f384594dSTobias Grosser   ///
451f384594dSTobias Grosser   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
452f384594dSTobias Grosser   ///
453f384594dSTobias Grosser   /// where 'id' is an additional space that references the memory access that
454f384594dSTobias Grosser   /// triggered the access.
455f384594dSTobias Grosser   ///
456f384594dSTobias Grosser   /// @param AccessTy The type of the memory accesses to collect.
457f384594dSTobias Grosser   ///
458f384594dSTobias Grosser   /// @return The relation describing all tagged memory accesses.
459f384594dSTobias Grosser   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
460f384594dSTobias Grosser     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
461f384594dSTobias Grosser 
462f384594dSTobias Grosser     for (auto &Stmt : *S)
463f384594dSTobias Grosser       for (auto &Acc : Stmt)
464f384594dSTobias Grosser         if (Acc->getType() == AccessTy) {
465f384594dSTobias Grosser           isl_map *Relation = Acc->getAccessRelation();
466f384594dSTobias Grosser           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
467f384594dSTobias Grosser 
468f384594dSTobias Grosser           isl_space *Space = isl_map_get_space(Relation);
469f384594dSTobias Grosser           Space = isl_space_range(Space);
470f384594dSTobias Grosser           Space = isl_space_from_range(Space);
4716293ba69STobias Grosser           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
472f384594dSTobias Grosser           isl_map *Universe = isl_map_universe(Space);
473f384594dSTobias Grosser           Relation = isl_map_domain_product(Relation, Universe);
474f384594dSTobias Grosser           Accesses = isl_union_map_add_map(Accesses, Relation);
475f384594dSTobias Grosser         }
476f384594dSTobias Grosser 
477f384594dSTobias Grosser     return Accesses;
478f384594dSTobias Grosser   }
479f384594dSTobias Grosser 
480f384594dSTobias Grosser   /// Get the set of all read accesses, tagged with the access id.
481f384594dSTobias Grosser   ///
482f384594dSTobias Grosser   /// @see getTaggedAccesses
483f384594dSTobias Grosser   isl_union_map *getTaggedReads() {
484f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::READ);
485f384594dSTobias Grosser   }
486f384594dSTobias Grosser 
487f384594dSTobias Grosser   /// Get the set of all may (and must) accesses, tagged with the access id.
488f384594dSTobias Grosser   ///
489f384594dSTobias Grosser   /// @see getTaggedAccesses
490f384594dSTobias Grosser   isl_union_map *getTaggedMayWrites() {
491f384594dSTobias Grosser     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
492f384594dSTobias Grosser                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
493f384594dSTobias Grosser   }
494f384594dSTobias Grosser 
495f384594dSTobias Grosser   /// Get the set of all must accesses, tagged with the access id.
496f384594dSTobias Grosser   ///
497f384594dSTobias Grosser   /// @see getTaggedAccesses
498f384594dSTobias Grosser   isl_union_map *getTaggedMustWrites() {
499f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
500f384594dSTobias Grosser   }
501f384594dSTobias Grosser 
502aef5196fSTobias Grosser   /// Collect parameter and array names as isl_ids.
503aef5196fSTobias Grosser   ///
504aef5196fSTobias Grosser   /// To reason about the different parameters and arrays used, ppcg requires
505aef5196fSTobias Grosser   /// a list of all isl_ids in use. As PPCG traditionally performs
506aef5196fSTobias Grosser   /// source-to-source compilation each of these isl_ids is mapped to the
507aef5196fSTobias Grosser   /// expression that represents it. As we do not have a corresponding
508aef5196fSTobias Grosser   /// expression in Polly, we just map each id to a 'zero' expression to match
509aef5196fSTobias Grosser   /// the data format that ppcg expects.
510aef5196fSTobias Grosser   ///
511aef5196fSTobias Grosser   /// @returns Retun a map from collected ids to 'zero' ast expressions.
512aef5196fSTobias Grosser   __isl_give isl_id_to_ast_expr *getNames() {
513aef5196fSTobias Grosser     auto *Names = isl_id_to_ast_expr_alloc(
514bd81a7eeSTobias Grosser         S->getIslCtx(),
515bd81a7eeSTobias Grosser         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
516aef5196fSTobias Grosser     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
517aef5196fSTobias Grosser     auto *Space = S->getParamSpace();
518aef5196fSTobias Grosser 
519aef5196fSTobias Grosser     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
520aef5196fSTobias Grosser       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
521aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
522aef5196fSTobias Grosser     }
523aef5196fSTobias Grosser 
524aef5196fSTobias Grosser     for (auto &Array : S->arrays()) {
525aef5196fSTobias Grosser       auto Id = Array.second->getBasePtrId();
526aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
527aef5196fSTobias Grosser     }
528aef5196fSTobias Grosser 
529aef5196fSTobias Grosser     isl_space_free(Space);
530aef5196fSTobias Grosser     isl_ast_expr_free(Zero);
531aef5196fSTobias Grosser 
532aef5196fSTobias Grosser     return Names;
533aef5196fSTobias Grosser   }
534aef5196fSTobias Grosser 
535e938517eSTobias Grosser   /// Create a new PPCG scop from the current scop.
536e938517eSTobias Grosser   ///
537f384594dSTobias Grosser   /// The PPCG scop is initialized with data from the current polly::Scop. From
538f384594dSTobias Grosser   /// this initial data, the data-dependences in the PPCG scop are initialized.
539f384594dSTobias Grosser   /// We do not use Polly's dependence analysis for now, to ensure we match
540f384594dSTobias Grosser   /// the PPCG default behaviour more closely.
541e938517eSTobias Grosser   ///
542e938517eSTobias Grosser   /// @returns A new ppcg scop.
543e938517eSTobias Grosser   ppcg_scop *createPPCGScop() {
544e938517eSTobias Grosser     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
545e938517eSTobias Grosser 
546e938517eSTobias Grosser     PPCGScop->options = createPPCGOptions();
547e938517eSTobias Grosser 
548e938517eSTobias Grosser     PPCGScop->start = 0;
549e938517eSTobias Grosser     PPCGScop->end = 0;
550e938517eSTobias Grosser 
551f384594dSTobias Grosser     PPCGScop->context = S->getContext();
552f384594dSTobias Grosser     PPCGScop->domain = S->getDomains();
553e938517eSTobias Grosser     PPCGScop->call = nullptr;
554f384594dSTobias Grosser     PPCGScop->tagged_reads = getTaggedReads();
555f384594dSTobias Grosser     PPCGScop->reads = S->getReads();
556e938517eSTobias Grosser     PPCGScop->live_in = nullptr;
557f384594dSTobias Grosser     PPCGScop->tagged_may_writes = getTaggedMayWrites();
558f384594dSTobias Grosser     PPCGScop->may_writes = S->getWrites();
559f384594dSTobias Grosser     PPCGScop->tagged_must_writes = getTaggedMustWrites();
560f384594dSTobias Grosser     PPCGScop->must_writes = S->getMustWrites();
561e938517eSTobias Grosser     PPCGScop->live_out = nullptr;
562f384594dSTobias Grosser     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
563e938517eSTobias Grosser     PPCGScop->tagger = nullptr;
564e938517eSTobias Grosser 
565e938517eSTobias Grosser     PPCGScop->independence = nullptr;
566e938517eSTobias Grosser     PPCGScop->dep_flow = nullptr;
567e938517eSTobias Grosser     PPCGScop->tagged_dep_flow = nullptr;
568e938517eSTobias Grosser     PPCGScop->dep_false = nullptr;
569e938517eSTobias Grosser     PPCGScop->dep_forced = nullptr;
570e938517eSTobias Grosser     PPCGScop->dep_order = nullptr;
571e938517eSTobias Grosser     PPCGScop->tagged_dep_order = nullptr;
572e938517eSTobias Grosser 
573f384594dSTobias Grosser     PPCGScop->schedule = S->getScheduleTree();
574aef5196fSTobias Grosser     PPCGScop->names = getNames();
575e938517eSTobias Grosser 
576e938517eSTobias Grosser     PPCGScop->pet = nullptr;
577e938517eSTobias Grosser 
578f384594dSTobias Grosser     compute_tagger(PPCGScop);
579f384594dSTobias Grosser     compute_dependences(PPCGScop);
580f384594dSTobias Grosser 
581e938517eSTobias Grosser     return PPCGScop;
582e938517eSTobias Grosser   }
583e938517eSTobias Grosser 
58460f63b49STobias Grosser   /// Collect the array acesses in a statement.
58560f63b49STobias Grosser   ///
58660f63b49STobias Grosser   /// @param Stmt The statement for which to collect the accesses.
58760f63b49STobias Grosser   ///
58860f63b49STobias Grosser   /// @returns A list of array accesses.
58960f63b49STobias Grosser   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
59060f63b49STobias Grosser     gpu_stmt_access *Accesses = nullptr;
59160f63b49STobias Grosser 
59260f63b49STobias Grosser     for (MemoryAccess *Acc : Stmt) {
59360f63b49STobias Grosser       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
59460f63b49STobias Grosser       Access->read = Acc->isRead();
59560f63b49STobias Grosser       Access->write = Acc->isWrite();
59660f63b49STobias Grosser       Access->access = Acc->getAccessRelation();
59760f63b49STobias Grosser       isl_space *Space = isl_map_get_space(Access->access);
59860f63b49STobias Grosser       Space = isl_space_range(Space);
59960f63b49STobias Grosser       Space = isl_space_from_range(Space);
6006293ba69STobias Grosser       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
60160f63b49STobias Grosser       isl_map *Universe = isl_map_universe(Space);
60260f63b49STobias Grosser       Access->tagged_access =
60360f63b49STobias Grosser           isl_map_domain_product(Acc->getAccessRelation(), Universe);
60460f63b49STobias Grosser       Access->exact_write = Acc->isWrite();
60560f63b49STobias Grosser       Access->ref_id = Acc->getId();
60660f63b49STobias Grosser       Access->next = Accesses;
60760f63b49STobias Grosser       Accesses = Access;
60860f63b49STobias Grosser     }
60960f63b49STobias Grosser 
61060f63b49STobias Grosser     return Accesses;
61160f63b49STobias Grosser   }
61260f63b49STobias Grosser 
61369b46751STobias Grosser   /// Collect the list of GPU statements.
61469b46751STobias Grosser   ///
61569b46751STobias Grosser   /// Each statement has an id, a pointer to the underlying data structure,
61669b46751STobias Grosser   /// as well as a list with all memory accesses.
61769b46751STobias Grosser   ///
61869b46751STobias Grosser   /// TODO: Initialize the list of memory accesses.
61969b46751STobias Grosser   ///
62069b46751STobias Grosser   /// @returns A linked-list of statements.
62169b46751STobias Grosser   gpu_stmt *getStatements() {
62269b46751STobias Grosser     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
62369b46751STobias Grosser                                        std::distance(S->begin(), S->end()));
62469b46751STobias Grosser 
62569b46751STobias Grosser     int i = 0;
62669b46751STobias Grosser     for (auto &Stmt : *S) {
62769b46751STobias Grosser       gpu_stmt *GPUStmt = &Stmts[i];
62869b46751STobias Grosser 
62969b46751STobias Grosser       GPUStmt->id = Stmt.getDomainId();
63069b46751STobias Grosser 
63169b46751STobias Grosser       // We use the pet stmt pointer to keep track of the Polly statements.
63269b46751STobias Grosser       GPUStmt->stmt = (pet_stmt *)&Stmt;
63360f63b49STobias Grosser       GPUStmt->accesses = getStmtAccesses(Stmt);
63469b46751STobias Grosser       i++;
63569b46751STobias Grosser     }
63669b46751STobias Grosser 
63769b46751STobias Grosser     return Stmts;
63869b46751STobias Grosser   }
63969b46751STobias Grosser 
64060f63b49STobias Grosser   /// Derive the extent of an array.
64160f63b49STobias Grosser   ///
64260f63b49STobias Grosser   /// The extent of an array is defined by the set of memory locations for
64360f63b49STobias Grosser   /// which a memory access in the iteration domain exists.
64460f63b49STobias Grosser   ///
64560f63b49STobias Grosser   /// @param Array The array to derive the extent for.
64660f63b49STobias Grosser   ///
64760f63b49STobias Grosser   /// @returns An isl_set describing the extent of the array.
64860f63b49STobias Grosser   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
64960f63b49STobias Grosser     isl_union_map *Accesses = S->getAccesses();
65060f63b49STobias Grosser     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
65160f63b49STobias Grosser     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
65260f63b49STobias Grosser     isl_set *AccessSet =
65360f63b49STobias Grosser         isl_union_set_extract_set(AccessUSet, Array->getSpace());
65460f63b49STobias Grosser     isl_union_set_free(AccessUSet);
65560f63b49STobias Grosser 
65660f63b49STobias Grosser     return AccessSet;
65760f63b49STobias Grosser   }
65860f63b49STobias Grosser 
65960f63b49STobias Grosser   /// Derive the bounds of an array.
66060f63b49STobias Grosser   ///
66160f63b49STobias Grosser   /// For the first dimension we derive the bound of the array from the extent
66260f63b49STobias Grosser   /// of this dimension. For inner dimensions we obtain their size directly from
66360f63b49STobias Grosser   /// ScopArrayInfo.
66460f63b49STobias Grosser   ///
66560f63b49STobias Grosser   /// @param PPCGArray The array to compute bounds for.
66660f63b49STobias Grosser   /// @param Array The polly array from which to take the information.
66760f63b49STobias Grosser   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
66860f63b49STobias Grosser     if (PPCGArray.n_index > 0) {
66960f63b49STobias Grosser       isl_set *Dom = isl_set_copy(PPCGArray.extent);
67060f63b49STobias Grosser       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
67160f63b49STobias Grosser       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
67260f63b49STobias Grosser       isl_set_free(Dom);
67360f63b49STobias Grosser       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
67460f63b49STobias Grosser       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
67560f63b49STobias Grosser       isl_aff *One = isl_aff_zero_on_domain(LS);
67660f63b49STobias Grosser       One = isl_aff_add_constant_si(One, 1);
67760f63b49STobias Grosser       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
67860f63b49STobias Grosser       Bound = isl_pw_aff_gist(Bound, S->getContext());
67960f63b49STobias Grosser       PPCGArray.bound[0] = Bound;
68060f63b49STobias Grosser     }
68160f63b49STobias Grosser 
68260f63b49STobias Grosser     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
68360f63b49STobias Grosser       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
68460f63b49STobias Grosser       auto LS = isl_pw_aff_get_domain_space(Bound);
68560f63b49STobias Grosser       auto Aff = isl_multi_aff_zero(LS);
68660f63b49STobias Grosser       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
68760f63b49STobias Grosser       PPCGArray.bound[i] = Bound;
68860f63b49STobias Grosser     }
68960f63b49STobias Grosser   }
69060f63b49STobias Grosser 
69160f63b49STobias Grosser   /// Create the arrays for @p PPCGProg.
69260f63b49STobias Grosser   ///
69360f63b49STobias Grosser   /// @param PPCGProg The program to compute the arrays for.
69460f63b49STobias Grosser   void createArrays(gpu_prog *PPCGProg) {
69560f63b49STobias Grosser     int i = 0;
69660f63b49STobias Grosser     for (auto &Element : S->arrays()) {
69760f63b49STobias Grosser       ScopArrayInfo *Array = Element.second.get();
69860f63b49STobias Grosser 
69960f63b49STobias Grosser       std::string TypeName;
70060f63b49STobias Grosser       raw_string_ostream OS(TypeName);
70160f63b49STobias Grosser 
70260f63b49STobias Grosser       OS << *Array->getElementType();
70360f63b49STobias Grosser       TypeName = OS.str();
70460f63b49STobias Grosser 
70560f63b49STobias Grosser       gpu_array_info &PPCGArray = PPCGProg->array[i];
70660f63b49STobias Grosser 
70760f63b49STobias Grosser       PPCGArray.space = Array->getSpace();
70860f63b49STobias Grosser       PPCGArray.type = strdup(TypeName.c_str());
70960f63b49STobias Grosser       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
71060f63b49STobias Grosser       PPCGArray.name = strdup(Array->getName().c_str());
71160f63b49STobias Grosser       PPCGArray.extent = nullptr;
71260f63b49STobias Grosser       PPCGArray.n_index = Array->getNumberOfDimensions();
71360f63b49STobias Grosser       PPCGArray.bound =
71460f63b49STobias Grosser           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
71560f63b49STobias Grosser       PPCGArray.extent = getExtent(Array);
71660f63b49STobias Grosser       PPCGArray.n_ref = 0;
71760f63b49STobias Grosser       PPCGArray.refs = nullptr;
71860f63b49STobias Grosser       PPCGArray.accessed = true;
71960f63b49STobias Grosser       PPCGArray.read_only_scalar = false;
72060f63b49STobias Grosser       PPCGArray.has_compound_element = false;
72160f63b49STobias Grosser       PPCGArray.local = false;
72260f63b49STobias Grosser       PPCGArray.declare_local = false;
72360f63b49STobias Grosser       PPCGArray.global = false;
72460f63b49STobias Grosser       PPCGArray.linearize = false;
72560f63b49STobias Grosser       PPCGArray.dep_order = nullptr;
72660f63b49STobias Grosser 
72760f63b49STobias Grosser       setArrayBounds(PPCGArray, Array);
7282d010dafSTobias Grosser       i++;
729b9fc860aSTobias Grosser 
730b9fc860aSTobias Grosser       collect_references(PPCGProg, &PPCGArray);
73160f63b49STobias Grosser     }
73260f63b49STobias Grosser   }
73360f63b49STobias Grosser 
73460f63b49STobias Grosser   /// Create an identity map between the arrays in the scop.
73560f63b49STobias Grosser   ///
73660f63b49STobias Grosser   /// @returns An identity map between the arrays in the scop.
73760f63b49STobias Grosser   isl_union_map *getArrayIdentity() {
73860f63b49STobias Grosser     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
73960f63b49STobias Grosser 
74060f63b49STobias Grosser     for (auto &Item : S->arrays()) {
74160f63b49STobias Grosser       ScopArrayInfo *Array = Item.second.get();
74260f63b49STobias Grosser       isl_space *Space = Array->getSpace();
74360f63b49STobias Grosser       Space = isl_space_map_from_set(Space);
74460f63b49STobias Grosser       isl_map *Identity = isl_map_identity(Space);
74560f63b49STobias Grosser       Maps = isl_union_map_add_map(Maps, Identity);
74660f63b49STobias Grosser     }
74760f63b49STobias Grosser 
74860f63b49STobias Grosser     return Maps;
74960f63b49STobias Grosser   }
75060f63b49STobias Grosser 
751e938517eSTobias Grosser   /// Create a default-initialized PPCG GPU program.
752e938517eSTobias Grosser   ///
753e938517eSTobias Grosser   /// @returns A new gpu grogram description.
754e938517eSTobias Grosser   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
755e938517eSTobias Grosser 
756e938517eSTobias Grosser     if (!PPCGScop)
757e938517eSTobias Grosser       return nullptr;
758e938517eSTobias Grosser 
759e938517eSTobias Grosser     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
760e938517eSTobias Grosser 
761e938517eSTobias Grosser     PPCGProg->ctx = S->getIslCtx();
762e938517eSTobias Grosser     PPCGProg->scop = PPCGScop;
763aef5196fSTobias Grosser     PPCGProg->context = isl_set_copy(PPCGScop->context);
76460f63b49STobias Grosser     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
76560f63b49STobias Grosser     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
76660f63b49STobias Grosser     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
76760f63b49STobias Grosser     PPCGProg->tagged_must_kill =
76860f63b49STobias Grosser         isl_union_map_copy(PPCGScop->tagged_must_kills);
76960f63b49STobias Grosser     PPCGProg->to_inner = getArrayIdentity();
77060f63b49STobias Grosser     PPCGProg->to_outer = getArrayIdentity();
77160f63b49STobias Grosser     PPCGProg->may_persist = compute_may_persist(PPCGProg);
772e938517eSTobias Grosser     PPCGProg->any_to_outer = nullptr;
773e938517eSTobias Grosser     PPCGProg->array_order = nullptr;
77469b46751STobias Grosser     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
77569b46751STobias Grosser     PPCGProg->stmts = getStatements();
77660f63b49STobias Grosser     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
77760f63b49STobias Grosser     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
77860f63b49STobias Grosser                                        PPCGProg->n_array);
77960f63b49STobias Grosser 
78060f63b49STobias Grosser     createArrays(PPCGProg);
781e938517eSTobias Grosser 
782e938517eSTobias Grosser     return PPCGProg;
783e938517eSTobias Grosser   }
784e938517eSTobias Grosser 
78569b46751STobias Grosser   struct PrintGPUUserData {
78669b46751STobias Grosser     struct cuda_info *CudaInfo;
78769b46751STobias Grosser     struct gpu_prog *PPCGProg;
78869b46751STobias Grosser     std::vector<ppcg_kernel *> Kernels;
78969b46751STobias Grosser   };
79069b46751STobias Grosser 
79169b46751STobias Grosser   /// Print a user statement node in the host code.
79269b46751STobias Grosser   ///
79369b46751STobias Grosser   /// We use ppcg's printing facilities to print the actual statement and
79469b46751STobias Grosser   /// additionally build up a list of all kernels that are encountered in the
79569b46751STobias Grosser   /// host ast.
79669b46751STobias Grosser   ///
79769b46751STobias Grosser   /// @param P The printer to print to
79869b46751STobias Grosser   /// @param Options The printing options to use
79969b46751STobias Grosser   /// @param Node The node to print
80069b46751STobias Grosser   /// @param User A user pointer to carry additional data. This pointer is
80169b46751STobias Grosser   ///             expected to be of type PrintGPUUserData.
80269b46751STobias Grosser   ///
80369b46751STobias Grosser   /// @returns A printer to which the output has been printed.
80469b46751STobias Grosser   static __isl_give isl_printer *
80569b46751STobias Grosser   printHostUser(__isl_take isl_printer *P,
80669b46751STobias Grosser                 __isl_take isl_ast_print_options *Options,
80769b46751STobias Grosser                 __isl_take isl_ast_node *Node, void *User) {
80869b46751STobias Grosser     auto Data = (struct PrintGPUUserData *)User;
80969b46751STobias Grosser     auto Id = isl_ast_node_get_annotation(Node);
81069b46751STobias Grosser 
81169b46751STobias Grosser     if (Id) {
81220251734STobias Grosser       bool IsUser = !strcmp(isl_id_get_name(Id), "user");
81320251734STobias Grosser 
81420251734STobias Grosser       // If this is a user statement, format it ourselves as ppcg would
81520251734STobias Grosser       // otherwise try to call pet functionality that is not available in
81620251734STobias Grosser       // Polly.
81720251734STobias Grosser       if (IsUser) {
81820251734STobias Grosser         P = isl_printer_start_line(P);
81920251734STobias Grosser         P = isl_printer_print_ast_node(P, Node);
82020251734STobias Grosser         P = isl_printer_end_line(P);
82120251734STobias Grosser         isl_id_free(Id);
82220251734STobias Grosser         isl_ast_print_options_free(Options);
82320251734STobias Grosser         return P;
82420251734STobias Grosser       }
82520251734STobias Grosser 
82669b46751STobias Grosser       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
82769b46751STobias Grosser       isl_id_free(Id);
82869b46751STobias Grosser       Data->Kernels.push_back(Kernel);
82969b46751STobias Grosser     }
83069b46751STobias Grosser 
83169b46751STobias Grosser     return print_host_user(P, Options, Node, User);
83269b46751STobias Grosser   }
83369b46751STobias Grosser 
83469b46751STobias Grosser   /// Print C code corresponding to the control flow in @p Kernel.
83569b46751STobias Grosser   ///
83669b46751STobias Grosser   /// @param Kernel The kernel to print
83769b46751STobias Grosser   void printKernel(ppcg_kernel *Kernel) {
83869b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
83969b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
84069b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
84169b46751STobias Grosser     P = isl_ast_node_print(Kernel->tree, P, Options);
84269b46751STobias Grosser     char *String = isl_printer_get_str(P);
84369b46751STobias Grosser     printf("%s\n", String);
84469b46751STobias Grosser     free(String);
84569b46751STobias Grosser     isl_printer_free(P);
84669b46751STobias Grosser   }
84769b46751STobias Grosser 
84869b46751STobias Grosser   /// Print C code corresponding to the GPU code described by @p Tree.
84969b46751STobias Grosser   ///
85069b46751STobias Grosser   /// @param Tree An AST describing GPU code
85169b46751STobias Grosser   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
85269b46751STobias Grosser   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
85369b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
85469b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
85569b46751STobias Grosser 
85669b46751STobias Grosser     PrintGPUUserData Data;
85769b46751STobias Grosser     Data.PPCGProg = PPCGProg;
85869b46751STobias Grosser 
85969b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
86069b46751STobias Grosser     Options =
86169b46751STobias Grosser         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
86269b46751STobias Grosser     P = isl_ast_node_print(Tree, P, Options);
86369b46751STobias Grosser     char *String = isl_printer_get_str(P);
86469b46751STobias Grosser     printf("# host\n");
86569b46751STobias Grosser     printf("%s\n", String);
86669b46751STobias Grosser     free(String);
86769b46751STobias Grosser     isl_printer_free(P);
86869b46751STobias Grosser 
86969b46751STobias Grosser     for (auto Kernel : Data.Kernels) {
87069b46751STobias Grosser       printf("# kernel%d\n", Kernel->id);
87169b46751STobias Grosser       printKernel(Kernel);
87269b46751STobias Grosser     }
87369b46751STobias Grosser   }
87469b46751STobias Grosser 
875f384594dSTobias Grosser   // Generate a GPU program using PPCG.
876f384594dSTobias Grosser   //
877f384594dSTobias Grosser   // GPU mapping consists of multiple steps:
878f384594dSTobias Grosser   //
879f384594dSTobias Grosser   //  1) Compute new schedule for the program.
880f384594dSTobias Grosser   //  2) Map schedule to GPU (TODO)
881f384594dSTobias Grosser   //  3) Generate code for new schedule (TODO)
882f384594dSTobias Grosser   //
883f384594dSTobias Grosser   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
884f384594dSTobias Grosser   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
885f384594dSTobias Grosser   // strategy directly from this pass.
886f384594dSTobias Grosser   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
887f384594dSTobias Grosser 
888f384594dSTobias Grosser     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
889f384594dSTobias Grosser 
890f384594dSTobias Grosser     PPCGGen->ctx = S->getIslCtx();
891f384594dSTobias Grosser     PPCGGen->options = PPCGScop->options;
892f384594dSTobias Grosser     PPCGGen->print = nullptr;
893f384594dSTobias Grosser     PPCGGen->print_user = nullptr;
89460c60025STobias Grosser     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
895f384594dSTobias Grosser     PPCGGen->prog = PPCGProg;
896f384594dSTobias Grosser     PPCGGen->tree = nullptr;
897f384594dSTobias Grosser     PPCGGen->types.n = 0;
898f384594dSTobias Grosser     PPCGGen->types.name = nullptr;
899f384594dSTobias Grosser     PPCGGen->sizes = nullptr;
900f384594dSTobias Grosser     PPCGGen->used_sizes = nullptr;
901f384594dSTobias Grosser     PPCGGen->kernel_id = 0;
902f384594dSTobias Grosser 
903f384594dSTobias Grosser     // Set scheduling strategy to same strategy PPCG is using.
904f384594dSTobias Grosser     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
905f384594dSTobias Grosser     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
9062341fe9eSTobias Grosser     isl_options_set_schedule_whole_component(PPCGGen->ctx, false);
907f384594dSTobias Grosser 
908f384594dSTobias Grosser     isl_schedule *Schedule = get_schedule(PPCGGen);
909f384594dSTobias Grosser 
910aef5196fSTobias Grosser     int has_permutable = has_any_permutable_node(Schedule);
911aef5196fSTobias Grosser 
91269b46751STobias Grosser     if (!has_permutable || has_permutable < 0) {
913aef5196fSTobias Grosser       Schedule = isl_schedule_free(Schedule);
91469b46751STobias Grosser     } else {
915aef5196fSTobias Grosser       Schedule = map_to_device(PPCGGen, Schedule);
91669b46751STobias Grosser       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
91769b46751STobias Grosser     }
918aef5196fSTobias Grosser 
919f384594dSTobias Grosser     if (DumpSchedule) {
920f384594dSTobias Grosser       isl_printer *P = isl_printer_to_str(S->getIslCtx());
921f384594dSTobias Grosser       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
922f384594dSTobias Grosser       P = isl_printer_print_str(P, "Schedule\n");
923f384594dSTobias Grosser       P = isl_printer_print_str(P, "========\n");
924f384594dSTobias Grosser       if (Schedule)
925f384594dSTobias Grosser         P = isl_printer_print_schedule(P, Schedule);
926f384594dSTobias Grosser       else
927f384594dSTobias Grosser         P = isl_printer_print_str(P, "No schedule found\n");
928f384594dSTobias Grosser 
929f384594dSTobias Grosser       printf("%s\n", isl_printer_get_str(P));
930f384594dSTobias Grosser       isl_printer_free(P);
931f384594dSTobias Grosser     }
932f384594dSTobias Grosser 
93369b46751STobias Grosser     if (DumpCode) {
93469b46751STobias Grosser       printf("Code\n");
93569b46751STobias Grosser       printf("====\n");
93669b46751STobias Grosser       if (PPCGGen->tree)
93769b46751STobias Grosser         printGPUTree(PPCGGen->tree, PPCGProg);
93869b46751STobias Grosser       else
93969b46751STobias Grosser         printf("No code generated\n");
94069b46751STobias Grosser     }
94169b46751STobias Grosser 
942f384594dSTobias Grosser     isl_schedule_free(Schedule);
943f384594dSTobias Grosser 
944f384594dSTobias Grosser     return PPCGGen;
945f384594dSTobias Grosser   }
946f384594dSTobias Grosser 
947f384594dSTobias Grosser   /// Free gpu_gen structure.
948f384594dSTobias Grosser   ///
949f384594dSTobias Grosser   /// @param PPCGGen The ppcg_gen object to free.
950f384594dSTobias Grosser   void freePPCGGen(gpu_gen *PPCGGen) {
951f384594dSTobias Grosser     isl_ast_node_free(PPCGGen->tree);
952f384594dSTobias Grosser     isl_union_map_free(PPCGGen->sizes);
953f384594dSTobias Grosser     isl_union_map_free(PPCGGen->used_sizes);
954f384594dSTobias Grosser     free(PPCGGen);
955f384594dSTobias Grosser   }
956f384594dSTobias Grosser 
957b307ed4dSTobias Grosser   /// Free the options in the ppcg scop structure.
958b307ed4dSTobias Grosser   ///
959b307ed4dSTobias Grosser   /// ppcg is not freeing these options for us. To avoid leaks we do this
960b307ed4dSTobias Grosser   /// ourselves.
961b307ed4dSTobias Grosser   ///
962b307ed4dSTobias Grosser   /// @param PPCGScop The scop referencing the options to free.
963b307ed4dSTobias Grosser   void freeOptions(ppcg_scop *PPCGScop) {
964b307ed4dSTobias Grosser     free(PPCGScop->options->debug);
965b307ed4dSTobias Grosser     PPCGScop->options->debug = nullptr;
966b307ed4dSTobias Grosser     free(PPCGScop->options);
967b307ed4dSTobias Grosser     PPCGScop->options = nullptr;
968b307ed4dSTobias Grosser   }
969b307ed4dSTobias Grosser 
97038fc0aedSTobias Grosser   /// Generate code for a given GPU AST described by @p Root.
97138fc0aedSTobias Grosser   ///
97232837fe3STobias Grosser   /// @param Root An isl_ast_node pointing to the root of the GPU AST.
97332837fe3STobias Grosser   /// @param Prog The GPU Program to generate code for.
97432837fe3STobias Grosser   void generateCode(__isl_take isl_ast_node *Root, gpu_prog *Prog) {
97538fc0aedSTobias Grosser     ScopAnnotator Annotator;
97638fc0aedSTobias Grosser     Annotator.buildAliasScopes(*S);
97738fc0aedSTobias Grosser 
97838fc0aedSTobias Grosser     Region *R = &S->getRegion();
97938fc0aedSTobias Grosser 
98038fc0aedSTobias Grosser     simplifyRegion(R, DT, LI, RI);
98138fc0aedSTobias Grosser 
98238fc0aedSTobias Grosser     BasicBlock *EnteringBB = R->getEnteringBlock();
98338fc0aedSTobias Grosser 
98438fc0aedSTobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
98538fc0aedSTobias Grosser 
98632837fe3STobias Grosser     GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, *S,
98732837fe3STobias Grosser                                Prog);
98838fc0aedSTobias Grosser 
98938fc0aedSTobias Grosser     // Only build the run-time condition and parameters _after_ having
99038fc0aedSTobias Grosser     // introduced the conditional branch. This is important as the conditional
99138fc0aedSTobias Grosser     // branch will guard the original scop from new induction variables that
99238fc0aedSTobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
99338fc0aedSTobias Grosser     // which may introduce scalar dependences that prevent us from correctly
99438fc0aedSTobias Grosser     // code generating this scop.
99538fc0aedSTobias Grosser     BasicBlock *StartBlock =
99638fc0aedSTobias Grosser         executeScopConditionally(*S, this, Builder.getTrue());
99738fc0aedSTobias Grosser 
99838fc0aedSTobias Grosser     // TODO: Handle LICM
99938fc0aedSTobias Grosser     // TODO: Verify run-time checks
100038fc0aedSTobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
100138fc0aedSTobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
100238fc0aedSTobias Grosser     NodeBuilder.addParameters(S->getContext());
100338fc0aedSTobias Grosser     Builder.SetInsertPoint(&*StartBlock->begin());
100438fc0aedSTobias Grosser     NodeBuilder.create(Root);
100538fc0aedSTobias Grosser     NodeBuilder.finalizeSCoP(*S);
100638fc0aedSTobias Grosser   }
100738fc0aedSTobias Grosser 
1008e938517eSTobias Grosser   bool runOnScop(Scop &CurrentScop) override {
1009e938517eSTobias Grosser     S = &CurrentScop;
101038fc0aedSTobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
101138fc0aedSTobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
101238fc0aedSTobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
101338fc0aedSTobias Grosser     DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
101438fc0aedSTobias Grosser     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
1015e938517eSTobias Grosser 
1016*2d58a64eSTobias Grosser     // We currently do not support scops with invariant loads.
1017*2d58a64eSTobias Grosser     if (S->hasInvariantAccesses())
1018*2d58a64eSTobias Grosser       return false;
1019*2d58a64eSTobias Grosser 
1020e938517eSTobias Grosser     auto PPCGScop = createPPCGScop();
1021e938517eSTobias Grosser     auto PPCGProg = createPPCGProg(PPCGScop);
1022f384594dSTobias Grosser     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
102338fc0aedSTobias Grosser 
102438fc0aedSTobias Grosser     if (PPCGGen->tree)
102532837fe3STobias Grosser       generateCode(isl_ast_node_copy(PPCGGen->tree), PPCGProg);
102638fc0aedSTobias Grosser 
1027b307ed4dSTobias Grosser     freeOptions(PPCGScop);
1028f384594dSTobias Grosser     freePPCGGen(PPCGGen);
1029e938517eSTobias Grosser     gpu_prog_free(PPCGProg);
1030e938517eSTobias Grosser     ppcg_scop_free(PPCGScop);
1031e938517eSTobias Grosser 
1032e938517eSTobias Grosser     return true;
1033e938517eSTobias Grosser   }
10349dfe4e7cSTobias Grosser 
10359dfe4e7cSTobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
10369dfe4e7cSTobias Grosser 
10379dfe4e7cSTobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
10389dfe4e7cSTobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
10399dfe4e7cSTobias Grosser     AU.addRequired<RegionInfoPass>();
10409dfe4e7cSTobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
10419dfe4e7cSTobias Grosser     AU.addRequired<ScopDetection>();
10429dfe4e7cSTobias Grosser     AU.addRequired<ScopInfoRegionPass>();
10439dfe4e7cSTobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
10449dfe4e7cSTobias Grosser 
10459dfe4e7cSTobias Grosser     AU.addPreserved<AAResultsWrapperPass>();
10469dfe4e7cSTobias Grosser     AU.addPreserved<BasicAAWrapperPass>();
10479dfe4e7cSTobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
10489dfe4e7cSTobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
10499dfe4e7cSTobias Grosser     AU.addPreserved<GlobalsAAWrapperPass>();
10509dfe4e7cSTobias Grosser     AU.addPreserved<PostDominatorTreeWrapperPass>();
10519dfe4e7cSTobias Grosser     AU.addPreserved<ScopDetection>();
10529dfe4e7cSTobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
10539dfe4e7cSTobias Grosser     AU.addPreserved<SCEVAAWrapperPass>();
10549dfe4e7cSTobias Grosser 
10559dfe4e7cSTobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
10569dfe4e7cSTobias Grosser     //        region tree.
10579dfe4e7cSTobias Grosser     AU.addPreserved<RegionInfoPass>();
10589dfe4e7cSTobias Grosser     AU.addPreserved<ScopInfoRegionPass>();
10599dfe4e7cSTobias Grosser   }
10609dfe4e7cSTobias Grosser };
10619dfe4e7cSTobias Grosser }
10629dfe4e7cSTobias Grosser 
10639dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1;
10649dfe4e7cSTobias Grosser 
10659dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
10669dfe4e7cSTobias Grosser 
10679dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
10689dfe4e7cSTobias Grosser                       "Polly - Apply PPCG translation to SCOP", false, false)
10699dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
10709dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
10719dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
10729dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
10739dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
10749dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
10759dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
10769dfe4e7cSTobias Grosser                     "Polly - Apply PPCG translation to SCOP", false, false)
1077