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 i++; 412 } 413 } 414 415 /// Create an identity map between the arrays in the scop. 416 /// 417 /// @returns An identity map between the arrays in the scop. 418 isl_union_map *getArrayIdentity() { 419 isl_union_map *Maps = isl_union_map_empty(S->getParamSpace()); 420 421 for (auto &Item : S->arrays()) { 422 ScopArrayInfo *Array = Item.second.get(); 423 isl_space *Space = Array->getSpace(); 424 Space = isl_space_map_from_set(Space); 425 isl_map *Identity = isl_map_identity(Space); 426 Maps = isl_union_map_add_map(Maps, Identity); 427 } 428 429 return Maps; 430 } 431 432 /// Create a default-initialized PPCG GPU program. 433 /// 434 /// @returns A new gpu grogram description. 435 gpu_prog *createPPCGProg(ppcg_scop *PPCGScop) { 436 437 if (!PPCGScop) 438 return nullptr; 439 440 auto PPCGProg = isl_calloc_type(S->getIslCtx(), struct gpu_prog); 441 442 PPCGProg->ctx = S->getIslCtx(); 443 PPCGProg->scop = PPCGScop; 444 PPCGProg->context = isl_set_copy(PPCGScop->context); 445 PPCGProg->read = isl_union_map_copy(PPCGScop->reads); 446 PPCGProg->may_write = isl_union_map_copy(PPCGScop->may_writes); 447 PPCGProg->must_write = isl_union_map_copy(PPCGScop->must_writes); 448 PPCGProg->tagged_must_kill = 449 isl_union_map_copy(PPCGScop->tagged_must_kills); 450 PPCGProg->to_inner = getArrayIdentity(); 451 PPCGProg->to_outer = getArrayIdentity(); 452 PPCGProg->may_persist = compute_may_persist(PPCGProg); 453 PPCGProg->any_to_outer = nullptr; 454 PPCGProg->array_order = nullptr; 455 PPCGProg->n_stmts = std::distance(S->begin(), S->end()); 456 PPCGProg->stmts = getStatements(); 457 PPCGProg->n_array = std::distance(S->array_begin(), S->array_end()); 458 PPCGProg->array = isl_calloc_array(S->getIslCtx(), struct gpu_array_info, 459 PPCGProg->n_array); 460 461 createArrays(PPCGProg); 462 463 return PPCGProg; 464 } 465 466 struct PrintGPUUserData { 467 struct cuda_info *CudaInfo; 468 struct gpu_prog *PPCGProg; 469 std::vector<ppcg_kernel *> Kernels; 470 }; 471 472 /// Print a user statement node in the host code. 473 /// 474 /// We use ppcg's printing facilities to print the actual statement and 475 /// additionally build up a list of all kernels that are encountered in the 476 /// host ast. 477 /// 478 /// @param P The printer to print to 479 /// @param Options The printing options to use 480 /// @param Node The node to print 481 /// @param User A user pointer to carry additional data. This pointer is 482 /// expected to be of type PrintGPUUserData. 483 /// 484 /// @returns A printer to which the output has been printed. 485 static __isl_give isl_printer * 486 printHostUser(__isl_take isl_printer *P, 487 __isl_take isl_ast_print_options *Options, 488 __isl_take isl_ast_node *Node, void *User) { 489 auto Data = (struct PrintGPUUserData *)User; 490 auto Id = isl_ast_node_get_annotation(Node); 491 492 if (Id) { 493 auto Kernel = (struct ppcg_kernel *)isl_id_get_user(Id); 494 isl_id_free(Id); 495 Data->Kernels.push_back(Kernel); 496 } 497 498 return print_host_user(P, Options, Node, User); 499 } 500 501 /// Print C code corresponding to the control flow in @p Kernel. 502 /// 503 /// @param Kernel The kernel to print 504 void printKernel(ppcg_kernel *Kernel) { 505 auto *P = isl_printer_to_str(S->getIslCtx()); 506 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 507 auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 508 P = isl_ast_node_print(Kernel->tree, P, Options); 509 char *String = isl_printer_get_str(P); 510 printf("%s\n", String); 511 free(String); 512 isl_printer_free(P); 513 } 514 515 /// Print C code corresponding to the GPU code described by @p Tree. 516 /// 517 /// @param Tree An AST describing GPU code 518 /// @param PPCGProg The PPCG program from which @Tree has been constructed. 519 void printGPUTree(isl_ast_node *Tree, gpu_prog *PPCGProg) { 520 auto *P = isl_printer_to_str(S->getIslCtx()); 521 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 522 523 PrintGPUUserData Data; 524 Data.PPCGProg = PPCGProg; 525 526 auto *Options = isl_ast_print_options_alloc(S->getIslCtx()); 527 Options = 528 isl_ast_print_options_set_print_user(Options, printHostUser, &Data); 529 P = isl_ast_node_print(Tree, P, Options); 530 char *String = isl_printer_get_str(P); 531 printf("# host\n"); 532 printf("%s\n", String); 533 free(String); 534 isl_printer_free(P); 535 536 for (auto Kernel : Data.Kernels) { 537 printf("# kernel%d\n", Kernel->id); 538 printKernel(Kernel); 539 } 540 } 541 542 // Generate a GPU program using PPCG. 543 // 544 // GPU mapping consists of multiple steps: 545 // 546 // 1) Compute new schedule for the program. 547 // 2) Map schedule to GPU (TODO) 548 // 3) Generate code for new schedule (TODO) 549 // 550 // We do not use here the Polly ScheduleOptimizer, as the schedule optimizer 551 // is mostly CPU specific. Instead, we use PPCG's GPU code generation 552 // strategy directly from this pass. 553 gpu_gen *generateGPU(ppcg_scop *PPCGScop, gpu_prog *PPCGProg) { 554 555 auto PPCGGen = isl_calloc_type(S->getIslCtx(), struct gpu_gen); 556 557 PPCGGen->ctx = S->getIslCtx(); 558 PPCGGen->options = PPCGScop->options; 559 PPCGGen->print = nullptr; 560 PPCGGen->print_user = nullptr; 561 PPCGGen->build_ast_expr = &pollyBuildAstExprForStmt; 562 PPCGGen->prog = PPCGProg; 563 PPCGGen->tree = nullptr; 564 PPCGGen->types.n = 0; 565 PPCGGen->types.name = nullptr; 566 PPCGGen->sizes = nullptr; 567 PPCGGen->used_sizes = nullptr; 568 PPCGGen->kernel_id = 0; 569 570 // Set scheduling strategy to same strategy PPCG is using. 571 isl_options_set_schedule_outer_coincidence(PPCGGen->ctx, true); 572 isl_options_set_schedule_maximize_band_depth(PPCGGen->ctx, true); 573 574 isl_schedule *Schedule = get_schedule(PPCGGen); 575 576 int has_permutable = has_any_permutable_node(Schedule); 577 578 if (!has_permutable || has_permutable < 0) { 579 Schedule = isl_schedule_free(Schedule); 580 } else { 581 Schedule = map_to_device(PPCGGen, Schedule); 582 PPCGGen->tree = generate_code(PPCGGen, isl_schedule_copy(Schedule)); 583 } 584 585 if (DumpSchedule) { 586 isl_printer *P = isl_printer_to_str(S->getIslCtx()); 587 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); 588 P = isl_printer_print_str(P, "Schedule\n"); 589 P = isl_printer_print_str(P, "========\n"); 590 if (Schedule) 591 P = isl_printer_print_schedule(P, Schedule); 592 else 593 P = isl_printer_print_str(P, "No schedule found\n"); 594 595 printf("%s\n", isl_printer_get_str(P)); 596 isl_printer_free(P); 597 } 598 599 if (DumpCode) { 600 printf("Code\n"); 601 printf("====\n"); 602 if (PPCGGen->tree) 603 printGPUTree(PPCGGen->tree, PPCGProg); 604 else 605 printf("No code generated\n"); 606 } 607 608 isl_schedule_free(Schedule); 609 610 return PPCGGen; 611 } 612 613 /// Free gpu_gen structure. 614 /// 615 /// @param PPCGGen The ppcg_gen object to free. 616 void freePPCGGen(gpu_gen *PPCGGen) { 617 isl_ast_node_free(PPCGGen->tree); 618 isl_union_map_free(PPCGGen->sizes); 619 isl_union_map_free(PPCGGen->used_sizes); 620 free(PPCGGen); 621 } 622 623 /// Free the options in the ppcg scop structure. 624 /// 625 /// ppcg is not freeing these options for us. To avoid leaks we do this 626 /// ourselves. 627 /// 628 /// @param PPCGScop The scop referencing the options to free. 629 void freeOptions(ppcg_scop *PPCGScop) { 630 free(PPCGScop->options->debug); 631 PPCGScop->options->debug = nullptr; 632 free(PPCGScop->options); 633 PPCGScop->options = nullptr; 634 } 635 636 bool runOnScop(Scop &CurrentScop) override { 637 S = &CurrentScop; 638 639 auto PPCGScop = createPPCGScop(); 640 auto PPCGProg = createPPCGProg(PPCGScop); 641 auto PPCGGen = generateGPU(PPCGScop, PPCGProg); 642 freeOptions(PPCGScop); 643 freePPCGGen(PPCGGen); 644 gpu_prog_free(PPCGProg); 645 ppcg_scop_free(PPCGScop); 646 647 return true; 648 } 649 650 void printScop(raw_ostream &, Scop &) const override {} 651 652 void getAnalysisUsage(AnalysisUsage &AU) const override { 653 AU.addRequired<DominatorTreeWrapperPass>(); 654 AU.addRequired<RegionInfoPass>(); 655 AU.addRequired<ScalarEvolutionWrapperPass>(); 656 AU.addRequired<ScopDetection>(); 657 AU.addRequired<ScopInfoRegionPass>(); 658 AU.addRequired<LoopInfoWrapperPass>(); 659 660 AU.addPreserved<AAResultsWrapperPass>(); 661 AU.addPreserved<BasicAAWrapperPass>(); 662 AU.addPreserved<LoopInfoWrapperPass>(); 663 AU.addPreserved<DominatorTreeWrapperPass>(); 664 AU.addPreserved<GlobalsAAWrapperPass>(); 665 AU.addPreserved<PostDominatorTreeWrapperPass>(); 666 AU.addPreserved<ScopDetection>(); 667 AU.addPreserved<ScalarEvolutionWrapperPass>(); 668 AU.addPreserved<SCEVAAWrapperPass>(); 669 670 // FIXME: We do not yet add regions for the newly generated code to the 671 // region tree. 672 AU.addPreserved<RegionInfoPass>(); 673 AU.addPreserved<ScopInfoRegionPass>(); 674 } 675 }; 676 } 677 678 char PPCGCodeGeneration::ID = 1; 679 680 Pass *polly::createPPCGCodeGenerationPass() { return new PPCGCodeGeneration(); } 681 682 INITIALIZE_PASS_BEGIN(PPCGCodeGeneration, "polly-codegen-ppcg", 683 "Polly - Apply PPCG translation to SCOP", false, false) 684 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 685 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass); 686 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass); 687 INITIALIZE_PASS_DEPENDENCY(RegionInfoPass); 688 INITIALIZE_PASS_DEPENDENCY(ScalarEvolutionWrapperPass); 689 INITIALIZE_PASS_DEPENDENCY(ScopDetection); 690 INITIALIZE_PASS_END(PPCGCodeGeneration, "polly-codegen-ppcg", 691 "Polly - Apply PPCG translation to SCOP", false, false) 692