1 //===------ PPCGCodeGeneration.cpp - Polly Accelerator Code Generation. ---===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Take a scop created by ScopInfo and map it to GPU code using the ppcg
11 // GPU mapping strategy.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "polly/CodeGen/IslNodeBuilder.h"
16 #include "polly/DependenceInfo.h"
17 #include "polly/LinkAllPasses.h"
18 #include "polly/Options.h"
19 #include "polly/ScopInfo.h"
20 #include "llvm/Analysis/AliasAnalysis.h"
21 #include "llvm/Analysis/BasicAliasAnalysis.h"
22 #include "llvm/Analysis/GlobalsModRef.h"
23 #include "llvm/Analysis/PostDominators.h"
24 #include "llvm/Analysis/ScalarEvolutionAliasAnalysis.h"
25 
26 #include "isl/union_map.h"
27 
28 extern "C" {
29 #include "ppcg/cuda.h"
30 #include "ppcg/gpu.h"
31 #include "ppcg/gpu_print.h"
32 #include "ppcg/ppcg.h"
33 #include "ppcg/schedule.h"
34 }
35 
36 #include "llvm/Support/Debug.h"
37 
38 using namespace polly;
39 using namespace llvm;
40 
41 #define DEBUG_TYPE "polly-codegen-ppcg"
42 
43 static cl::opt<bool> DumpSchedule("polly-acc-dump-schedule",
44                                   cl::desc("Dump the computed GPU Schedule"),
45                                   cl::Hidden, cl::init(false), cl::ZeroOrMore,
46                                   cl::cat(PollyCategory));
47 
48 static cl::opt<bool>
49     DumpCode("polly-acc-dump-code",
50              cl::desc("Dump C code describing the GPU mapping"), cl::Hidden,
51              cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory));
52 
53 /// Create the ast expressions for a ScopStmt.
54 ///
55 /// This function is a callback for to generate the ast expressions for each
56 /// of the scheduled ScopStmts.
57 static __isl_give isl_id_to_ast_expr *pollyBuildAstExprForStmt(
58     void *Stmt, isl_ast_build *Build,
59     isl_multi_pw_aff *(*FunctionIndex)(__isl_take isl_multi_pw_aff *MPA,
60                                        isl_id *Id, void *User),
61     void *UserIndex,
62     isl_ast_expr *(*FunctionExpr)(isl_ast_expr *Expr, isl_id *Id, void *User),
63     void *User_expr) {
64 
65   // TODO: Implement the AST expression generation. For now we just return a
66   // nullptr to ensure that we do not free uninitialized pointers.
67 
68   return nullptr;
69 }
70 
71 namespace {
72 class PPCGCodeGeneration : public ScopPass {
73 public:
74   static char ID;
75 
76   /// The scop that is currently processed.
77   Scop *S;
78 
79   PPCGCodeGeneration() : ScopPass(ID) {}
80 
81   /// Construct compilation options for PPCG.
82   ///
83   /// @returns The compilation options.
84   ppcg_options *createPPCGOptions() {
85     auto DebugOptions =
86         (ppcg_debug_options *)malloc(sizeof(ppcg_debug_options));
87     auto Options = (ppcg_options *)malloc(sizeof(ppcg_options));
88 
89     DebugOptions->dump_schedule_constraints = false;
90     DebugOptions->dump_schedule = false;
91     DebugOptions->dump_final_schedule = false;
92     DebugOptions->dump_sizes = false;
93 
94     Options->debug = DebugOptions;
95 
96     Options->reschedule = true;
97     Options->scale_tile_loops = false;
98     Options->wrap = false;
99 
100     Options->non_negative_parameters = false;
101     Options->ctx = nullptr;
102     Options->sizes = nullptr;
103 
104     Options->tile_size = 32;
105 
106     Options->use_private_memory = false;
107     Options->use_shared_memory = false;
108     Options->max_shared_memory = 0;
109 
110     Options->target = PPCG_TARGET_CUDA;
111     Options->openmp = false;
112     Options->linearize_device_arrays = true;
113     Options->live_range_reordering = false;
114 
115     Options->opencl_compiler_options = nullptr;
116     Options->opencl_use_gpu = false;
117     Options->opencl_n_include_file = 0;
118     Options->opencl_include_files = nullptr;
119     Options->opencl_print_kernel_types = false;
120     Options->opencl_embed_kernel_code = false;
121 
122     Options->save_schedule_file = nullptr;
123     Options->load_schedule_file = nullptr;
124 
125     return Options;
126   }
127 
128   /// Get a tagged access relation containing all accesses of type @p AccessTy.
129   ///
130   /// Instead of a normal access of the form:
131   ///
132   ///   Stmt[i,j,k] -> Array[f_0(i,j,k), f_1(i,j,k)]
133   ///
134   /// a tagged access has the form
135   ///
136   ///   [Stmt[i,j,k] -> id[]] -> Array[f_0(i,j,k), f_1(i,j,k)]
137   ///
138   /// where 'id' is an additional space that references the memory access that
139   /// triggered the access.
140   ///
141   /// @param AccessTy The type of the memory accesses to collect.
142   ///
143   /// @return The relation describing all tagged memory accesses.
144   isl_union_map *getTaggedAccesses(enum MemoryAccess::AccessType AccessTy) {
145     isl_union_map *Accesses = isl_union_map_empty(S->getParamSpace());
146 
147     for (auto &Stmt : *S)
148       for (auto &Acc : Stmt)
149         if (Acc->getType() == AccessTy) {
150           isl_map *Relation = Acc->getAccessRelation();
151           Relation = isl_map_intersect_domain(Relation, Stmt.getDomain());
152 
153           isl_space *Space = isl_map_get_space(Relation);
154           Space = isl_space_range(Space);
155           Space = isl_space_from_range(Space);
156           isl_map *Universe = isl_map_universe(Space);
157           Relation = isl_map_domain_product(Relation, Universe);
158           Accesses = isl_union_map_add_map(Accesses, Relation);
159         }
160 
161     return Accesses;
162   }
163 
164   /// Get the set of all read accesses, tagged with the access id.
165   ///
166   /// @see getTaggedAccesses
167   isl_union_map *getTaggedReads() {
168     return getTaggedAccesses(MemoryAccess::READ);
169   }
170 
171   /// Get the set of all may (and must) accesses, tagged with the access id.
172   ///
173   /// @see getTaggedAccesses
174   isl_union_map *getTaggedMayWrites() {
175     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
176                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
177   }
178 
179   /// Get the set of all must accesses, tagged with the access id.
180   ///
181   /// @see getTaggedAccesses
182   isl_union_map *getTaggedMustWrites() {
183     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
184   }
185 
186   /// Collect parameter and array names as isl_ids.
187   ///
188   /// To reason about the different parameters and arrays used, ppcg requires
189   /// a list of all isl_ids in use. As PPCG traditionally performs
190   /// source-to-source compilation each of these isl_ids is mapped to the
191   /// expression that represents it. As we do not have a corresponding
192   /// expression in Polly, we just map each id to a 'zero' expression to match
193   /// the data format that ppcg expects.
194   ///
195   /// @returns Retun a map from collected ids to 'zero' ast expressions.
196   __isl_give isl_id_to_ast_expr *getNames() {
197     auto *Names = isl_id_to_ast_expr_alloc(
198         S->getIslCtx(),
199         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
200     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
201     auto *Space = S->getParamSpace();
202 
203     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
204       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
205       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
206     }
207 
208     for (auto &Array : S->arrays()) {
209       auto Id = Array.second->getBasePtrId();
210       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
211     }
212 
213     isl_space_free(Space);
214     isl_ast_expr_free(Zero);
215 
216     return Names;
217   }
218 
219   /// Create a new PPCG scop from the current scop.
220   ///
221   /// The PPCG scop is initialized with data from the current polly::Scop. From
222   /// this initial data, the data-dependences in the PPCG scop are initialized.
223   /// We do not use Polly's dependence analysis for now, to ensure we match
224   /// the PPCG default behaviour more closely.
225   ///
226   /// @returns A new ppcg scop.
227   ppcg_scop *createPPCGScop() {
228     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
229 
230     PPCGScop->options = createPPCGOptions();
231 
232     PPCGScop->start = 0;
233     PPCGScop->end = 0;
234 
235     PPCGScop->context = S->getContext();
236     PPCGScop->domain = S->getDomains();
237     PPCGScop->call = nullptr;
238     PPCGScop->tagged_reads = getTaggedReads();
239     PPCGScop->reads = S->getReads();
240     PPCGScop->live_in = nullptr;
241     PPCGScop->tagged_may_writes = getTaggedMayWrites();
242     PPCGScop->may_writes = S->getWrites();
243     PPCGScop->tagged_must_writes = getTaggedMustWrites();
244     PPCGScop->must_writes = S->getMustWrites();
245     PPCGScop->live_out = nullptr;
246     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
247     PPCGScop->tagger = nullptr;
248 
249     PPCGScop->independence = nullptr;
250     PPCGScop->dep_flow = nullptr;
251     PPCGScop->tagged_dep_flow = nullptr;
252     PPCGScop->dep_false = nullptr;
253     PPCGScop->dep_forced = nullptr;
254     PPCGScop->dep_order = nullptr;
255     PPCGScop->tagged_dep_order = nullptr;
256 
257     PPCGScop->schedule = S->getScheduleTree();
258     PPCGScop->names = getNames();
259 
260     PPCGScop->pet = nullptr;
261 
262     compute_tagger(PPCGScop);
263     compute_dependences(PPCGScop);
264 
265     return PPCGScop;
266   }
267 
268   /// Collect the array acesses in a statement.
269   ///
270   /// @param Stmt The statement for which to collect the accesses.
271   ///
272   /// @returns A list of array accesses.
273   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
274     gpu_stmt_access *Accesses = nullptr;
275 
276     for (MemoryAccess *Acc : Stmt) {
277       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
278       Access->read = Acc->isRead();
279       Access->write = Acc->isWrite();
280       Access->access = Acc->getAccessRelation();
281       isl_space *Space = isl_map_get_space(Access->access);
282       Space = isl_space_range(Space);
283       Space = isl_space_from_range(Space);
284       isl_map *Universe = isl_map_universe(Space);
285       Access->tagged_access =
286           isl_map_domain_product(Acc->getAccessRelation(), Universe);
287       Access->exact_write = Acc->isWrite();
288       Access->ref_id = Acc->getId();
289       Access->next = Accesses;
290       Accesses = Access;
291     }
292 
293     return Accesses;
294   }
295 
296   /// Collect the list of GPU statements.
297   ///
298   /// Each statement has an id, a pointer to the underlying data structure,
299   /// as well as a list with all memory accesses.
300   ///
301   /// TODO: Initialize the list of memory accesses.
302   ///
303   /// @returns A linked-list of statements.
304   gpu_stmt *getStatements() {
305     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
306                                        std::distance(S->begin(), S->end()));
307 
308     int i = 0;
309     for (auto &Stmt : *S) {
310       gpu_stmt *GPUStmt = &Stmts[i];
311 
312       GPUStmt->id = Stmt.getDomainId();
313 
314       // We use the pet stmt pointer to keep track of the Polly statements.
315       GPUStmt->stmt = (pet_stmt *)&Stmt;
316       GPUStmt->accesses = getStmtAccesses(Stmt);
317       i++;
318     }
319 
320     return Stmts;
321   }
322 
323   /// Derive the extent of an array.
324   ///
325   /// The extent of an array is defined by the set of memory locations for
326   /// which a memory access in the iteration domain exists.
327   ///
328   /// @param Array The array to derive the extent for.
329   ///
330   /// @returns An isl_set describing the extent of the array.
331   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
332     isl_union_map *Accesses = S->getAccesses();
333     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
334     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
335     isl_set *AccessSet =
336         isl_union_set_extract_set(AccessUSet, Array->getSpace());
337     isl_union_set_free(AccessUSet);
338 
339     return AccessSet;
340   }
341 
342   /// Derive the bounds of an array.
343   ///
344   /// For the first dimension we derive the bound of the array from the extent
345   /// of this dimension. For inner dimensions we obtain their size directly from
346   /// ScopArrayInfo.
347   ///
348   /// @param PPCGArray The array to compute bounds for.
349   /// @param Array The polly array from which to take the information.
350   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
351     if (PPCGArray.n_index > 0) {
352       isl_set *Dom = isl_set_copy(PPCGArray.extent);
353       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
354       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
355       isl_set_free(Dom);
356       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
357       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
358       isl_aff *One = isl_aff_zero_on_domain(LS);
359       One = isl_aff_add_constant_si(One, 1);
360       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
361       Bound = isl_pw_aff_gist(Bound, S->getContext());
362       PPCGArray.bound[0] = Bound;
363     }
364 
365     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
366       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
367       auto LS = isl_pw_aff_get_domain_space(Bound);
368       auto Aff = isl_multi_aff_zero(LS);
369       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
370       PPCGArray.bound[i] = Bound;
371     }
372   }
373 
374   /// Create the arrays for @p PPCGProg.
375   ///
376   /// @param PPCGProg The program to compute the arrays for.
377   void createArrays(gpu_prog *PPCGProg) {
378     int i = 0;
379     for (auto &Element : S->arrays()) {
380       ScopArrayInfo *Array = Element.second.get();
381 
382       std::string TypeName;
383       raw_string_ostream OS(TypeName);
384 
385       OS << *Array->getElementType();
386       TypeName = OS.str();
387 
388       gpu_array_info &PPCGArray = PPCGProg->array[i];
389 
390       PPCGArray.space = Array->getSpace();
391       PPCGArray.type = strdup(TypeName.c_str());
392       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
393       PPCGArray.name = strdup(Array->getName().c_str());
394       PPCGArray.extent = nullptr;
395       PPCGArray.n_index = Array->getNumberOfDimensions();
396       PPCGArray.bound =
397           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
398       PPCGArray.extent = getExtent(Array);
399       PPCGArray.n_ref = 0;
400       PPCGArray.refs = nullptr;
401       PPCGArray.accessed = true;
402       PPCGArray.read_only_scalar = false;
403       PPCGArray.has_compound_element = false;
404       PPCGArray.local = false;
405       PPCGArray.declare_local = false;
406       PPCGArray.global = false;
407       PPCGArray.linearize = false;
408       PPCGArray.dep_order = nullptr;
409 
410       setArrayBounds(PPCGArray, Array);
411     }
412   }
413 
414   /// Create an identity map between the arrays in the scop.
415   ///
416   /// @returns An identity map between the arrays in the scop.
417   isl_union_map *getArrayIdentity() {
418     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
419 
420     for (auto &Item : S->arrays()) {
421       ScopArrayInfo *Array = Item.second.get();
422       isl_space *Space = Array->getSpace();
423       Space = isl_space_map_from_set(Space);
424       isl_map *Identity = isl_map_identity(Space);
425       Maps = isl_union_map_add_map(Maps, Identity);
426     }
427 
428     return Maps;
429   }
430 
431   /// Create a default-initialized PPCG GPU program.
432   ///
433   /// @returns A new gpu grogram description.
434   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
435 
436     if (!PPCGScop)
437       return nullptr;
438 
439     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
440 
441     PPCGProg->ctx = S->getIslCtx();
442     PPCGProg->scop = PPCGScop;
443     PPCGProg->context = isl_set_copy(PPCGScop->context);
444     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
445     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
446     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
447     PPCGProg->tagged_must_kill =
448         isl_union_map_copy(PPCGScop->tagged_must_kills);
449     PPCGProg->to_inner = getArrayIdentity();
450     PPCGProg->to_outer = getArrayIdentity();
451     PPCGProg->may_persist = compute_may_persist(PPCGProg);
452     PPCGProg->any_to_outer = nullptr;
453     PPCGProg->array_order = nullptr;
454     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
455     PPCGProg->stmts = getStatements();
456     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
457     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
458                                        PPCGProg->n_array);
459 
460     createArrays(PPCGProg);
461 
462     return PPCGProg;
463   }
464 
465   struct PrintGPUUserData {
466     struct cuda_info *CudaInfo;
467     struct gpu_prog *PPCGProg;
468     std::vector<ppcg_kernel *> Kernels;
469   };
470 
471   /// Print a user statement node in the host code.
472   ///
473   /// We use ppcg's printing facilities to print the actual statement and
474   /// additionally build up a list of all kernels that are encountered in the
475   /// host ast.
476   ///
477   /// @param P The printer to print to
478   /// @param Options The printing options to use
479   /// @param Node The node to print
480   /// @param User A user pointer to carry additional data. This pointer is
481   ///             expected to be of type PrintGPUUserData.
482   ///
483   /// @returns A printer to which the output has been printed.
484   static __isl_give isl_printer *
485   printHostUser(__isl_take isl_printer *P,
486                 __isl_take isl_ast_print_options *Options,
487                 __isl_take isl_ast_node *Node, void *User) {
488     auto Data = (struct PrintGPUUserData *)User;
489     auto Id = isl_ast_node_get_annotation(Node);
490 
491     if (Id) {
492       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
493       isl_id_free(Id);
494       Data->Kernels.push_back(Kernel);
495     }
496 
497     return print_host_user(P, Options, Node, User);
498   }
499 
500   /// Print C code corresponding to the control flow in @p Kernel.
501   ///
502   /// @param Kernel The kernel to print
503   void printKernel(ppcg_kernel *Kernel) {
504     auto *P = isl_printer_to_str(S->getIslCtx());
505     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
506     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
507     P = isl_ast_node_print(Kernel->tree, P, Options);
508     char *String = isl_printer_get_str(P);
509     printf("%s\n", String);
510     free(String);
511     isl_printer_free(P);
512   }
513 
514   /// Print C code corresponding to the GPU code described by @p Tree.
515   ///
516   /// @param Tree An AST describing GPU code
517   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
518   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
519     auto *P = isl_printer_to_str(S->getIslCtx());
520     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
521 
522     PrintGPUUserData Data;
523     Data.PPCGProg = PPCGProg;
524 
525     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
526     Options =
527         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
528     P = isl_ast_node_print(Tree, P, Options);
529     char *String = isl_printer_get_str(P);
530     printf("# host\n");
531     printf("%s\n", String);
532     free(String);
533     isl_printer_free(P);
534 
535     for (auto Kernel : Data.Kernels) {
536       printf("# kernel%d\n", Kernel->id);
537       printKernel(Kernel);
538     }
539   }
540 
541   // Generate a GPU program using PPCG.
542   //
543   // GPU mapping consists of multiple steps:
544   //
545   //  1) Compute new schedule for the program.
546   //  2) Map schedule to GPU (TODO)
547   //  3) Generate code for new schedule (TODO)
548   //
549   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
550   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
551   // strategy directly from this pass.
552   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
553 
554     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
555 
556     PPCGGen->ctx = S->getIslCtx();
557     PPCGGen->options = PPCGScop->options;
558     PPCGGen->print = nullptr;
559     PPCGGen->print_user = nullptr;
560     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
561     PPCGGen->prog = PPCGProg;
562     PPCGGen->tree = nullptr;
563     PPCGGen->types.n = 0;
564     PPCGGen->types.name = nullptr;
565     PPCGGen->sizes = nullptr;
566     PPCGGen->used_sizes = nullptr;
567     PPCGGen->kernel_id = 0;
568 
569     // Set scheduling strategy to same strategy PPCG is using.
570     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
571     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
572 
573     isl_schedule *Schedule = get_schedule(PPCGGen);
574 
575     int has_permutable = has_any_permutable_node(Schedule);
576 
577     if (!has_permutable || has_permutable < 0) {
578       Schedule = isl_schedule_free(Schedule);
579     } else {
580       Schedule = map_to_device(PPCGGen, Schedule);
581       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
582     }
583 
584     if (DumpSchedule) {
585       isl_printer *P = isl_printer_to_str(S->getIslCtx());
586       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
587       P = isl_printer_print_str(P, "Schedule\n");
588       P = isl_printer_print_str(P, "========\n");
589       if (Schedule)
590         P = isl_printer_print_schedule(P, Schedule);
591       else
592         P = isl_printer_print_str(P, "No schedule found\n");
593 
594       printf("%s\n", isl_printer_get_str(P));
595       isl_printer_free(P);
596     }
597 
598     if (DumpCode) {
599       printf("Code\n");
600       printf("====\n");
601       if (PPCGGen->tree)
602         printGPUTree(PPCGGen->tree, PPCGProg);
603       else
604         printf("No code generated\n");
605     }
606 
607     isl_schedule_free(Schedule);
608 
609     return PPCGGen;
610   }
611 
612   /// Free gpu_gen structure.
613   ///
614   /// @param PPCGGen The ppcg_gen object to free.
615   void freePPCGGen(gpu_gen *PPCGGen) {
616     isl_ast_node_free(PPCGGen->tree);
617     isl_union_map_free(PPCGGen->sizes);
618     isl_union_map_free(PPCGGen->used_sizes);
619     free(PPCGGen);
620   }
621 
622   /// Free the options in the ppcg scop structure.
623   ///
624   /// ppcg is not freeing these options for us. To avoid leaks we do this
625   /// ourselves.
626   ///
627   /// @param PPCGScop The scop referencing the options to free.
628   void freeOptions(ppcg_scop *PPCGScop) {
629     free(PPCGScop->options->debug);
630     PPCGScop->options->debug = nullptr;
631     free(PPCGScop->options);
632     PPCGScop->options = nullptr;
633   }
634 
635   bool runOnScop(Scop &CurrentScop) override {
636     S = &CurrentScop;
637 
638     auto PPCGScop = createPPCGScop();
639     auto PPCGProg = createPPCGProg(PPCGScop);
640     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
641     freeOptions(PPCGScop);
642     freePPCGGen(PPCGGen);
643     gpu_prog_free(PPCGProg);
644     ppcg_scop_free(PPCGScop);
645 
646     return true;
647   }
648 
649   void printScop(raw_ostream &, Scop &) const override {}
650 
651   void getAnalysisUsage(AnalysisUsage &AU) const override {
652     AU.addRequired<DominatorTreeWrapperPass>();
653     AU.addRequired<RegionInfoPass>();
654     AU.addRequired<ScalarEvolutionWrapperPass>();
655     AU.addRequired<ScopDetection>();
656     AU.addRequired<ScopInfoRegionPass>();
657     AU.addRequired<LoopInfoWrapperPass>();
658 
659     AU.addPreserved<AAResultsWrapperPass>();
660     AU.addPreserved<BasicAAWrapperPass>();
661     AU.addPreserved<LoopInfoWrapperPass>();
662     AU.addPreserved<DominatorTreeWrapperPass>();
663     AU.addPreserved<GlobalsAAWrapperPass>();
664     AU.addPreserved<PostDominatorTreeWrapperPass>();
665     AU.addPreserved<ScopDetection>();
666     AU.addPreserved<ScalarEvolutionWrapperPass>();
667     AU.addPreserved<SCEVAAWrapperPass>();
668 
669     // FIXME: We do not yet add regions for the newly generated code to the
670     //        region tree.
671     AU.addPreserved<RegionInfoPass>();
672     AU.addPreserved<ScopInfoRegionPass>();
673   }
674 };
675 }
676 
677 char PPCGCodeGeneration::ID = 1;
678 
679 Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
680 
681 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
682                       "Polly - Apply PPCG translation to SCOP", false, false)
683 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
684 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
685 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
686 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
687 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
688 INITIALIZE_PASS_DEPENDENCY(ScopDetection);
689 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
690                     "Polly - Apply PPCG translation to SCOP", false, false)
691