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"
21edb885cbSTobias Grosser #include "polly/Support/SCEVValidator.h"
2274dc3cb4STobias Grosser #include "llvm/ADT/PostOrderIterator.h"
239dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h"
249dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h"
259dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h"
269dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h"
279dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
2874dc3cb4STobias Grosser #include "llvm/Analysis/TargetLibraryInfo.h"
2974dc3cb4STobias Grosser #include "llvm/Analysis/TargetTransformInfo.h"
3074dc3cb4STobias Grosser #include "llvm/IR/LegacyPassManager.h"
31*e1a98343STobias Grosser #include "llvm/IR/Verifier.h"
3274dc3cb4STobias Grosser #include "llvm/Support/TargetRegistry.h"
3374dc3cb4STobias Grosser #include "llvm/Support/TargetSelect.h"
3474dc3cb4STobias Grosser #include "llvm/Target/TargetMachine.h"
359dfe4e7cSTobias Grosser 
36f384594dSTobias Grosser #include "isl/union_map.h"
37f384594dSTobias Grosser 
38e938517eSTobias Grosser extern "C" {
39a56f8f8eSTobias Grosser #include "ppcg/cuda.h"
40a56f8f8eSTobias Grosser #include "ppcg/gpu.h"
41a56f8f8eSTobias Grosser #include "ppcg/gpu_print.h"
42a56f8f8eSTobias Grosser #include "ppcg/ppcg.h"
43a56f8f8eSTobias Grosser #include "ppcg/schedule.h"
44e938517eSTobias Grosser }
45e938517eSTobias Grosser 
469dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h"
479dfe4e7cSTobias Grosser 
489dfe4e7cSTobias Grosser using namespace polly;
499dfe4e7cSTobias Grosser using namespace llvm;
509dfe4e7cSTobias Grosser 
519dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg"
529dfe4e7cSTobias Grosser 
53f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
54f384594dSTobias Grosser                                   cl::desc("Dump the computed GPU Schedule"),
55681bd568STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
56f384594dSTobias Grosser                                   cl::cat(PollyCategory));
5769b46751STobias Grosser 
5869b46751STobias Grosser static cl::opt<bool>
5969b46751STobias Grosser     DumpCode("polly-acc-dump-code",
6069b46751STobias Grosser              cl::desc("Dump C code describing the GPU mapping"), cl::Hidden,
6169b46751STobias Grosser              cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
6269b46751STobias Grosser 
6332837fe3STobias Grosser static cl::opt<bool> DumpKernelIR("polly-acc-dump-kernel-ir",
6432837fe3STobias Grosser                                   cl::desc("Dump the kernel LLVM-IR"),
6532837fe3STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
6632837fe3STobias Grosser                                   cl::cat(PollyCategory));
6732837fe3STobias Grosser 
6874dc3cb4STobias Grosser static cl::opt<bool> DumpKernelASM("polly-acc-dump-kernel-asm",
6974dc3cb4STobias Grosser                                    cl::desc("Dump the kernel assembly code"),
7074dc3cb4STobias Grosser                                    cl::Hidden, cl::init(false), cl::ZeroOrMore,
7174dc3cb4STobias Grosser                                    cl::cat(PollyCategory));
7274dc3cb4STobias Grosser 
7374dc3cb4STobias Grosser static cl::opt<bool> FastMath("polly-acc-fastmath",
7474dc3cb4STobias Grosser                               cl::desc("Allow unsafe math optimizations"),
7574dc3cb4STobias Grosser                               cl::Hidden, cl::init(false), cl::ZeroOrMore,
7674dc3cb4STobias Grosser                               cl::cat(PollyCategory));
7774dc3cb4STobias Grosser 
7874dc3cb4STobias Grosser static cl::opt<std::string>
7974dc3cb4STobias Grosser     CudaVersion("polly-acc-cuda-version",
8074dc3cb4STobias Grosser                 cl::desc("The CUDA version to compile for"), cl::Hidden,
8174dc3cb4STobias Grosser                 cl::init("sm_30"), cl::ZeroOrMore, cl::cat(PollyCategory));
8274dc3cb4STobias Grosser 
8360c60025STobias Grosser /// Create the ast expressions for a ScopStmt.
8460c60025STobias Grosser ///
8560c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each
8660c60025STobias Grosser /// of the scheduled ScopStmts.
8760c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
88edb885cbSTobias Grosser     void *StmtT, isl_ast_build *Build,
8960c60025STobias Grosser     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
9060c60025STobias Grosser                                        isl_id *Id, void *User),
9160c60025STobias Grosser     void *UserIndex,
9260c60025STobias Grosser     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
93edb885cbSTobias Grosser     void *UserExpr) {
9460c60025STobias Grosser 
95edb885cbSTobias Grosser   ScopStmt *Stmt = (ScopStmt *)StmtT;
9660c60025STobias Grosser 
97edb885cbSTobias Grosser   isl_ctx *Ctx;
98edb885cbSTobias Grosser 
99edb885cbSTobias Grosser   if (!Stmt || !Build)
100edb885cbSTobias Grosser     return NULL;
101edb885cbSTobias Grosser 
102edb885cbSTobias Grosser   Ctx = isl_ast_build_get_ctx(Build);
103edb885cbSTobias Grosser   isl_id_to_ast_expr *RefToExpr = isl_id_to_ast_expr_alloc(Ctx, 0);
104edb885cbSTobias Grosser 
105edb885cbSTobias Grosser   for (MemoryAccess *Acc : *Stmt) {
106edb885cbSTobias Grosser     isl_map *AddrFunc = Acc->getAddressFunction();
107edb885cbSTobias Grosser     AddrFunc = isl_map_intersect_domain(AddrFunc, Stmt->getDomain());
108edb885cbSTobias Grosser     isl_id *RefId = Acc->getId();
109edb885cbSTobias Grosser     isl_pw_multi_aff *PMA = isl_pw_multi_aff_from_map(AddrFunc);
110edb885cbSTobias Grosser     isl_multi_pw_aff *MPA = isl_multi_pw_aff_from_pw_multi_aff(PMA);
111edb885cbSTobias Grosser     MPA = isl_multi_pw_aff_coalesce(MPA);
112edb885cbSTobias Grosser     MPA = FunctionIndex(MPA, RefId, UserIndex);
113edb885cbSTobias Grosser     isl_ast_expr *Access = isl_ast_build_access_from_multi_pw_aff(Build, MPA);
114edb885cbSTobias Grosser     Access = FunctionExpr(Access, RefId, UserExpr);
115edb885cbSTobias Grosser     RefToExpr = isl_id_to_ast_expr_set(RefToExpr, RefId, Access);
116edb885cbSTobias Grosser   }
117edb885cbSTobias Grosser 
118edb885cbSTobias Grosser   return RefToExpr;
11960c60025STobias Grosser }
120f384594dSTobias Grosser 
12138fc0aedSTobias Grosser /// Generate code for a GPU specific isl AST.
12238fc0aedSTobias Grosser ///
12338fc0aedSTobias Grosser /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which
12438fc0aedSTobias Grosser /// generates code for general-prupose AST nodes, with special functionality
12538fc0aedSTobias Grosser /// for generating GPU specific user nodes.
12638fc0aedSTobias Grosser ///
12738fc0aedSTobias Grosser /// @see GPUNodeBuilder::createUser
12838fc0aedSTobias Grosser class GPUNodeBuilder : public IslNodeBuilder {
12938fc0aedSTobias Grosser public:
13038fc0aedSTobias Grosser   GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
13138fc0aedSTobias Grosser                  const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
13232837fe3STobias Grosser                  DominatorTree &DT, Scop &S, gpu_prog *Prog)
133edb885cbSTobias Grosser       : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S), Prog(Prog) {
134edb885cbSTobias Grosser     getExprBuilder().setIDToSAI(&IDToSAI);
135edb885cbSTobias Grosser   }
13638fc0aedSTobias Grosser 
13738fc0aedSTobias Grosser private:
13874dc3cb4STobias Grosser   /// A vector of array base pointers for which a new ScopArrayInfo was created.
13974dc3cb4STobias Grosser   ///
14074dc3cb4STobias Grosser   /// This vector is used to delete the ScopArrayInfo when it is not needed any
14174dc3cb4STobias Grosser   /// more.
14274dc3cb4STobias Grosser   std::vector<Value *> LocalArrays;
14374dc3cb4STobias Grosser 
14432837fe3STobias Grosser   /// A module containing GPU code.
14532837fe3STobias Grosser   ///
14632837fe3STobias Grosser   /// This pointer is only set in case we are currently generating GPU code.
14732837fe3STobias Grosser   std::unique_ptr<Module> GPUModule;
14832837fe3STobias Grosser 
14932837fe3STobias Grosser   /// The GPU program we generate code for.
15032837fe3STobias Grosser   gpu_prog *Prog;
15132837fe3STobias Grosser 
152472f9654STobias Grosser   /// Class to free isl_ids.
153472f9654STobias Grosser   class IslIdDeleter {
154472f9654STobias Grosser   public:
155472f9654STobias Grosser     void operator()(__isl_take isl_id *Id) { isl_id_free(Id); };
156472f9654STobias Grosser   };
157472f9654STobias Grosser 
158472f9654STobias Grosser   /// A set containing all isl_ids allocated in a GPU kernel.
159472f9654STobias Grosser   ///
160472f9654STobias Grosser   /// By releasing this set all isl_ids will be freed.
161472f9654STobias Grosser   std::set<std::unique_ptr<isl_id, IslIdDeleter>> KernelIDs;
162472f9654STobias Grosser 
163edb885cbSTobias Grosser   IslExprBuilder::IDToScopArrayInfoTy IDToSAI;
164edb885cbSTobias Grosser 
16538fc0aedSTobias Grosser   /// Create code for user-defined AST nodes.
16638fc0aedSTobias Grosser   ///
16738fc0aedSTobias Grosser   /// These AST nodes can be of type:
16838fc0aedSTobias Grosser   ///
16938fc0aedSTobias Grosser   ///   - ScopStmt:      A computational statement (TODO)
17038fc0aedSTobias Grosser   ///   - Kernel:        A GPU kernel call (TODO)
17138fc0aedSTobias Grosser   ///   - Data-Transfer: A GPU <-> CPU data-transfer (TODO)
1725260c041STobias Grosser   ///   - In-kernel synchronization
1735260c041STobias Grosser   ///   - In-kernel memory copy statement
17438fc0aedSTobias Grosser   ///
1751fb9b64dSTobias Grosser   /// @param UserStmt The ast node to generate code for.
1761fb9b64dSTobias Grosser   virtual void createUser(__isl_take isl_ast_node *UserStmt);
17732837fe3STobias Grosser 
178edb885cbSTobias Grosser   /// Find llvm::Values referenced in GPU kernel.
179edb885cbSTobias Grosser   ///
180edb885cbSTobias Grosser   /// @param Kernel The kernel to scan for llvm::Values
181edb885cbSTobias Grosser   ///
182edb885cbSTobias Grosser   /// @returns A set of values referenced by the kernel.
183edb885cbSTobias Grosser   SetVector<Value *> getReferencesInKernel(ppcg_kernel *Kernel);
184edb885cbSTobias Grosser 
18532837fe3STobias Grosser   /// Create GPU kernel.
18632837fe3STobias Grosser   ///
18732837fe3STobias Grosser   /// Code generate the kernel described by @p KernelStmt.
18832837fe3STobias Grosser   ///
18932837fe3STobias Grosser   /// @param KernelStmt The ast node to generate kernel code for.
19032837fe3STobias Grosser   void createKernel(__isl_take isl_ast_node *KernelStmt);
19132837fe3STobias Grosser 
19232837fe3STobias Grosser   /// Create kernel function.
19332837fe3STobias Grosser   ///
19432837fe3STobias Grosser   /// Create a kernel function located in a newly created module that can serve
19532837fe3STobias Grosser   /// as target for device code generation. Set the Builder to point to the
19632837fe3STobias Grosser   /// start block of this newly created function.
19732837fe3STobias Grosser   ///
19832837fe3STobias Grosser   /// @param Kernel The kernel to generate code for.
199edb885cbSTobias Grosser   /// @param SubtreeValues The set of llvm::Values referenced by this kernel.
200edb885cbSTobias Grosser   void createKernelFunction(ppcg_kernel *Kernel,
201edb885cbSTobias Grosser                             SetVector<Value *> &SubtreeValues);
20232837fe3STobias Grosser 
20332837fe3STobias Grosser   /// Create the declaration of a kernel function.
20432837fe3STobias Grosser   ///
20532837fe3STobias Grosser   /// The kernel function takes as arguments:
20632837fe3STobias Grosser   ///
20732837fe3STobias Grosser   ///   - One i8 pointer for each external array reference used in the kernel.
208f6044bd0STobias Grosser   ///   - Host iterators
209c84a1995STobias Grosser   ///   - Parameters
21032837fe3STobias Grosser   ///   - Other LLVM Value references (TODO)
21132837fe3STobias Grosser   ///
21232837fe3STobias Grosser   /// @param Kernel The kernel to generate the function declaration for.
213edb885cbSTobias Grosser   /// @param SubtreeValues The set of llvm::Values referenced by this kernel.
214edb885cbSTobias Grosser   ///
21532837fe3STobias Grosser   /// @returns The newly declared function.
216edb885cbSTobias Grosser   Function *createKernelFunctionDecl(ppcg_kernel *Kernel,
217edb885cbSTobias Grosser                                      SetVector<Value *> &SubtreeValues);
21832837fe3STobias Grosser 
219472f9654STobias Grosser   /// Insert intrinsic functions to obtain thread and block ids.
220472f9654STobias Grosser   ///
221472f9654STobias Grosser   /// @param The kernel to generate the intrinsic functions for.
222472f9654STobias Grosser   void insertKernelIntrinsics(ppcg_kernel *Kernel);
223472f9654STobias Grosser 
224edb885cbSTobias Grosser   /// Create code for a ScopStmt called in @p Expr.
225edb885cbSTobias Grosser   ///
226edb885cbSTobias Grosser   /// @param Expr The expression containing the call.
227edb885cbSTobias Grosser   /// @param KernelStmt The kernel statement referenced in the call.
228edb885cbSTobias Grosser   void createScopStmt(isl_ast_expr *Expr, ppcg_kernel_stmt *KernelStmt);
229edb885cbSTobias Grosser 
2305260c041STobias Grosser   /// Create an in-kernel synchronization call.
2315260c041STobias Grosser   void createKernelSync();
2325260c041STobias Grosser 
23374dc3cb4STobias Grosser   /// Create a PTX assembly string for the current GPU kernel.
23474dc3cb4STobias Grosser   ///
23574dc3cb4STobias Grosser   /// @returns A string containing the corresponding PTX assembly code.
23674dc3cb4STobias Grosser   std::string createKernelASM();
23774dc3cb4STobias Grosser 
23874dc3cb4STobias Grosser   /// Remove references from the dominator tree to the kernel function @p F.
23974dc3cb4STobias Grosser   ///
24074dc3cb4STobias Grosser   /// @param F The function to remove references to.
24174dc3cb4STobias Grosser   void clearDominators(Function *F);
24274dc3cb4STobias Grosser 
24374dc3cb4STobias Grosser   /// Remove references from scalar evolution to the kernel function @p F.
24474dc3cb4STobias Grosser   ///
24574dc3cb4STobias Grosser   /// @param F The function to remove references to.
24674dc3cb4STobias Grosser   void clearScalarEvolution(Function *F);
24774dc3cb4STobias Grosser 
24874dc3cb4STobias Grosser   /// Remove references from loop info to the kernel function @p F.
24974dc3cb4STobias Grosser   ///
25074dc3cb4STobias Grosser   /// @param F The function to remove references to.
25174dc3cb4STobias Grosser   void clearLoops(Function *F);
25274dc3cb4STobias Grosser 
25332837fe3STobias Grosser   /// Finalize the generation of the kernel function.
25432837fe3STobias Grosser   ///
25532837fe3STobias Grosser   /// Free the LLVM-IR module corresponding to the kernel and -- if requested --
25632837fe3STobias Grosser   /// dump its IR to stderr.
25732837fe3STobias Grosser   void finalizeKernelFunction();
2581fb9b64dSTobias Grosser };
2591fb9b64dSTobias Grosser 
2605260c041STobias Grosser /// Check if one string is a prefix of another.
2615260c041STobias Grosser ///
2625260c041STobias Grosser /// @param String The string in which to look for the prefix.
2635260c041STobias Grosser /// @param Prefix The prefix to look for.
2645260c041STobias Grosser static bool isPrefix(std::string String, std::string Prefix) {
2655260c041STobias Grosser   return String.find(Prefix) == 0;
2665260c041STobias Grosser }
2675260c041STobias Grosser 
2681fb9b64dSTobias Grosser void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) {
26932837fe3STobias Grosser   isl_ast_expr *Expr = isl_ast_node_user_get_expr(UserStmt);
27032837fe3STobias Grosser   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
27132837fe3STobias Grosser   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
27232837fe3STobias Grosser   isl_id_free(Id);
27332837fe3STobias Grosser   isl_ast_expr_free(StmtExpr);
27432837fe3STobias Grosser 
27532837fe3STobias Grosser   const char *Str = isl_id_get_name(Id);
27632837fe3STobias Grosser   if (!strcmp(Str, "kernel")) {
27732837fe3STobias Grosser     createKernel(UserStmt);
27832837fe3STobias Grosser     isl_ast_expr_free(Expr);
27932837fe3STobias Grosser     return;
28032837fe3STobias Grosser   }
28132837fe3STobias Grosser 
2825260c041STobias Grosser   if (isPrefix(Str, "to_device") || isPrefix(Str, "from_device")) {
2835260c041STobias Grosser     // TODO: Insert memory copies
28432837fe3STobias Grosser     isl_ast_expr_free(Expr);
2851fb9b64dSTobias Grosser     isl_ast_node_free(UserStmt);
28638fc0aedSTobias Grosser     return;
28738fc0aedSTobias Grosser   }
28838fc0aedSTobias Grosser 
2895260c041STobias Grosser   isl_id *Anno = isl_ast_node_get_annotation(UserStmt);
2905260c041STobias Grosser   struct ppcg_kernel_stmt *KernelStmt =
2915260c041STobias Grosser       (struct ppcg_kernel_stmt *)isl_id_get_user(Anno);
2925260c041STobias Grosser   isl_id_free(Anno);
2935260c041STobias Grosser 
2945260c041STobias Grosser   switch (KernelStmt->type) {
2955260c041STobias Grosser   case ppcg_kernel_domain:
296edb885cbSTobias Grosser     createScopStmt(Expr, KernelStmt);
2975260c041STobias Grosser     isl_ast_node_free(UserStmt);
2985260c041STobias Grosser     return;
2995260c041STobias Grosser   case ppcg_kernel_copy:
3005260c041STobias Grosser     // TODO: Create kernel copy stmt
3015260c041STobias Grosser     isl_ast_expr_free(Expr);
3025260c041STobias Grosser     isl_ast_node_free(UserStmt);
3035260c041STobias Grosser     return;
3045260c041STobias Grosser   case ppcg_kernel_sync:
3055260c041STobias Grosser     createKernelSync();
3065260c041STobias Grosser     isl_ast_expr_free(Expr);
3075260c041STobias Grosser     isl_ast_node_free(UserStmt);
3085260c041STobias Grosser     return;
3095260c041STobias Grosser   }
3105260c041STobias Grosser 
3115260c041STobias Grosser   isl_ast_expr_free(Expr);
3125260c041STobias Grosser   isl_ast_node_free(UserStmt);
3135260c041STobias Grosser   return;
3145260c041STobias Grosser }
3155260c041STobias Grosser 
316edb885cbSTobias Grosser void GPUNodeBuilder::createScopStmt(isl_ast_expr *Expr,
317edb885cbSTobias Grosser                                     ppcg_kernel_stmt *KernelStmt) {
318edb885cbSTobias Grosser   auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt;
319edb885cbSTobias Grosser   isl_id_to_ast_expr *Indexes = KernelStmt->u.d.ref2expr;
320edb885cbSTobias Grosser 
321edb885cbSTobias Grosser   LoopToScevMapT LTS;
322edb885cbSTobias Grosser   LTS.insert(OutsideLoopIterations.begin(), OutsideLoopIterations.end());
323edb885cbSTobias Grosser 
324edb885cbSTobias Grosser   createSubstitutions(Expr, Stmt, LTS);
325edb885cbSTobias Grosser 
326edb885cbSTobias Grosser   if (Stmt->isBlockStmt())
327edb885cbSTobias Grosser     BlockGen.copyStmt(*Stmt, LTS, Indexes);
328edb885cbSTobias Grosser   else
329edb885cbSTobias Grosser     assert(0 && "Region statement not supported\n");
330edb885cbSTobias Grosser }
331edb885cbSTobias Grosser 
3325260c041STobias Grosser void GPUNodeBuilder::createKernelSync() {
3335260c041STobias Grosser   Module *M = Builder.GetInsertBlock()->getParent()->getParent();
3345260c041STobias Grosser   auto *Sync = Intrinsic::getDeclaration(M, Intrinsic::nvvm_barrier0);
3355260c041STobias Grosser   Builder.CreateCall(Sync, {});
3365260c041STobias Grosser }
3375260c041STobias Grosser 
338edb885cbSTobias Grosser /// Collect llvm::Values referenced from @p Node
339edb885cbSTobias Grosser ///
340edb885cbSTobias Grosser /// This function only applies to isl_ast_nodes that are user_nodes referring
341edb885cbSTobias Grosser /// to a ScopStmt. All other node types are ignore.
342edb885cbSTobias Grosser ///
343edb885cbSTobias Grosser /// @param Node The node to collect references for.
344edb885cbSTobias Grosser /// @param User A user pointer used as storage for the data that is collected.
345edb885cbSTobias Grosser ///
346edb885cbSTobias Grosser /// @returns isl_bool_true if data could be collected successfully.
347edb885cbSTobias Grosser isl_bool collectReferencesInGPUStmt(__isl_keep isl_ast_node *Node, void *User) {
348edb885cbSTobias Grosser   if (isl_ast_node_get_type(Node) != isl_ast_node_user)
349edb885cbSTobias Grosser     return isl_bool_true;
350edb885cbSTobias Grosser 
351edb885cbSTobias Grosser   isl_ast_expr *Expr = isl_ast_node_user_get_expr(Node);
352edb885cbSTobias Grosser   isl_ast_expr *StmtExpr = isl_ast_expr_get_op_arg(Expr, 0);
353edb885cbSTobias Grosser   isl_id *Id = isl_ast_expr_get_id(StmtExpr);
354edb885cbSTobias Grosser   const char *Str = isl_id_get_name(Id);
355edb885cbSTobias Grosser   isl_id_free(Id);
356edb885cbSTobias Grosser   isl_ast_expr_free(StmtExpr);
357edb885cbSTobias Grosser   isl_ast_expr_free(Expr);
358edb885cbSTobias Grosser 
359edb885cbSTobias Grosser   if (!isPrefix(Str, "Stmt"))
360edb885cbSTobias Grosser     return isl_bool_true;
361edb885cbSTobias Grosser 
362edb885cbSTobias Grosser   Id = isl_ast_node_get_annotation(Node);
363edb885cbSTobias Grosser   auto *KernelStmt = (ppcg_kernel_stmt *)isl_id_get_user(Id);
364edb885cbSTobias Grosser   auto Stmt = (ScopStmt *)KernelStmt->u.d.stmt->stmt;
365edb885cbSTobias Grosser   isl_id_free(Id);
366edb885cbSTobias Grosser 
367edb885cbSTobias Grosser   addReferencesFromStmt(Stmt, User);
368edb885cbSTobias Grosser 
369edb885cbSTobias Grosser   return isl_bool_true;
370edb885cbSTobias Grosser }
371edb885cbSTobias Grosser 
372edb885cbSTobias Grosser SetVector<Value *> GPUNodeBuilder::getReferencesInKernel(ppcg_kernel *Kernel) {
373edb885cbSTobias Grosser   SetVector<Value *> SubtreeValues;
374edb885cbSTobias Grosser   SetVector<const SCEV *> SCEVs;
375edb885cbSTobias Grosser   SetVector<const Loop *> Loops;
376edb885cbSTobias Grosser   SubtreeReferences References = {
377edb885cbSTobias Grosser       LI, SE, S, ValueMap, SubtreeValues, SCEVs, getBlockGenerator()};
378edb885cbSTobias Grosser 
379edb885cbSTobias Grosser   for (const auto &I : IDToValue)
380edb885cbSTobias Grosser     SubtreeValues.insert(I.second);
381edb885cbSTobias Grosser 
382edb885cbSTobias Grosser   isl_ast_node_foreach_descendant_top_down(
383edb885cbSTobias Grosser       Kernel->tree, collectReferencesInGPUStmt, &References);
384edb885cbSTobias Grosser 
385edb885cbSTobias Grosser   for (const SCEV *Expr : SCEVs)
386edb885cbSTobias Grosser     findValues(Expr, SE, SubtreeValues);
387edb885cbSTobias Grosser 
388edb885cbSTobias Grosser   for (auto &SAI : S.arrays())
389edb885cbSTobias Grosser     SubtreeValues.remove(SAI.second->getBasePtr());
390edb885cbSTobias Grosser 
391edb885cbSTobias Grosser   isl_space *Space = S.getParamSpace();
392edb885cbSTobias Grosser   for (long i = 0; i < isl_space_dim(Space, isl_dim_param); i++) {
393edb885cbSTobias Grosser     isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, i);
394edb885cbSTobias Grosser     assert(IDToValue.count(Id));
395edb885cbSTobias Grosser     Value *Val = IDToValue[Id];
396edb885cbSTobias Grosser     SubtreeValues.remove(Val);
397edb885cbSTobias Grosser     isl_id_free(Id);
398edb885cbSTobias Grosser   }
399edb885cbSTobias Grosser   isl_space_free(Space);
400edb885cbSTobias Grosser 
401edb885cbSTobias Grosser   for (long i = 0; i < isl_space_dim(Kernel->space, isl_dim_set); i++) {
402edb885cbSTobias Grosser     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
403edb885cbSTobias Grosser     assert(IDToValue.count(Id));
404edb885cbSTobias Grosser     Value *Val = IDToValue[Id];
405edb885cbSTobias Grosser     SubtreeValues.remove(Val);
406edb885cbSTobias Grosser     isl_id_free(Id);
407edb885cbSTobias Grosser   }
408edb885cbSTobias Grosser 
409edb885cbSTobias Grosser   return SubtreeValues;
410edb885cbSTobias Grosser }
411edb885cbSTobias Grosser 
41274dc3cb4STobias Grosser void GPUNodeBuilder::clearDominators(Function *F) {
41374dc3cb4STobias Grosser   DomTreeNode *N = DT.getNode(&F->getEntryBlock());
41474dc3cb4STobias Grosser   std::vector<BasicBlock *> Nodes;
41574dc3cb4STobias Grosser   for (po_iterator<DomTreeNode *> I = po_begin(N), E = po_end(N); I != E; ++I)
41674dc3cb4STobias Grosser     Nodes.push_back(I->getBlock());
41774dc3cb4STobias Grosser 
41874dc3cb4STobias Grosser   for (BasicBlock *BB : Nodes)
41974dc3cb4STobias Grosser     DT.eraseNode(BB);
42074dc3cb4STobias Grosser }
42174dc3cb4STobias Grosser 
42274dc3cb4STobias Grosser void GPUNodeBuilder::clearScalarEvolution(Function *F) {
42374dc3cb4STobias Grosser   for (BasicBlock &BB : *F) {
42474dc3cb4STobias Grosser     Loop *L = LI.getLoopFor(&BB);
42574dc3cb4STobias Grosser     if (L)
42674dc3cb4STobias Grosser       SE.forgetLoop(L);
42774dc3cb4STobias Grosser   }
42874dc3cb4STobias Grosser }
42974dc3cb4STobias Grosser 
43074dc3cb4STobias Grosser void GPUNodeBuilder::clearLoops(Function *F) {
43174dc3cb4STobias Grosser   for (BasicBlock &BB : *F) {
43274dc3cb4STobias Grosser     Loop *L = LI.getLoopFor(&BB);
43374dc3cb4STobias Grosser     if (L)
43474dc3cb4STobias Grosser       SE.forgetLoop(L);
43574dc3cb4STobias Grosser     LI.removeBlock(&BB);
43674dc3cb4STobias Grosser   }
43774dc3cb4STobias Grosser }
43874dc3cb4STobias Grosser 
43932837fe3STobias Grosser void GPUNodeBuilder::createKernel(__isl_take isl_ast_node *KernelStmt) {
44032837fe3STobias Grosser   isl_id *Id = isl_ast_node_get_annotation(KernelStmt);
44132837fe3STobias Grosser   ppcg_kernel *Kernel = (ppcg_kernel *)isl_id_get_user(Id);
44232837fe3STobias Grosser   isl_id_free(Id);
44332837fe3STobias Grosser   isl_ast_node_free(KernelStmt);
44432837fe3STobias Grosser 
445edb885cbSTobias Grosser   SetVector<Value *> SubtreeValues = getReferencesInKernel(Kernel);
446edb885cbSTobias Grosser 
44732837fe3STobias Grosser   assert(Kernel->tree && "Device AST of kernel node is empty");
44832837fe3STobias Grosser 
44932837fe3STobias Grosser   Instruction &HostInsertPoint = *Builder.GetInsertPoint();
450472f9654STobias Grosser   IslExprBuilder::IDToValueTy HostIDs = IDToValue;
451edb885cbSTobias Grosser   ValueMapT HostValueMap = ValueMap;
45232837fe3STobias Grosser 
453edb885cbSTobias Grosser   SetVector<const Loop *> Loops;
454edb885cbSTobias Grosser 
455edb885cbSTobias Grosser   // Create for all loops we depend on values that contain the current loop
456edb885cbSTobias Grosser   // iteration. These values are necessary to generate code for SCEVs that
457edb885cbSTobias Grosser   // depend on such loops. As a result we need to pass them to the subfunction.
458edb885cbSTobias Grosser   for (const Loop *L : Loops) {
459edb885cbSTobias Grosser     const SCEV *OuterLIV = SE.getAddRecExpr(SE.getUnknown(Builder.getInt64(0)),
460edb885cbSTobias Grosser                                             SE.getUnknown(Builder.getInt64(1)),
461edb885cbSTobias Grosser                                             L, SCEV::FlagAnyWrap);
462edb885cbSTobias Grosser     Value *V = generateSCEV(OuterLIV);
463edb885cbSTobias Grosser     OutsideLoopIterations[L] = SE.getUnknown(V);
464edb885cbSTobias Grosser     SubtreeValues.insert(V);
465edb885cbSTobias Grosser   }
466edb885cbSTobias Grosser 
467edb885cbSTobias Grosser   createKernelFunction(Kernel, SubtreeValues);
46832837fe3STobias Grosser 
46959ab0705STobias Grosser   create(isl_ast_node_copy(Kernel->tree));
47059ab0705STobias Grosser 
47174dc3cb4STobias Grosser   Function *F = Builder.GetInsertBlock()->getParent();
47274dc3cb4STobias Grosser   clearDominators(F);
47374dc3cb4STobias Grosser   clearScalarEvolution(F);
47474dc3cb4STobias Grosser   clearLoops(F);
47574dc3cb4STobias Grosser 
47632837fe3STobias Grosser   Builder.SetInsertPoint(&HostInsertPoint);
477472f9654STobias Grosser   IDToValue = HostIDs;
47832837fe3STobias Grosser 
479edb885cbSTobias Grosser   ValueMap = HostValueMap;
480edb885cbSTobias Grosser   ScalarMap.clear();
481edb885cbSTobias Grosser   PHIOpMap.clear();
482edb885cbSTobias Grosser   EscapeMap.clear();
483edb885cbSTobias Grosser   IDToSAI.clear();
48474dc3cb4STobias Grosser   Annotator.resetAlternativeAliasBases();
48574dc3cb4STobias Grosser   for (auto &BasePtr : LocalArrays)
48674dc3cb4STobias Grosser     S.invalidateScopArrayInfo(BasePtr, ScopArrayInfo::MK_Array);
48774dc3cb4STobias Grosser   LocalArrays.clear();
488edb885cbSTobias Grosser 
48932837fe3STobias Grosser   finalizeKernelFunction();
49032837fe3STobias Grosser }
49132837fe3STobias Grosser 
49232837fe3STobias Grosser /// Compute the DataLayout string for the NVPTX backend.
49332837fe3STobias Grosser ///
49432837fe3STobias Grosser /// @param is64Bit Are we looking for a 64 bit architecture?
49532837fe3STobias Grosser static std::string computeNVPTXDataLayout(bool is64Bit) {
49632837fe3STobias Grosser   std::string Ret = "e";
49732837fe3STobias Grosser 
49832837fe3STobias Grosser   if (!is64Bit)
49932837fe3STobias Grosser     Ret += "-p:32:32";
50032837fe3STobias Grosser 
50132837fe3STobias Grosser   Ret += "-i64:64-v16:16-v32:32-n16:32:64";
50232837fe3STobias Grosser 
50332837fe3STobias Grosser   return Ret;
50432837fe3STobias Grosser }
50532837fe3STobias Grosser 
506edb885cbSTobias Grosser Function *
507edb885cbSTobias Grosser GPUNodeBuilder::createKernelFunctionDecl(ppcg_kernel *Kernel,
508edb885cbSTobias Grosser                                          SetVector<Value *> &SubtreeValues) {
50932837fe3STobias Grosser   std::vector<Type *> Args;
51032837fe3STobias Grosser   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
51132837fe3STobias Grosser 
51232837fe3STobias Grosser   for (long i = 0; i < Prog->n_array; i++) {
51332837fe3STobias Grosser     if (!ppcg_kernel_requires_array_argument(Kernel, i))
51432837fe3STobias Grosser       continue;
51532837fe3STobias Grosser 
51632837fe3STobias Grosser     Args.push_back(Builder.getInt8PtrTy());
51732837fe3STobias Grosser   }
51832837fe3STobias Grosser 
519f6044bd0STobias Grosser   int NumHostIters = isl_space_dim(Kernel->space, isl_dim_set);
520f6044bd0STobias Grosser 
521f6044bd0STobias Grosser   for (long i = 0; i < NumHostIters; i++)
522f6044bd0STobias Grosser     Args.push_back(Builder.getInt64Ty());
523f6044bd0STobias Grosser 
524c84a1995STobias Grosser   int NumVars = isl_space_dim(Kernel->space, isl_dim_param);
525c84a1995STobias Grosser 
526c84a1995STobias Grosser   for (long i = 0; i < NumVars; i++)
527c84a1995STobias Grosser     Args.push_back(Builder.getInt64Ty());
528c84a1995STobias Grosser 
529edb885cbSTobias Grosser   for (auto *V : SubtreeValues)
530edb885cbSTobias Grosser     Args.push_back(V->getType());
531edb885cbSTobias Grosser 
53232837fe3STobias Grosser   auto *FT = FunctionType::get(Builder.getVoidTy(), Args, false);
53332837fe3STobias Grosser   auto *FN = Function::Create(FT, Function::ExternalLinkage, Identifier,
53432837fe3STobias Grosser                               GPUModule.get());
53532837fe3STobias Grosser   FN->setCallingConv(CallingConv::PTX_Kernel);
53632837fe3STobias Grosser 
53732837fe3STobias Grosser   auto Arg = FN->arg_begin();
53832837fe3STobias Grosser   for (long i = 0; i < Kernel->n_array; i++) {
53932837fe3STobias Grosser     if (!ppcg_kernel_requires_array_argument(Kernel, i))
54032837fe3STobias Grosser       continue;
54132837fe3STobias Grosser 
542edb885cbSTobias Grosser     Arg->setName(Kernel->array[i].array->name);
543edb885cbSTobias Grosser 
544edb885cbSTobias Grosser     isl_id *Id = isl_space_get_tuple_id(Prog->array[i].space, isl_dim_set);
545edb885cbSTobias Grosser     const ScopArrayInfo *SAI = ScopArrayInfo::getFromId(isl_id_copy(Id));
546edb885cbSTobias Grosser     Type *EleTy = SAI->getElementType();
547edb885cbSTobias Grosser     Value *Val = &*Arg;
548edb885cbSTobias Grosser     SmallVector<const SCEV *, 4> Sizes;
549edb885cbSTobias Grosser     isl_ast_build *Build =
550edb885cbSTobias Grosser         isl_ast_build_from_context(isl_set_copy(Prog->context));
551edb885cbSTobias Grosser     for (long j = 1; j < Kernel->array[i].array->n_index; j++) {
552edb885cbSTobias Grosser       isl_ast_expr *DimSize = isl_ast_build_expr_from_pw_aff(
553edb885cbSTobias Grosser           Build, isl_pw_aff_copy(Kernel->array[i].array->bound[j]));
554edb885cbSTobias Grosser       auto V = ExprBuilder.create(DimSize);
555edb885cbSTobias Grosser       Sizes.push_back(SE.getSCEV(V));
556edb885cbSTobias Grosser     }
557edb885cbSTobias Grosser     const ScopArrayInfo *SAIRep =
558edb885cbSTobias Grosser         S.getOrCreateScopArrayInfo(Val, EleTy, Sizes, ScopArrayInfo::MK_Array);
55974dc3cb4STobias Grosser     LocalArrays.push_back(Val);
560edb885cbSTobias Grosser 
561edb885cbSTobias Grosser     isl_ast_build_free(Build);
562edb885cbSTobias Grosser     isl_id_free(Id);
563edb885cbSTobias Grosser     IDToSAI[Id] = SAIRep;
56432837fe3STobias Grosser     Arg++;
56532837fe3STobias Grosser   }
56632837fe3STobias Grosser 
567f6044bd0STobias Grosser   for (long i = 0; i < NumHostIters; i++) {
568f6044bd0STobias Grosser     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_set, i);
569f6044bd0STobias Grosser     Arg->setName(isl_id_get_name(Id));
570f6044bd0STobias Grosser     IDToValue[Id] = &*Arg;
571f6044bd0STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
572f6044bd0STobias Grosser     Arg++;
573f6044bd0STobias Grosser   }
574f6044bd0STobias Grosser 
575c84a1995STobias Grosser   for (long i = 0; i < NumVars; i++) {
576c84a1995STobias Grosser     isl_id *Id = isl_space_get_dim_id(Kernel->space, isl_dim_param, i);
577c84a1995STobias Grosser     Arg->setName(isl_id_get_name(Id));
578c84a1995STobias Grosser     IDToValue[Id] = &*Arg;
579c84a1995STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
580c84a1995STobias Grosser     Arg++;
581c84a1995STobias Grosser   }
582c84a1995STobias Grosser 
583edb885cbSTobias Grosser   for (auto *V : SubtreeValues) {
584edb885cbSTobias Grosser     Arg->setName(V->getName());
585edb885cbSTobias Grosser     ValueMap[V] = &*Arg;
586edb885cbSTobias Grosser     Arg++;
587edb885cbSTobias Grosser   }
588edb885cbSTobias Grosser 
58932837fe3STobias Grosser   return FN;
59032837fe3STobias Grosser }
59132837fe3STobias Grosser 
592472f9654STobias Grosser void GPUNodeBuilder::insertKernelIntrinsics(ppcg_kernel *Kernel) {
593472f9654STobias Grosser   Intrinsic::ID IntrinsicsBID[] = {Intrinsic::nvvm_read_ptx_sreg_ctaid_x,
594472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_ctaid_y};
595472f9654STobias Grosser 
596472f9654STobias Grosser   Intrinsic::ID IntrinsicsTID[] = {Intrinsic::nvvm_read_ptx_sreg_tid_x,
597472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_tid_y,
598472f9654STobias Grosser                                    Intrinsic::nvvm_read_ptx_sreg_tid_z};
599472f9654STobias Grosser 
600472f9654STobias Grosser   auto addId = [this](__isl_take isl_id *Id, Intrinsic::ID Intr) mutable {
601472f9654STobias Grosser     std::string Name = isl_id_get_name(Id);
602472f9654STobias Grosser     Module *M = Builder.GetInsertBlock()->getParent()->getParent();
603472f9654STobias Grosser     Function *IntrinsicFn = Intrinsic::getDeclaration(M, Intr);
604472f9654STobias Grosser     Value *Val = Builder.CreateCall(IntrinsicFn, {});
605472f9654STobias Grosser     Val = Builder.CreateIntCast(Val, Builder.getInt64Ty(), false, Name);
606472f9654STobias Grosser     IDToValue[Id] = Val;
607472f9654STobias Grosser     KernelIDs.insert(std::unique_ptr<isl_id, IslIdDeleter>(Id));
608472f9654STobias Grosser   };
609472f9654STobias Grosser 
610472f9654STobias Grosser   for (int i = 0; i < Kernel->n_grid; ++i) {
611472f9654STobias Grosser     isl_id *Id = isl_id_list_get_id(Kernel->block_ids, i);
612472f9654STobias Grosser     addId(Id, IntrinsicsBID[i]);
613472f9654STobias Grosser   }
614472f9654STobias Grosser 
615472f9654STobias Grosser   for (int i = 0; i < Kernel->n_block; ++i) {
616472f9654STobias Grosser     isl_id *Id = isl_id_list_get_id(Kernel->thread_ids, i);
617472f9654STobias Grosser     addId(Id, IntrinsicsTID[i]);
618472f9654STobias Grosser   }
619472f9654STobias Grosser }
620472f9654STobias Grosser 
621edb885cbSTobias Grosser void GPUNodeBuilder::createKernelFunction(ppcg_kernel *Kernel,
622edb885cbSTobias Grosser                                           SetVector<Value *> &SubtreeValues) {
62332837fe3STobias Grosser 
62432837fe3STobias Grosser   std::string Identifier = "kernel_" + std::to_string(Kernel->id);
62532837fe3STobias Grosser   GPUModule.reset(new Module(Identifier, Builder.getContext()));
62632837fe3STobias Grosser   GPUModule->setTargetTriple(Triple::normalize("nvptx64-nvidia-cuda"));
62732837fe3STobias Grosser   GPUModule->setDataLayout(computeNVPTXDataLayout(true /* is64Bit */));
62832837fe3STobias Grosser 
629edb885cbSTobias Grosser   Function *FN = createKernelFunctionDecl(Kernel, SubtreeValues);
63032837fe3STobias Grosser 
63159ab0705STobias Grosser   BasicBlock *PrevBlock = Builder.GetInsertBlock();
63232837fe3STobias Grosser   auto EntryBlock = BasicBlock::Create(Builder.getContext(), "entry", FN);
63332837fe3STobias Grosser 
63459ab0705STobias Grosser   DominatorTree &DT = P->getAnalysis<DominatorTreeWrapperPass>().getDomTree();
63559ab0705STobias Grosser   DT.addNewBlock(EntryBlock, PrevBlock);
63659ab0705STobias Grosser 
63732837fe3STobias Grosser   Builder.SetInsertPoint(EntryBlock);
63832837fe3STobias Grosser   Builder.CreateRetVoid();
63932837fe3STobias Grosser   Builder.SetInsertPoint(EntryBlock, EntryBlock->begin());
640472f9654STobias Grosser 
641472f9654STobias Grosser   insertKernelIntrinsics(Kernel);
64232837fe3STobias Grosser }
64332837fe3STobias Grosser 
64474dc3cb4STobias Grosser std::string GPUNodeBuilder::createKernelASM() {
64574dc3cb4STobias Grosser   llvm::Triple GPUTriple(Triple::normalize("nvptx64-nvidia-cuda"));
64674dc3cb4STobias Grosser   std::string ErrMsg;
64774dc3cb4STobias Grosser   auto GPUTarget = TargetRegistry::lookupTarget(GPUTriple.getTriple(), ErrMsg);
64874dc3cb4STobias Grosser 
64974dc3cb4STobias Grosser   if (!GPUTarget) {
65074dc3cb4STobias Grosser     errs() << ErrMsg << "\n";
65174dc3cb4STobias Grosser     return "";
65274dc3cb4STobias Grosser   }
65374dc3cb4STobias Grosser 
65474dc3cb4STobias Grosser   TargetOptions Options;
65574dc3cb4STobias Grosser   Options.UnsafeFPMath = FastMath;
65674dc3cb4STobias Grosser   std::unique_ptr<TargetMachine> TargetM(
65774dc3cb4STobias Grosser       GPUTarget->createTargetMachine(GPUTriple.getTriple(), CudaVersion, "",
65874dc3cb4STobias Grosser                                      Options, Optional<Reloc::Model>()));
65974dc3cb4STobias Grosser 
66074dc3cb4STobias Grosser   SmallString<0> ASMString;
66174dc3cb4STobias Grosser   raw_svector_ostream ASMStream(ASMString);
66274dc3cb4STobias Grosser   llvm::legacy::PassManager PM;
66374dc3cb4STobias Grosser 
66474dc3cb4STobias Grosser   PM.add(createTargetTransformInfoWrapperPass(TargetM->getTargetIRAnalysis()));
66574dc3cb4STobias Grosser 
66674dc3cb4STobias Grosser   if (TargetM->addPassesToEmitFile(
66774dc3cb4STobias Grosser           PM, ASMStream, TargetMachine::CGFT_AssemblyFile, true /* verify */)) {
66874dc3cb4STobias Grosser     errs() << "The target does not support generation of this file type!\n";
66974dc3cb4STobias Grosser     return "";
67074dc3cb4STobias Grosser   }
67174dc3cb4STobias Grosser 
67274dc3cb4STobias Grosser   PM.run(*GPUModule);
67374dc3cb4STobias Grosser 
67474dc3cb4STobias Grosser   return ASMStream.str();
67574dc3cb4STobias Grosser }
67674dc3cb4STobias Grosser 
67732837fe3STobias Grosser void GPUNodeBuilder::finalizeKernelFunction() {
678*e1a98343STobias Grosser   // Verify module.
679*e1a98343STobias Grosser   llvm::legacy::PassManager Passes;
680*e1a98343STobias Grosser   Passes.add(createVerifierPass());
681*e1a98343STobias Grosser   Passes.run(*GPUModule);
68232837fe3STobias Grosser 
68332837fe3STobias Grosser   if (DumpKernelIR)
68432837fe3STobias Grosser     outs() << *GPUModule << "\n";
68532837fe3STobias Grosser 
68674dc3cb4STobias Grosser   std::string Assembly = createKernelASM();
68774dc3cb4STobias Grosser 
68874dc3cb4STobias Grosser   if (DumpKernelASM)
68974dc3cb4STobias Grosser     outs() << Assembly << "\n";
69074dc3cb4STobias Grosser 
69132837fe3STobias Grosser   GPUModule.release();
692472f9654STobias Grosser   KernelIDs.clear();
69332837fe3STobias Grosser }
69432837fe3STobias Grosser 
6959dfe4e7cSTobias Grosser namespace {
6969dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass {
6979dfe4e7cSTobias Grosser public:
6989dfe4e7cSTobias Grosser   static char ID;
6999dfe4e7cSTobias Grosser 
700e938517eSTobias Grosser   /// The scop that is currently processed.
701e938517eSTobias Grosser   Scop *S;
702e938517eSTobias Grosser 
70338fc0aedSTobias Grosser   LoopInfo *LI;
70438fc0aedSTobias Grosser   DominatorTree *DT;
70538fc0aedSTobias Grosser   ScalarEvolution *SE;
70638fc0aedSTobias Grosser   const DataLayout *DL;
70738fc0aedSTobias Grosser   RegionInfo *RI;
70838fc0aedSTobias Grosser 
7099dfe4e7cSTobias Grosser   PPCGCodeGeneration() : ScopPass(ID) {}
7109dfe4e7cSTobias Grosser 
711e938517eSTobias Grosser   /// Construct compilation options for PPCG.
712e938517eSTobias Grosser   ///
713e938517eSTobias Grosser   /// @returns The compilation options.
714e938517eSTobias Grosser   ppcg_options *createPPCGOptions() {
715e938517eSTobias Grosser     auto DebugOptions =
716e938517eSTobias Grosser         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
717e938517eSTobias Grosser     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
718e938517eSTobias Grosser 
719e938517eSTobias Grosser     DebugOptions->dump_schedule_constraints = false;
720e938517eSTobias Grosser     DebugOptions->dump_schedule = false;
721e938517eSTobias Grosser     DebugOptions->dump_final_schedule = false;
722e938517eSTobias Grosser     DebugOptions->dump_sizes = false;
723e938517eSTobias Grosser 
724e938517eSTobias Grosser     Options->debug = DebugOptions;
725e938517eSTobias Grosser 
726e938517eSTobias Grosser     Options->reschedule = true;
727e938517eSTobias Grosser     Options->scale_tile_loops = false;
728e938517eSTobias Grosser     Options->wrap = false;
729e938517eSTobias Grosser 
730e938517eSTobias Grosser     Options->non_negative_parameters = false;
731e938517eSTobias Grosser     Options->ctx = nullptr;
732e938517eSTobias Grosser     Options->sizes = nullptr;
733e938517eSTobias Grosser 
7344eaedde5STobias Grosser     Options->tile_size = 32;
7354eaedde5STobias Grosser 
736e938517eSTobias Grosser     Options->use_private_memory = false;
737e938517eSTobias Grosser     Options->use_shared_memory = false;
738e938517eSTobias Grosser     Options->max_shared_memory = 0;
739e938517eSTobias Grosser 
740e938517eSTobias Grosser     Options->target = PPCG_TARGET_CUDA;
741e938517eSTobias Grosser     Options->openmp = false;
742e938517eSTobias Grosser     Options->linearize_device_arrays = true;
743e938517eSTobias Grosser     Options->live_range_reordering = false;
744e938517eSTobias Grosser 
745e938517eSTobias Grosser     Options->opencl_compiler_options = nullptr;
746e938517eSTobias Grosser     Options->opencl_use_gpu = false;
747e938517eSTobias Grosser     Options->opencl_n_include_file = 0;
748e938517eSTobias Grosser     Options->opencl_include_files = nullptr;
749e938517eSTobias Grosser     Options->opencl_print_kernel_types = false;
750e938517eSTobias Grosser     Options->opencl_embed_kernel_code = false;
751e938517eSTobias Grosser 
752e938517eSTobias Grosser     Options->save_schedule_file = nullptr;
753e938517eSTobias Grosser     Options->load_schedule_file = nullptr;
754e938517eSTobias Grosser 
755e938517eSTobias Grosser     return Options;
756e938517eSTobias Grosser   }
757e938517eSTobias Grosser 
758f384594dSTobias Grosser   /// Get a tagged access relation containing all accesses of type @p AccessTy.
759f384594dSTobias Grosser   ///
760f384594dSTobias Grosser   /// Instead of a normal access of the form:
761f384594dSTobias Grosser   ///
762f384594dSTobias Grosser   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
763f384594dSTobias Grosser   ///
764f384594dSTobias Grosser   /// a tagged access has the form
765f384594dSTobias Grosser   ///
766f384594dSTobias Grosser   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
767f384594dSTobias Grosser   ///
768f384594dSTobias Grosser   /// where 'id' is an additional space that references the memory access that
769f384594dSTobias Grosser   /// triggered the access.
770f384594dSTobias Grosser   ///
771f384594dSTobias Grosser   /// @param AccessTy The type of the memory accesses to collect.
772f384594dSTobias Grosser   ///
773f384594dSTobias Grosser   /// @return The relation describing all tagged memory accesses.
774f384594dSTobias Grosser   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
775f384594dSTobias Grosser     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
776f384594dSTobias Grosser 
777f384594dSTobias Grosser     for (auto &Stmt : *S)
778f384594dSTobias Grosser       for (auto &Acc : Stmt)
779f384594dSTobias Grosser         if (Acc->getType() == AccessTy) {
780f384594dSTobias Grosser           isl_map *Relation = Acc->getAccessRelation();
781f384594dSTobias Grosser           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
782f384594dSTobias Grosser 
783f384594dSTobias Grosser           isl_space *Space = isl_map_get_space(Relation);
784f384594dSTobias Grosser           Space = isl_space_range(Space);
785f384594dSTobias Grosser           Space = isl_space_from_range(Space);
7866293ba69STobias Grosser           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
787f384594dSTobias Grosser           isl_map *Universe = isl_map_universe(Space);
788f384594dSTobias Grosser           Relation = isl_map_domain_product(Relation, Universe);
789f384594dSTobias Grosser           Accesses = isl_union_map_add_map(Accesses, Relation);
790f384594dSTobias Grosser         }
791f384594dSTobias Grosser 
792f384594dSTobias Grosser     return Accesses;
793f384594dSTobias Grosser   }
794f384594dSTobias Grosser 
795f384594dSTobias Grosser   /// Get the set of all read accesses, tagged with the access id.
796f384594dSTobias Grosser   ///
797f384594dSTobias Grosser   /// @see getTaggedAccesses
798f384594dSTobias Grosser   isl_union_map *getTaggedReads() {
799f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::READ);
800f384594dSTobias Grosser   }
801f384594dSTobias Grosser 
802f384594dSTobias Grosser   /// Get the set of all may (and must) accesses, tagged with the access id.
803f384594dSTobias Grosser   ///
804f384594dSTobias Grosser   /// @see getTaggedAccesses
805f384594dSTobias Grosser   isl_union_map *getTaggedMayWrites() {
806f384594dSTobias Grosser     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
807f384594dSTobias Grosser                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
808f384594dSTobias Grosser   }
809f384594dSTobias Grosser 
810f384594dSTobias Grosser   /// Get the set of all must accesses, tagged with the access id.
811f384594dSTobias Grosser   ///
812f384594dSTobias Grosser   /// @see getTaggedAccesses
813f384594dSTobias Grosser   isl_union_map *getTaggedMustWrites() {
814f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
815f384594dSTobias Grosser   }
816f384594dSTobias Grosser 
817aef5196fSTobias Grosser   /// Collect parameter and array names as isl_ids.
818aef5196fSTobias Grosser   ///
819aef5196fSTobias Grosser   /// To reason about the different parameters and arrays used, ppcg requires
820aef5196fSTobias Grosser   /// a list of all isl_ids in use. As PPCG traditionally performs
821aef5196fSTobias Grosser   /// source-to-source compilation each of these isl_ids is mapped to the
822aef5196fSTobias Grosser   /// expression that represents it. As we do not have a corresponding
823aef5196fSTobias Grosser   /// expression in Polly, we just map each id to a 'zero' expression to match
824aef5196fSTobias Grosser   /// the data format that ppcg expects.
825aef5196fSTobias Grosser   ///
826aef5196fSTobias Grosser   /// @returns Retun a map from collected ids to 'zero' ast expressions.
827aef5196fSTobias Grosser   __isl_give isl_id_to_ast_expr *getNames() {
828aef5196fSTobias Grosser     auto *Names = isl_id_to_ast_expr_alloc(
829bd81a7eeSTobias Grosser         S->getIslCtx(),
830bd81a7eeSTobias Grosser         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
831aef5196fSTobias Grosser     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
832aef5196fSTobias Grosser     auto *Space = S->getParamSpace();
833aef5196fSTobias Grosser 
834aef5196fSTobias Grosser     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
835aef5196fSTobias Grosser       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
836aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
837aef5196fSTobias Grosser     }
838aef5196fSTobias Grosser 
839aef5196fSTobias Grosser     for (auto &Array : S->arrays()) {
840aef5196fSTobias Grosser       auto Id = Array.second->getBasePtrId();
841aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
842aef5196fSTobias Grosser     }
843aef5196fSTobias Grosser 
844aef5196fSTobias Grosser     isl_space_free(Space);
845aef5196fSTobias Grosser     isl_ast_expr_free(Zero);
846aef5196fSTobias Grosser 
847aef5196fSTobias Grosser     return Names;
848aef5196fSTobias Grosser   }
849aef5196fSTobias Grosser 
850e938517eSTobias Grosser   /// Create a new PPCG scop from the current scop.
851e938517eSTobias Grosser   ///
852f384594dSTobias Grosser   /// The PPCG scop is initialized with data from the current polly::Scop. From
853f384594dSTobias Grosser   /// this initial data, the data-dependences in the PPCG scop are initialized.
854f384594dSTobias Grosser   /// We do not use Polly's dependence analysis for now, to ensure we match
855f384594dSTobias Grosser   /// the PPCG default behaviour more closely.
856e938517eSTobias Grosser   ///
857e938517eSTobias Grosser   /// @returns A new ppcg scop.
858e938517eSTobias Grosser   ppcg_scop *createPPCGScop() {
859e938517eSTobias Grosser     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
860e938517eSTobias Grosser 
861e938517eSTobias Grosser     PPCGScop->options = createPPCGOptions();
862e938517eSTobias Grosser 
863e938517eSTobias Grosser     PPCGScop->start = 0;
864e938517eSTobias Grosser     PPCGScop->end = 0;
865e938517eSTobias Grosser 
866f384594dSTobias Grosser     PPCGScop->context = S->getContext();
867f384594dSTobias Grosser     PPCGScop->domain = S->getDomains();
868e938517eSTobias Grosser     PPCGScop->call = nullptr;
869f384594dSTobias Grosser     PPCGScop->tagged_reads = getTaggedReads();
870f384594dSTobias Grosser     PPCGScop->reads = S->getReads();
871e938517eSTobias Grosser     PPCGScop->live_in = nullptr;
872f384594dSTobias Grosser     PPCGScop->tagged_may_writes = getTaggedMayWrites();
873f384594dSTobias Grosser     PPCGScop->may_writes = S->getWrites();
874f384594dSTobias Grosser     PPCGScop->tagged_must_writes = getTaggedMustWrites();
875f384594dSTobias Grosser     PPCGScop->must_writes = S->getMustWrites();
876e938517eSTobias Grosser     PPCGScop->live_out = nullptr;
877f384594dSTobias Grosser     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
878e938517eSTobias Grosser     PPCGScop->tagger = nullptr;
879e938517eSTobias Grosser 
880e938517eSTobias Grosser     PPCGScop->independence = nullptr;
881e938517eSTobias Grosser     PPCGScop->dep_flow = nullptr;
882e938517eSTobias Grosser     PPCGScop->tagged_dep_flow = nullptr;
883e938517eSTobias Grosser     PPCGScop->dep_false = nullptr;
884e938517eSTobias Grosser     PPCGScop->dep_forced = nullptr;
885e938517eSTobias Grosser     PPCGScop->dep_order = nullptr;
886e938517eSTobias Grosser     PPCGScop->tagged_dep_order = nullptr;
887e938517eSTobias Grosser 
888f384594dSTobias Grosser     PPCGScop->schedule = S->getScheduleTree();
889aef5196fSTobias Grosser     PPCGScop->names = getNames();
890e938517eSTobias Grosser 
891e938517eSTobias Grosser     PPCGScop->pet = nullptr;
892e938517eSTobias Grosser 
893f384594dSTobias Grosser     compute_tagger(PPCGScop);
894f384594dSTobias Grosser     compute_dependences(PPCGScop);
895f384594dSTobias Grosser 
896e938517eSTobias Grosser     return PPCGScop;
897e938517eSTobias Grosser   }
898e938517eSTobias Grosser 
89960f63b49STobias Grosser   /// Collect the array acesses in a statement.
90060f63b49STobias Grosser   ///
90160f63b49STobias Grosser   /// @param Stmt The statement for which to collect the accesses.
90260f63b49STobias Grosser   ///
90360f63b49STobias Grosser   /// @returns A list of array accesses.
90460f63b49STobias Grosser   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
90560f63b49STobias Grosser     gpu_stmt_access *Accesses = nullptr;
90660f63b49STobias Grosser 
90760f63b49STobias Grosser     for (MemoryAccess *Acc : Stmt) {
90860f63b49STobias Grosser       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
90960f63b49STobias Grosser       Access->read = Acc->isRead();
91060f63b49STobias Grosser       Access->write = Acc->isWrite();
91160f63b49STobias Grosser       Access->access = Acc->getAccessRelation();
91260f63b49STobias Grosser       isl_space *Space = isl_map_get_space(Access->access);
91360f63b49STobias Grosser       Space = isl_space_range(Space);
91460f63b49STobias Grosser       Space = isl_space_from_range(Space);
9156293ba69STobias Grosser       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
91660f63b49STobias Grosser       isl_map *Universe = isl_map_universe(Space);
91760f63b49STobias Grosser       Access->tagged_access =
91860f63b49STobias Grosser           isl_map_domain_product(Acc->getAccessRelation(), Universe);
91960f63b49STobias Grosser       Access->exact_write = Acc->isWrite();
92060f63b49STobias Grosser       Access->ref_id = Acc->getId();
92160f63b49STobias Grosser       Access->next = Accesses;
92260f63b49STobias Grosser       Accesses = Access;
92360f63b49STobias Grosser     }
92460f63b49STobias Grosser 
92560f63b49STobias Grosser     return Accesses;
92660f63b49STobias Grosser   }
92760f63b49STobias Grosser 
92869b46751STobias Grosser   /// Collect the list of GPU statements.
92969b46751STobias Grosser   ///
93069b46751STobias Grosser   /// Each statement has an id, a pointer to the underlying data structure,
93169b46751STobias Grosser   /// as well as a list with all memory accesses.
93269b46751STobias Grosser   ///
93369b46751STobias Grosser   /// TODO: Initialize the list of memory accesses.
93469b46751STobias Grosser   ///
93569b46751STobias Grosser   /// @returns A linked-list of statements.
93669b46751STobias Grosser   gpu_stmt *getStatements() {
93769b46751STobias Grosser     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
93869b46751STobias Grosser                                        std::distance(S->begin(), S->end()));
93969b46751STobias Grosser 
94069b46751STobias Grosser     int i = 0;
94169b46751STobias Grosser     for (auto &Stmt : *S) {
94269b46751STobias Grosser       gpu_stmt *GPUStmt = &Stmts[i];
94369b46751STobias Grosser 
94469b46751STobias Grosser       GPUStmt->id = Stmt.getDomainId();
94569b46751STobias Grosser 
94669b46751STobias Grosser       // We use the pet stmt pointer to keep track of the Polly statements.
94769b46751STobias Grosser       GPUStmt->stmt = (pet_stmt *)&Stmt;
94860f63b49STobias Grosser       GPUStmt->accesses = getStmtAccesses(Stmt);
94969b46751STobias Grosser       i++;
95069b46751STobias Grosser     }
95169b46751STobias Grosser 
95269b46751STobias Grosser     return Stmts;
95369b46751STobias Grosser   }
95469b46751STobias Grosser 
95560f63b49STobias Grosser   /// Derive the extent of an array.
95660f63b49STobias Grosser   ///
95760f63b49STobias Grosser   /// The extent of an array is defined by the set of memory locations for
95860f63b49STobias Grosser   /// which a memory access in the iteration domain exists.
95960f63b49STobias Grosser   ///
96060f63b49STobias Grosser   /// @param Array The array to derive the extent for.
96160f63b49STobias Grosser   ///
96260f63b49STobias Grosser   /// @returns An isl_set describing the extent of the array.
96360f63b49STobias Grosser   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
96460f63b49STobias Grosser     isl_union_map *Accesses = S->getAccesses();
96560f63b49STobias Grosser     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
96660f63b49STobias Grosser     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
96760f63b49STobias Grosser     isl_set *AccessSet =
96860f63b49STobias Grosser         isl_union_set_extract_set(AccessUSet, Array->getSpace());
96960f63b49STobias Grosser     isl_union_set_free(AccessUSet);
97060f63b49STobias Grosser 
97160f63b49STobias Grosser     return AccessSet;
97260f63b49STobias Grosser   }
97360f63b49STobias Grosser 
97460f63b49STobias Grosser   /// Derive the bounds of an array.
97560f63b49STobias Grosser   ///
97660f63b49STobias Grosser   /// For the first dimension we derive the bound of the array from the extent
97760f63b49STobias Grosser   /// of this dimension. For inner dimensions we obtain their size directly from
97860f63b49STobias Grosser   /// ScopArrayInfo.
97960f63b49STobias Grosser   ///
98060f63b49STobias Grosser   /// @param PPCGArray The array to compute bounds for.
98160f63b49STobias Grosser   /// @param Array The polly array from which to take the information.
98260f63b49STobias Grosser   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
98360f63b49STobias Grosser     if (PPCGArray.n_index > 0) {
98460f63b49STobias Grosser       isl_set *Dom = isl_set_copy(PPCGArray.extent);
98560f63b49STobias Grosser       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
98660f63b49STobias Grosser       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
98760f63b49STobias Grosser       isl_set_free(Dom);
98860f63b49STobias Grosser       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
98960f63b49STobias Grosser       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
99060f63b49STobias Grosser       isl_aff *One = isl_aff_zero_on_domain(LS);
99160f63b49STobias Grosser       One = isl_aff_add_constant_si(One, 1);
99260f63b49STobias Grosser       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
99360f63b49STobias Grosser       Bound = isl_pw_aff_gist(Bound, S->getContext());
99460f63b49STobias Grosser       PPCGArray.bound[0] = Bound;
99560f63b49STobias Grosser     }
99660f63b49STobias Grosser 
99760f63b49STobias Grosser     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
99860f63b49STobias Grosser       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
99960f63b49STobias Grosser       auto LS = isl_pw_aff_get_domain_space(Bound);
100060f63b49STobias Grosser       auto Aff = isl_multi_aff_zero(LS);
100160f63b49STobias Grosser       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
100260f63b49STobias Grosser       PPCGArray.bound[i] = Bound;
100360f63b49STobias Grosser     }
100460f63b49STobias Grosser   }
100560f63b49STobias Grosser 
100660f63b49STobias Grosser   /// Create the arrays for @p PPCGProg.
100760f63b49STobias Grosser   ///
100860f63b49STobias Grosser   /// @param PPCGProg The program to compute the arrays for.
100960f63b49STobias Grosser   void createArrays(gpu_prog *PPCGProg) {
101060f63b49STobias Grosser     int i = 0;
101160f63b49STobias Grosser     for (auto &Element : S->arrays()) {
101260f63b49STobias Grosser       ScopArrayInfo *Array = Element.second.get();
101360f63b49STobias Grosser 
101460f63b49STobias Grosser       std::string TypeName;
101560f63b49STobias Grosser       raw_string_ostream OS(TypeName);
101660f63b49STobias Grosser 
101760f63b49STobias Grosser       OS << *Array->getElementType();
101860f63b49STobias Grosser       TypeName = OS.str();
101960f63b49STobias Grosser 
102060f63b49STobias Grosser       gpu_array_info &PPCGArray = PPCGProg->array[i];
102160f63b49STobias Grosser 
102260f63b49STobias Grosser       PPCGArray.space = Array->getSpace();
102360f63b49STobias Grosser       PPCGArray.type = strdup(TypeName.c_str());
102460f63b49STobias Grosser       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
102560f63b49STobias Grosser       PPCGArray.name = strdup(Array->getName().c_str());
102660f63b49STobias Grosser       PPCGArray.extent = nullptr;
102760f63b49STobias Grosser       PPCGArray.n_index = Array->getNumberOfDimensions();
102860f63b49STobias Grosser       PPCGArray.bound =
102960f63b49STobias Grosser           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
103060f63b49STobias Grosser       PPCGArray.extent = getExtent(Array);
103160f63b49STobias Grosser       PPCGArray.n_ref = 0;
103260f63b49STobias Grosser       PPCGArray.refs = nullptr;
103360f63b49STobias Grosser       PPCGArray.accessed = true;
103460f63b49STobias Grosser       PPCGArray.read_only_scalar = false;
103560f63b49STobias Grosser       PPCGArray.has_compound_element = false;
103660f63b49STobias Grosser       PPCGArray.local = false;
103760f63b49STobias Grosser       PPCGArray.declare_local = false;
103860f63b49STobias Grosser       PPCGArray.global = false;
103960f63b49STobias Grosser       PPCGArray.linearize = false;
104060f63b49STobias Grosser       PPCGArray.dep_order = nullptr;
104160f63b49STobias Grosser 
104260f63b49STobias Grosser       setArrayBounds(PPCGArray, Array);
10432d010dafSTobias Grosser       i++;
1044b9fc860aSTobias Grosser 
1045b9fc860aSTobias Grosser       collect_references(PPCGProg, &PPCGArray);
104660f63b49STobias Grosser     }
104760f63b49STobias Grosser   }
104860f63b49STobias Grosser 
104960f63b49STobias Grosser   /// Create an identity map between the arrays in the scop.
105060f63b49STobias Grosser   ///
105160f63b49STobias Grosser   /// @returns An identity map between the arrays in the scop.
105260f63b49STobias Grosser   isl_union_map *getArrayIdentity() {
105360f63b49STobias Grosser     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
105460f63b49STobias Grosser 
105560f63b49STobias Grosser     for (auto &Item : S->arrays()) {
105660f63b49STobias Grosser       ScopArrayInfo *Array = Item.second.get();
105760f63b49STobias Grosser       isl_space *Space = Array->getSpace();
105860f63b49STobias Grosser       Space = isl_space_map_from_set(Space);
105960f63b49STobias Grosser       isl_map *Identity = isl_map_identity(Space);
106060f63b49STobias Grosser       Maps = isl_union_map_add_map(Maps, Identity);
106160f63b49STobias Grosser     }
106260f63b49STobias Grosser 
106360f63b49STobias Grosser     return Maps;
106460f63b49STobias Grosser   }
106560f63b49STobias Grosser 
1066e938517eSTobias Grosser   /// Create a default-initialized PPCG GPU program.
1067e938517eSTobias Grosser   ///
1068e938517eSTobias Grosser   /// @returns A new gpu grogram description.
1069e938517eSTobias Grosser   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
1070e938517eSTobias Grosser 
1071e938517eSTobias Grosser     if (!PPCGScop)
1072e938517eSTobias Grosser       return nullptr;
1073e938517eSTobias Grosser 
1074e938517eSTobias Grosser     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
1075e938517eSTobias Grosser 
1076e938517eSTobias Grosser     PPCGProg->ctx = S->getIslCtx();
1077e938517eSTobias Grosser     PPCGProg->scop = PPCGScop;
1078aef5196fSTobias Grosser     PPCGProg->context = isl_set_copy(PPCGScop->context);
107960f63b49STobias Grosser     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
108060f63b49STobias Grosser     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
108160f63b49STobias Grosser     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
108260f63b49STobias Grosser     PPCGProg->tagged_must_kill =
108360f63b49STobias Grosser         isl_union_map_copy(PPCGScop->tagged_must_kills);
108460f63b49STobias Grosser     PPCGProg->to_inner = getArrayIdentity();
108560f63b49STobias Grosser     PPCGProg->to_outer = getArrayIdentity();
108660f63b49STobias Grosser     PPCGProg->may_persist = compute_may_persist(PPCGProg);
1087e938517eSTobias Grosser     PPCGProg->any_to_outer = nullptr;
1088e938517eSTobias Grosser     PPCGProg->array_order = nullptr;
108969b46751STobias Grosser     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
109069b46751STobias Grosser     PPCGProg->stmts = getStatements();
109160f63b49STobias Grosser     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
109260f63b49STobias Grosser     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
109360f63b49STobias Grosser                                        PPCGProg->n_array);
109460f63b49STobias Grosser 
109560f63b49STobias Grosser     createArrays(PPCGProg);
1096e938517eSTobias Grosser 
1097e938517eSTobias Grosser     return PPCGProg;
1098e938517eSTobias Grosser   }
1099e938517eSTobias Grosser 
110069b46751STobias Grosser   struct PrintGPUUserData {
110169b46751STobias Grosser     struct cuda_info *CudaInfo;
110269b46751STobias Grosser     struct gpu_prog *PPCGProg;
110369b46751STobias Grosser     std::vector<ppcg_kernel *> Kernels;
110469b46751STobias Grosser   };
110569b46751STobias Grosser 
110669b46751STobias Grosser   /// Print a user statement node in the host code.
110769b46751STobias Grosser   ///
110869b46751STobias Grosser   /// We use ppcg's printing facilities to print the actual statement and
110969b46751STobias Grosser   /// additionally build up a list of all kernels that are encountered in the
111069b46751STobias Grosser   /// host ast.
111169b46751STobias Grosser   ///
111269b46751STobias Grosser   /// @param P The printer to print to
111369b46751STobias Grosser   /// @param Options The printing options to use
111469b46751STobias Grosser   /// @param Node The node to print
111569b46751STobias Grosser   /// @param User A user pointer to carry additional data. This pointer is
111669b46751STobias Grosser   ///             expected to be of type PrintGPUUserData.
111769b46751STobias Grosser   ///
111869b46751STobias Grosser   /// @returns A printer to which the output has been printed.
111969b46751STobias Grosser   static __isl_give isl_printer *
112069b46751STobias Grosser   printHostUser(__isl_take isl_printer *P,
112169b46751STobias Grosser                 __isl_take isl_ast_print_options *Options,
112269b46751STobias Grosser                 __isl_take isl_ast_node *Node, void *User) {
112369b46751STobias Grosser     auto Data = (struct PrintGPUUserData *)User;
112469b46751STobias Grosser     auto Id = isl_ast_node_get_annotation(Node);
112569b46751STobias Grosser 
112669b46751STobias Grosser     if (Id) {
112720251734STobias Grosser       bool IsUser = !strcmp(isl_id_get_name(Id), "user");
112820251734STobias Grosser 
112920251734STobias Grosser       // If this is a user statement, format it ourselves as ppcg would
113020251734STobias Grosser       // otherwise try to call pet functionality that is not available in
113120251734STobias Grosser       // Polly.
113220251734STobias Grosser       if (IsUser) {
113320251734STobias Grosser         P = isl_printer_start_line(P);
113420251734STobias Grosser         P = isl_printer_print_ast_node(P, Node);
113520251734STobias Grosser         P = isl_printer_end_line(P);
113620251734STobias Grosser         isl_id_free(Id);
113720251734STobias Grosser         isl_ast_print_options_free(Options);
113820251734STobias Grosser         return P;
113920251734STobias Grosser       }
114020251734STobias Grosser 
114169b46751STobias Grosser       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
114269b46751STobias Grosser       isl_id_free(Id);
114369b46751STobias Grosser       Data->Kernels.push_back(Kernel);
114469b46751STobias Grosser     }
114569b46751STobias Grosser 
114669b46751STobias Grosser     return print_host_user(P, Options, Node, User);
114769b46751STobias Grosser   }
114869b46751STobias Grosser 
114969b46751STobias Grosser   /// Print C code corresponding to the control flow in @p Kernel.
115069b46751STobias Grosser   ///
115169b46751STobias Grosser   /// @param Kernel The kernel to print
115269b46751STobias Grosser   void printKernel(ppcg_kernel *Kernel) {
115369b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
115469b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
115569b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
115669b46751STobias Grosser     P = isl_ast_node_print(Kernel->tree, P, Options);
115769b46751STobias Grosser     char *String = isl_printer_get_str(P);
115869b46751STobias Grosser     printf("%s\n", String);
115969b46751STobias Grosser     free(String);
116069b46751STobias Grosser     isl_printer_free(P);
116169b46751STobias Grosser   }
116269b46751STobias Grosser 
116369b46751STobias Grosser   /// Print C code corresponding to the GPU code described by @p Tree.
116469b46751STobias Grosser   ///
116569b46751STobias Grosser   /// @param Tree An AST describing GPU code
116669b46751STobias Grosser   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
116769b46751STobias Grosser   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
116869b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
116969b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
117069b46751STobias Grosser 
117169b46751STobias Grosser     PrintGPUUserData Data;
117269b46751STobias Grosser     Data.PPCGProg = PPCGProg;
117369b46751STobias Grosser 
117469b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
117569b46751STobias Grosser     Options =
117669b46751STobias Grosser         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
117769b46751STobias Grosser     P = isl_ast_node_print(Tree, P, Options);
117869b46751STobias Grosser     char *String = isl_printer_get_str(P);
117969b46751STobias Grosser     printf("# host\n");
118069b46751STobias Grosser     printf("%s\n", String);
118169b46751STobias Grosser     free(String);
118269b46751STobias Grosser     isl_printer_free(P);
118369b46751STobias Grosser 
118469b46751STobias Grosser     for (auto Kernel : Data.Kernels) {
118569b46751STobias Grosser       printf("# kernel%d\n", Kernel->id);
118669b46751STobias Grosser       printKernel(Kernel);
118769b46751STobias Grosser     }
118869b46751STobias Grosser   }
118969b46751STobias Grosser 
1190f384594dSTobias Grosser   // Generate a GPU program using PPCG.
1191f384594dSTobias Grosser   //
1192f384594dSTobias Grosser   // GPU mapping consists of multiple steps:
1193f384594dSTobias Grosser   //
1194f384594dSTobias Grosser   //  1) Compute new schedule for the program.
1195f384594dSTobias Grosser   //  2) Map schedule to GPU (TODO)
1196f384594dSTobias Grosser   //  3) Generate code for new schedule (TODO)
1197f384594dSTobias Grosser   //
1198f384594dSTobias Grosser   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
1199f384594dSTobias Grosser   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
1200f384594dSTobias Grosser   // strategy directly from this pass.
1201f384594dSTobias Grosser   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
1202f384594dSTobias Grosser 
1203f384594dSTobias Grosser     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
1204f384594dSTobias Grosser 
1205f384594dSTobias Grosser     PPCGGen->ctx = S->getIslCtx();
1206f384594dSTobias Grosser     PPCGGen->options = PPCGScop->options;
1207f384594dSTobias Grosser     PPCGGen->print = nullptr;
1208f384594dSTobias Grosser     PPCGGen->print_user = nullptr;
120960c60025STobias Grosser     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
1210f384594dSTobias Grosser     PPCGGen->prog = PPCGProg;
1211f384594dSTobias Grosser     PPCGGen->tree = nullptr;
1212f384594dSTobias Grosser     PPCGGen->types.n = 0;
1213f384594dSTobias Grosser     PPCGGen->types.name = nullptr;
1214f384594dSTobias Grosser     PPCGGen->sizes = nullptr;
1215f384594dSTobias Grosser     PPCGGen->used_sizes = nullptr;
1216f384594dSTobias Grosser     PPCGGen->kernel_id = 0;
1217f384594dSTobias Grosser 
1218f384594dSTobias Grosser     // Set scheduling strategy to same strategy PPCG is using.
1219f384594dSTobias Grosser     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
1220f384594dSTobias Grosser     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
12212341fe9eSTobias Grosser     isl_options_set_schedule_whole_component(PPCGGen->ctx, false);
1222f384594dSTobias Grosser 
1223f384594dSTobias Grosser     isl_schedule *Schedule = get_schedule(PPCGGen);
1224f384594dSTobias Grosser 
1225aef5196fSTobias Grosser     int has_permutable = has_any_permutable_node(Schedule);
1226aef5196fSTobias Grosser 
122769b46751STobias Grosser     if (!has_permutable || has_permutable < 0) {
1228aef5196fSTobias Grosser       Schedule = isl_schedule_free(Schedule);
122969b46751STobias Grosser     } else {
1230aef5196fSTobias Grosser       Schedule = map_to_device(PPCGGen, Schedule);
123169b46751STobias Grosser       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
123269b46751STobias Grosser     }
1233aef5196fSTobias Grosser 
1234f384594dSTobias Grosser     if (DumpSchedule) {
1235f384594dSTobias Grosser       isl_printer *P = isl_printer_to_str(S->getIslCtx());
1236f384594dSTobias Grosser       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
1237f384594dSTobias Grosser       P = isl_printer_print_str(P, "Schedule\n");
1238f384594dSTobias Grosser       P = isl_printer_print_str(P, "========\n");
1239f384594dSTobias Grosser       if (Schedule)
1240f384594dSTobias Grosser         P = isl_printer_print_schedule(P, Schedule);
1241f384594dSTobias Grosser       else
1242f384594dSTobias Grosser         P = isl_printer_print_str(P, "No schedule found\n");
1243f384594dSTobias Grosser 
1244f384594dSTobias Grosser       printf("%s\n", isl_printer_get_str(P));
1245f384594dSTobias Grosser       isl_printer_free(P);
1246f384594dSTobias Grosser     }
1247f384594dSTobias Grosser 
124869b46751STobias Grosser     if (DumpCode) {
124969b46751STobias Grosser       printf("Code\n");
125069b46751STobias Grosser       printf("====\n");
125169b46751STobias Grosser       if (PPCGGen->tree)
125269b46751STobias Grosser         printGPUTree(PPCGGen->tree, PPCGProg);
125369b46751STobias Grosser       else
125469b46751STobias Grosser         printf("No code generated\n");
125569b46751STobias Grosser     }
125669b46751STobias Grosser 
1257f384594dSTobias Grosser     isl_schedule_free(Schedule);
1258f384594dSTobias Grosser 
1259f384594dSTobias Grosser     return PPCGGen;
1260f384594dSTobias Grosser   }
1261f384594dSTobias Grosser 
1262f384594dSTobias Grosser   /// Free gpu_gen structure.
1263f384594dSTobias Grosser   ///
1264f384594dSTobias Grosser   /// @param PPCGGen The ppcg_gen object to free.
1265f384594dSTobias Grosser   void freePPCGGen(gpu_gen *PPCGGen) {
1266f384594dSTobias Grosser     isl_ast_node_free(PPCGGen->tree);
1267f384594dSTobias Grosser     isl_union_map_free(PPCGGen->sizes);
1268f384594dSTobias Grosser     isl_union_map_free(PPCGGen->used_sizes);
1269f384594dSTobias Grosser     free(PPCGGen);
1270f384594dSTobias Grosser   }
1271f384594dSTobias Grosser 
1272b307ed4dSTobias Grosser   /// Free the options in the ppcg scop structure.
1273b307ed4dSTobias Grosser   ///
1274b307ed4dSTobias Grosser   /// ppcg is not freeing these options for us. To avoid leaks we do this
1275b307ed4dSTobias Grosser   /// ourselves.
1276b307ed4dSTobias Grosser   ///
1277b307ed4dSTobias Grosser   /// @param PPCGScop The scop referencing the options to free.
1278b307ed4dSTobias Grosser   void freeOptions(ppcg_scop *PPCGScop) {
1279b307ed4dSTobias Grosser     free(PPCGScop->options->debug);
1280b307ed4dSTobias Grosser     PPCGScop->options->debug = nullptr;
1281b307ed4dSTobias Grosser     free(PPCGScop->options);
1282b307ed4dSTobias Grosser     PPCGScop->options = nullptr;
1283b307ed4dSTobias Grosser   }
1284b307ed4dSTobias Grosser 
128538fc0aedSTobias Grosser   /// Generate code for a given GPU AST described by @p Root.
128638fc0aedSTobias Grosser   ///
128732837fe3STobias Grosser   /// @param Root An isl_ast_node pointing to the root of the GPU AST.
128832837fe3STobias Grosser   /// @param Prog The GPU Program to generate code for.
128932837fe3STobias Grosser   void generateCode(__isl_take isl_ast_node *Root, gpu_prog *Prog) {
129038fc0aedSTobias Grosser     ScopAnnotator Annotator;
129138fc0aedSTobias Grosser     Annotator.buildAliasScopes(*S);
129238fc0aedSTobias Grosser 
129338fc0aedSTobias Grosser     Region *R = &S->getRegion();
129438fc0aedSTobias Grosser 
129538fc0aedSTobias Grosser     simplifyRegion(R, DT, LI, RI);
129638fc0aedSTobias Grosser 
129738fc0aedSTobias Grosser     BasicBlock *EnteringBB = R->getEnteringBlock();
129838fc0aedSTobias Grosser 
129938fc0aedSTobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
130038fc0aedSTobias Grosser 
130132837fe3STobias Grosser     GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT, *S,
130232837fe3STobias Grosser                                Prog);
130338fc0aedSTobias Grosser 
130438fc0aedSTobias Grosser     // Only build the run-time condition and parameters _after_ having
130538fc0aedSTobias Grosser     // introduced the conditional branch. This is important as the conditional
130638fc0aedSTobias Grosser     // branch will guard the original scop from new induction variables that
130738fc0aedSTobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
130838fc0aedSTobias Grosser     // which may introduce scalar dependences that prevent us from correctly
130938fc0aedSTobias Grosser     // code generating this scop.
131038fc0aedSTobias Grosser     BasicBlock *StartBlock =
131138fc0aedSTobias Grosser         executeScopConditionally(*S, this, Builder.getTrue());
131238fc0aedSTobias Grosser 
131338fc0aedSTobias Grosser     // TODO: Handle LICM
131438fc0aedSTobias Grosser     // TODO: Verify run-time checks
131538fc0aedSTobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
131638fc0aedSTobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
131738fc0aedSTobias Grosser     NodeBuilder.addParameters(S->getContext());
131838fc0aedSTobias Grosser     Builder.SetInsertPoint(&*StartBlock->begin());
131938fc0aedSTobias Grosser     NodeBuilder.create(Root);
132038fc0aedSTobias Grosser     NodeBuilder.finalizeSCoP(*S);
132138fc0aedSTobias Grosser   }
132238fc0aedSTobias Grosser 
1323e938517eSTobias Grosser   bool runOnScop(Scop &CurrentScop) override {
1324e938517eSTobias Grosser     S = &CurrentScop;
132538fc0aedSTobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
132638fc0aedSTobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
132738fc0aedSTobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
132838fc0aedSTobias Grosser     DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
132938fc0aedSTobias Grosser     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
1330e938517eSTobias Grosser 
13312d58a64eSTobias Grosser     // We currently do not support scops with invariant loads.
13322d58a64eSTobias Grosser     if (S->hasInvariantAccesses())
13332d58a64eSTobias Grosser       return false;
13342d58a64eSTobias Grosser 
1335e938517eSTobias Grosser     auto PPCGScop = createPPCGScop();
1336e938517eSTobias Grosser     auto PPCGProg = createPPCGProg(PPCGScop);
1337f384594dSTobias Grosser     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
133838fc0aedSTobias Grosser 
133938fc0aedSTobias Grosser     if (PPCGGen->tree)
134032837fe3STobias Grosser       generateCode(isl_ast_node_copy(PPCGGen->tree), PPCGProg);
134138fc0aedSTobias Grosser 
1342b307ed4dSTobias Grosser     freeOptions(PPCGScop);
1343f384594dSTobias Grosser     freePPCGGen(PPCGGen);
1344e938517eSTobias Grosser     gpu_prog_free(PPCGProg);
1345e938517eSTobias Grosser     ppcg_scop_free(PPCGScop);
1346e938517eSTobias Grosser 
1347e938517eSTobias Grosser     return true;
1348e938517eSTobias Grosser   }
13499dfe4e7cSTobias Grosser 
13509dfe4e7cSTobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
13519dfe4e7cSTobias Grosser 
13529dfe4e7cSTobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
13539dfe4e7cSTobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
13549dfe4e7cSTobias Grosser     AU.addRequired<RegionInfoPass>();
13559dfe4e7cSTobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
13569dfe4e7cSTobias Grosser     AU.addRequired<ScopDetection>();
13579dfe4e7cSTobias Grosser     AU.addRequired<ScopInfoRegionPass>();
13589dfe4e7cSTobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
13599dfe4e7cSTobias Grosser 
13609dfe4e7cSTobias Grosser     AU.addPreserved<AAResultsWrapperPass>();
13619dfe4e7cSTobias Grosser     AU.addPreserved<BasicAAWrapperPass>();
13629dfe4e7cSTobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
13639dfe4e7cSTobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
13649dfe4e7cSTobias Grosser     AU.addPreserved<GlobalsAAWrapperPass>();
13659dfe4e7cSTobias Grosser     AU.addPreserved<PostDominatorTreeWrapperPass>();
13669dfe4e7cSTobias Grosser     AU.addPreserved<ScopDetection>();
13679dfe4e7cSTobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
13689dfe4e7cSTobias Grosser     AU.addPreserved<SCEVAAWrapperPass>();
13699dfe4e7cSTobias Grosser 
13709dfe4e7cSTobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
13719dfe4e7cSTobias Grosser     //        region tree.
13729dfe4e7cSTobias Grosser     AU.addPreserved<RegionInfoPass>();
13739dfe4e7cSTobias Grosser     AU.addPreserved<ScopInfoRegionPass>();
13749dfe4e7cSTobias Grosser   }
13759dfe4e7cSTobias Grosser };
13769dfe4e7cSTobias Grosser }
13779dfe4e7cSTobias Grosser 
13789dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1;
13799dfe4e7cSTobias Grosser 
13809dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
13819dfe4e7cSTobias Grosser 
13829dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
13839dfe4e7cSTobias Grosser                       "Polly - Apply PPCG translation to SCOP", false, false)
13849dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
13859dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
13869dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
13879dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
13889dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
13899dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
13909dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
13919dfe4e7cSTobias Grosser                     "Polly - Apply PPCG translation to SCOP", false, false)
1392