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     return PPCGScop;
267   }
268 
269   /// Collect the array acesses in a statement.
270   ///
271   /// @param Stmt The statement for which to collect the accesses.
272   ///
273   /// @returns A list of array accesses.
274   gpu_stmt_access *getStmtAccesses(ScopStmt &Stmt) {
275     gpu_stmt_access *Accesses = nullptr;
276 
277     for (MemoryAccess *Acc : Stmt) {
278       auto Access = isl_alloc_type(S->getIslCtx(), struct gpu_stmt_access);
279       Access->read = Acc->isRead();
280       Access->write = Acc->isWrite();
281       Access->access = Acc->getAccessRelation();
282       isl_space *Space = isl_map_get_space(Access->access);
283       Space = isl_space_range(Space);
284       Space = isl_space_from_range(Space);
285       Space = isl_space_set_tuple_id(Space, isl_dim_in, Acc->getId());
286       isl_map *Universe = isl_map_universe(Space);
287       Access->tagged_access =
288           isl_map_domain_product(Acc->getAccessRelation(), Universe);
289       Access->exact_write = Acc->isWrite();
290       Access->ref_id = Acc->getId();
291       Access->next = Accesses;
292       Accesses = Access;
293     }
294 
295     return Accesses;
296   }
297 
298   /// Collect the list of GPU statements.
299   ///
300   /// Each statement has an id, a pointer to the underlying data structure,
301   /// as well as a list with all memory accesses.
302   ///
303   /// TODO: Initialize the list of memory accesses.
304   ///
305   /// @returns A linked-list of statements.
306   gpu_stmt *getStatements() {
307     gpu_stmt *Stmts = isl_calloc_array(S->getIslCtx(), struct gpu_stmt,
308                                        std::distance(S->begin(), S->end()));
309 
310     int i = 0;
311     for (auto &Stmt : *S) {
312       gpu_stmt *GPUStmt = &Stmts[i];
313 
314       GPUStmt->id = Stmt.getDomainId();
315 
316       // We use the pet stmt pointer to keep track of the Polly statements.
317       GPUStmt->stmt = (pet_stmt *)&Stmt;
318       GPUStmt->accesses = getStmtAccesses(Stmt);
319       i++;
320     }
321 
322     return Stmts;
323   }
324 
325   /// Derive the extent of an array.
326   ///
327   /// The extent of an array is defined by the set of memory locations for
328   /// which a memory access in the iteration domain exists.
329   ///
330   /// @param Array The array to derive the extent for.
331   ///
332   /// @returns An isl_set describing the extent of the array.
333   __isl_give isl_set *getExtent(ScopArrayInfo *Array) {
334     isl_union_map *Accesses = S->getAccesses();
335     Accesses = isl_union_map_intersect_domain(Accesses, S->getDomains());
336     isl_union_set *AccessUSet = isl_union_map_range(Accesses);
337     isl_set *AccessSet =
338         isl_union_set_extract_set(AccessUSet, Array->getSpace());
339     isl_union_set_free(AccessUSet);
340 
341     return AccessSet;
342   }
343 
344   /// Derive the bounds of an array.
345   ///
346   /// For the first dimension we derive the bound of the array from the extent
347   /// of this dimension. For inner dimensions we obtain their size directly from
348   /// ScopArrayInfo.
349   ///
350   /// @param PPCGArray The array to compute bounds for.
351   /// @param Array The polly array from which to take the information.
352   void setArrayBounds(gpu_array_info &PPCGArray, ScopArrayInfo *Array) {
353     if (PPCGArray.n_index > 0) {
354       isl_set *Dom = isl_set_copy(PPCGArray.extent);
355       Dom = isl_set_project_out(Dom, isl_dim_set, 1, PPCGArray.n_index - 1);
356       isl_pw_aff *Bound = isl_set_dim_max(isl_set_copy(Dom), 0);
357       isl_set_free(Dom);
358       Dom = isl_pw_aff_domain(isl_pw_aff_copy(Bound));
359       isl_local_space *LS = isl_local_space_from_space(isl_set_get_space(Dom));
360       isl_aff *One = isl_aff_zero_on_domain(LS);
361       One = isl_aff_add_constant_si(One, 1);
362       Bound = isl_pw_aff_add(Bound, isl_pw_aff_alloc(Dom, One));
363       Bound = isl_pw_aff_gist(Bound, S->getContext());
364       PPCGArray.bound[0] = Bound;
365     }
366 
367     for (unsigned i = 1; i < PPCGArray.n_index; ++i) {
368       isl_pw_aff *Bound = Array->getDimensionSizePw(i);
369       auto LS = isl_pw_aff_get_domain_space(Bound);
370       auto Aff = isl_multi_aff_zero(LS);
371       Bound = isl_pw_aff_pullback_multi_aff(Bound, Aff);
372       PPCGArray.bound[i] = Bound;
373     }
374   }
375 
376   /// Create the arrays for @p PPCGProg.
377   ///
378   /// @param PPCGProg The program to compute the arrays for.
379   void createArrays(gpu_prog *PPCGProg) {
380     int i = 0;
381     for (auto &Element : S->arrays()) {
382       ScopArrayInfo *Array = Element.second.get();
383 
384       std::string TypeName;
385       raw_string_ostream OS(TypeName);
386 
387       OS << *Array->getElementType();
388       TypeName = OS.str();
389 
390       gpu_array_info &PPCGArray = PPCGProg->array[i];
391 
392       PPCGArray.space = Array->getSpace();
393       PPCGArray.type = strdup(TypeName.c_str());
394       PPCGArray.size = Array->getElementType()->getPrimitiveSizeInBits() / 8;
395       PPCGArray.name = strdup(Array->getName().c_str());
396       PPCGArray.extent = nullptr;
397       PPCGArray.n_index = Array->getNumberOfDimensions();
398       PPCGArray.bound =
399           isl_alloc_array(S->getIslCtx(), isl_pw_aff *, PPCGArray.n_index);
400       PPCGArray.extent = getExtent(Array);
401       PPCGArray.n_ref = 0;
402       PPCGArray.refs = nullptr;
403       PPCGArray.accessed = true;
404       PPCGArray.read_only_scalar = false;
405       PPCGArray.has_compound_element = false;
406       PPCGArray.local = false;
407       PPCGArray.declare_local = false;
408       PPCGArray.global = false;
409       PPCGArray.linearize = false;
410       PPCGArray.dep_order = nullptr;
411 
412       setArrayBounds(PPCGArray, Array);
413       i++;
414     }
415   }
416 
417   /// Create an identity map between the arrays in the scop.
418   ///
419   /// @returns An identity map between the arrays in the scop.
420   isl_union_map *getArrayIdentity() {
421     isl_union_map *Maps = isl_union_map_empty(S->getParamSpace());
422 
423     for (auto &Item : S->arrays()) {
424       ScopArrayInfo *Array = Item.second.get();
425       isl_space *Space = Array->getSpace();
426       Space = isl_space_map_from_set(Space);
427       isl_map *Identity = isl_map_identity(Space);
428       Maps = isl_union_map_add_map(Maps, Identity);
429     }
430 
431     return Maps;
432   }
433 
434   /// Create a default-initialized PPCG GPU program.
435   ///
436   /// @returns A new gpu grogram description.
437   gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) {
438 
439     if (!PPCGScop)
440       return nullptr;
441 
442     auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog);
443 
444     PPCGProg->ctx = S->getIslCtx();
445     PPCGProg->scop = PPCGScop;
446     PPCGProg->context = isl_set_copy(PPCGScop->context);
447     PPCGProg->read = isl_union_map_copy(PPCGScop->reads);
448     PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes);
449     PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes);
450     PPCGProg->tagged_must_kill =
451         isl_union_map_copy(PPCGScop->tagged_must_kills);
452     PPCGProg->to_inner = getArrayIdentity();
453     PPCGProg->to_outer = getArrayIdentity();
454     PPCGProg->may_persist = compute_may_persist(PPCGProg);
455     PPCGProg->any_to_outer = nullptr;
456     PPCGProg->array_order = nullptr;
457     PPCGProg->n_stmts = std::distance(S->begin(), S->end());
458     PPCGProg->stmts = getStatements();
459     PPCGProg->n_array = std::distance(S->array_begin(), S->array_end());
460     PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info,
461                                        PPCGProg->n_array);
462 
463     createArrays(PPCGProg);
464 
465     return PPCGProg;
466   }
467 
468   struct PrintGPUUserData {
469     struct cuda_info *CudaInfo;
470     struct gpu_prog *PPCGProg;
471     std::vector<ppcg_kernel *> Kernels;
472   };
473 
474   /// Print a user statement node in the host code.
475   ///
476   /// We use ppcg's printing facilities to print the actual statement and
477   /// additionally build up a list of all kernels that are encountered in the
478   /// host ast.
479   ///
480   /// @param P The printer to print to
481   /// @param Options The printing options to use
482   /// @param Node The node to print
483   /// @param User A user pointer to carry additional data. This pointer is
484   ///             expected to be of type PrintGPUUserData.
485   ///
486   /// @returns A printer to which the output has been printed.
487   static __isl_give isl_printer *
488   printHostUser(__isl_take isl_printer *P,
489                 __isl_take isl_ast_print_options *Options,
490                 __isl_take isl_ast_node *Node, void *User) {
491     auto Data = (struct PrintGPUUserData *)User;
492     auto Id = isl_ast_node_get_annotation(Node);
493 
494     if (Id) {
495       auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id);
496       isl_id_free(Id);
497       Data->Kernels.push_back(Kernel);
498     }
499 
500     return print_host_user(P, Options, Node, User);
501   }
502 
503   /// Print C code corresponding to the control flow in @p Kernel.
504   ///
505   /// @param Kernel The kernel to print
506   void printKernel(ppcg_kernel *Kernel) {
507     auto *P = isl_printer_to_str(S->getIslCtx());
508     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
509     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
510     P = isl_ast_node_print(Kernel->tree, P, Options);
511     char *String = isl_printer_get_str(P);
512     printf("%s\n", String);
513     free(String);
514     isl_printer_free(P);
515   }
516 
517   /// Print C code corresponding to the GPU code described by @p Tree.
518   ///
519   /// @param Tree An AST describing GPU code
520   /// @param PPCGProg The PPCG program from which @Tree has been constructed.
521   void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) {
522     auto *P = isl_printer_to_str(S->getIslCtx());
523     P = isl_printer_set_output_format(P, ISL_FORMAT_C);
524 
525     PrintGPUUserData Data;
526     Data.PPCGProg = PPCGProg;
527 
528     auto *Options = isl_ast_print_options_alloc(S->getIslCtx());
529     Options =
530         isl_ast_print_options_set_print_user(Options, printHostUser, &Data);
531     P = isl_ast_node_print(Tree, P, Options);
532     char *String = isl_printer_get_str(P);
533     printf("# host\n");
534     printf("%s\n", String);
535     free(String);
536     isl_printer_free(P);
537 
538     for (auto Kernel : Data.Kernels) {
539       printf("# kernel%d\n", Kernel->id);
540       printKernel(Kernel);
541     }
542   }
543 
544   // Generate a GPU program using PPCG.
545   //
546   // GPU mapping consists of multiple steps:
547   //
548   //  1) Compute new schedule for the program.
549   //  2) Map schedule to GPU (TODO)
550   //  3) Generate code for new schedule (TODO)
551   //
552   // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer
553   // is mostly CPU specific. Instead, we use PPCG's GPU code generation
554   // strategy directly from this pass.
555   gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) {
556 
557     auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen);
558 
559     PPCGGen->ctx = S->getIslCtx();
560     PPCGGen->options = PPCGScop->options;
561     PPCGGen->print = nullptr;
562     PPCGGen->print_user = nullptr;
563     PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt;
564     PPCGGen->prog = PPCGProg;
565     PPCGGen->tree = nullptr;
566     PPCGGen->types.n = 0;
567     PPCGGen->types.name = nullptr;
568     PPCGGen->sizes = nullptr;
569     PPCGGen->used_sizes = nullptr;
570     PPCGGen->kernel_id = 0;
571 
572     // Set scheduling strategy to same strategy PPCG is using.
573     isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true);
574     isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true);
575 
576     isl_schedule *Schedule = get_schedule(PPCGGen);
577 
578     int has_permutable = has_any_permutable_node(Schedule);
579 
580     if (!has_permutable || has_permutable < 0) {
581       Schedule = isl_schedule_free(Schedule);
582     } else {
583       Schedule = map_to_device(PPCGGen, Schedule);
584       PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule));
585     }
586 
587     if (DumpSchedule) {
588       isl_printer *P = isl_printer_to_str(S->getIslCtx());
589       P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK);
590       P = isl_printer_print_str(P, "Schedule\n");
591       P = isl_printer_print_str(P, "========\n");
592       if (Schedule)
593         P = isl_printer_print_schedule(P, Schedule);
594       else
595         P = isl_printer_print_str(P, "No schedule found\n");
596 
597       printf("%s\n", isl_printer_get_str(P));
598       isl_printer_free(P);
599     }
600 
601     if (DumpCode) {
602       printf("Code\n");
603       printf("====\n");
604       if (PPCGGen->tree)
605         printGPUTree(PPCGGen->tree, PPCGProg);
606       else
607         printf("No code generated\n");
608     }
609 
610     isl_schedule_free(Schedule);
611 
612     return PPCGGen;
613   }
614 
615   /// Free gpu_gen structure.
616   ///
617   /// @param PPCGGen The ppcg_gen object to free.
618   void freePPCGGen(gpu_gen *PPCGGen) {
619     isl_ast_node_free(PPCGGen->tree);
620     isl_union_map_free(PPCGGen->sizes);
621     isl_union_map_free(PPCGGen->used_sizes);
622     free(PPCGGen);
623   }
624 
625   /// Free the options in the ppcg scop structure.
626   ///
627   /// ppcg is not freeing these options for us. To avoid leaks we do this
628   /// ourselves.
629   ///
630   /// @param PPCGScop The scop referencing the options to free.
631   void freeOptions(ppcg_scop *PPCGScop) {
632     free(PPCGScop->options->debug);
633     PPCGScop->options->debug = nullptr;
634     free(PPCGScop->options);
635     PPCGScop->options = nullptr;
636   }
637 
638   bool runOnScop(Scop &CurrentScop) override {
639     S = &CurrentScop;
640 
641     auto PPCGScop = createPPCGScop();
642     auto PPCGProg = createPPCGProg(PPCGScop);
643     auto PPCGGen = generateGPU(PPCGScop, PPCGProg);
644     freeOptions(PPCGScop);
645     freePPCGGen(PPCGGen);
646     gpu_prog_free(PPCGProg);
647     ppcg_scop_free(PPCGScop);
648 
649     return true;
650   }
651 
652   void printScop(raw_ostream &, Scop &) const override {}
653 
654   void getAnalysisUsage(AnalysisUsage &AU) const override {
655     AU.addRequired<DominatorTreeWrapperPass>();
656     AU.addRequired<RegionInfoPass>();
657     AU.addRequired<ScalarEvolutionWrapperPass>();
658     AU.addRequired<ScopDetection>();
659     AU.addRequired<ScopInfoRegionPass>();
660     AU.addRequired<LoopInfoWrapperPass>();
661 
662     AU.addPreserved<AAResultsWrapperPass>();
663     AU.addPreserved<BasicAAWrapperPass>();
664     AU.addPreserved<LoopInfoWrapperPass>();
665     AU.addPreserved<DominatorTreeWrapperPass>();
666     AU.addPreserved<GlobalsAAWrapperPass>();
667     AU.addPreserved<PostDominatorTreeWrapperPass>();
668     AU.addPreserved<ScopDetection>();
669     AU.addPreserved<ScalarEvolutionWrapperPass>();
670     AU.addPreserved<SCEVAAWrapperPass>();
671 
672     // FIXME: We do not yet add regions for the newly generated code to the
673     //        region tree.
674     AU.addPreserved<RegionInfoPass>();
675     AU.addPreserved<ScopInfoRegionPass>();
676   }
677 };
678 }
679 
680 char PPCGCodeGeneration::ID = 1;
681 
682 Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); }
683 
684 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg",
685                       "Polly - Apply PPCG translation to SCOP", false, false)
686 INITIALIZE_PASS_DEPENDENCY(DependenceInfo);
687 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass);
688 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass);
689 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass);
690 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass);
691 INITIALIZE_PASS_DEPENDENCY(ScopDetection);
692 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg",
693                     "Polly - Apply PPCG translation to SCOP", false, false)
694