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"
169dfe4e7cSTobias Grosser #include "polly/DependenceInfo.h"
179dfe4e7cSTobias Grosser #include "polly/LinkAllPasses.h"
18f384594dSTobias Grosser #include "polly/Options.h"
199dfe4e7cSTobias Grosser #include "polly/ScopInfo.h"
209dfe4e7cSTobias Grosser #include "llvm/Analysis/AliasAnalysis.h"
219dfe4e7cSTobias Grosser #include "llvm/Analysis/BasicAliasAnalysis.h"
229dfe4e7cSTobias Grosser #include "llvm/Analysis/GlobalsModRef.h"
239dfe4e7cSTobias Grosser #include "llvm/Analysis/PostDominators.h"
249dfe4e7cSTobias Grosser #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
259dfe4e7cSTobias Grosser 
26f384594dSTobias Grosser #include "isl/union_map.h"
27f384594dSTobias Grosser 
28e938517eSTobias Grosser extern "C" {
29e938517eSTobias Grosser #include "gpu.h"
30e938517eSTobias Grosser #include "ppcg.h"
31e938517eSTobias Grosser }
32e938517eSTobias Grosser 
339dfe4e7cSTobias Grosser #include "llvm/Support/Debug.h"
349dfe4e7cSTobias Grosser 
359dfe4e7cSTobias Grosser using namespace polly;
369dfe4e7cSTobias Grosser using namespace llvm;
379dfe4e7cSTobias Grosser 
389dfe4e7cSTobias Grosser #define DEBUG_TYPE "polly-codegen-ppcg"
399dfe4e7cSTobias Grosser 
40f384594dSTobias Grosser static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
41f384594dSTobias Grosser                                   cl::desc("Dump the computed GPU Schedule"),
42681bd568STobias Grosser                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
43f384594dSTobias Grosser                                   cl::cat(PollyCategory));
44*60c60025STobias Grosser /// Create the ast expressions for a ScopStmt.
45*60c60025STobias Grosser ///
46*60c60025STobias Grosser /// This function is a callback for to generate the ast expressions for each
47*60c60025STobias Grosser /// of the scheduled ScopStmts.
48*60c60025STobias Grosser static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
49*60c60025STobias Grosser     void *Stmt, isl_ast_build *Build,
50*60c60025STobias Grosser     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
51*60c60025STobias Grosser                                        isl_id *Id, void *User),
52*60c60025STobias Grosser     void *UserIndex,
53*60c60025STobias Grosser     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
54*60c60025STobias Grosser     void *User_expr) {
55*60c60025STobias Grosser 
56*60c60025STobias Grosser   // TODO: Implement the AST expression generation. For now we just return a
57*60c60025STobias Grosser   // nullptr to ensure that we do not free uninitialized pointers.
58*60c60025STobias Grosser 
59*60c60025STobias Grosser   return nullptr;
60*60c60025STobias Grosser }
61f384594dSTobias Grosser 
629dfe4e7cSTobias Grosser namespace {
639dfe4e7cSTobias Grosser class PPCGCodeGeneration : public ScopPass {
649dfe4e7cSTobias Grosser public:
659dfe4e7cSTobias Grosser   static char ID;
669dfe4e7cSTobias Grosser 
67e938517eSTobias Grosser   /// The scop that is currently processed.
68e938517eSTobias Grosser   Scop *S;
69e938517eSTobias Grosser 
709dfe4e7cSTobias Grosser   PPCGCodeGeneration() : ScopPass(ID) {}
719dfe4e7cSTobias Grosser 
72e938517eSTobias Grosser   /// Construct compilation options for PPCG.
73e938517eSTobias Grosser   ///
74e938517eSTobias Grosser   /// @returns The compilation options.
75e938517eSTobias Grosser   ppcg_options *createPPCGOptions() {
76e938517eSTobias Grosser     auto DebugOptions =
77e938517eSTobias Grosser         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
78e938517eSTobias Grosser     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
79e938517eSTobias Grosser 
80e938517eSTobias Grosser     DebugOptions->dump_schedule_constraints = false;
81e938517eSTobias Grosser     DebugOptions->dump_schedule = false;
82e938517eSTobias Grosser     DebugOptions->dump_final_schedule = false;
83e938517eSTobias Grosser     DebugOptions->dump_sizes = false;
84e938517eSTobias Grosser 
85e938517eSTobias Grosser     Options->debug = DebugOptions;
86e938517eSTobias Grosser 
87e938517eSTobias Grosser     Options->reschedule = true;
88e938517eSTobias Grosser     Options->scale_tile_loops = false;
89e938517eSTobias Grosser     Options->wrap = false;
90e938517eSTobias Grosser 
91e938517eSTobias Grosser     Options->non_negative_parameters = false;
92e938517eSTobias Grosser     Options->ctx = nullptr;
93e938517eSTobias Grosser     Options->sizes = nullptr;
94e938517eSTobias Grosser 
954eaedde5STobias Grosser     Options->tile_size = 32;
964eaedde5STobias Grosser 
97e938517eSTobias Grosser     Options->use_private_memory = false;
98e938517eSTobias Grosser     Options->use_shared_memory = false;
99e938517eSTobias Grosser     Options->max_shared_memory = 0;
100e938517eSTobias Grosser 
101e938517eSTobias Grosser     Options->target = PPCG_TARGET_CUDA;
102e938517eSTobias Grosser     Options->openmp = false;
103e938517eSTobias Grosser     Options->linearize_device_arrays = true;
104e938517eSTobias Grosser     Options->live_range_reordering = false;
105e938517eSTobias Grosser 
106e938517eSTobias Grosser     Options->opencl_compiler_options = nullptr;
107e938517eSTobias Grosser     Options->opencl_use_gpu = false;
108e938517eSTobias Grosser     Options->opencl_n_include_file = 0;
109e938517eSTobias Grosser     Options->opencl_include_files = nullptr;
110e938517eSTobias Grosser     Options->opencl_print_kernel_types = false;
111e938517eSTobias Grosser     Options->opencl_embed_kernel_code = false;
112e938517eSTobias Grosser 
113e938517eSTobias Grosser     Options->save_schedule_file = nullptr;
114e938517eSTobias Grosser     Options->load_schedule_file = nullptr;
115e938517eSTobias Grosser 
116e938517eSTobias Grosser     return Options;
117e938517eSTobias Grosser   }
118e938517eSTobias Grosser 
119f384594dSTobias Grosser   /// Get a tagged access relation containing all accesses of type @p AccessTy.
120f384594dSTobias Grosser   ///
121f384594dSTobias Grosser   /// Instead of a normal access of the form:
122f384594dSTobias Grosser   ///
123f384594dSTobias Grosser   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
124f384594dSTobias Grosser   ///
125f384594dSTobias Grosser   /// a tagged access has the form
126f384594dSTobias Grosser   ///
127f384594dSTobias Grosser   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
128f384594dSTobias Grosser   ///
129f384594dSTobias Grosser   /// where 'id' is an additional space that references the memory access that
130f384594dSTobias Grosser   /// triggered the access.
131f384594dSTobias Grosser   ///
132f384594dSTobias Grosser   /// @param AccessTy The type of the memory accesses to collect.
133f384594dSTobias Grosser   ///
134f384594dSTobias Grosser   /// @return The relation describing all tagged memory accesses.
135f384594dSTobias Grosser   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
136f384594dSTobias Grosser     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
137f384594dSTobias Grosser 
138f384594dSTobias Grosser     for (auto &Stmt : *S)
139f384594dSTobias Grosser       for (auto &Acc : Stmt)
140f384594dSTobias Grosser         if (Acc->getType() == AccessTy) {
141f384594dSTobias Grosser           isl_map *Relation = Acc->getAccessRelation();
142f384594dSTobias Grosser           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
143f384594dSTobias Grosser 
144f384594dSTobias Grosser           isl_space *Space = isl_map_get_space(Relation);
145f384594dSTobias Grosser           Space = isl_space_range(Space);
146f384594dSTobias Grosser           Space = isl_space_from_range(Space);
147f384594dSTobias Grosser           isl_map *Universe = isl_map_universe(Space);
148f384594dSTobias Grosser           Relation = isl_map_domain_product(Relation, Universe);
149f384594dSTobias Grosser           Accesses = isl_union_map_add_map(Accesses, Relation);
150f384594dSTobias Grosser         }
151f384594dSTobias Grosser 
152f384594dSTobias Grosser     return Accesses;
153f384594dSTobias Grosser   }
154f384594dSTobias Grosser 
155f384594dSTobias Grosser   /// Get the set of all read accesses, tagged with the access id.
156f384594dSTobias Grosser   ///
157f384594dSTobias Grosser   /// @see getTaggedAccesses
158f384594dSTobias Grosser   isl_union_map *getTaggedReads() {
159f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::READ);
160f384594dSTobias Grosser   }
161f384594dSTobias Grosser 
162f384594dSTobias Grosser   /// Get the set of all may (and must) accesses, tagged with the access id.
163f384594dSTobias Grosser   ///
164f384594dSTobias Grosser   /// @see getTaggedAccesses
165f384594dSTobias Grosser   isl_union_map *getTaggedMayWrites() {
166f384594dSTobias Grosser     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
167f384594dSTobias Grosser                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
168f384594dSTobias Grosser   }
169f384594dSTobias Grosser 
170f384594dSTobias Grosser   /// Get the set of all must accesses, tagged with the access id.
171f384594dSTobias Grosser   ///
172f384594dSTobias Grosser   /// @see getTaggedAccesses
173f384594dSTobias Grosser   isl_union_map *getTaggedMustWrites() {
174f384594dSTobias Grosser     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
175f384594dSTobias Grosser   }
176f384594dSTobias Grosser 
177aef5196fSTobias Grosser   /// Collect parameter and array names as isl_ids.
178aef5196fSTobias Grosser   ///
179aef5196fSTobias Grosser   /// To reason about the different parameters and arrays used, ppcg requires
180aef5196fSTobias Grosser   /// a list of all isl_ids in use. As PPCG traditionally performs
181aef5196fSTobias Grosser   /// source-to-source compilation each of these isl_ids is mapped to the
182aef5196fSTobias Grosser   /// expression that represents it. As we do not have a corresponding
183aef5196fSTobias Grosser   /// expression in Polly, we just map each id to a 'zero' expression to match
184aef5196fSTobias Grosser   /// the data format that ppcg expects.
185aef5196fSTobias Grosser   ///
186aef5196fSTobias Grosser   /// @returns Retun a map from collected ids to 'zero' ast expressions.
187aef5196fSTobias Grosser   __isl_give isl_id_to_ast_expr *getNames() {
188aef5196fSTobias Grosser     auto *Names = isl_id_to_ast_expr_alloc(
189bd81a7eeSTobias Grosser         S->getIslCtx(),
190bd81a7eeSTobias Grosser         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
191aef5196fSTobias Grosser     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
192aef5196fSTobias Grosser     auto *Space = S->getParamSpace();
193aef5196fSTobias Grosser 
194aef5196fSTobias Grosser     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
195aef5196fSTobias Grosser       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
196aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
197aef5196fSTobias Grosser     }
198aef5196fSTobias Grosser 
199aef5196fSTobias Grosser     for (auto &Array : S->arrays()) {
200aef5196fSTobias Grosser       auto Id = Array.second->getBasePtrId();
201aef5196fSTobias Grosser       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
202aef5196fSTobias Grosser     }
203aef5196fSTobias Grosser 
204aef5196fSTobias Grosser     isl_space_free(Space);
205aef5196fSTobias Grosser     isl_ast_expr_free(Zero);
206aef5196fSTobias Grosser 
207aef5196fSTobias Grosser     return Names;
208aef5196fSTobias Grosser   }
209aef5196fSTobias Grosser 
210e938517eSTobias Grosser   /// Create a new PPCG scop from the current scop.
211e938517eSTobias Grosser   ///
212f384594dSTobias Grosser   /// The PPCG scop is initialized with data from the current polly::Scop. From
213f384594dSTobias Grosser   /// this initial data, the data-dependences in the PPCG scop are initialized.
214f384594dSTobias Grosser   /// We do not use Polly's dependence analysis for now, to ensure we match
215f384594dSTobias Grosser   /// the PPCG default behaviour more closely.
216e938517eSTobias Grosser   ///
217e938517eSTobias Grosser   /// @returns A new ppcg scop.
218e938517eSTobias Grosser   ppcg_scop *createPPCGScop() {
219e938517eSTobias Grosser     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
220e938517eSTobias Grosser 
221e938517eSTobias Grosser     PPCGScop->options = createPPCGOptions();
222e938517eSTobias Grosser 
223e938517eSTobias Grosser     PPCGScop->start = 0;
224e938517eSTobias Grosser     PPCGScop->end = 0;
225e938517eSTobias Grosser 
226f384594dSTobias Grosser     PPCGScop->context = S->getContext();
227f384594dSTobias Grosser     PPCGScop->domain = S->getDomains();
228e938517eSTobias Grosser     PPCGScop->call = nullptr;
229f384594dSTobias Grosser     PPCGScop->tagged_reads = getTaggedReads();
230f384594dSTobias Grosser     PPCGScop->reads = S->getReads();
231e938517eSTobias Grosser     PPCGScop->live_in = nullptr;
232f384594dSTobias Grosser     PPCGScop->tagged_may_writes = getTaggedMayWrites();
233f384594dSTobias Grosser     PPCGScop->may_writes = S->getWrites();
234f384594dSTobias Grosser     PPCGScop->tagged_must_writes = getTaggedMustWrites();
235f384594dSTobias Grosser     PPCGScop->must_writes = S->getMustWrites();
236e938517eSTobias Grosser     PPCGScop->live_out = nullptr;
237f384594dSTobias Grosser     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
238e938517eSTobias Grosser     PPCGScop->tagger = nullptr;
239e938517eSTobias Grosser 
240e938517eSTobias Grosser     PPCGScop->independence = nullptr;
241e938517eSTobias Grosser     PPCGScop->dep_flow = nullptr;
242e938517eSTobias Grosser     PPCGScop->tagged_dep_flow = nullptr;
243e938517eSTobias Grosser     PPCGScop->dep_false = nullptr;
244e938517eSTobias Grosser     PPCGScop->dep_forced = nullptr;
245e938517eSTobias Grosser     PPCGScop->dep_order = nullptr;
246e938517eSTobias Grosser     PPCGScop->tagged_dep_order = nullptr;
247e938517eSTobias Grosser 
248f384594dSTobias Grosser     PPCGScop->schedule = S->getScheduleTree();
249aef5196fSTobias Grosser     PPCGScop->names = getNames();
250e938517eSTobias Grosser 
251e938517eSTobias Grosser     PPCGScop->pet = nullptr;
252e938517eSTobias Grosser 
253f384594dSTobias Grosser     compute_tagger(PPCGScop);
254f384594dSTobias Grosser     compute_dependences(PPCGScop);
255f384594dSTobias Grosser 
256e938517eSTobias Grosser     return PPCGScop;
257e938517eSTobias Grosser   }
258e938517eSTobias Grosser 
259e938517eSTobias Grosser   /// Create a default-initialized PPCG GPU program.
260e938517eSTobias Grosser   ///
261e938517eSTobias Grosser   /// @returns A new gpu grogram description.
262e938517eSTobias Grosser   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
263e938517eSTobias Grosser 
264e938517eSTobias Grosser     if (!PPCGScop)
265e938517eSTobias Grosser       return nullptr;
266e938517eSTobias Grosser 
267e938517eSTobias Grosser     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
268e938517eSTobias Grosser 
269e938517eSTobias Grosser     PPCGProg->ctx = S->getIslCtx();
270e938517eSTobias Grosser     PPCGProg->scop = PPCGScop;
271aef5196fSTobias Grosser     PPCGProg->context = isl_set_copy(PPCGScop->context);
272e938517eSTobias Grosser     PPCGProg->read = nullptr;
273e938517eSTobias Grosser     PPCGProg->may_write = nullptr;
274e938517eSTobias Grosser     PPCGProg->must_write = nullptr;
275e938517eSTobias Grosser     PPCGProg->tagged_must_kill = nullptr;
276e938517eSTobias Grosser     PPCGProg->may_persist = nullptr;
277e938517eSTobias Grosser     PPCGProg->to_outer = nullptr;
278e938517eSTobias Grosser     PPCGProg->to_inner = nullptr;
279e938517eSTobias Grosser     PPCGProg->any_to_outer = nullptr;
280e938517eSTobias Grosser     PPCGProg->array_order = nullptr;
281e938517eSTobias Grosser     PPCGProg->n_stmts = 0;
282e938517eSTobias Grosser     PPCGProg->stmts = nullptr;
283e938517eSTobias Grosser     PPCGProg->n_array = 0;
284e938517eSTobias Grosser     PPCGProg->array = nullptr;
285e938517eSTobias Grosser 
286e938517eSTobias Grosser     return PPCGProg;
287e938517eSTobias Grosser   }
288e938517eSTobias Grosser 
289f384594dSTobias Grosser   // Generate a GPU program using PPCG.
290f384594dSTobias Grosser   //
291f384594dSTobias Grosser   // GPU mapping consists of multiple steps:
292f384594dSTobias Grosser   //
293f384594dSTobias Grosser   //  1) Compute new schedule for the program.
294f384594dSTobias Grosser   //  2) Map schedule to GPU (TODO)
295f384594dSTobias Grosser   //  3) Generate code for new schedule (TODO)
296f384594dSTobias Grosser   //
297f384594dSTobias Grosser   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
298f384594dSTobias Grosser   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
299f384594dSTobias Grosser   // strategy directly from this pass.
300f384594dSTobias Grosser   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
301f384594dSTobias Grosser 
302f384594dSTobias Grosser     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
303f384594dSTobias Grosser 
304f384594dSTobias Grosser     PPCGGen->ctx = S->getIslCtx();
305f384594dSTobias Grosser     PPCGGen->options = PPCGScop->options;
306f384594dSTobias Grosser     PPCGGen->print = nullptr;
307f384594dSTobias Grosser     PPCGGen->print_user = nullptr;
308*60c60025STobias Grosser     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
309f384594dSTobias Grosser     PPCGGen->prog = PPCGProg;
310f384594dSTobias Grosser     PPCGGen->tree = nullptr;
311f384594dSTobias Grosser     PPCGGen->types.n = 0;
312f384594dSTobias Grosser     PPCGGen->types.name = nullptr;
313f384594dSTobias Grosser     PPCGGen->sizes = nullptr;
314f384594dSTobias Grosser     PPCGGen->used_sizes = nullptr;
315f384594dSTobias Grosser     PPCGGen->kernel_id = 0;
316f384594dSTobias Grosser 
317f384594dSTobias Grosser     // Set scheduling strategy to same strategy PPCG is using.
318f384594dSTobias Grosser     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
319f384594dSTobias Grosser     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
320f384594dSTobias Grosser 
321f384594dSTobias Grosser     isl_schedule *Schedule = get_schedule(PPCGGen);
322f384594dSTobias Grosser 
323aef5196fSTobias Grosser     int has_permutable = has_any_permutable_node(Schedule);
324aef5196fSTobias Grosser 
325aef5196fSTobias Grosser     if (!has_permutable || has_permutable < 0)
326aef5196fSTobias Grosser       Schedule = isl_schedule_free(Schedule);
327aef5196fSTobias Grosser     else
328aef5196fSTobias Grosser       Schedule = map_to_device(PPCGGen, Schedule);
329aef5196fSTobias Grosser 
330f384594dSTobias Grosser     if (DumpSchedule) {
331f384594dSTobias Grosser       isl_printer *P = isl_printer_to_str(S->getIslCtx());
332f384594dSTobias Grosser       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
333f384594dSTobias Grosser       P = isl_printer_print_str(P, "Schedule\n");
334f384594dSTobias Grosser       P = isl_printer_print_str(P, "========\n");
335f384594dSTobias Grosser       if (Schedule)
336f384594dSTobias Grosser         P = isl_printer_print_schedule(P, Schedule);
337f384594dSTobias Grosser       else
338f384594dSTobias Grosser         P = isl_printer_print_str(P, "No schedule found\n");
339f384594dSTobias Grosser 
340f384594dSTobias Grosser       printf("%s\n", isl_printer_get_str(P));
341f384594dSTobias Grosser       isl_printer_free(P);
342f384594dSTobias Grosser     }
343f384594dSTobias Grosser 
344f384594dSTobias Grosser     isl_schedule_free(Schedule);
345f384594dSTobias Grosser 
346f384594dSTobias Grosser     return PPCGGen;
347f384594dSTobias Grosser   }
348f384594dSTobias Grosser 
349f384594dSTobias Grosser   /// Free gpu_gen structure.
350f384594dSTobias Grosser   ///
351f384594dSTobias Grosser   /// @param PPCGGen The ppcg_gen object to free.
352f384594dSTobias Grosser   void freePPCGGen(gpu_gen *PPCGGen) {
353f384594dSTobias Grosser     isl_ast_node_free(PPCGGen->tree);
354f384594dSTobias Grosser     isl_union_map_free(PPCGGen->sizes);
355f384594dSTobias Grosser     isl_union_map_free(PPCGGen->used_sizes);
356f384594dSTobias Grosser     free(PPCGGen);
357f384594dSTobias Grosser   }
358f384594dSTobias Grosser 
359e938517eSTobias Grosser   bool runOnScop(Scop &CurrentScop) override {
360e938517eSTobias Grosser     S = &CurrentScop;
361e938517eSTobias Grosser 
362e938517eSTobias Grosser     auto PPCGScop = createPPCGScop();
363e938517eSTobias Grosser     auto PPCGProg = createPPCGProg(PPCGScop);
364f384594dSTobias Grosser     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
365f384594dSTobias Grosser     freePPCGGen(PPCGGen);
366e938517eSTobias Grosser     gpu_prog_free(PPCGProg);
367e938517eSTobias Grosser     ppcg_scop_free(PPCGScop);
368e938517eSTobias Grosser 
369e938517eSTobias Grosser     return true;
370e938517eSTobias Grosser   }
3719dfe4e7cSTobias Grosser 
3729dfe4e7cSTobias Grosser   void printScop(raw_ostream &, Scop &) const override {}
3739dfe4e7cSTobias Grosser 
3749dfe4e7cSTobias Grosser   void getAnalysisUsage(AnalysisUsage &AU) const override {
3759dfe4e7cSTobias Grosser     AU.addRequired<DominatorTreeWrapperPass>();
3769dfe4e7cSTobias Grosser     AU.addRequired<RegionInfoPass>();
3779dfe4e7cSTobias Grosser     AU.addRequired<ScalarEvolutionWrapperPass>();
3789dfe4e7cSTobias Grosser     AU.addRequired<ScopDetection>();
3799dfe4e7cSTobias Grosser     AU.addRequired<ScopInfoRegionPass>();
3809dfe4e7cSTobias Grosser     AU.addRequired<LoopInfoWrapperPass>();
3819dfe4e7cSTobias Grosser 
3829dfe4e7cSTobias Grosser     AU.addPreserved<AAResultsWrapperPass>();
3839dfe4e7cSTobias Grosser     AU.addPreserved<BasicAAWrapperPass>();
3849dfe4e7cSTobias Grosser     AU.addPreserved<LoopInfoWrapperPass>();
3859dfe4e7cSTobias Grosser     AU.addPreserved<DominatorTreeWrapperPass>();
3869dfe4e7cSTobias Grosser     AU.addPreserved<GlobalsAAWrapperPass>();
3879dfe4e7cSTobias Grosser     AU.addPreserved<PostDominatorTreeWrapperPass>();
3889dfe4e7cSTobias Grosser     AU.addPreserved<ScopDetection>();
3899dfe4e7cSTobias Grosser     AU.addPreserved<ScalarEvolutionWrapperPass>();
3909dfe4e7cSTobias Grosser     AU.addPreserved<SCEVAAWrapperPass>();
3919dfe4e7cSTobias Grosser 
3929dfe4e7cSTobias Grosser     // FIXME: We do not yet add regions for the newly generated code to the
3939dfe4e7cSTobias Grosser     //        region tree.
3949dfe4e7cSTobias Grosser     AU.addPreserved<RegionInfoPass>();
3959dfe4e7cSTobias Grosser     AU.addPreserved<ScopInfoRegionPass>();
3969dfe4e7cSTobias Grosser   }
3979dfe4e7cSTobias Grosser };
3989dfe4e7cSTobias Grosser }
3999dfe4e7cSTobias Grosser 
4009dfe4e7cSTobias Grosser char PPCGCodeGeneration::ID = 1;
4019dfe4e7cSTobias Grosser 
4029dfe4e7cSTobias Grosser Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
4039dfe4e7cSTobias Grosser 
4049dfe4e7cSTobias Grosser INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
4059dfe4e7cSTobias Grosser                       "Polly - Apply PPCG translation to SCOP", false, false)
4069dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
4079dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
4089dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
4099dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
4109dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
4119dfe4e7cSTobias Grosser INITIALIZE_PASS_DEPENDENCY(ScopDetection);
4129dfe4e7cSTobias Grosser INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
4139dfe4e7cSTobias Grosser                     "Polly - Apply PPCG translation to SCOP", false, false)
414