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"
16*38fc0aedSTobias 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 
5460c60025STobias Grosser /// Create the ast expressions for a ScopStmt.
5560c60025STobias Grosser ///
5660c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each
5760c60025STobias Grosser /// of the scheduled ScopStmts.
5860c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
5960c60025STobias Grosser     void *Stmt, isl_ast_build *Build,
6060c60025STobias Grosser     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
6160c60025STobias Grosser                                        isl_id *Id, void *User),
6260c60025STobias Grosser     void *UserIndex,
6360c60025STobias Grosser     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
6460c60025STobias Grosser     void *User_expr) {
6560c60025STobias Grosser 
6660c60025STobias Grosser   // TODO: Implement the AST expression generation. For now we just return a
6760c60025STobias Grosser   // nullptr to ensure that we do not free uninitialized pointers.
6860c60025STobias Grosser 
6960c60025STobias Grosser   return nullptr;
7060c60025STobias Grosser }
71f384594dSTobias Grosser 
72*38fc0aedSTobias Grosser /// Generate code for a GPU specific isl AST.
73*38fc0aedSTobias Grosser ///
74*38fc0aedSTobias Grosser /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which
75*38fc0aedSTobias Grosser /// generates code for general-prupose AST nodes, with special functionality
76*38fc0aedSTobias Grosser /// for generating GPU specific user nodes.
77*38fc0aedSTobias Grosser ///
78*38fc0aedSTobias Grosser /// @see GPUNodeBuilder::createUser
79*38fc0aedSTobias Grosser class GPUNodeBuilder : public IslNodeBuilder {
80*38fc0aedSTobias Grosser public:
81*38fc0aedSTobias Grosser   GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
82*38fc0aedSTobias Grosser                  const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
83*38fc0aedSTobias Grosser                  DominatorTree &DT, Scop &S)
84*38fc0aedSTobias Grosser       : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S) {}
85*38fc0aedSTobias Grosser 
86*38fc0aedSTobias Grosser private:
87*38fc0aedSTobias Grosser   /// Create code for user-defined AST nodes.
88*38fc0aedSTobias Grosser   ///
89*38fc0aedSTobias Grosser   /// These AST nodes can be of type:
90*38fc0aedSTobias Grosser   ///
91*38fc0aedSTobias Grosser   ///   - ScopStmt:      A computational statement (TODO)
92*38fc0aedSTobias Grosser   ///   - Kernel:        A GPU kernel call (TODO)
93*38fc0aedSTobias Grosser   ///   - Data-Transfer: A GPU <-> CPU data-transfer (TODO)
94*38fc0aedSTobias Grosser   ///
95*38fc0aedSTobias Grosser   virtual void createUser(__isl_take isl_ast_node *User) {
96*38fc0aedSTobias Grosser     isl_ast_node_free(User);
97*38fc0aedSTobias Grosser     return;
98*38fc0aedSTobias Grosser   }
99*38fc0aedSTobias Grosser };
100*38fc0aedSTobias Grosser 
1019dfe4e7cSTobias Grosser namespace {
1029dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass {
1039dfe4e7cSTobias Grosser public:
1049dfe4e7cSTobias Grosser   static char ID;
1059dfe4e7cSTobias Grosser 
106e938517eSTobias Grosser   /// The scop that is currently processed.
107e938517eSTobias Grosser   Scop *S;
108e938517eSTobias Grosser 
109*38fc0aedSTobias Grosser   LoopInfo *LI;
110*38fc0aedSTobias Grosser   DominatorTree *DT;
111*38fc0aedSTobias Grosser   ScalarEvolution *SE;
112*38fc0aedSTobias Grosser   const DataLayout *DL;
113*38fc0aedSTobias Grosser   RegionInfo *RI;
114*38fc0aedSTobias Grosser 
1159dfe4e7cSTobias Grosser   PPCGCodeGeneration() : ScopPass(ID) {}
1169dfe4e7cSTobias Grosser 
117e938517eSTobias Grosser   /// Construct compilation options for PPCG.
118e938517eSTobias Grosser   ///
119e938517eSTobias Grosser   /// @returns The compilation options.
120e938517eSTobias Grosser   ppcg_options *createPPCGOptions() {
121e938517eSTobias Grosser     auto DebugOptions =
122e938517eSTobias Grosser         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
123e938517eSTobias Grosser     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
124e938517eSTobias Grosser 
125e938517eSTobias Grosser     DebugOptions->dump_schedule_constraints = false;
126e938517eSTobias Grosser     DebugOptions->dump_schedule = false;
127e938517eSTobias Grosser     DebugOptions->dump_final_schedule = false;
128e938517eSTobias Grosser     DebugOptions->dump_sizes = false;
129e938517eSTobias Grosser 
130e938517eSTobias Grosser     Options->debug = DebugOptions;
131e938517eSTobias Grosser 
132e938517eSTobias Grosser     Options->reschedule = true;
133e938517eSTobias Grosser     Options->scale_tile_loops = false;
134e938517eSTobias Grosser     Options->wrap = false;
135e938517eSTobias Grosser 
136e938517eSTobias Grosser     Options->non_negative_parameters = false;
137e938517eSTobias Grosser     Options->ctx = nullptr;
138e938517eSTobias Grosser     Options->sizes = nullptr;
139e938517eSTobias Grosser 
1404eaedde5STobias Grosser     Options->tile_size = 32;
1414eaedde5STobias Grosser 
142e938517eSTobias Grosser     Options->use_private_memory = false;
143e938517eSTobias Grosser     Options->use_shared_memory = false;
144e938517eSTobias Grosser     Options->max_shared_memory = 0;
145e938517eSTobias Grosser 
146e938517eSTobias Grosser     Options->target = PPCG_TARGET_CUDA;
147e938517eSTobias Grosser     Options->openmp = false;
148e938517eSTobias Grosser     Options->linearize_device_arrays = true;
149e938517eSTobias Grosser     Options->live_range_reordering = false;
150e938517eSTobias Grosser 
151e938517eSTobias Grosser     Options->opencl_compiler_options = nullptr;
152e938517eSTobias Grosser     Options->opencl_use_gpu = false;
153e938517eSTobias Grosser     Options->opencl_n_include_file = 0;
154e938517eSTobias Grosser     Options->opencl_include_files = nullptr;
155e938517eSTobias Grosser     Options->opencl_print_kernel_types = false;
156e938517eSTobias Grosser     Options->opencl_embed_kernel_code = false;
157e938517eSTobias Grosser 
158e938517eSTobias Grosser     Options->save_schedule_file = nullptr;
159e938517eSTobias Grosser     Options->load_schedule_file = nullptr;
160e938517eSTobias Grosser 
161e938517eSTobias Grosser     return Options;
162e938517eSTobias Grosser   }
163e938517eSTobias Grosser 
164f384594dSTobias Grosser   /// Get a tagged access relation containing all accesses of type @p AccessTy.
165f384594dSTobias Grosser   ///
166f384594dSTobias Grosser   /// Instead of a normal access of the form:
167f384594dSTobias Grosser   ///
168f384594dSTobias Grosser   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
169f384594dSTobias Grosser   ///
170f384594dSTobias Grosser   /// a tagged access has the form
171f384594dSTobias Grosser   ///
172f384594dSTobias Grosser   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
173f384594dSTobias Grosser   ///
174f384594dSTobias Grosser   /// where 'id' is an additional space that references the memory access that
175f384594dSTobias Grosser   /// triggered the access.
176f384594dSTobias Grosser   ///
177f384594dSTobias Grosser   /// @param AccessTy The type of the memory accesses to collect.
178f384594dSTobias Grosser   ///
179f384594dSTobias Grosser   /// @return The relation describing all tagged memory accesses.
180f384594dSTobias Grosser   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
181f384594dSTobias Grosser     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
182f384594dSTobias Grosser 
183f384594dSTobias Grosser     for (auto &Stmt : *S)
184f384594dSTobias Grosser       for (auto &Acc : Stmt)
185f384594dSTobias Grosser         if (Acc->getType() == AccessTy) {
186f384594dSTobias Grosser           isl_map *Relation = Acc->getAccessRelation();
187f384594dSTobias Grosser           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
188f384594dSTobias Grosser 
189f384594dSTobias Grosser           isl_space *Space = isl_map_get_space(Relation);
190f384594dSTobias Grosser           Space = isl_space_range(Space);
191f384594dSTobias Grosser           Space = isl_space_from_range(Space);
1926293ba69STobias Grosser           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
193f384594dSTobias Grosser           isl_map *Universe = isl_map_universe(Space);
194f384594dSTobias Grosser           Relation = isl_map_domain_product(Relation, Universe);
195f384594dSTobias Grosser           Accesses = isl_union_map_add_map(Accesses, Relation);
196f384594dSTobias Grosser         }
197f384594dSTobias Grosser 
198f384594dSTobias Grosser     return Accesses;
199f384594dSTobias Grosser   }
200f384594dSTobias Grosser 
201f384594dSTobias Grosser   /// Get the set of all read accesses, tagged with the access id.
202f384594dSTobias Grosser   ///
203f384594dSTobias Grosser   /// @see getTaggedAccesses
204f384594dSTobias Grosser   isl_union_map *getTaggedReads() {
205f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::READ);
206f384594dSTobias Grosser   }
207f384594dSTobias Grosser 
208f384594dSTobias Grosser   /// Get the set of all may (and must) accesses, tagged with the access id.
209f384594dSTobias Grosser   ///
210f384594dSTobias Grosser   /// @see getTaggedAccesses
211f384594dSTobias Grosser   isl_union_map *getTaggedMayWrites() {
212f384594dSTobias Grosser     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
213f384594dSTobias Grosser                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
214f384594dSTobias Grosser   }
215f384594dSTobias Grosser 
216f384594dSTobias Grosser   /// Get the set of all must accesses, tagged with the access id.
217f384594dSTobias Grosser   ///
218f384594dSTobias Grosser   /// @see getTaggedAccesses
219f384594dSTobias Grosser   isl_union_map *getTaggedMustWrites() {
220f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
221f384594dSTobias Grosser   }
222f384594dSTobias Grosser 
223aef5196fSTobias Grosser   /// Collect parameter and array names as isl_ids.
224aef5196fSTobias Grosser   ///
225aef5196fSTobias Grosser   /// To reason about the different parameters and arrays used, ppcg requires
226aef5196fSTobias Grosser   /// a list of all isl_ids in use. As PPCG traditionally performs
227aef5196fSTobias Grosser   /// source-to-source compilation each of these isl_ids is mapped to the
228aef5196fSTobias Grosser   /// expression that represents it. As we do not have a corresponding
229aef5196fSTobias Grosser   /// expression in Polly, we just map each id to a 'zero' expression to match
230aef5196fSTobias Grosser   /// the data format that ppcg expects.
231aef5196fSTobias Grosser   ///
232aef5196fSTobias Grosser   /// @returns Retun a map from collected ids to 'zero' ast expressions.
233aef5196fSTobias Grosser   __isl_give isl_id_to_ast_expr *getNames() {
234aef5196fSTobias Grosser     auto *Names = isl_id_to_ast_expr_alloc(
235bd81a7eeSTobias Grosser         S->getIslCtx(),
236bd81a7eeSTobias Grosser         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
237aef5196fSTobias Grosser     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
238aef5196fSTobias Grosser     auto *Space = S->getParamSpace();
239aef5196fSTobias Grosser 
240aef5196fSTobias Grosser     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
241aef5196fSTobias Grosser       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
242aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
243aef5196fSTobias Grosser     }
244aef5196fSTobias Grosser 
245aef5196fSTobias Grosser     for (auto &Array : S->arrays()) {
246aef5196fSTobias Grosser       auto Id = Array.second->getBasePtrId();
247aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
248aef5196fSTobias Grosser     }
249aef5196fSTobias Grosser 
250aef5196fSTobias Grosser     isl_space_free(Space);
251aef5196fSTobias Grosser     isl_ast_expr_free(Zero);
252aef5196fSTobias Grosser 
253aef5196fSTobias Grosser     return Names;
254aef5196fSTobias Grosser   }
255aef5196fSTobias Grosser 
256e938517eSTobias Grosser   /// Create a new PPCG scop from the current scop.
257e938517eSTobias Grosser   ///
258f384594dSTobias Grosser   /// The PPCG scop is initialized with data from the current polly::Scop. From
259f384594dSTobias Grosser   /// this initial data, the data-dependences in the PPCG scop are initialized.
260f384594dSTobias Grosser   /// We do not use Polly's dependence analysis for now, to ensure we match
261f384594dSTobias Grosser   /// the PPCG default behaviour more closely.
262e938517eSTobias Grosser   ///
263e938517eSTobias Grosser   /// @returns A new ppcg scop.
264e938517eSTobias Grosser   ppcg_scop *createPPCGScop() {
265e938517eSTobias Grosser     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
266e938517eSTobias Grosser 
267e938517eSTobias Grosser     PPCGScop->options = createPPCGOptions();
268e938517eSTobias Grosser 
269e938517eSTobias Grosser     PPCGScop->start = 0;
270e938517eSTobias Grosser     PPCGScop->end = 0;
271e938517eSTobias Grosser 
272f384594dSTobias Grosser     PPCGScop->context = S->getContext();
273f384594dSTobias Grosser     PPCGScop->domain = S->getDomains();
274e938517eSTobias Grosser     PPCGScop->call = nullptr;
275f384594dSTobias Grosser     PPCGScop->tagged_reads = getTaggedReads();
276f384594dSTobias Grosser     PPCGScop->reads = S->getReads();
277e938517eSTobias Grosser     PPCGScop->live_in = nullptr;
278f384594dSTobias Grosser     PPCGScop->tagged_may_writes = getTaggedMayWrites();
279f384594dSTobias Grosser     PPCGScop->may_writes = S->getWrites();
280f384594dSTobias Grosser     PPCGScop->tagged_must_writes = getTaggedMustWrites();
281f384594dSTobias Grosser     PPCGScop->must_writes = S->getMustWrites();
282e938517eSTobias Grosser     PPCGScop->live_out = nullptr;
283f384594dSTobias Grosser     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
284e938517eSTobias Grosser     PPCGScop->tagger = nullptr;
285e938517eSTobias Grosser 
286e938517eSTobias Grosser     PPCGScop->independence = nullptr;
287e938517eSTobias Grosser     PPCGScop->dep_flow = nullptr;
288e938517eSTobias Grosser     PPCGScop->tagged_dep_flow = nullptr;
289e938517eSTobias Grosser     PPCGScop->dep_false = nullptr;
290e938517eSTobias Grosser     PPCGScop->dep_forced = nullptr;
291e938517eSTobias Grosser     PPCGScop->dep_order = nullptr;
292e938517eSTobias Grosser     PPCGScop->tagged_dep_order = nullptr;
293e938517eSTobias Grosser 
294f384594dSTobias Grosser     PPCGScop->schedule = S->getScheduleTree();
295aef5196fSTobias Grosser     PPCGScop->names = getNames();
296e938517eSTobias Grosser 
297e938517eSTobias Grosser     PPCGScop->pet = nullptr;
298e938517eSTobias Grosser 
299f384594dSTobias Grosser     compute_tagger(PPCGScop);
300f384594dSTobias Grosser     compute_dependences(PPCGScop);
301f384594dSTobias Grosser 
302e938517eSTobias Grosser     return PPCGScop;
303e938517eSTobias Grosser   }
304e938517eSTobias Grosser 
30560f63b49STobias Grosser   /// Collect the array acesses in a statement.
30660f63b49STobias Grosser   ///
30760f63b49STobias Grosser   /// @param Stmt The statement for which to collect the accesses.
30860f63b49STobias Grosser   ///
30960f63b49STobias Grosser   /// @returns A list of array accesses.
31060f63b49STobias Grosser   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
31160f63b49STobias Grosser     gpu_stmt_access *Accesses = nullptr;
31260f63b49STobias Grosser 
31360f63b49STobias Grosser     for (MemoryAccess *Acc : Stmt) {
31460f63b49STobias Grosser       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
31560f63b49STobias Grosser       Access->read = Acc->isRead();
31660f63b49STobias Grosser       Access->write = Acc->isWrite();
31760f63b49STobias Grosser       Access->access = Acc->getAccessRelation();
31860f63b49STobias Grosser       isl_space *Space = isl_map_get_space(Access->access);
31960f63b49STobias Grosser       Space = isl_space_range(Space);
32060f63b49STobias Grosser       Space = isl_space_from_range(Space);
3216293ba69STobias Grosser       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
32260f63b49STobias Grosser       isl_map *Universe = isl_map_universe(Space);
32360f63b49STobias Grosser       Access->tagged_access =
32460f63b49STobias Grosser           isl_map_domain_product(Acc->getAccessRelation(), Universe);
32560f63b49STobias Grosser       Access->exact_write = Acc->isWrite();
32660f63b49STobias Grosser       Access->ref_id = Acc->getId();
32760f63b49STobias Grosser       Access->next = Accesses;
32860f63b49STobias Grosser       Accesses = Access;
32960f63b49STobias Grosser     }
33060f63b49STobias Grosser 
33160f63b49STobias Grosser     return Accesses;
33260f63b49STobias Grosser   }
33360f63b49STobias Grosser 
33469b46751STobias Grosser   /// Collect the list of GPU statements.
33569b46751STobias Grosser   ///
33669b46751STobias Grosser   /// Each statement has an id, a pointer to the underlying data structure,
33769b46751STobias Grosser   /// as well as a list with all memory accesses.
33869b46751STobias Grosser   ///
33969b46751STobias Grosser   /// TODO: Initialize the list of memory accesses.
34069b46751STobias Grosser   ///
34169b46751STobias Grosser   /// @returns A linked-list of statements.
34269b46751STobias Grosser   gpu_stmt *getStatements() {
34369b46751STobias Grosser     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
34469b46751STobias Grosser                                        std::distance(S->begin(), S->end()));
34569b46751STobias Grosser 
34669b46751STobias Grosser     int i = 0;
34769b46751STobias Grosser     for (auto &Stmt : *S) {
34869b46751STobias Grosser       gpu_stmt *GPUStmt = &Stmts[i];
34969b46751STobias Grosser 
35069b46751STobias Grosser       GPUStmt->id = Stmt.getDomainId();
35169b46751STobias Grosser 
35269b46751STobias Grosser       // We use the pet stmt pointer to keep track of the Polly statements.
35369b46751STobias Grosser       GPUStmt->stmt = (pet_stmt *)&Stmt;
35460f63b49STobias Grosser       GPUStmt->accesses = getStmtAccesses(Stmt);
35569b46751STobias Grosser       i++;
35669b46751STobias Grosser     }
35769b46751STobias Grosser 
35869b46751STobias Grosser     return Stmts;
35969b46751STobias Grosser   }
36069b46751STobias Grosser 
36160f63b49STobias Grosser   /// Derive the extent of an array.
36260f63b49STobias Grosser   ///
36360f63b49STobias Grosser   /// The extent of an array is defined by the set of memory locations for
36460f63b49STobias Grosser   /// which a memory access in the iteration domain exists.
36560f63b49STobias Grosser   ///
36660f63b49STobias Grosser   /// @param Array The array to derive the extent for.
36760f63b49STobias Grosser   ///
36860f63b49STobias Grosser   /// @returns An isl_set describing the extent of the array.
36960f63b49STobias Grosser   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
37060f63b49STobias Grosser     isl_union_map *Accesses = S->getAccesses();
37160f63b49STobias Grosser     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
37260f63b49STobias Grosser     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
37360f63b49STobias Grosser     isl_set *AccessSet =
37460f63b49STobias Grosser         isl_union_set_extract_set(AccessUSet, Array->getSpace());
37560f63b49STobias Grosser     isl_union_set_free(AccessUSet);
37660f63b49STobias Grosser 
37760f63b49STobias Grosser     return AccessSet;
37860f63b49STobias Grosser   }
37960f63b49STobias Grosser 
38060f63b49STobias Grosser   /// Derive the bounds of an array.
38160f63b49STobias Grosser   ///
38260f63b49STobias Grosser   /// For the first dimension we derive the bound of the array from the extent
38360f63b49STobias Grosser   /// of this dimension. For inner dimensions we obtain their size directly from
38460f63b49STobias Grosser   /// ScopArrayInfo.
38560f63b49STobias Grosser   ///
38660f63b49STobias Grosser   /// @param PPCGArray The array to compute bounds for.
38760f63b49STobias Grosser   /// @param Array The polly array from which to take the information.
38860f63b49STobias Grosser   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
38960f63b49STobias Grosser     if (PPCGArray.n_index > 0) {
39060f63b49STobias Grosser       isl_set *Dom = isl_set_copy(PPCGArray.extent);
39160f63b49STobias Grosser       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
39260f63b49STobias Grosser       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
39360f63b49STobias Grosser       isl_set_free(Dom);
39460f63b49STobias Grosser       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
39560f63b49STobias Grosser       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
39660f63b49STobias Grosser       isl_aff *One = isl_aff_zero_on_domain(LS);
39760f63b49STobias Grosser       One = isl_aff_add_constant_si(One, 1);
39860f63b49STobias Grosser       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
39960f63b49STobias Grosser       Bound = isl_pw_aff_gist(Bound, S->getContext());
40060f63b49STobias Grosser       PPCGArray.bound[0] = Bound;
40160f63b49STobias Grosser     }
40260f63b49STobias Grosser 
40360f63b49STobias Grosser     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
40460f63b49STobias Grosser       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
40560f63b49STobias Grosser       auto LS = isl_pw_aff_get_domain_space(Bound);
40660f63b49STobias Grosser       auto Aff = isl_multi_aff_zero(LS);
40760f63b49STobias Grosser       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
40860f63b49STobias Grosser       PPCGArray.bound[i] = Bound;
40960f63b49STobias Grosser     }
41060f63b49STobias Grosser   }
41160f63b49STobias Grosser 
41260f63b49STobias Grosser   /// Create the arrays for @p PPCGProg.
41360f63b49STobias Grosser   ///
41460f63b49STobias Grosser   /// @param PPCGProg The program to compute the arrays for.
41560f63b49STobias Grosser   void createArrays(gpu_prog *PPCGProg) {
41660f63b49STobias Grosser     int i = 0;
41760f63b49STobias Grosser     for (auto &Element : S->arrays()) {
41860f63b49STobias Grosser       ScopArrayInfo *Array = Element.second.get();
41960f63b49STobias Grosser 
42060f63b49STobias Grosser       std::string TypeName;
42160f63b49STobias Grosser       raw_string_ostream OS(TypeName);
42260f63b49STobias Grosser 
42360f63b49STobias Grosser       OS << *Array->getElementType();
42460f63b49STobias Grosser       TypeName = OS.str();
42560f63b49STobias Grosser 
42660f63b49STobias Grosser       gpu_array_info &PPCGArray = PPCGProg->array[i];
42760f63b49STobias Grosser 
42860f63b49STobias Grosser       PPCGArray.space = Array->getSpace();
42960f63b49STobias Grosser       PPCGArray.type = strdup(TypeName.c_str());
43060f63b49STobias Grosser       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
43160f63b49STobias Grosser       PPCGArray.name = strdup(Array->getName().c_str());
43260f63b49STobias Grosser       PPCGArray.extent = nullptr;
43360f63b49STobias Grosser       PPCGArray.n_index = Array->getNumberOfDimensions();
43460f63b49STobias Grosser       PPCGArray.bound =
43560f63b49STobias Grosser           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
43660f63b49STobias Grosser       PPCGArray.extent = getExtent(Array);
43760f63b49STobias Grosser       PPCGArray.n_ref = 0;
43860f63b49STobias Grosser       PPCGArray.refs = nullptr;
43960f63b49STobias Grosser       PPCGArray.accessed = true;
44060f63b49STobias Grosser       PPCGArray.read_only_scalar = false;
44160f63b49STobias Grosser       PPCGArray.has_compound_element = false;
44260f63b49STobias Grosser       PPCGArray.local = false;
44360f63b49STobias Grosser       PPCGArray.declare_local = false;
44460f63b49STobias Grosser       PPCGArray.global = false;
44560f63b49STobias Grosser       PPCGArray.linearize = false;
44660f63b49STobias Grosser       PPCGArray.dep_order = nullptr;
44760f63b49STobias Grosser 
44860f63b49STobias Grosser       setArrayBounds(PPCGArray, Array);
4492d010dafSTobias Grosser       i++;
45060f63b49STobias Grosser     }
45160f63b49STobias Grosser   }
45260f63b49STobias Grosser 
45360f63b49STobias Grosser   /// Create an identity map between the arrays in the scop.
45460f63b49STobias Grosser   ///
45560f63b49STobias Grosser   /// @returns An identity map between the arrays in the scop.
45660f63b49STobias Grosser   isl_union_map *getArrayIdentity() {
45760f63b49STobias Grosser     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
45860f63b49STobias Grosser 
45960f63b49STobias Grosser     for (auto &Item : S->arrays()) {
46060f63b49STobias Grosser       ScopArrayInfo *Array = Item.second.get();
46160f63b49STobias Grosser       isl_space *Space = Array->getSpace();
46260f63b49STobias Grosser       Space = isl_space_map_from_set(Space);
46360f63b49STobias Grosser       isl_map *Identity = isl_map_identity(Space);
46460f63b49STobias Grosser       Maps = isl_union_map_add_map(Maps, Identity);
46560f63b49STobias Grosser     }
46660f63b49STobias Grosser 
46760f63b49STobias Grosser     return Maps;
46860f63b49STobias Grosser   }
46960f63b49STobias Grosser 
470e938517eSTobias Grosser   /// Create a default-initialized PPCG GPU program.
471e938517eSTobias Grosser   ///
472e938517eSTobias Grosser   /// @returns A new gpu grogram description.
473e938517eSTobias Grosser   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
474e938517eSTobias Grosser 
475e938517eSTobias Grosser     if (!PPCGScop)
476e938517eSTobias Grosser       return nullptr;
477e938517eSTobias Grosser 
478e938517eSTobias Grosser     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
479e938517eSTobias Grosser 
480e938517eSTobias Grosser     PPCGProg->ctx = S->getIslCtx();
481e938517eSTobias Grosser     PPCGProg->scop = PPCGScop;
482aef5196fSTobias Grosser     PPCGProg->context = isl_set_copy(PPCGScop->context);
48360f63b49STobias Grosser     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
48460f63b49STobias Grosser     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
48560f63b49STobias Grosser     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
48660f63b49STobias Grosser     PPCGProg->tagged_must_kill =
48760f63b49STobias Grosser         isl_union_map_copy(PPCGScop->tagged_must_kills);
48860f63b49STobias Grosser     PPCGProg->to_inner = getArrayIdentity();
48960f63b49STobias Grosser     PPCGProg->to_outer = getArrayIdentity();
49060f63b49STobias Grosser     PPCGProg->may_persist = compute_may_persist(PPCGProg);
491e938517eSTobias Grosser     PPCGProg->any_to_outer = nullptr;
492e938517eSTobias Grosser     PPCGProg->array_order = nullptr;
49369b46751STobias Grosser     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
49469b46751STobias Grosser     PPCGProg->stmts = getStatements();
49560f63b49STobias Grosser     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
49660f63b49STobias Grosser     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
49760f63b49STobias Grosser                                        PPCGProg->n_array);
49860f63b49STobias Grosser 
49960f63b49STobias Grosser     createArrays(PPCGProg);
500e938517eSTobias Grosser 
501e938517eSTobias Grosser     return PPCGProg;
502e938517eSTobias Grosser   }
503e938517eSTobias Grosser 
50469b46751STobias Grosser   struct PrintGPUUserData {
50569b46751STobias Grosser     struct cuda_info *CudaInfo;
50669b46751STobias Grosser     struct gpu_prog *PPCGProg;
50769b46751STobias Grosser     std::vector<ppcg_kernel *> Kernels;
50869b46751STobias Grosser   };
50969b46751STobias Grosser 
51069b46751STobias Grosser   /// Print a user statement node in the host code.
51169b46751STobias Grosser   ///
51269b46751STobias Grosser   /// We use ppcg's printing facilities to print the actual statement and
51369b46751STobias Grosser   /// additionally build up a list of all kernels that are encountered in the
51469b46751STobias Grosser   /// host ast.
51569b46751STobias Grosser   ///
51669b46751STobias Grosser   /// @param P The printer to print to
51769b46751STobias Grosser   /// @param Options The printing options to use
51869b46751STobias Grosser   /// @param Node The node to print
51969b46751STobias Grosser   /// @param User A user pointer to carry additional data. This pointer is
52069b46751STobias Grosser   ///             expected to be of type PrintGPUUserData.
52169b46751STobias Grosser   ///
52269b46751STobias Grosser   /// @returns A printer to which the output has been printed.
52369b46751STobias Grosser   static __isl_give isl_printer *
52469b46751STobias Grosser   printHostUser(__isl_take isl_printer *P,
52569b46751STobias Grosser                 __isl_take isl_ast_print_options *Options,
52669b46751STobias Grosser                 __isl_take isl_ast_node *Node, void *User) {
52769b46751STobias Grosser     auto Data = (struct PrintGPUUserData *)User;
52869b46751STobias Grosser     auto Id = isl_ast_node_get_annotation(Node);
52969b46751STobias Grosser 
53069b46751STobias Grosser     if (Id) {
53120251734STobias Grosser       bool IsUser = !strcmp(isl_id_get_name(Id), "user");
53220251734STobias Grosser 
53320251734STobias Grosser       // If this is a user statement, format it ourselves as ppcg would
53420251734STobias Grosser       // otherwise try to call pet functionality that is not available in
53520251734STobias Grosser       // Polly.
53620251734STobias Grosser       if (IsUser) {
53720251734STobias Grosser         P = isl_printer_start_line(P);
53820251734STobias Grosser         P = isl_printer_print_ast_node(P, Node);
53920251734STobias Grosser         P = isl_printer_end_line(P);
54020251734STobias Grosser         isl_id_free(Id);
54120251734STobias Grosser         isl_ast_print_options_free(Options);
54220251734STobias Grosser         return P;
54320251734STobias Grosser       }
54420251734STobias Grosser 
54569b46751STobias Grosser       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
54669b46751STobias Grosser       isl_id_free(Id);
54769b46751STobias Grosser       Data->Kernels.push_back(Kernel);
54869b46751STobias Grosser     }
54969b46751STobias Grosser 
55069b46751STobias Grosser     return print_host_user(P, Options, Node, User);
55169b46751STobias Grosser   }
55269b46751STobias Grosser 
55369b46751STobias Grosser   /// Print C code corresponding to the control flow in @p Kernel.
55469b46751STobias Grosser   ///
55569b46751STobias Grosser   /// @param Kernel The kernel to print
55669b46751STobias Grosser   void printKernel(ppcg_kernel *Kernel) {
55769b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
55869b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
55969b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
56069b46751STobias Grosser     P = isl_ast_node_print(Kernel->tree, P, Options);
56169b46751STobias Grosser     char *String = isl_printer_get_str(P);
56269b46751STobias Grosser     printf("%s\n", String);
56369b46751STobias Grosser     free(String);
56469b46751STobias Grosser     isl_printer_free(P);
56569b46751STobias Grosser   }
56669b46751STobias Grosser 
56769b46751STobias Grosser   /// Print C code corresponding to the GPU code described by @p Tree.
56869b46751STobias Grosser   ///
56969b46751STobias Grosser   /// @param Tree An AST describing GPU code
57069b46751STobias Grosser   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
57169b46751STobias Grosser   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
57269b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
57369b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
57469b46751STobias Grosser 
57569b46751STobias Grosser     PrintGPUUserData Data;
57669b46751STobias Grosser     Data.PPCGProg = PPCGProg;
57769b46751STobias Grosser 
57869b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
57969b46751STobias Grosser     Options =
58069b46751STobias Grosser         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
58169b46751STobias Grosser     P = isl_ast_node_print(Tree, P, Options);
58269b46751STobias Grosser     char *String = isl_printer_get_str(P);
58369b46751STobias Grosser     printf("# host\n");
58469b46751STobias Grosser     printf("%s\n", String);
58569b46751STobias Grosser     free(String);
58669b46751STobias Grosser     isl_printer_free(P);
58769b46751STobias Grosser 
58869b46751STobias Grosser     for (auto Kernel : Data.Kernels) {
58969b46751STobias Grosser       printf("# kernel%d\n", Kernel->id);
59069b46751STobias Grosser       printKernel(Kernel);
59169b46751STobias Grosser     }
59269b46751STobias Grosser   }
59369b46751STobias Grosser 
594f384594dSTobias Grosser   // Generate a GPU program using PPCG.
595f384594dSTobias Grosser   //
596f384594dSTobias Grosser   // GPU mapping consists of multiple steps:
597f384594dSTobias Grosser   //
598f384594dSTobias Grosser   //  1) Compute new schedule for the program.
599f384594dSTobias Grosser   //  2) Map schedule to GPU (TODO)
600f384594dSTobias Grosser   //  3) Generate code for new schedule (TODO)
601f384594dSTobias Grosser   //
602f384594dSTobias Grosser   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
603f384594dSTobias Grosser   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
604f384594dSTobias Grosser   // strategy directly from this pass.
605f384594dSTobias Grosser   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
606f384594dSTobias Grosser 
607f384594dSTobias Grosser     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
608f384594dSTobias Grosser 
609f384594dSTobias Grosser     PPCGGen->ctx = S->getIslCtx();
610f384594dSTobias Grosser     PPCGGen->options = PPCGScop->options;
611f384594dSTobias Grosser     PPCGGen->print = nullptr;
612f384594dSTobias Grosser     PPCGGen->print_user = nullptr;
61360c60025STobias Grosser     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
614f384594dSTobias Grosser     PPCGGen->prog = PPCGProg;
615f384594dSTobias Grosser     PPCGGen->tree = nullptr;
616f384594dSTobias Grosser     PPCGGen->types.n = 0;
617f384594dSTobias Grosser     PPCGGen->types.name = nullptr;
618f384594dSTobias Grosser     PPCGGen->sizes = nullptr;
619f384594dSTobias Grosser     PPCGGen->used_sizes = nullptr;
620f384594dSTobias Grosser     PPCGGen->kernel_id = 0;
621f384594dSTobias Grosser 
622f384594dSTobias Grosser     // Set scheduling strategy to same strategy PPCG is using.
623f384594dSTobias Grosser     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
624f384594dSTobias Grosser     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
6252341fe9eSTobias Grosser     isl_options_set_schedule_whole_component(PPCGGen->ctx, false);
626f384594dSTobias Grosser 
627f384594dSTobias Grosser     isl_schedule *Schedule = get_schedule(PPCGGen);
628f384594dSTobias Grosser 
629aef5196fSTobias Grosser     int has_permutable = has_any_permutable_node(Schedule);
630aef5196fSTobias Grosser 
63169b46751STobias Grosser     if (!has_permutable || has_permutable < 0) {
632aef5196fSTobias Grosser       Schedule = isl_schedule_free(Schedule);
63369b46751STobias Grosser     } else {
634aef5196fSTobias Grosser       Schedule = map_to_device(PPCGGen, Schedule);
63569b46751STobias Grosser       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
63669b46751STobias Grosser     }
637aef5196fSTobias Grosser 
638f384594dSTobias Grosser     if (DumpSchedule) {
639f384594dSTobias Grosser       isl_printer *P = isl_printer_to_str(S->getIslCtx());
640f384594dSTobias Grosser       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
641f384594dSTobias Grosser       P = isl_printer_print_str(P, "Schedule\n");
642f384594dSTobias Grosser       P = isl_printer_print_str(P, "========\n");
643f384594dSTobias Grosser       if (Schedule)
644f384594dSTobias Grosser         P = isl_printer_print_schedule(P, Schedule);
645f384594dSTobias Grosser       else
646f384594dSTobias Grosser         P = isl_printer_print_str(P, "No schedule found\n");
647f384594dSTobias Grosser 
648f384594dSTobias Grosser       printf("%s\n", isl_printer_get_str(P));
649f384594dSTobias Grosser       isl_printer_free(P);
650f384594dSTobias Grosser     }
651f384594dSTobias Grosser 
65269b46751STobias Grosser     if (DumpCode) {
65369b46751STobias Grosser       printf("Code\n");
65469b46751STobias Grosser       printf("====\n");
65569b46751STobias Grosser       if (PPCGGen->tree)
65669b46751STobias Grosser         printGPUTree(PPCGGen->tree, PPCGProg);
65769b46751STobias Grosser       else
65869b46751STobias Grosser         printf("No code generated\n");
65969b46751STobias Grosser     }
66069b46751STobias Grosser 
661f384594dSTobias Grosser     isl_schedule_free(Schedule);
662f384594dSTobias Grosser 
663f384594dSTobias Grosser     return PPCGGen;
664f384594dSTobias Grosser   }
665f384594dSTobias Grosser 
666f384594dSTobias Grosser   /// Free gpu_gen structure.
667f384594dSTobias Grosser   ///
668f384594dSTobias Grosser   /// @param PPCGGen The ppcg_gen object to free.
669f384594dSTobias Grosser   void freePPCGGen(gpu_gen *PPCGGen) {
670f384594dSTobias Grosser     isl_ast_node_free(PPCGGen->tree);
671f384594dSTobias Grosser     isl_union_map_free(PPCGGen->sizes);
672f384594dSTobias Grosser     isl_union_map_free(PPCGGen->used_sizes);
673f384594dSTobias Grosser     free(PPCGGen);
674f384594dSTobias Grosser   }
675f384594dSTobias Grosser 
676b307ed4dSTobias Grosser   /// Free the options in the ppcg scop structure.
677b307ed4dSTobias Grosser   ///
678b307ed4dSTobias Grosser   /// ppcg is not freeing these options for us. To avoid leaks we do this
679b307ed4dSTobias Grosser   /// ourselves.
680b307ed4dSTobias Grosser   ///
681b307ed4dSTobias Grosser   /// @param PPCGScop The scop referencing the options to free.
682b307ed4dSTobias Grosser   void freeOptions(ppcg_scop *PPCGScop) {
683b307ed4dSTobias Grosser     free(PPCGScop->options->debug);
684b307ed4dSTobias Grosser     PPCGScop->options->debug = nullptr;
685b307ed4dSTobias Grosser     free(PPCGScop->options);
686b307ed4dSTobias Grosser     PPCGScop->options = nullptr;
687b307ed4dSTobias Grosser   }
688b307ed4dSTobias Grosser 
689*38fc0aedSTobias Grosser   /// Generate code for a given GPU AST described by @p Root.
690*38fc0aedSTobias Grosser   ///
691*38fc0aedSTobias Grosser   /// @param An isl_ast_node pointing to the root of the GPU AST.
692*38fc0aedSTobias Grosser   void generateCode(__isl_take isl_ast_node *Root) {
693*38fc0aedSTobias Grosser     ScopAnnotator Annotator;
694*38fc0aedSTobias Grosser     Annotator.buildAliasScopes(*S);
695*38fc0aedSTobias Grosser 
696*38fc0aedSTobias Grosser     Region *R = &S->getRegion();
697*38fc0aedSTobias Grosser 
698*38fc0aedSTobias Grosser     simplifyRegion(R, DT, LI, RI);
699*38fc0aedSTobias Grosser 
700*38fc0aedSTobias Grosser     BasicBlock *EnteringBB = R->getEnteringBlock();
701*38fc0aedSTobias Grosser 
702*38fc0aedSTobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
703*38fc0aedSTobias Grosser 
704*38fc0aedSTobias Grosser     GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT,
705*38fc0aedSTobias Grosser                                *S);
706*38fc0aedSTobias Grosser 
707*38fc0aedSTobias Grosser     // Only build the run-time condition and parameters _after_ having
708*38fc0aedSTobias Grosser     // introduced the conditional branch. This is important as the conditional
709*38fc0aedSTobias Grosser     // branch will guard the original scop from new induction variables that
710*38fc0aedSTobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
711*38fc0aedSTobias Grosser     // which may introduce scalar dependences that prevent us from correctly
712*38fc0aedSTobias Grosser     // code generating this scop.
713*38fc0aedSTobias Grosser     BasicBlock *StartBlock =
714*38fc0aedSTobias Grosser         executeScopConditionally(*S, this, Builder.getTrue());
715*38fc0aedSTobias Grosser 
716*38fc0aedSTobias Grosser     // TODO: Handle LICM
717*38fc0aedSTobias Grosser     // TODO: Verify run-time checks
718*38fc0aedSTobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
719*38fc0aedSTobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
720*38fc0aedSTobias Grosser     NodeBuilder.addParameters(S->getContext());
721*38fc0aedSTobias Grosser     Builder.SetInsertPoint(&*StartBlock->begin());
722*38fc0aedSTobias Grosser     NodeBuilder.create(Root);
723*38fc0aedSTobias Grosser     NodeBuilder.finalizeSCoP(*S);
724*38fc0aedSTobias Grosser   }
725*38fc0aedSTobias Grosser 
726e938517eSTobias Grosser   bool runOnScop(Scop &CurrentScop) override {
727e938517eSTobias Grosser     S = &CurrentScop;
728*38fc0aedSTobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
729*38fc0aedSTobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
730*38fc0aedSTobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
731*38fc0aedSTobias Grosser     DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
732*38fc0aedSTobias Grosser     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
733e938517eSTobias Grosser 
734e938517eSTobias Grosser     auto PPCGScop = createPPCGScop();
735e938517eSTobias Grosser     auto PPCGProg = createPPCGProg(PPCGScop);
736f384594dSTobias Grosser     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
737*38fc0aedSTobias Grosser 
738*38fc0aedSTobias Grosser     if (PPCGGen->tree)
739*38fc0aedSTobias Grosser       generateCode(isl_ast_node_copy(PPCGGen->tree));
740*38fc0aedSTobias Grosser 
741b307ed4dSTobias Grosser     freeOptions(PPCGScop);
742f384594dSTobias Grosser     freePPCGGen(PPCGGen);
743e938517eSTobias Grosser     gpu_prog_free(PPCGProg);
744e938517eSTobias Grosser     ppcg_scop_free(PPCGScop);
745e938517eSTobias Grosser 
746e938517eSTobias Grosser     return true;
747e938517eSTobias Grosser   }
7489dfe4e7cSTobias Grosser 
7499dfe4e7cSTobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
7509dfe4e7cSTobias Grosser 
7519dfe4e7cSTobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
7529dfe4e7cSTobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
7539dfe4e7cSTobias Grosser     AU.addRequired<RegionInfoPass>();
7549dfe4e7cSTobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
7559dfe4e7cSTobias Grosser     AU.addRequired<ScopDetection>();
7569dfe4e7cSTobias Grosser     AU.addRequired<ScopInfoRegionPass>();
7579dfe4e7cSTobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
7589dfe4e7cSTobias Grosser 
7599dfe4e7cSTobias Grosser     AU.addPreserved<AAResultsWrapperPass>();
7609dfe4e7cSTobias Grosser     AU.addPreserved<BasicAAWrapperPass>();
7619dfe4e7cSTobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
7629dfe4e7cSTobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
7639dfe4e7cSTobias Grosser     AU.addPreserved<GlobalsAAWrapperPass>();
7649dfe4e7cSTobias Grosser     AU.addPreserved<PostDominatorTreeWrapperPass>();
7659dfe4e7cSTobias Grosser     AU.addPreserved<ScopDetection>();
7669dfe4e7cSTobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
7679dfe4e7cSTobias Grosser     AU.addPreserved<SCEVAAWrapperPass>();
7689dfe4e7cSTobias Grosser 
7699dfe4e7cSTobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
7709dfe4e7cSTobias Grosser     //        region tree.
7719dfe4e7cSTobias Grosser     AU.addPreserved<RegionInfoPass>();
7729dfe4e7cSTobias Grosser     AU.addPreserved<ScopInfoRegionPass>();
7739dfe4e7cSTobias Grosser   }
7749dfe4e7cSTobias Grosser };
7759dfe4e7cSTobias Grosser }
7769dfe4e7cSTobias Grosser 
7779dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1;
7789dfe4e7cSTobias Grosser 
7799dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
7809dfe4e7cSTobias Grosser 
7819dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
7829dfe4e7cSTobias Grosser                       "Polly - Apply PPCG translation to SCOP", false, false)
7839dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
7849dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
7859dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
7869dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
7879dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
7889dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
7899dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
7909dfe4e7cSTobias Grosser                     "Polly - Apply PPCG translation to SCOP", false, false)
791