1 //===- IslAst.cpp - isl code generator interface --------------------------===// 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 // The isl code generator interface takes a Scop and generates an isl_ast. This 11 // ist_ast can either be returned directly or it can be pretty printed to 12 // stdout. 13 // 14 // A typical isl_ast output looks like this: 15 // 16 // for (c2 = max(0, ceild(n + m, 2); c2 <= min(511, floord(5 * n, 3)); c2++) { 17 // bb2(c2); 18 // } 19 // 20 // An in-depth discussion of our AST generation approach can be found in: 21 // 22 // Polyhedral AST generation is more than scanning polyhedra 23 // Tobias Grosser, Sven Verdoolaege, Albert Cohen 24 // ACM Transations on Programming Languages and Systems (TOPLAS), 25 // 37(4), July 2015 26 // http://www.grosser.es/#pub-polyhedral-AST-generation 27 // 28 //===----------------------------------------------------------------------===// 29 30 #include "polly/CodeGen/IslAst.h" 31 #include "polly/CodeGen/CodeGeneration.h" 32 #include "polly/DependenceInfo.h" 33 #include "polly/LinkAllPasses.h" 34 #include "polly/Options.h" 35 #include "polly/ScopInfo.h" 36 #include "polly/Support/GICHelper.h" 37 #include "llvm/Analysis/RegionInfo.h" 38 #include "llvm/Support/Debug.h" 39 #include "isl/aff.h" 40 #include "isl/ast_build.h" 41 #include "isl/list.h" 42 #include "isl/map.h" 43 #include "isl/set.h" 44 #include "isl/union_map.h" 45 46 #define DEBUG_TYPE "polly-ast" 47 48 using namespace llvm; 49 using namespace polly; 50 51 using IslAstUserPayload = IslAstInfo::IslAstUserPayload; 52 53 static cl::opt<bool> 54 PollyParallel("polly-parallel", 55 cl::desc("Generate thread parallel code (isl codegen only)"), 56 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 57 58 static cl::opt<bool> PollyParallelForce( 59 "polly-parallel-force", 60 cl::desc( 61 "Force generation of thread parallel code ignoring any cost model"), 62 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 63 64 static cl::opt<bool> UseContext("polly-ast-use-context", 65 cl::desc("Use context"), cl::Hidden, 66 cl::init(false), cl::ZeroOrMore, 67 cl::cat(PollyCategory)); 68 69 static cl::opt<bool> DetectParallel("polly-ast-detect-parallel", 70 cl::desc("Detect parallelism"), cl::Hidden, 71 cl::init(false), cl::ZeroOrMore, 72 cl::cat(PollyCategory)); 73 74 namespace polly { 75 /// @brief Temporary information used when building the ast. 76 struct AstBuildUserInfo { 77 /// @brief Construct and initialize the helper struct for AST creation. 78 AstBuildUserInfo() 79 : Deps(nullptr), InParallelFor(false), LastForNodeId(nullptr) {} 80 81 /// @brief The dependence information used for the parallelism check. 82 const Dependences *Deps; 83 84 /// @brief Flag to indicate that we are inside a parallel for node. 85 bool InParallelFor; 86 87 /// @brief The last iterator id created for the current SCoP. 88 isl_id *LastForNodeId; 89 }; 90 } 91 92 /// @brief Free an IslAstUserPayload object pointed to by @p Ptr 93 static void freeIslAstUserPayload(void *Ptr) { 94 delete ((IslAstInfo::IslAstUserPayload *)Ptr); 95 } 96 97 IslAstInfo::IslAstUserPayload::~IslAstUserPayload() { 98 isl_ast_build_free(Build); 99 isl_pw_aff_free(MinimalDependenceDistance); 100 } 101 102 /// @brief Print a string @p str in a single line using @p Printer. 103 static isl_printer *printLine(__isl_take isl_printer *Printer, 104 const std::string &str, 105 __isl_keep isl_pw_aff *PWA = nullptr) { 106 Printer = isl_printer_start_line(Printer); 107 Printer = isl_printer_print_str(Printer, str.c_str()); 108 if (PWA) 109 Printer = isl_printer_print_pw_aff(Printer, PWA); 110 return isl_printer_end_line(Printer); 111 } 112 113 /// @brief Return all broken reductions as a string of clauses (OpenMP style). 114 static const std::string getBrokenReductionsStr(__isl_keep isl_ast_node *Node) { 115 IslAstInfo::MemoryAccessSet *BrokenReductions; 116 std::string str; 117 118 BrokenReductions = IslAstInfo::getBrokenReductions(Node); 119 if (!BrokenReductions || BrokenReductions->empty()) 120 return ""; 121 122 // Map each type of reduction to a comma separated list of the base addresses. 123 std::map<MemoryAccess::ReductionType, std::string> Clauses; 124 for (MemoryAccess *MA : *BrokenReductions) 125 if (MA->isWrite()) 126 Clauses[MA->getReductionType()] += 127 ", " + MA->getBaseAddr()->getName().str(); 128 129 // Now print the reductions sorted by type. Each type will cause a clause 130 // like: reduction (+ : sum0, sum1, sum2) 131 for (const auto &ReductionClause : Clauses) { 132 str += " reduction ("; 133 str += MemoryAccess::getReductionOperatorStr(ReductionClause.first); 134 // Remove the first two symbols (", ") to make the output look pretty. 135 str += " : " + ReductionClause.second.substr(2) + ")"; 136 } 137 138 return str; 139 } 140 141 /// @brief Callback executed for each for node in the ast in order to print it. 142 static isl_printer *cbPrintFor(__isl_take isl_printer *Printer, 143 __isl_take isl_ast_print_options *Options, 144 __isl_keep isl_ast_node *Node, void *) { 145 146 isl_pw_aff *DD = IslAstInfo::getMinimalDependenceDistance(Node); 147 const std::string BrokenReductionsStr = getBrokenReductionsStr(Node); 148 const std::string KnownParallelStr = "#pragma known-parallel"; 149 const std::string DepDisPragmaStr = "#pragma minimal dependence distance: "; 150 const std::string SimdPragmaStr = "#pragma simd"; 151 const std::string OmpPragmaStr = "#pragma omp parallel for"; 152 153 if (DD) 154 Printer = printLine(Printer, DepDisPragmaStr, DD); 155 156 if (IslAstInfo::isInnermostParallel(Node)) 157 Printer = printLine(Printer, SimdPragmaStr + BrokenReductionsStr); 158 159 if (IslAstInfo::isExecutedInParallel(Node)) 160 Printer = printLine(Printer, OmpPragmaStr); 161 else if (IslAstInfo::isOutermostParallel(Node)) 162 Printer = printLine(Printer, KnownParallelStr + BrokenReductionsStr); 163 164 isl_pw_aff_free(DD); 165 return isl_ast_node_for_print(Node, Printer, Options); 166 } 167 168 /// @brief Check if the current scheduling dimension is parallel 169 /// 170 /// In case the dimension is parallel we also check if any reduction 171 /// dependences is broken when we exploit this parallelism. If so, 172 /// @p IsReductionParallel will be set to true. The reduction dependences we use 173 /// to check are actually the union of the transitive closure of the initial 174 /// reduction dependences together with their reveresal. Even though these 175 /// dependences connect all iterations with each other (thus they are cyclic) 176 /// we can perform the parallelism check as we are only interested in a zero 177 /// (or non-zero) dependence distance on the dimension in question. 178 static bool astScheduleDimIsParallel(__isl_keep isl_ast_build *Build, 179 const Dependences *D, 180 IslAstUserPayload *NodeInfo) { 181 if (!D->hasValidDependences()) 182 return false; 183 184 isl_union_map *Schedule = isl_ast_build_get_schedule(Build); 185 isl_union_map *Deps = D->getDependences( 186 Dependences::TYPE_RAW | Dependences::TYPE_WAW | Dependences::TYPE_WAR); 187 188 if (!D->isParallel(Schedule, Deps, &NodeInfo->MinimalDependenceDistance) && 189 !isl_union_map_free(Schedule)) 190 return false; 191 192 isl_union_map *RedDeps = D->getDependences(Dependences::TYPE_TC_RED); 193 if (!D->isParallel(Schedule, RedDeps)) 194 NodeInfo->IsReductionParallel = true; 195 196 if (!NodeInfo->IsReductionParallel && !isl_union_map_free(Schedule)) 197 return true; 198 199 // Annotate reduction parallel nodes with the memory accesses which caused the 200 // reduction dependences parallel execution of the node conflicts with. 201 for (const auto &MaRedPair : D->getReductionDependences()) { 202 if (!MaRedPair.second) 203 continue; 204 RedDeps = isl_union_map_from_map(isl_map_copy(MaRedPair.second)); 205 if (!D->isParallel(Schedule, RedDeps)) 206 NodeInfo->BrokenReductions.insert(MaRedPair.first); 207 } 208 209 isl_union_map_free(Schedule); 210 return true; 211 } 212 213 // This method is executed before the construction of a for node. It creates 214 // an isl_id that is used to annotate the subsequently generated ast for nodes. 215 // 216 // In this function we also run the following analyses: 217 // 218 // - Detection of openmp parallel loops 219 // 220 static __isl_give isl_id *astBuildBeforeFor(__isl_keep isl_ast_build *Build, 221 void *User) { 222 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User; 223 IslAstUserPayload *Payload = new IslAstUserPayload(); 224 isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload); 225 Id = isl_id_set_free_user(Id, freeIslAstUserPayload); 226 BuildInfo->LastForNodeId = Id; 227 228 // Test for parallelism only if we are not already inside a parallel loop 229 if (!BuildInfo->InParallelFor) 230 BuildInfo->InParallelFor = Payload->IsOutermostParallel = 231 astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload); 232 233 return Id; 234 } 235 236 // This method is executed after the construction of a for node. 237 // 238 // It performs the following actions: 239 // 240 // - Reset the 'InParallelFor' flag, as soon as we leave a for node, 241 // that is marked as openmp parallel. 242 // 243 static __isl_give isl_ast_node * 244 astBuildAfterFor(__isl_take isl_ast_node *Node, __isl_keep isl_ast_build *Build, 245 void *User) { 246 isl_id *Id = isl_ast_node_get_annotation(Node); 247 assert(Id && "Post order visit assumes annotated for nodes"); 248 IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id); 249 assert(Payload && "Post order visit assumes annotated for nodes"); 250 251 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User; 252 assert(!Payload->Build && "Build environment already set"); 253 Payload->Build = isl_ast_build_copy(Build); 254 Payload->IsInnermost = (Id == BuildInfo->LastForNodeId); 255 256 // Innermost loops that are surrounded by parallel loops have not yet been 257 // tested for parallelism. Test them here to ensure we check all innermost 258 // loops for parallelism. 259 if (Payload->IsInnermost && BuildInfo->InParallelFor) { 260 if (Payload->IsOutermostParallel) { 261 Payload->IsInnermostParallel = true; 262 } else { 263 if (PollyVectorizerChoice == VECTORIZER_NONE) 264 Payload->IsInnermostParallel = 265 astScheduleDimIsParallel(Build, BuildInfo->Deps, Payload); 266 } 267 } 268 if (Payload->IsOutermostParallel) 269 BuildInfo->InParallelFor = false; 270 271 isl_id_free(Id); 272 return Node; 273 } 274 275 static isl_stat astBuildBeforeMark(__isl_keep isl_id *MarkId, 276 __isl_keep isl_ast_build *Build, 277 void *User) { 278 if (!MarkId) 279 return isl_stat_error; 280 281 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User; 282 if (!strcmp(isl_id_get_name(MarkId), "SIMD")) 283 BuildInfo->InParallelFor = true; 284 285 return isl_stat_ok; 286 } 287 288 static __isl_give isl_ast_node * 289 astBuildAfterMark(__isl_take isl_ast_node *Node, 290 __isl_keep isl_ast_build *Build, void *User) { 291 assert(isl_ast_node_get_type(Node) == isl_ast_node_mark); 292 AstBuildUserInfo *BuildInfo = (AstBuildUserInfo *)User; 293 auto *Id = isl_ast_node_mark_get_id(Node); 294 if (!strcmp(isl_id_get_name(Id), "SIMD")) 295 BuildInfo->InParallelFor = false; 296 isl_id_free(Id); 297 return Node; 298 } 299 300 static __isl_give isl_ast_node *AtEachDomain(__isl_take isl_ast_node *Node, 301 __isl_keep isl_ast_build *Build, 302 void *User) { 303 assert(!isl_ast_node_get_annotation(Node) && "Node already annotated"); 304 305 IslAstUserPayload *Payload = new IslAstUserPayload(); 306 isl_id *Id = isl_id_alloc(isl_ast_build_get_ctx(Build), "", Payload); 307 Id = isl_id_set_free_user(Id, freeIslAstUserPayload); 308 309 Payload->Build = isl_ast_build_copy(Build); 310 311 return isl_ast_node_set_annotation(Node, Id); 312 } 313 314 // Build alias check condition given a pair of minimal/maximal access. 315 static __isl_give isl_ast_expr * 316 buildCondition(__isl_keep isl_ast_build *Build, const Scop::MinMaxAccessTy *It0, 317 const Scop::MinMaxAccessTy *It1) { 318 isl_ast_expr *NonAliasGroup, *MinExpr, *MaxExpr; 319 MinExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff( 320 Build, isl_pw_multi_aff_copy(It0->first))); 321 MaxExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff( 322 Build, isl_pw_multi_aff_copy(It1->second))); 323 NonAliasGroup = isl_ast_expr_le(MaxExpr, MinExpr); 324 MinExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff( 325 Build, isl_pw_multi_aff_copy(It1->first))); 326 MaxExpr = isl_ast_expr_address_of(isl_ast_build_access_from_pw_multi_aff( 327 Build, isl_pw_multi_aff_copy(It0->second))); 328 NonAliasGroup = 329 isl_ast_expr_or(NonAliasGroup, isl_ast_expr_le(MaxExpr, MinExpr)); 330 331 return NonAliasGroup; 332 } 333 334 __isl_give isl_ast_expr *IslAst::buildRunCondition() { 335 isl_ast_expr *RunCondition; 336 337 // The conditions that need to be checked at run-time for this scop are 338 // available as an isl_set in the runtime check context from which we can 339 // directly derive a run-time condition. 340 auto *PosCond = isl_ast_build_expr_from_set(Build, S->getAssumedContext()); 341 if (S->hasTrivialInvalidContext()) { 342 RunCondition = PosCond; 343 } else { 344 auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build)); 345 auto *NegCond = isl_ast_build_expr_from_set(Build, S->getInvalidContext()); 346 auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond); 347 RunCondition = isl_ast_expr_and(PosCond, NotNegCond); 348 } 349 350 // Create the alias checks from the minimal/maximal accesses in each alias 351 // group which consists of read only and non read only (read write) accesses. 352 // This operation is by construction quadratic in the read-write pointers and 353 // linear int the read only pointers in each alias group. 354 for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S->getAliasGroups()) { 355 auto &MinMaxReadWrite = MinMaxAccessPair.first; 356 auto &MinMaxReadOnly = MinMaxAccessPair.second; 357 auto RWAccEnd = MinMaxReadWrite.end(); 358 359 for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd; 360 ++RWAccIt0) { 361 for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1) 362 RunCondition = isl_ast_expr_and( 363 RunCondition, buildCondition(Build, RWAccIt0, RWAccIt1)); 364 for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly) 365 RunCondition = isl_ast_expr_and( 366 RunCondition, buildCondition(Build, RWAccIt0, &ROAccIt)); 367 } 368 } 369 370 return RunCondition; 371 } 372 373 /// @brief Simple cost analysis for a given SCoP 374 /// 375 /// TODO: Improve this analysis and extract it to make it usable in other 376 /// places too. 377 /// In order to improve the cost model we could either keep track of 378 /// performed optimizations (e.g., tiling) or compute properties on the 379 /// original as well as optimized SCoP (e.g., #stride-one-accesses). 380 static bool benefitsFromPolly(Scop *Scop, bool PerformParallelTest) { 381 382 if (PollyProcessUnprofitable) 383 return true; 384 385 // Check if nothing interesting happened. 386 if (!PerformParallelTest && !Scop->isOptimized() && 387 Scop->getAliasGroups().empty()) 388 return false; 389 390 // The default assumption is that Polly improves the code. 391 return true; 392 } 393 394 IslAst::IslAst(Scop *Scop) 395 : S(Scop), Root(nullptr), Build(nullptr), Ctx(Scop->getSharedIslCtx()) {} 396 397 void IslAst::init(const Dependences &D) { 398 bool PerformParallelTest = PollyParallel || DetectParallel || 399 PollyVectorizerChoice != VECTORIZER_NONE; 400 401 // Skip AST and code generation if there was no benefit achieved. 402 if (!benefitsFromPolly(S, PerformParallelTest)) 403 return; 404 405 isl_ctx *Ctx = S->getIslCtx(); 406 isl_options_set_ast_build_atomic_upper_bound(Ctx, true); 407 isl_options_set_ast_build_detect_min_max(Ctx, true); 408 AstBuildUserInfo BuildInfo; 409 410 if (UseContext) 411 Build = isl_ast_build_from_context(S->getContext()); 412 else 413 Build = isl_ast_build_from_context(isl_set_universe(S->getParamSpace())); 414 415 Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr); 416 417 if (PerformParallelTest) { 418 BuildInfo.Deps = &D; 419 BuildInfo.InParallelFor = 0; 420 421 Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor, 422 &BuildInfo); 423 Build = 424 isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo); 425 426 Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark, 427 &BuildInfo); 428 429 Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark, 430 &BuildInfo); 431 } 432 433 Root = isl_ast_build_node_from_schedule(Build, S->getScheduleTree()); 434 } 435 436 IslAst *IslAst::create(Scop *Scop, const Dependences &D) { 437 auto Ast = new IslAst(Scop); 438 Ast->init(D); 439 return Ast; 440 } 441 442 IslAst::~IslAst() { 443 isl_ast_node_free(Root); 444 isl_ast_build_free(Build); 445 } 446 447 __isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); } 448 449 void IslAstInfo::releaseMemory() { 450 if (Ast) { 451 delete Ast; 452 Ast = nullptr; 453 } 454 } 455 456 bool IslAstInfo::runOnScop(Scop &Scop) { 457 if (Ast) 458 delete Ast; 459 460 S = &Scop; 461 462 const Dependences &D = 463 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); 464 465 Ast = IslAst::create(&Scop, D); 466 467 DEBUG(printScop(dbgs(), Scop)); 468 return false; 469 } 470 471 __isl_give isl_ast_node *IslAstInfo::getAst() const { return Ast->getAst(); } 472 __isl_give isl_ast_expr *IslAstInfo::getRunCondition() const { 473 return Ast->buildRunCondition(); 474 } 475 476 IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) { 477 isl_id *Id = isl_ast_node_get_annotation(Node); 478 if (!Id) 479 return nullptr; 480 IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id); 481 isl_id_free(Id); 482 return Payload; 483 } 484 485 bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) { 486 IslAstUserPayload *Payload = getNodePayload(Node); 487 return Payload && Payload->IsInnermost; 488 } 489 490 bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) { 491 return IslAstInfo::isInnermostParallel(Node) || 492 IslAstInfo::isOutermostParallel(Node); 493 } 494 495 bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) { 496 IslAstUserPayload *Payload = getNodePayload(Node); 497 return Payload && Payload->IsInnermostParallel; 498 } 499 500 bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) { 501 IslAstUserPayload *Payload = getNodePayload(Node); 502 return Payload && Payload->IsOutermostParallel; 503 } 504 505 bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) { 506 IslAstUserPayload *Payload = getNodePayload(Node); 507 return Payload && Payload->IsReductionParallel; 508 } 509 510 bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) { 511 512 if (!PollyParallel) 513 return false; 514 515 // Do not parallelize innermost loops. 516 // 517 // Parallelizing innermost loops is often not profitable, especially if 518 // they have a low number of iterations. 519 // 520 // TODO: Decide this based on the number of loop iterations that will be 521 // executed. This can possibly require run-time checks, which again 522 // raises the question of both run-time check overhead and code size 523 // costs. 524 if (!PollyParallelForce && isInnermost(Node)) 525 return false; 526 527 return isOutermostParallel(Node) && !isReductionParallel(Node); 528 } 529 530 __isl_give isl_union_map * 531 IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) { 532 IslAstUserPayload *Payload = getNodePayload(Node); 533 return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr; 534 } 535 536 __isl_give isl_pw_aff * 537 IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) { 538 IslAstUserPayload *Payload = getNodePayload(Node); 539 return Payload ? isl_pw_aff_copy(Payload->MinimalDependenceDistance) 540 : nullptr; 541 } 542 543 IslAstInfo::MemoryAccessSet * 544 IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) { 545 IslAstUserPayload *Payload = getNodePayload(Node); 546 return Payload ? &Payload->BrokenReductions : nullptr; 547 } 548 549 isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) { 550 IslAstUserPayload *Payload = getNodePayload(Node); 551 return Payload ? Payload->Build : nullptr; 552 } 553 554 void IslAstInfo::printScop(raw_ostream &OS, Scop &S) const { 555 isl_ast_print_options *Options; 556 isl_ast_node *RootNode = getAst(); 557 Function &F = S.getFunction(); 558 559 OS << ":: isl ast :: " << F.getName() << " :: " << S.getNameStr() << "\n"; 560 561 if (!RootNode) { 562 OS << ":: isl ast generation and code generation was skipped!\n\n"; 563 OS << ":: This is either because no useful optimizations could be applied " 564 "(use -polly-process-unprofitable to enforce code generation) or " 565 "because earlier passes such as dependence analysis timed out (use " 566 "-polly-dependences-computeout=0 to set dependence analysis timeout " 567 "to infinity)\n\n"; 568 return; 569 } 570 571 isl_ast_expr *RunCondition = getRunCondition(); 572 char *RtCStr, *AstStr; 573 574 Options = isl_ast_print_options_alloc(S.getIslCtx()); 575 Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr); 576 577 isl_printer *P = isl_printer_to_str(S.getIslCtx()); 578 P = isl_printer_print_ast_expr(P, RunCondition); 579 RtCStr = isl_printer_get_str(P); 580 P = isl_printer_flush(P); 581 P = isl_printer_indent(P, 4); 582 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 583 P = isl_ast_node_print(RootNode, P, Options); 584 AstStr = isl_printer_get_str(P); 585 586 isl_union_map *Schedule = 587 isl_union_map_intersect_domain(S.getSchedule(), S.getDomains()); 588 589 DEBUG({ 590 dbgs() << S.getContextStr() << "\n"; 591 dbgs() << stringFromIslObj(Schedule); 592 }); 593 OS << "\nif (" << RtCStr << ")\n\n"; 594 OS << AstStr << "\n"; 595 OS << "else\n"; 596 OS << " { /* original code */ }\n\n"; 597 598 free(RtCStr); 599 free(AstStr); 600 601 isl_ast_expr_free(RunCondition); 602 isl_union_map_free(Schedule); 603 isl_ast_node_free(RootNode); 604 isl_printer_free(P); 605 } 606 607 void IslAstInfo::getAnalysisUsage(AnalysisUsage &AU) const { 608 // Get the Common analysis usage of ScopPasses. 609 ScopPass::getAnalysisUsage(AU); 610 AU.addRequired<ScopInfoRegionPass>(); 611 AU.addRequired<DependenceInfo>(); 612 } 613 614 char IslAstInfo::ID = 0; 615 616 Pass *polly::createIslAstInfoPass() { return new IslAstInfo(); } 617 618 INITIALIZE_PASS_BEGIN(IslAstInfo, "polly-ast", 619 "Polly - Generate an AST of the SCoP (isl)", false, 620 false); 621 INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass); 622 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 623 INITIALIZE_PASS_END(IslAstInfo, "polly-ast", 624 "Polly - Generate an AST from the SCoP (isl)", false, false) 625