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           Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
157           isl_map *Universe = isl_map_universe(Space);
158           Relation = isl_map_domain_product(Relation, Universe);
159           Accesses = isl_union_map_add_map(Accesses, Relation);
160         }
161 
162     return Accesses;
163   }
164 
165   /// Get the set of all read accesses, tagged with the access id.
166   ///
167   /// @see getTaggedAccesses
168   isl_union_map *getTaggedReads() {
169     return getTaggedAccesses(MemoryAccess::READ);
170   }
171 
172   /// Get the set of all may (and must) accesses, tagged with the access id.
173   ///
174   /// @see getTaggedAccesses
175   isl_union_map *getTaggedMayWrites() {
176     return isl_union_map_union(getTaggedAccesses(MemoryAccess::MAY_WRITE),
177                                getTaggedAccesses(MemoryAccess::MUST_WRITE));
178   }
179 
180   /// Get the set of all must accesses, tagged with the access id.
181   ///
182   /// @see getTaggedAccesses
183   isl_union_map *getTaggedMustWrites() {
184     return getTaggedAccesses(MemoryAccess::MUST_WRITE);
185   }
186 
187   /// Collect parameter and array names as isl_ids.
188   ///
189   /// To reason about the different parameters and arrays used, ppcg requires
190   /// a list of all isl_ids in use. As PPCG traditionally performs
191   /// source-to-source compilation each of these isl_ids is mapped to the
192   /// expression that represents it. As we do not have a corresponding
193   /// expression in Polly, we just map each id to a 'zero' expression to match
194   /// the data format that ppcg expects.
195   ///
196   /// @returns Retun a map from collected ids to 'zero' ast expressions.
197   __isl_give isl_id_to_ast_expr *getNames() {
198     auto *Names = isl_id_to_ast_expr_alloc(
199         S->getIslCtx(),
200         S->getNumParams() + std::distance(S->array_begin(), S->array_end()));
201     auto *Zero = isl_ast_expr_from_val(isl_val_zero(S->getIslCtx()));
202     auto *Space = S->getParamSpace();
203 
204     for (int I = 0, E = S->getNumParams(); I < E; ++I) {
205       isl_id *Id = isl_space_get_dim_id(Space, isl_dim_param, I);
206       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
207     }
208 
209     for (auto &Array : S->arrays()) {
210       auto Id = Array.second->getBasePtrId();
211       Names = isl_id_to_ast_expr_set(Names, Id, isl_ast_expr_copy(Zero));
212     }
213 
214     isl_space_free(Space);
215     isl_ast_expr_free(Zero);
216 
217     return Names;
218   }
219 
220   /// Create a new PPCG scop from the current scop.
221   ///
222   /// The PPCG scop is initialized with data from the current polly::Scop. From
223   /// this initial data, the data-dependences in the PPCG scop are initialized.
224   /// We do not use Polly's dependence analysis for now, to ensure we match
225   /// the PPCG default behaviour more closely.
226   ///
227   /// @returns A new ppcg scop.
228   ppcg_scop *createPPCGScop() {
229     auto PPCGScop = (ppcg_scop *)malloc(sizeof(ppcg_scop));
230 
231     PPCGScop->options = createPPCGOptions();
232 
233     PPCGScop->start = 0;
234     PPCGScop->end = 0;
235 
236     PPCGScop->context = S->getContext();
237     PPCGScop->domain = S->getDomains();
238     PPCGScop->call = nullptr;
239     PPCGScop->tagged_reads = getTaggedReads();
240     PPCGScop->reads = S->getReads();
241     PPCGScop->live_in = nullptr;
242     PPCGScop->tagged_may_writes = getTaggedMayWrites();
243     PPCGScop->may_writes = S->getWrites();
244     PPCGScop->tagged_must_writes = getTaggedMustWrites();
245     PPCGScop->must_writes = S->getMustWrites();
246     PPCGScop->live_out = nullptr;
247     PPCGScop->tagged_must_kills = isl_union_map_empty(S->getParamSpace());
248     PPCGScop->tagger = nullptr;
249 
250     PPCGScop->independence = nullptr;
251     PPCGScop->dep_flow = nullptr;
252     PPCGScop->tagged_dep_flow = nullptr;
253     PPCGScop->dep_false = nullptr;
254     PPCGScop->dep_forced = nullptr;
255     PPCGScop->dep_order = nullptr;
256     PPCGScop->tagged_dep_order = nullptr;
257 
258     PPCGScop->schedule = S->getScheduleTree();
259     PPCGScop->names = getNames();
260 
261     PPCGScop->pet = nullptr;
262 
263     compute_tagger(PPCGScop);
264     compute_dependences(PPCGScop);
265 
266     // Remove domain constraints from flow dependences.
267     //
268     // The isl scheduler does not terminate even for some smaller cases in case
269     // domain constraints remain within these dependences.
270     //
271     // TODO: Improve the isl scheduler to not handle this case better.
272     PPCGScop->dep_flow = isl_union_map_gist_domain(
273         PPCGScop->dep_flow, isl_union_set_copy(PPCGScop->domain));
274 
275     return PPCGScop;
276   }
277 
278   /// Collect the array acesses in a statement.
279   ///
280   /// @param Stmt The statement for which to collect the accesses.
281   ///
282   /// @returns A list of array accesses.
283   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
284     gpu_stmt_access *Accesses = nullptr;
285 
286     for (MemoryAccess *Acc : Stmt) {
287       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
288       Access->read = Acc->isRead();
289       Access->write = Acc->isWrite();
290       Access->access = Acc->getAccessRelation();
291       isl_space *Space = isl_map_get_space(Access->access);
292       Space = isl_space_range(Space);
293       Space = isl_space_from_range(Space);
294       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
295       isl_map *Universe = isl_map_universe(Space);
296       Access->tagged_access =
297           isl_map_domain_product(Acc->getAccessRelation(), Universe);
298       Access->exact_write = Acc->isWrite();
299       Access->ref_id = Acc->getId();
300       Access->next = Accesses;
301       Accesses = Access;
302     }
303 
304     return Accesses;
305   }
306 
307   /// Collect the list of GPU statements.
308   ///
309   /// Each statement has an id, a pointer to the underlying data structure,
310   /// as well as a list with all memory accesses.
311   ///
312   /// TODO: Initialize the list of memory accesses.
313   ///
314   /// @returns A linked-list of statements.
315   gpu_stmt *getStatements() {
316     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
317                                        std::distance(S->begin(), S->end()));
318 
319     int i = 0;
320     for (auto &Stmt : *S) {
321       gpu_stmt *GPUStmt = &Stmts[i];
322 
323       GPUStmt->id = Stmt.getDomainId();
324 
325       // We use the pet stmt pointer to keep track of the Polly statements.
326       GPUStmt->stmt = (pet_stmt *)&Stmt;
327       GPUStmt->accesses = getStmtAccesses(Stmt);
328       i++;
329     }
330 
331     return Stmts;
332   }
333 
334   /// Derive the extent of an array.
335   ///
336   /// The extent of an array is defined by the set of memory locations for
337   /// which a memory access in the iteration domain exists.
338   ///
339   /// @param Array The array to derive the extent for.
340   ///
341   /// @returns An isl_set describing the extent of the array.
342   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
343     isl_union_map *Accesses = S->getAccesses();
344     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
345     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
346     isl_set *AccessSet =
347         isl_union_set_extract_set(AccessUSet, Array->getSpace());
348     isl_union_set_free(AccessUSet);
349 
350     return AccessSet;
351   }
352 
353   /// Derive the bounds of an array.
354   ///
355   /// For the first dimension we derive the bound of the array from the extent
356   /// of this dimension. For inner dimensions we obtain their size directly from
357   /// ScopArrayInfo.
358   ///
359   /// @param PPCGArray The array to compute bounds for.
360   /// @param Array The polly array from which to take the information.
361   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
362     if (PPCGArray.n_index > 0) {
363       isl_set *Dom = isl_set_copy(PPCGArray.extent);
364       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
365       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
366       isl_set_free(Dom);
367       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
368       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
369       isl_aff *One = isl_aff_zero_on_domain(LS);
370       One = isl_aff_add_constant_si(One, 1);
371       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
372       Bound = isl_pw_aff_gist(Bound, S->getContext());
373       PPCGArray.bound[0] = Bound;
374     }
375 
376     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
377       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
378       auto LS = isl_pw_aff_get_domain_space(Bound);
379       auto Aff = isl_multi_aff_zero(LS);
380       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
381       PPCGArray.bound[i] = Bound;
382     }
383   }
384 
385   /// Create the arrays for @p PPCGProg.
386   ///
387   /// @param PPCGProg The program to compute the arrays for.
388   void createArrays(gpu_prog *PPCGProg) {
389     int i = 0;
390     for (auto &Element : S->arrays()) {
391       ScopArrayInfo *Array = Element.second.get();
392 
393       std::string TypeName;
394       raw_string_ostream OS(TypeName);
395 
396       OS << *Array->getElementType();
397       TypeName = OS.str();
398 
399       gpu_array_info &PPCGArray = PPCGProg->array[i];
400 
401       PPCGArray.space = Array->getSpace();
402       PPCGArray.type = strdup(TypeName.c_str());
403       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
404       PPCGArray.name = strdup(Array->getName().c_str());
405       PPCGArray.extent = nullptr;
406       PPCGArray.n_index = Array->getNumberOfDimensions();
407       PPCGArray.bound =
408           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
409       PPCGArray.extent = getExtent(Array);
410       PPCGArray.n_ref = 0;
411       PPCGArray.refs = nullptr;
412       PPCGArray.accessed = true;
413       PPCGArray.read_only_scalar = false;
414       PPCGArray.has_compound_element = false;
415       PPCGArray.local = false;
416       PPCGArray.declare_local = false;
417       PPCGArray.global = false;
418       PPCGArray.linearize = false;
419       PPCGArray.dep_order = nullptr;
420 
421       setArrayBounds(PPCGArray, Array);
422       i++;
423     }
424   }
425 
426   /// Create an identity map between the arrays in the scop.
427   ///
428   /// @returns An identity map between the arrays in the scop.
429   isl_union_map *getArrayIdentity() {
430     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
431 
432     for (auto &Item : S->arrays()) {
433       ScopArrayInfo *Array = Item.second.get();
434       isl_space *Space = Array->getSpace();
435       Space = isl_space_map_from_set(Space);
436       isl_map *Identity = isl_map_identity(Space);
437       Maps = isl_union_map_add_map(Maps, Identity);
438     }
439 
440     return Maps;
441   }
442 
443   /// Create a default-initialized PPCG GPU program.
444   ///
445   /// @returns A new gpu grogram description.
446   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
447 
448     if (!PPCGScop)
449       return nullptr;
450 
451     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
452 
453     PPCGProg->ctx = S->getIslCtx();
454     PPCGProg->scop = PPCGScop;
455     PPCGProg->context = isl_set_copy(PPCGScop->context);
456     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
457     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
458     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
459     PPCGProg->tagged_must_kill =
460         isl_union_map_copy(PPCGScop->tagged_must_kills);
461     PPCGProg->to_inner = getArrayIdentity();
462     PPCGProg->to_outer = getArrayIdentity();
463     PPCGProg->may_persist = compute_may_persist(PPCGProg);
464     PPCGProg->any_to_outer = nullptr;
465     PPCGProg->array_order = nullptr;
466     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
467     PPCGProg->stmts = getStatements();
468     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
469     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
470                                        PPCGProg->n_array);
471 
472     createArrays(PPCGProg);
473 
474     return PPCGProg;
475   }
476 
477   struct PrintGPUUserData {
478     struct cuda_info *CudaInfo;
479     struct gpu_prog *PPCGProg;
480     std::vector<ppcg_kernel *> Kernels;
481   };
482 
483   /// Print a user statement node in the host code.
484   ///
485   /// We use ppcg's printing facilities to print the actual statement and
486   /// additionally build up a list of all kernels that are encountered in the
487   /// host ast.
488   ///
489   /// @param P The printer to print to
490   /// @param Options The printing options to use
491   /// @param Node The node to print
492   /// @param User A user pointer to carry additional data. This pointer is
493   ///             expected to be of type PrintGPUUserData.
494   ///
495   /// @returns A printer to which the output has been printed.
496   static __isl_give isl_printer *
497   printHostUser(__isl_take isl_printer *P,
498                 __isl_take isl_ast_print_options *Options,
499                 __isl_take isl_ast_node *Node, void *User) {
500     auto Data = (struct PrintGPUUserData *)User;
501     auto Id = isl_ast_node_get_annotation(Node);
502 
503     if (Id) {
504       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
505       isl_id_free(Id);
506       Data->Kernels.push_back(Kernel);
507     }
508 
509     return print_host_user(P, Options, Node, User);
510   }
511 
512   /// Print C code corresponding to the control flow in @p Kernel.
513   ///
514   /// @param Kernel The kernel to print
515   void printKernel(ppcg_kernel *Kernel) {
516     auto *P = isl_printer_to_str(S->getIslCtx());
517     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
518     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
519     P = isl_ast_node_print(Kernel->tree, P, Options);
520     char *String = isl_printer_get_str(P);
521     printf("%s\n", String);
522     free(String);
523     isl_printer_free(P);
524   }
525 
526   /// Print C code corresponding to the GPU code described by @p Tree.
527   ///
528   /// @param Tree An AST describing GPU code
529   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
530   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
531     auto *P = isl_printer_to_str(S->getIslCtx());
532     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
533 
534     PrintGPUUserData Data;
535     Data.PPCGProg = PPCGProg;
536 
537     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
538     Options =
539         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
540     P = isl_ast_node_print(Tree, P, Options);
541     char *String = isl_printer_get_str(P);
542     printf("# host\n");
543     printf("%s\n", String);
544     free(String);
545     isl_printer_free(P);
546 
547     for (auto Kernel : Data.Kernels) {
548       printf("# kernel%d\n", Kernel->id);
549       printKernel(Kernel);
550     }
551   }
552 
553   // Generate a GPU program using PPCG.
554   //
555   // GPU mapping consists of multiple steps:
556   //
557   //  1) Compute new schedule for the program.
558   //  2) Map schedule to GPU (TODO)
559   //  3) Generate code for new schedule (TODO)
560   //
561   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
562   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
563   // strategy directly from this pass.
564   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
565 
566     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
567 
568     PPCGGen->ctx = S->getIslCtx();
569     PPCGGen->options = PPCGScop->options;
570     PPCGGen->print = nullptr;
571     PPCGGen->print_user = nullptr;
572     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
573     PPCGGen->prog = PPCGProg;
574     PPCGGen->tree = nullptr;
575     PPCGGen->types.n = 0;
576     PPCGGen->types.name = nullptr;
577     PPCGGen->sizes = nullptr;
578     PPCGGen->used_sizes = nullptr;
579     PPCGGen->kernel_id = 0;
580 
581     // Set scheduling strategy to same strategy PPCG is using.
582     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
583     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
584 
585     isl_schedule *Schedule = get_schedule(PPCGGen);
586 
587     int has_permutable = has_any_permutable_node(Schedule);
588 
589     if (!has_permutable || has_permutable < 0) {
590       Schedule = isl_schedule_free(Schedule);
591     } else {
592       Schedule = map_to_device(PPCGGen, Schedule);
593       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
594     }
595 
596     if (DumpSchedule) {
597       isl_printer *P = isl_printer_to_str(S->getIslCtx());
598       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
599       P = isl_printer_print_str(P, "Schedule\n");
600       P = isl_printer_print_str(P, "========\n");
601       if (Schedule)
602         P = isl_printer_print_schedule(P, Schedule);
603       else
604         P = isl_printer_print_str(P, "No schedule found\n");
605 
606       printf("%s\n", isl_printer_get_str(P));
607       isl_printer_free(P);
608     }
609 
610     if (DumpCode) {
611       printf("Code\n");
612       printf("====\n");
613       if (PPCGGen->tree)
614         printGPUTree(PPCGGen->tree, PPCGProg);
615       else
616         printf("No code generated\n");
617     }
618 
619     isl_schedule_free(Schedule);
620 
621     return PPCGGen;
622   }
623 
624   /// Free gpu_gen structure.
625   ///
626   /// @param PPCGGen The ppcg_gen object to free.
627   void freePPCGGen(gpu_gen *PPCGGen) {
628     isl_ast_node_free(PPCGGen->tree);
629     isl_union_map_free(PPCGGen->sizes);
630     isl_union_map_free(PPCGGen->used_sizes);
631     free(PPCGGen);
632   }
633 
634   /// Free the options in the ppcg scop structure.
635   ///
636   /// ppcg is not freeing these options for us. To avoid leaks we do this
637   /// ourselves.
638   ///
639   /// @param PPCGScop The scop referencing the options to free.
640   void freeOptions(ppcg_scop *PPCGScop) {
641     free(PPCGScop->options->debug);
642     PPCGScop->options->debug = nullptr;
643     free(PPCGScop->options);
644     PPCGScop->options = nullptr;
645   }
646 
647   bool runOnScop(Scop &CurrentScop) override {
648     S = &CurrentScop;
649 
650     auto PPCGScop = createPPCGScop();
651     auto PPCGProg = createPPCGProg(PPCGScop);
652     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
653     freeOptions(PPCGScop);
654     freePPCGGen(PPCGGen);
655     gpu_prog_free(PPCGProg);
656     ppcg_scop_free(PPCGScop);
657 
658     return true;
659   }
660 
661   void printScop(raw_ostream &, Scop &) const override {}
662 
663   void getAnalysisUsage(AnalysisUsage &AU) const override {
664     AU.addRequired<DominatorTreeWrapperPass>();
665     AU.addRequired<RegionInfoPass>();
666     AU.addRequired<ScalarEvolutionWrapperPass>();
667     AU.addRequired<ScopDetection>();
668     AU.addRequired<ScopInfoRegionPass>();
669     AU.addRequired<LoopInfoWrapperPass>();
670 
671     AU.addPreserved<AAResultsWrapperPass>();
672     AU.addPreserved<BasicAAWrapperPass>();
673     AU.addPreserved<LoopInfoWrapperPass>();
674     AU.addPreserved<DominatorTreeWrapperPass>();
675     AU.addPreserved<GlobalsAAWrapperPass>();
676     AU.addPreserved<PostDominatorTreeWrapperPass>();
677     AU.addPreserved<ScopDetection>();
678     AU.addPreserved<ScalarEvolutionWrapperPass>();
679     AU.addPreserved<SCEVAAWrapperPass>();
680 
681     // FIXME: We do not yet add regions for the newly generated code to the
682     //        region tree.
683     AU.addPreserved<RegionInfoPass>();
684     AU.addPreserved<ScopInfoRegionPass>();
685   }
686 };
687 }
688 
689 char PPCGCodeGeneration::ID = 1;
690 
691 Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
692 
693 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
694                       "Polly - Apply PPCG translation to SCOP", false, false)
695 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
696 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
697 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
698 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
699 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
700 INITIALIZE_PASS_DEPENDENCY(ScopDetection);
701 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
702                     "Polly - Apply PPCG translation to SCOP", false, false)
703