19dfe4e7cSTobias Grosser //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===//
29dfe4e7cSTobias Grosser //
39dfe4e7cSTobias Grosser //                     The LLVM Compiler Infrastructure
49dfe4e7cSTobias Grosser //
59dfe4e7cSTobias Grosser // This file is distributed under the University of Illinois Open Source
69dfe4e7cSTobias Grosser // License. See LICENSE.TXT for details.
79dfe4e7cSTobias Grosser //
89dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===//
99dfe4e7cSTobias Grosser //
109dfe4e7cSTobias Grosser // Take a scop created by ScopInfo and map it to GPU code using the ppcg
119dfe4e7cSTobias Grosser // GPU mapping strategy.
129dfe4e7cSTobias Grosser //
139dfe4e7cSTobias Grosser //===----------------------------------------------------------------------===//
149dfe4e7cSTobias Grosser 
159dfe4e7cSTobias Grosser #include "polly/CodeGen/IslNodeBuilder.h"
1638fc0aedSTobias Grosser #include "polly/CodeGen/Utils.h"
179dfe4e7cSTobias Grosser #include "polly/DependenceInfo.h"
189dfe4e7cSTobias Grosser #include "polly/LinkAllPasses.h"
19f384594dSTobias Grosser #include "polly/Options.h"
209dfe4e7cSTobias Grosser #include "polly/ScopInfo.h"
219dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h"
229dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h"
239dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h"
249dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h"
259dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
269dfe4e7cSTobias Grosser 
27f384594dSTobias Grosser #include "isl/union_map.h"
28f384594dSTobias Grosser 
29e938517eSTobias Grosser extern "C" {
30a56f8f8eSTobias Grosser #include "ppcg/cuda.h"
31a56f8f8eSTobias Grosser #include "ppcg/gpu.h"
32a56f8f8eSTobias Grosser #include "ppcg/gpu_print.h"
33a56f8f8eSTobias Grosser #include "ppcg/ppcg.h"
34a56f8f8eSTobias Grosser #include "ppcg/schedule.h"
35e938517eSTobias Grosser }
36e938517eSTobias Grosser 
379dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h"
389dfe4e7cSTobias Grosser 
399dfe4e7cSTobias Grosser using namespace polly;
409dfe4e7cSTobias Grosser using namespace llvm;
419dfe4e7cSTobias Grosser 
429dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg"
439dfe4e7cSTobias Grosser 
44f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
45f384594dSTobias Grosser                                   cl::desc("Dump the computed GPU Schedule"),
46681bd568STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
47f384594dSTobias Grosser                                   cl::cat(PollyCategory));
4869b46751STobias Grosser 
4969b46751STobias Grosser static cl::opt<bool>
5069b46751STobias Grosser     DumpCode("polly-acc-dump-code",
5169b46751STobias Grosser              cl::desc("Dump C code describing the GPU mapping"), cl::Hidden,
5269b46751STobias Grosser              cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
5369b46751STobias Grosser 
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 
7238fc0aedSTobias Grosser /// Generate code for a GPU specific isl AST.
7338fc0aedSTobias Grosser ///
7438fc0aedSTobias Grosser /// The GPUNodeBuilder augments the general existing IslNodeBuilder, which
7538fc0aedSTobias Grosser /// generates code for general-prupose AST nodes, with special functionality
7638fc0aedSTobias Grosser /// for generating GPU specific user nodes.
7738fc0aedSTobias Grosser ///
7838fc0aedSTobias Grosser /// @see GPUNodeBuilder::createUser
7938fc0aedSTobias Grosser class GPUNodeBuilder : public IslNodeBuilder {
8038fc0aedSTobias Grosser public:
8138fc0aedSTobias Grosser   GPUNodeBuilder(PollyIRBuilder &Builder, ScopAnnotator &Annotator, Pass *P,
8238fc0aedSTobias Grosser                  const DataLayout &DL, LoopInfo &LI, ScalarEvolution &SE,
8338fc0aedSTobias Grosser                  DominatorTree &DT, Scop &S)
8438fc0aedSTobias Grosser       : IslNodeBuilder(Builder, Annotator, P, DL, LI, SE, DT, S) {}
8538fc0aedSTobias Grosser 
8638fc0aedSTobias Grosser private:
8738fc0aedSTobias Grosser   /// Create code for user-defined AST nodes.
8838fc0aedSTobias Grosser   ///
8938fc0aedSTobias Grosser   /// These AST nodes can be of type:
9038fc0aedSTobias Grosser   ///
9138fc0aedSTobias Grosser   ///   - ScopStmt:      A computational statement (TODO)
9238fc0aedSTobias Grosser   ///   - Kernel:        A GPU kernel call (TODO)
9338fc0aedSTobias Grosser   ///   - Data-Transfer: A GPU <-> CPU data-transfer (TODO)
9438fc0aedSTobias Grosser   ///
951fb9b64dSTobias Grosser   /// @param UserStmt The ast node to generate code for.
961fb9b64dSTobias Grosser   virtual void createUser(__isl_take isl_ast_node *UserStmt);
971fb9b64dSTobias Grosser };
981fb9b64dSTobias Grosser 
991fb9b64dSTobias Grosser void GPUNodeBuilder::createUser(__isl_take isl_ast_node *UserStmt) {
1001fb9b64dSTobias Grosser   isl_ast_node_free(UserStmt);
10138fc0aedSTobias Grosser   return;
10238fc0aedSTobias Grosser }
10338fc0aedSTobias Grosser 
1049dfe4e7cSTobias Grosser namespace {
1059dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass {
1069dfe4e7cSTobias Grosser public:
1079dfe4e7cSTobias Grosser   static char ID;
1089dfe4e7cSTobias Grosser 
109e938517eSTobias Grosser   /// The scop that is currently processed.
110e938517eSTobias Grosser   Scop *S;
111e938517eSTobias Grosser 
11238fc0aedSTobias Grosser   LoopInfo *LI;
11338fc0aedSTobias Grosser   DominatorTree *DT;
11438fc0aedSTobias Grosser   ScalarEvolution *SE;
11538fc0aedSTobias Grosser   const DataLayout *DL;
11638fc0aedSTobias Grosser   RegionInfo *RI;
11738fc0aedSTobias Grosser 
1189dfe4e7cSTobias Grosser   PPCGCodeGeneration() : ScopPass(ID) {}
1199dfe4e7cSTobias Grosser 
120e938517eSTobias Grosser   /// Construct compilation options for PPCG.
121e938517eSTobias Grosser   ///
122e938517eSTobias Grosser   /// @returns The compilation options.
123e938517eSTobias Grosser   ppcg_options *createPPCGOptions() {
124e938517eSTobias Grosser     auto DebugOptions =
125e938517eSTobias Grosser         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
126e938517eSTobias Grosser     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
127e938517eSTobias Grosser 
128e938517eSTobias Grosser     DebugOptions->dump_schedule_constraints = false;
129e938517eSTobias Grosser     DebugOptions->dump_schedule = false;
130e938517eSTobias Grosser     DebugOptions->dump_final_schedule = false;
131e938517eSTobias Grosser     DebugOptions->dump_sizes = false;
132e938517eSTobias Grosser 
133e938517eSTobias Grosser     Options->debug = DebugOptions;
134e938517eSTobias Grosser 
135e938517eSTobias Grosser     Options->reschedule = true;
136e938517eSTobias Grosser     Options->scale_tile_loops = false;
137e938517eSTobias Grosser     Options->wrap = false;
138e938517eSTobias Grosser 
139e938517eSTobias Grosser     Options->non_negative_parameters = false;
140e938517eSTobias Grosser     Options->ctx = nullptr;
141e938517eSTobias Grosser     Options->sizes = nullptr;
142e938517eSTobias Grosser 
1434eaedde5STobias Grosser     Options->tile_size = 32;
1444eaedde5STobias Grosser 
145e938517eSTobias Grosser     Options->use_private_memory = false;
146e938517eSTobias Grosser     Options->use_shared_memory = false;
147e938517eSTobias Grosser     Options->max_shared_memory = 0;
148e938517eSTobias Grosser 
149e938517eSTobias Grosser     Options->target = PPCG_TARGET_CUDA;
150e938517eSTobias Grosser     Options->openmp = false;
151e938517eSTobias Grosser     Options->linearize_device_arrays = true;
152e938517eSTobias Grosser     Options->live_range_reordering = false;
153e938517eSTobias Grosser 
154e938517eSTobias Grosser     Options->opencl_compiler_options = nullptr;
155e938517eSTobias Grosser     Options->opencl_use_gpu = false;
156e938517eSTobias Grosser     Options->opencl_n_include_file = 0;
157e938517eSTobias Grosser     Options->opencl_include_files = nullptr;
158e938517eSTobias Grosser     Options->opencl_print_kernel_types = false;
159e938517eSTobias Grosser     Options->opencl_embed_kernel_code = false;
160e938517eSTobias Grosser 
161e938517eSTobias Grosser     Options->save_schedule_file = nullptr;
162e938517eSTobias Grosser     Options->load_schedule_file = nullptr;
163e938517eSTobias Grosser 
164e938517eSTobias Grosser     return Options;
165e938517eSTobias Grosser   }
166e938517eSTobias Grosser 
167f384594dSTobias Grosser   /// Get a tagged access relation containing all accesses of type @p AccessTy.
168f384594dSTobias Grosser   ///
169f384594dSTobias Grosser   /// Instead of a normal access of the form:
170f384594dSTobias Grosser   ///
171f384594dSTobias Grosser   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
172f384594dSTobias Grosser   ///
173f384594dSTobias Grosser   /// a tagged access has the form
174f384594dSTobias Grosser   ///
175f384594dSTobias Grosser   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
176f384594dSTobias Grosser   ///
177f384594dSTobias Grosser   /// where 'id' is an additional space that references the memory access that
178f384594dSTobias Grosser   /// triggered the access.
179f384594dSTobias Grosser   ///
180f384594dSTobias Grosser   /// @param AccessTy The type of the memory accesses to collect.
181f384594dSTobias Grosser   ///
182f384594dSTobias Grosser   /// @return The relation describing all tagged memory accesses.
183f384594dSTobias Grosser   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
184f384594dSTobias Grosser     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
185f384594dSTobias Grosser 
186f384594dSTobias Grosser     for (auto &Stmt : *S)
187f384594dSTobias Grosser       for (auto &Acc : Stmt)
188f384594dSTobias Grosser         if (Acc->getType() == AccessTy) {
189f384594dSTobias Grosser           isl_map *Relation = Acc->getAccessRelation();
190f384594dSTobias Grosser           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
191f384594dSTobias Grosser 
192f384594dSTobias Grosser           isl_space *Space = isl_map_get_space(Relation);
193f384594dSTobias Grosser           Space = isl_space_range(Space);
194f384594dSTobias Grosser           Space = isl_space_from_range(Space);
1956293ba69STobias Grosser           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
196f384594dSTobias Grosser           isl_map *Universe = isl_map_universe(Space);
197f384594dSTobias Grosser           Relation = isl_map_domain_product(Relation, Universe);
198f384594dSTobias Grosser           Accesses = isl_union_map_add_map(Accesses, Relation);
199f384594dSTobias Grosser         }
200f384594dSTobias Grosser 
201f384594dSTobias Grosser     return Accesses;
202f384594dSTobias Grosser   }
203f384594dSTobias Grosser 
204f384594dSTobias Grosser   /// Get the set of all read accesses, tagged with the access id.
205f384594dSTobias Grosser   ///
206f384594dSTobias Grosser   /// @see getTaggedAccesses
207f384594dSTobias Grosser   isl_union_map *getTaggedReads() {
208f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::READ);
209f384594dSTobias Grosser   }
210f384594dSTobias Grosser 
211f384594dSTobias Grosser   /// Get the set of all may (and must) accesses, tagged with the access id.
212f384594dSTobias Grosser   ///
213f384594dSTobias Grosser   /// @see getTaggedAccesses
214f384594dSTobias Grosser   isl_union_map *getTaggedMayWrites() {
215f384594dSTobias Grosser     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
216f384594dSTobias Grosser                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
217f384594dSTobias Grosser   }
218f384594dSTobias Grosser 
219f384594dSTobias Grosser   /// Get the set of all must accesses, tagged with the access id.
220f384594dSTobias Grosser   ///
221f384594dSTobias Grosser   /// @see getTaggedAccesses
222f384594dSTobias Grosser   isl_union_map *getTaggedMustWrites() {
223f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
224f384594dSTobias Grosser   }
225f384594dSTobias Grosser 
226aef5196fSTobias Grosser   /// Collect parameter and array names as isl_ids.
227aef5196fSTobias Grosser   ///
228aef5196fSTobias Grosser   /// To reason about the different parameters and arrays used, ppcg requires
229aef5196fSTobias Grosser   /// a list of all isl_ids in use. As PPCG traditionally performs
230aef5196fSTobias Grosser   /// source-to-source compilation each of these isl_ids is mapped to the
231aef5196fSTobias Grosser   /// expression that represents it. As we do not have a corresponding
232aef5196fSTobias Grosser   /// expression in Polly, we just map each id to a 'zero' expression to match
233aef5196fSTobias Grosser   /// the data format that ppcg expects.
234aef5196fSTobias Grosser   ///
235aef5196fSTobias Grosser   /// @returns Retun a map from collected ids to 'zero' ast expressions.
236aef5196fSTobias Grosser   __isl_give isl_id_to_ast_expr *getNames() {
237aef5196fSTobias Grosser     auto *Names = isl_id_to_ast_expr_alloc(
238bd81a7eeSTobias Grosser         S->getIslCtx(),
239bd81a7eeSTobias Grosser         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
240aef5196fSTobias Grosser     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
241aef5196fSTobias Grosser     auto *Space = S->getParamSpace();
242aef5196fSTobias Grosser 
243aef5196fSTobias Grosser     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
244aef5196fSTobias Grosser       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
245aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
246aef5196fSTobias Grosser     }
247aef5196fSTobias Grosser 
248aef5196fSTobias Grosser     for (auto &Array : S->arrays()) {
249aef5196fSTobias Grosser       auto Id = Array.second->getBasePtrId();
250aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
251aef5196fSTobias Grosser     }
252aef5196fSTobias Grosser 
253aef5196fSTobias Grosser     isl_space_free(Space);
254aef5196fSTobias Grosser     isl_ast_expr_free(Zero);
255aef5196fSTobias Grosser 
256aef5196fSTobias Grosser     return Names;
257aef5196fSTobias Grosser   }
258aef5196fSTobias Grosser 
259e938517eSTobias Grosser   /// Create a new PPCG scop from the current scop.
260e938517eSTobias Grosser   ///
261f384594dSTobias Grosser   /// The PPCG scop is initialized with data from the current polly::Scop. From
262f384594dSTobias Grosser   /// this initial data, the data-dependences in the PPCG scop are initialized.
263f384594dSTobias Grosser   /// We do not use Polly's dependence analysis for now, to ensure we match
264f384594dSTobias Grosser   /// the PPCG default behaviour more closely.
265e938517eSTobias Grosser   ///
266e938517eSTobias Grosser   /// @returns A new ppcg scop.
267e938517eSTobias Grosser   ppcg_scop *createPPCGScop() {
268e938517eSTobias Grosser     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
269e938517eSTobias Grosser 
270e938517eSTobias Grosser     PPCGScop->options = createPPCGOptions();
271e938517eSTobias Grosser 
272e938517eSTobias Grosser     PPCGScop->start = 0;
273e938517eSTobias Grosser     PPCGScop->end = 0;
274e938517eSTobias Grosser 
275f384594dSTobias Grosser     PPCGScop->context = S->getContext();
276f384594dSTobias Grosser     PPCGScop->domain = S->getDomains();
277e938517eSTobias Grosser     PPCGScop->call = nullptr;
278f384594dSTobias Grosser     PPCGScop->tagged_reads = getTaggedReads();
279f384594dSTobias Grosser     PPCGScop->reads = S->getReads();
280e938517eSTobias Grosser     PPCGScop->live_in = nullptr;
281f384594dSTobias Grosser     PPCGScop->tagged_may_writes = getTaggedMayWrites();
282f384594dSTobias Grosser     PPCGScop->may_writes = S->getWrites();
283f384594dSTobias Grosser     PPCGScop->tagged_must_writes = getTaggedMustWrites();
284f384594dSTobias Grosser     PPCGScop->must_writes = S->getMustWrites();
285e938517eSTobias Grosser     PPCGScop->live_out = nullptr;
286f384594dSTobias Grosser     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
287e938517eSTobias Grosser     PPCGScop->tagger = nullptr;
288e938517eSTobias Grosser 
289e938517eSTobias Grosser     PPCGScop->independence = nullptr;
290e938517eSTobias Grosser     PPCGScop->dep_flow = nullptr;
291e938517eSTobias Grosser     PPCGScop->tagged_dep_flow = nullptr;
292e938517eSTobias Grosser     PPCGScop->dep_false = nullptr;
293e938517eSTobias Grosser     PPCGScop->dep_forced = nullptr;
294e938517eSTobias Grosser     PPCGScop->dep_order = nullptr;
295e938517eSTobias Grosser     PPCGScop->tagged_dep_order = nullptr;
296e938517eSTobias Grosser 
297f384594dSTobias Grosser     PPCGScop->schedule = S->getScheduleTree();
298aef5196fSTobias Grosser     PPCGScop->names = getNames();
299e938517eSTobias Grosser 
300e938517eSTobias Grosser     PPCGScop->pet = nullptr;
301e938517eSTobias Grosser 
302f384594dSTobias Grosser     compute_tagger(PPCGScop);
303f384594dSTobias Grosser     compute_dependences(PPCGScop);
304f384594dSTobias Grosser 
305e938517eSTobias Grosser     return PPCGScop;
306e938517eSTobias Grosser   }
307e938517eSTobias Grosser 
30860f63b49STobias Grosser   /// Collect the array acesses in a statement.
30960f63b49STobias Grosser   ///
31060f63b49STobias Grosser   /// @param Stmt The statement for which to collect the accesses.
31160f63b49STobias Grosser   ///
31260f63b49STobias Grosser   /// @returns A list of array accesses.
31360f63b49STobias Grosser   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
31460f63b49STobias Grosser     gpu_stmt_access *Accesses = nullptr;
31560f63b49STobias Grosser 
31660f63b49STobias Grosser     for (MemoryAccess *Acc : Stmt) {
31760f63b49STobias Grosser       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
31860f63b49STobias Grosser       Access->read = Acc->isRead();
31960f63b49STobias Grosser       Access->write = Acc->isWrite();
32060f63b49STobias Grosser       Access->access = Acc->getAccessRelation();
32160f63b49STobias Grosser       isl_space *Space = isl_map_get_space(Access->access);
32260f63b49STobias Grosser       Space = isl_space_range(Space);
32360f63b49STobias Grosser       Space = isl_space_from_range(Space);
3246293ba69STobias Grosser       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
32560f63b49STobias Grosser       isl_map *Universe = isl_map_universe(Space);
32660f63b49STobias Grosser       Access->tagged_access =
32760f63b49STobias Grosser           isl_map_domain_product(Acc->getAccessRelation(), Universe);
32860f63b49STobias Grosser       Access->exact_write = Acc->isWrite();
32960f63b49STobias Grosser       Access->ref_id = Acc->getId();
33060f63b49STobias Grosser       Access->next = Accesses;
33160f63b49STobias Grosser       Accesses = Access;
33260f63b49STobias Grosser     }
33360f63b49STobias Grosser 
33460f63b49STobias Grosser     return Accesses;
33560f63b49STobias Grosser   }
33660f63b49STobias Grosser 
33769b46751STobias Grosser   /// Collect the list of GPU statements.
33869b46751STobias Grosser   ///
33969b46751STobias Grosser   /// Each statement has an id, a pointer to the underlying data structure,
34069b46751STobias Grosser   /// as well as a list with all memory accesses.
34169b46751STobias Grosser   ///
34269b46751STobias Grosser   /// TODO: Initialize the list of memory accesses.
34369b46751STobias Grosser   ///
34469b46751STobias Grosser   /// @returns A linked-list of statements.
34569b46751STobias Grosser   gpu_stmt *getStatements() {
34669b46751STobias Grosser     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
34769b46751STobias Grosser                                        std::distance(S->begin(), S->end()));
34869b46751STobias Grosser 
34969b46751STobias Grosser     int i = 0;
35069b46751STobias Grosser     for (auto &Stmt : *S) {
35169b46751STobias Grosser       gpu_stmt *GPUStmt = &Stmts[i];
35269b46751STobias Grosser 
35369b46751STobias Grosser       GPUStmt->id = Stmt.getDomainId();
35469b46751STobias Grosser 
35569b46751STobias Grosser       // We use the pet stmt pointer to keep track of the Polly statements.
35669b46751STobias Grosser       GPUStmt->stmt = (pet_stmt *)&Stmt;
35760f63b49STobias Grosser       GPUStmt->accesses = getStmtAccesses(Stmt);
35869b46751STobias Grosser       i++;
35969b46751STobias Grosser     }
36069b46751STobias Grosser 
36169b46751STobias Grosser     return Stmts;
36269b46751STobias Grosser   }
36369b46751STobias Grosser 
36460f63b49STobias Grosser   /// Derive the extent of an array.
36560f63b49STobias Grosser   ///
36660f63b49STobias Grosser   /// The extent of an array is defined by the set of memory locations for
36760f63b49STobias Grosser   /// which a memory access in the iteration domain exists.
36860f63b49STobias Grosser   ///
36960f63b49STobias Grosser   /// @param Array The array to derive the extent for.
37060f63b49STobias Grosser   ///
37160f63b49STobias Grosser   /// @returns An isl_set describing the extent of the array.
37260f63b49STobias Grosser   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
37360f63b49STobias Grosser     isl_union_map *Accesses = S->getAccesses();
37460f63b49STobias Grosser     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
37560f63b49STobias Grosser     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
37660f63b49STobias Grosser     isl_set *AccessSet =
37760f63b49STobias Grosser         isl_union_set_extract_set(AccessUSet, Array->getSpace());
37860f63b49STobias Grosser     isl_union_set_free(AccessUSet);
37960f63b49STobias Grosser 
38060f63b49STobias Grosser     return AccessSet;
38160f63b49STobias Grosser   }
38260f63b49STobias Grosser 
38360f63b49STobias Grosser   /// Derive the bounds of an array.
38460f63b49STobias Grosser   ///
38560f63b49STobias Grosser   /// For the first dimension we derive the bound of the array from the extent
38660f63b49STobias Grosser   /// of this dimension. For inner dimensions we obtain their size directly from
38760f63b49STobias Grosser   /// ScopArrayInfo.
38860f63b49STobias Grosser   ///
38960f63b49STobias Grosser   /// @param PPCGArray The array to compute bounds for.
39060f63b49STobias Grosser   /// @param Array The polly array from which to take the information.
39160f63b49STobias Grosser   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
39260f63b49STobias Grosser     if (PPCGArray.n_index > 0) {
39360f63b49STobias Grosser       isl_set *Dom = isl_set_copy(PPCGArray.extent);
39460f63b49STobias Grosser       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
39560f63b49STobias Grosser       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
39660f63b49STobias Grosser       isl_set_free(Dom);
39760f63b49STobias Grosser       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
39860f63b49STobias Grosser       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
39960f63b49STobias Grosser       isl_aff *One = isl_aff_zero_on_domain(LS);
40060f63b49STobias Grosser       One = isl_aff_add_constant_si(One, 1);
40160f63b49STobias Grosser       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
40260f63b49STobias Grosser       Bound = isl_pw_aff_gist(Bound, S->getContext());
40360f63b49STobias Grosser       PPCGArray.bound[0] = Bound;
40460f63b49STobias Grosser     }
40560f63b49STobias Grosser 
40660f63b49STobias Grosser     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
40760f63b49STobias Grosser       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
40860f63b49STobias Grosser       auto LS = isl_pw_aff_get_domain_space(Bound);
40960f63b49STobias Grosser       auto Aff = isl_multi_aff_zero(LS);
41060f63b49STobias Grosser       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
41160f63b49STobias Grosser       PPCGArray.bound[i] = Bound;
41260f63b49STobias Grosser     }
41360f63b49STobias Grosser   }
41460f63b49STobias Grosser 
41560f63b49STobias Grosser   /// Create the arrays for @p PPCGProg.
41660f63b49STobias Grosser   ///
41760f63b49STobias Grosser   /// @param PPCGProg The program to compute the arrays for.
41860f63b49STobias Grosser   void createArrays(gpu_prog *PPCGProg) {
41960f63b49STobias Grosser     int i = 0;
42060f63b49STobias Grosser     for (auto &Element : S->arrays()) {
42160f63b49STobias Grosser       ScopArrayInfo *Array = Element.second.get();
42260f63b49STobias Grosser 
42360f63b49STobias Grosser       std::string TypeName;
42460f63b49STobias Grosser       raw_string_ostream OS(TypeName);
42560f63b49STobias Grosser 
42660f63b49STobias Grosser       OS << *Array->getElementType();
42760f63b49STobias Grosser       TypeName = OS.str();
42860f63b49STobias Grosser 
42960f63b49STobias Grosser       gpu_array_info &PPCGArray = PPCGProg->array[i];
43060f63b49STobias Grosser 
43160f63b49STobias Grosser       PPCGArray.space = Array->getSpace();
43260f63b49STobias Grosser       PPCGArray.type = strdup(TypeName.c_str());
43360f63b49STobias Grosser       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
43460f63b49STobias Grosser       PPCGArray.name = strdup(Array->getName().c_str());
43560f63b49STobias Grosser       PPCGArray.extent = nullptr;
43660f63b49STobias Grosser       PPCGArray.n_index = Array->getNumberOfDimensions();
43760f63b49STobias Grosser       PPCGArray.bound =
43860f63b49STobias Grosser           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
43960f63b49STobias Grosser       PPCGArray.extent = getExtent(Array);
44060f63b49STobias Grosser       PPCGArray.n_ref = 0;
44160f63b49STobias Grosser       PPCGArray.refs = nullptr;
44260f63b49STobias Grosser       PPCGArray.accessed = true;
44360f63b49STobias Grosser       PPCGArray.read_only_scalar = false;
44460f63b49STobias Grosser       PPCGArray.has_compound_element = false;
44560f63b49STobias Grosser       PPCGArray.local = false;
44660f63b49STobias Grosser       PPCGArray.declare_local = false;
44760f63b49STobias Grosser       PPCGArray.global = false;
44860f63b49STobias Grosser       PPCGArray.linearize = false;
44960f63b49STobias Grosser       PPCGArray.dep_order = nullptr;
45060f63b49STobias Grosser 
45160f63b49STobias Grosser       setArrayBounds(PPCGArray, Array);
4522d010dafSTobias Grosser       i++;
453*b9fc860aSTobias Grosser 
454*b9fc860aSTobias Grosser       collect_references(PPCGProg, &PPCGArray);
45560f63b49STobias Grosser     }
45660f63b49STobias Grosser   }
45760f63b49STobias Grosser 
45860f63b49STobias Grosser   /// Create an identity map between the arrays in the scop.
45960f63b49STobias Grosser   ///
46060f63b49STobias Grosser   /// @returns An identity map between the arrays in the scop.
46160f63b49STobias Grosser   isl_union_map *getArrayIdentity() {
46260f63b49STobias Grosser     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
46360f63b49STobias Grosser 
46460f63b49STobias Grosser     for (auto &Item : S->arrays()) {
46560f63b49STobias Grosser       ScopArrayInfo *Array = Item.second.get();
46660f63b49STobias Grosser       isl_space *Space = Array->getSpace();
46760f63b49STobias Grosser       Space = isl_space_map_from_set(Space);
46860f63b49STobias Grosser       isl_map *Identity = isl_map_identity(Space);
46960f63b49STobias Grosser       Maps = isl_union_map_add_map(Maps, Identity);
47060f63b49STobias Grosser     }
47160f63b49STobias Grosser 
47260f63b49STobias Grosser     return Maps;
47360f63b49STobias Grosser   }
47460f63b49STobias Grosser 
475e938517eSTobias Grosser   /// Create a default-initialized PPCG GPU program.
476e938517eSTobias Grosser   ///
477e938517eSTobias Grosser   /// @returns A new gpu grogram description.
478e938517eSTobias Grosser   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
479e938517eSTobias Grosser 
480e938517eSTobias Grosser     if (!PPCGScop)
481e938517eSTobias Grosser       return nullptr;
482e938517eSTobias Grosser 
483e938517eSTobias Grosser     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
484e938517eSTobias Grosser 
485e938517eSTobias Grosser     PPCGProg->ctx = S->getIslCtx();
486e938517eSTobias Grosser     PPCGProg->scop = PPCGScop;
487aef5196fSTobias Grosser     PPCGProg->context = isl_set_copy(PPCGScop->context);
48860f63b49STobias Grosser     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
48960f63b49STobias Grosser     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
49060f63b49STobias Grosser     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
49160f63b49STobias Grosser     PPCGProg->tagged_must_kill =
49260f63b49STobias Grosser         isl_union_map_copy(PPCGScop->tagged_must_kills);
49360f63b49STobias Grosser     PPCGProg->to_inner = getArrayIdentity();
49460f63b49STobias Grosser     PPCGProg->to_outer = getArrayIdentity();
49560f63b49STobias Grosser     PPCGProg->may_persist = compute_may_persist(PPCGProg);
496e938517eSTobias Grosser     PPCGProg->any_to_outer = nullptr;
497e938517eSTobias Grosser     PPCGProg->array_order = nullptr;
49869b46751STobias Grosser     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
49969b46751STobias Grosser     PPCGProg->stmts = getStatements();
50060f63b49STobias Grosser     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
50160f63b49STobias Grosser     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
50260f63b49STobias Grosser                                        PPCGProg->n_array);
50360f63b49STobias Grosser 
50460f63b49STobias Grosser     createArrays(PPCGProg);
505e938517eSTobias Grosser 
506e938517eSTobias Grosser     return PPCGProg;
507e938517eSTobias Grosser   }
508e938517eSTobias Grosser 
50969b46751STobias Grosser   struct PrintGPUUserData {
51069b46751STobias Grosser     struct cuda_info *CudaInfo;
51169b46751STobias Grosser     struct gpu_prog *PPCGProg;
51269b46751STobias Grosser     std::vector<ppcg_kernel *> Kernels;
51369b46751STobias Grosser   };
51469b46751STobias Grosser 
51569b46751STobias Grosser   /// Print a user statement node in the host code.
51669b46751STobias Grosser   ///
51769b46751STobias Grosser   /// We use ppcg's printing facilities to print the actual statement and
51869b46751STobias Grosser   /// additionally build up a list of all kernels that are encountered in the
51969b46751STobias Grosser   /// host ast.
52069b46751STobias Grosser   ///
52169b46751STobias Grosser   /// @param P The printer to print to
52269b46751STobias Grosser   /// @param Options The printing options to use
52369b46751STobias Grosser   /// @param Node The node to print
52469b46751STobias Grosser   /// @param User A user pointer to carry additional data. This pointer is
52569b46751STobias Grosser   ///             expected to be of type PrintGPUUserData.
52669b46751STobias Grosser   ///
52769b46751STobias Grosser   /// @returns A printer to which the output has been printed.
52869b46751STobias Grosser   static __isl_give isl_printer *
52969b46751STobias Grosser   printHostUser(__isl_take isl_printer *P,
53069b46751STobias Grosser                 __isl_take isl_ast_print_options *Options,
53169b46751STobias Grosser                 __isl_take isl_ast_node *Node, void *User) {
53269b46751STobias Grosser     auto Data = (struct PrintGPUUserData *)User;
53369b46751STobias Grosser     auto Id = isl_ast_node_get_annotation(Node);
53469b46751STobias Grosser 
53569b46751STobias Grosser     if (Id) {
53620251734STobias Grosser       bool IsUser = !strcmp(isl_id_get_name(Id), "user");
53720251734STobias Grosser 
53820251734STobias Grosser       // If this is a user statement, format it ourselves as ppcg would
53920251734STobias Grosser       // otherwise try to call pet functionality that is not available in
54020251734STobias Grosser       // Polly.
54120251734STobias Grosser       if (IsUser) {
54220251734STobias Grosser         P = isl_printer_start_line(P);
54320251734STobias Grosser         P = isl_printer_print_ast_node(P, Node);
54420251734STobias Grosser         P = isl_printer_end_line(P);
54520251734STobias Grosser         isl_id_free(Id);
54620251734STobias Grosser         isl_ast_print_options_free(Options);
54720251734STobias Grosser         return P;
54820251734STobias Grosser       }
54920251734STobias Grosser 
55069b46751STobias Grosser       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
55169b46751STobias Grosser       isl_id_free(Id);
55269b46751STobias Grosser       Data->Kernels.push_back(Kernel);
55369b46751STobias Grosser     }
55469b46751STobias Grosser 
55569b46751STobias Grosser     return print_host_user(P, Options, Node, User);
55669b46751STobias Grosser   }
55769b46751STobias Grosser 
55869b46751STobias Grosser   /// Print C code corresponding to the control flow in @p Kernel.
55969b46751STobias Grosser   ///
56069b46751STobias Grosser   /// @param Kernel The kernel to print
56169b46751STobias Grosser   void printKernel(ppcg_kernel *Kernel) {
56269b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
56369b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
56469b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
56569b46751STobias Grosser     P = isl_ast_node_print(Kernel->tree, P, Options);
56669b46751STobias Grosser     char *String = isl_printer_get_str(P);
56769b46751STobias Grosser     printf("%s\n", String);
56869b46751STobias Grosser     free(String);
56969b46751STobias Grosser     isl_printer_free(P);
57069b46751STobias Grosser   }
57169b46751STobias Grosser 
57269b46751STobias Grosser   /// Print C code corresponding to the GPU code described by @p Tree.
57369b46751STobias Grosser   ///
57469b46751STobias Grosser   /// @param Tree An AST describing GPU code
57569b46751STobias Grosser   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
57669b46751STobias Grosser   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
57769b46751STobias Grosser     auto *P = isl_printer_to_str(S->getIslCtx());
57869b46751STobias Grosser     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
57969b46751STobias Grosser 
58069b46751STobias Grosser     PrintGPUUserData Data;
58169b46751STobias Grosser     Data.PPCGProg = PPCGProg;
58269b46751STobias Grosser 
58369b46751STobias Grosser     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
58469b46751STobias Grosser     Options =
58569b46751STobias Grosser         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
58669b46751STobias Grosser     P = isl_ast_node_print(Tree, P, Options);
58769b46751STobias Grosser     char *String = isl_printer_get_str(P);
58869b46751STobias Grosser     printf("# host\n");
58969b46751STobias Grosser     printf("%s\n", String);
59069b46751STobias Grosser     free(String);
59169b46751STobias Grosser     isl_printer_free(P);
59269b46751STobias Grosser 
59369b46751STobias Grosser     for (auto Kernel : Data.Kernels) {
59469b46751STobias Grosser       printf("# kernel%d\n", Kernel->id);
59569b46751STobias Grosser       printKernel(Kernel);
59669b46751STobias Grosser     }
59769b46751STobias Grosser   }
59869b46751STobias Grosser 
599f384594dSTobias Grosser   // Generate a GPU program using PPCG.
600f384594dSTobias Grosser   //
601f384594dSTobias Grosser   // GPU mapping consists of multiple steps:
602f384594dSTobias Grosser   //
603f384594dSTobias Grosser   //  1) Compute new schedule for the program.
604f384594dSTobias Grosser   //  2) Map schedule to GPU (TODO)
605f384594dSTobias Grosser   //  3) Generate code for new schedule (TODO)
606f384594dSTobias Grosser   //
607f384594dSTobias Grosser   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
608f384594dSTobias Grosser   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
609f384594dSTobias Grosser   // strategy directly from this pass.
610f384594dSTobias Grosser   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
611f384594dSTobias Grosser 
612f384594dSTobias Grosser     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
613f384594dSTobias Grosser 
614f384594dSTobias Grosser     PPCGGen->ctx = S->getIslCtx();
615f384594dSTobias Grosser     PPCGGen->options = PPCGScop->options;
616f384594dSTobias Grosser     PPCGGen->print = nullptr;
617f384594dSTobias Grosser     PPCGGen->print_user = nullptr;
61860c60025STobias Grosser     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
619f384594dSTobias Grosser     PPCGGen->prog = PPCGProg;
620f384594dSTobias Grosser     PPCGGen->tree = nullptr;
621f384594dSTobias Grosser     PPCGGen->types.n = 0;
622f384594dSTobias Grosser     PPCGGen->types.name = nullptr;
623f384594dSTobias Grosser     PPCGGen->sizes = nullptr;
624f384594dSTobias Grosser     PPCGGen->used_sizes = nullptr;
625f384594dSTobias Grosser     PPCGGen->kernel_id = 0;
626f384594dSTobias Grosser 
627f384594dSTobias Grosser     // Set scheduling strategy to same strategy PPCG is using.
628f384594dSTobias Grosser     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
629f384594dSTobias Grosser     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
6302341fe9eSTobias Grosser     isl_options_set_schedule_whole_component(PPCGGen->ctx, false);
631f384594dSTobias Grosser 
632f384594dSTobias Grosser     isl_schedule *Schedule = get_schedule(PPCGGen);
633f384594dSTobias Grosser 
634aef5196fSTobias Grosser     int has_permutable = has_any_permutable_node(Schedule);
635aef5196fSTobias Grosser 
63669b46751STobias Grosser     if (!has_permutable || has_permutable < 0) {
637aef5196fSTobias Grosser       Schedule = isl_schedule_free(Schedule);
63869b46751STobias Grosser     } else {
639aef5196fSTobias Grosser       Schedule = map_to_device(PPCGGen, Schedule);
64069b46751STobias Grosser       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
64169b46751STobias Grosser     }
642aef5196fSTobias Grosser 
643f384594dSTobias Grosser     if (DumpSchedule) {
644f384594dSTobias Grosser       isl_printer *P = isl_printer_to_str(S->getIslCtx());
645f384594dSTobias Grosser       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
646f384594dSTobias Grosser       P = isl_printer_print_str(P, "Schedule\n");
647f384594dSTobias Grosser       P = isl_printer_print_str(P, "========\n");
648f384594dSTobias Grosser       if (Schedule)
649f384594dSTobias Grosser         P = isl_printer_print_schedule(P, Schedule);
650f384594dSTobias Grosser       else
651f384594dSTobias Grosser         P = isl_printer_print_str(P, "No schedule found\n");
652f384594dSTobias Grosser 
653f384594dSTobias Grosser       printf("%s\n", isl_printer_get_str(P));
654f384594dSTobias Grosser       isl_printer_free(P);
655f384594dSTobias Grosser     }
656f384594dSTobias Grosser 
65769b46751STobias Grosser     if (DumpCode) {
65869b46751STobias Grosser       printf("Code\n");
65969b46751STobias Grosser       printf("====\n");
66069b46751STobias Grosser       if (PPCGGen->tree)
66169b46751STobias Grosser         printGPUTree(PPCGGen->tree, PPCGProg);
66269b46751STobias Grosser       else
66369b46751STobias Grosser         printf("No code generated\n");
66469b46751STobias Grosser     }
66569b46751STobias Grosser 
666f384594dSTobias Grosser     isl_schedule_free(Schedule);
667f384594dSTobias Grosser 
668f384594dSTobias Grosser     return PPCGGen;
669f384594dSTobias Grosser   }
670f384594dSTobias Grosser 
671f384594dSTobias Grosser   /// Free gpu_gen structure.
672f384594dSTobias Grosser   ///
673f384594dSTobias Grosser   /// @param PPCGGen The ppcg_gen object to free.
674f384594dSTobias Grosser   void freePPCGGen(gpu_gen *PPCGGen) {
675f384594dSTobias Grosser     isl_ast_node_free(PPCGGen->tree);
676f384594dSTobias Grosser     isl_union_map_free(PPCGGen->sizes);
677f384594dSTobias Grosser     isl_union_map_free(PPCGGen->used_sizes);
678f384594dSTobias Grosser     free(PPCGGen);
679f384594dSTobias Grosser   }
680f384594dSTobias Grosser 
681b307ed4dSTobias Grosser   /// Free the options in the ppcg scop structure.
682b307ed4dSTobias Grosser   ///
683b307ed4dSTobias Grosser   /// ppcg is not freeing these options for us. To avoid leaks we do this
684b307ed4dSTobias Grosser   /// ourselves.
685b307ed4dSTobias Grosser   ///
686b307ed4dSTobias Grosser   /// @param PPCGScop The scop referencing the options to free.
687b307ed4dSTobias Grosser   void freeOptions(ppcg_scop *PPCGScop) {
688b307ed4dSTobias Grosser     free(PPCGScop->options->debug);
689b307ed4dSTobias Grosser     PPCGScop->options->debug = nullptr;
690b307ed4dSTobias Grosser     free(PPCGScop->options);
691b307ed4dSTobias Grosser     PPCGScop->options = nullptr;
692b307ed4dSTobias Grosser   }
693b307ed4dSTobias Grosser 
69438fc0aedSTobias Grosser   /// Generate code for a given GPU AST described by @p Root.
69538fc0aedSTobias Grosser   ///
69638fc0aedSTobias Grosser   /// @param An isl_ast_node pointing to the root of the GPU AST.
69738fc0aedSTobias Grosser   void generateCode(__isl_take isl_ast_node *Root) {
69838fc0aedSTobias Grosser     ScopAnnotator Annotator;
69938fc0aedSTobias Grosser     Annotator.buildAliasScopes(*S);
70038fc0aedSTobias Grosser 
70138fc0aedSTobias Grosser     Region *R = &S->getRegion();
70238fc0aedSTobias Grosser 
70338fc0aedSTobias Grosser     simplifyRegion(R, DT, LI, RI);
70438fc0aedSTobias Grosser 
70538fc0aedSTobias Grosser     BasicBlock *EnteringBB = R->getEnteringBlock();
70638fc0aedSTobias Grosser 
70738fc0aedSTobias Grosser     PollyIRBuilder Builder = createPollyIRBuilder(EnteringBB, Annotator);
70838fc0aedSTobias Grosser 
70938fc0aedSTobias Grosser     GPUNodeBuilder NodeBuilder(Builder, Annotator, this, *DL, *LI, *SE, *DT,
71038fc0aedSTobias Grosser                                *S);
71138fc0aedSTobias Grosser 
71238fc0aedSTobias Grosser     // Only build the run-time condition and parameters _after_ having
71338fc0aedSTobias Grosser     // introduced the conditional branch. This is important as the conditional
71438fc0aedSTobias Grosser     // branch will guard the original scop from new induction variables that
71538fc0aedSTobias Grosser     // the SCEVExpander may introduce while code generating the parameters and
71638fc0aedSTobias Grosser     // which may introduce scalar dependences that prevent us from correctly
71738fc0aedSTobias Grosser     // code generating this scop.
71838fc0aedSTobias Grosser     BasicBlock *StartBlock =
71938fc0aedSTobias Grosser         executeScopConditionally(*S, this, Builder.getTrue());
72038fc0aedSTobias Grosser 
72138fc0aedSTobias Grosser     // TODO: Handle LICM
72238fc0aedSTobias Grosser     // TODO: Verify run-time checks
72338fc0aedSTobias Grosser     auto SplitBlock = StartBlock->getSinglePredecessor();
72438fc0aedSTobias Grosser     Builder.SetInsertPoint(SplitBlock->getTerminator());
72538fc0aedSTobias Grosser     NodeBuilder.addParameters(S->getContext());
72638fc0aedSTobias Grosser     Builder.SetInsertPoint(&*StartBlock->begin());
72738fc0aedSTobias Grosser     NodeBuilder.create(Root);
72838fc0aedSTobias Grosser     NodeBuilder.finalizeSCoP(*S);
72938fc0aedSTobias Grosser   }
73038fc0aedSTobias Grosser 
731e938517eSTobias Grosser   bool runOnScop(Scop &CurrentScop) override {
732e938517eSTobias Grosser     S = &CurrentScop;
73338fc0aedSTobias Grosser     LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
73438fc0aedSTobias Grosser     DT = &getAnalysis<DominatorTreeWrapperPass>().getDomTree();
73538fc0aedSTobias Grosser     SE = &getAnalysis<ScalarEvolutionWrapperPass>().getSE();
73638fc0aedSTobias Grosser     DL = &S->getRegion().getEntry()->getParent()->getParent()->getDataLayout();
73738fc0aedSTobias Grosser     RI = &getAnalysis<RegionInfoPass>().getRegionInfo();
738e938517eSTobias Grosser 
739e938517eSTobias Grosser     auto PPCGScop = createPPCGScop();
740e938517eSTobias Grosser     auto PPCGProg = createPPCGProg(PPCGScop);
741f384594dSTobias Grosser     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
74238fc0aedSTobias Grosser 
74338fc0aedSTobias Grosser     if (PPCGGen->tree)
74438fc0aedSTobias Grosser       generateCode(isl_ast_node_copy(PPCGGen->tree));
74538fc0aedSTobias Grosser 
746b307ed4dSTobias Grosser     freeOptions(PPCGScop);
747f384594dSTobias Grosser     freePPCGGen(PPCGGen);
748e938517eSTobias Grosser     gpu_prog_free(PPCGProg);
749e938517eSTobias Grosser     ppcg_scop_free(PPCGScop);
750e938517eSTobias Grosser 
751e938517eSTobias Grosser     return true;
752e938517eSTobias Grosser   }
7539dfe4e7cSTobias Grosser 
7549dfe4e7cSTobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
7559dfe4e7cSTobias Grosser 
7569dfe4e7cSTobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
7579dfe4e7cSTobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
7589dfe4e7cSTobias Grosser     AU.addRequired<RegionInfoPass>();
7599dfe4e7cSTobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
7609dfe4e7cSTobias Grosser     AU.addRequired<ScopDetection>();
7619dfe4e7cSTobias Grosser     AU.addRequired<ScopInfoRegionPass>();
7629dfe4e7cSTobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
7639dfe4e7cSTobias Grosser 
7649dfe4e7cSTobias Grosser     AU.addPreserved<AAResultsWrapperPass>();
7659dfe4e7cSTobias Grosser     AU.addPreserved<BasicAAWrapperPass>();
7669dfe4e7cSTobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
7679dfe4e7cSTobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
7689dfe4e7cSTobias Grosser     AU.addPreserved<GlobalsAAWrapperPass>();
7699dfe4e7cSTobias Grosser     AU.addPreserved<PostDominatorTreeWrapperPass>();
7709dfe4e7cSTobias Grosser     AU.addPreserved<ScopDetection>();
7719dfe4e7cSTobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
7729dfe4e7cSTobias Grosser     AU.addPreserved<SCEVAAWrapperPass>();
7739dfe4e7cSTobias Grosser 
7749dfe4e7cSTobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
7759dfe4e7cSTobias Grosser     //        region tree.
7769dfe4e7cSTobias Grosser     AU.addPreserved<RegionInfoPass>();
7779dfe4e7cSTobias Grosser     AU.addPreserved<ScopInfoRegionPass>();
7789dfe4e7cSTobias Grosser   }
7799dfe4e7cSTobias Grosser };
7809dfe4e7cSTobias Grosser }
7819dfe4e7cSTobias Grosser 
7829dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1;
7839dfe4e7cSTobias Grosser 
7849dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
7859dfe4e7cSTobias Grosser 
7869dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
7879dfe4e7cSTobias Grosser                       "Polly - Apply PPCG translation to SCOP", false, false)
7889dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
7899dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
7909dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
7919dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
7929dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
7939dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
7949dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
7959dfe4e7cSTobias Grosser                     "Polly - Apply PPCG translation to SCOP", false, false)
796