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 Transactions 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(true), 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 /// Temporary information used when building the ast. 76 struct AstBuildUserInfo { 77 /// Construct and initialize the helper struct for AST creation. 78 AstBuildUserInfo() 79 : Deps(nullptr), InParallelFor(false), LastForNodeId(nullptr) {} 80 81 /// The dependence information used for the parallelism check. 82 const Dependences *Deps; 83 84 /// Flag to indicate that we are inside a parallel for node. 85 bool InParallelFor; 86 87 /// The last iterator id created for the current SCoP. 88 isl_id *LastForNodeId; 89 }; 90 } // namespace polly 91 92 /// 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 /// 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 /// 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->getScopArrayInfo()->getName(); 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 /// 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 /// 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 reversal. 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 * 335 IslAst::buildRunCondition(Scop &S, __isl_keep isl_ast_build *Build) { 336 isl_ast_expr *RunCondition; 337 338 // The conditions that need to be checked at run-time for this scop are 339 // available as an isl_set in the runtime check context from which we can 340 // directly derive a run-time condition. 341 auto *PosCond = isl_ast_build_expr_from_set(Build, S.getAssumedContext()); 342 if (S.hasTrivialInvalidContext()) { 343 RunCondition = PosCond; 344 } else { 345 auto *ZeroV = isl_val_zero(isl_ast_build_get_ctx(Build)); 346 auto *NegCond = isl_ast_build_expr_from_set(Build, S.getInvalidContext()); 347 auto *NotNegCond = isl_ast_expr_eq(isl_ast_expr_from_val(ZeroV), NegCond); 348 RunCondition = isl_ast_expr_and(PosCond, NotNegCond); 349 } 350 351 // Create the alias checks from the minimal/maximal accesses in each alias 352 // group which consists of read only and non read only (read write) accesses. 353 // This operation is by construction quadratic in the read-write pointers and 354 // linear in the read only pointers in each alias group. 355 for (const Scop::MinMaxVectorPairTy &MinMaxAccessPair : S.getAliasGroups()) { 356 auto &MinMaxReadWrite = MinMaxAccessPair.first; 357 auto &MinMaxReadOnly = MinMaxAccessPair.second; 358 auto RWAccEnd = MinMaxReadWrite.end(); 359 360 for (auto RWAccIt0 = MinMaxReadWrite.begin(); RWAccIt0 != RWAccEnd; 361 ++RWAccIt0) { 362 for (auto RWAccIt1 = RWAccIt0 + 1; RWAccIt1 != RWAccEnd; ++RWAccIt1) 363 RunCondition = isl_ast_expr_and( 364 RunCondition, buildCondition(Build, RWAccIt0, RWAccIt1)); 365 for (const Scop::MinMaxAccessTy &ROAccIt : MinMaxReadOnly) 366 RunCondition = isl_ast_expr_and( 367 RunCondition, buildCondition(Build, RWAccIt0, &ROAccIt)); 368 } 369 } 370 371 return RunCondition; 372 } 373 374 /// Simple cost analysis for a given SCoP. 375 /// 376 /// TODO: Improve this analysis and extract it to make it usable in other 377 /// places too. 378 /// In order to improve the cost model we could either keep track of 379 /// performed optimizations (e.g., tiling) or compute properties on the 380 /// original as well as optimized SCoP (e.g., #stride-one-accesses). 381 static bool benefitsFromPolly(Scop &Scop, bool PerformParallelTest) { 382 383 if (PollyProcessUnprofitable) 384 return true; 385 386 // Check if nothing interesting happened. 387 if (!PerformParallelTest && !Scop.isOptimized() && 388 Scop.getAliasGroups().empty()) 389 return false; 390 391 // The default assumption is that Polly improves the code. 392 return true; 393 } 394 395 IslAst::IslAst(Scop &Scop) 396 : S(Scop), Root(nullptr), RunCondition(nullptr), 397 Ctx(Scop.getSharedIslCtx()) {} 398 399 void IslAst::init(const Dependences &D) { 400 bool PerformParallelTest = PollyParallel || DetectParallel || 401 PollyVectorizerChoice != VECTORIZER_NONE; 402 403 // We can not perform the dependence analysis and, consequently, 404 // the parallel code generation in case the schedule tree contains 405 // extension nodes. 406 auto *ScheduleTree = S.getScheduleTree(); 407 PerformParallelTest = 408 PerformParallelTest && !S.containsExtensionNode(ScheduleTree); 409 isl_schedule_free(ScheduleTree); 410 411 // Skip AST and code generation if there was no benefit achieved. 412 if (!benefitsFromPolly(S, PerformParallelTest)) 413 return; 414 415 isl_ctx *Ctx = S.getIslCtx(); 416 isl_options_set_ast_build_atomic_upper_bound(Ctx, true); 417 isl_options_set_ast_build_detect_min_max(Ctx, true); 418 isl_ast_build *Build; 419 AstBuildUserInfo BuildInfo; 420 421 if (UseContext) 422 Build = isl_ast_build_from_context(S.getContext()); 423 else 424 Build = isl_ast_build_from_context(isl_set_universe(S.getParamSpace())); 425 426 Build = isl_ast_build_set_at_each_domain(Build, AtEachDomain, nullptr); 427 428 if (PerformParallelTest) { 429 BuildInfo.Deps = &D; 430 BuildInfo.InParallelFor = 0; 431 432 Build = isl_ast_build_set_before_each_for(Build, &astBuildBeforeFor, 433 &BuildInfo); 434 Build = 435 isl_ast_build_set_after_each_for(Build, &astBuildAfterFor, &BuildInfo); 436 437 Build = isl_ast_build_set_before_each_mark(Build, &astBuildBeforeMark, 438 &BuildInfo); 439 440 Build = isl_ast_build_set_after_each_mark(Build, &astBuildAfterMark, 441 &BuildInfo); 442 } 443 444 RunCondition = buildRunCondition(S, Build); 445 446 Root = isl_ast_build_node_from_schedule(Build, S.getScheduleTree()); 447 448 isl_ast_build_free(Build); 449 } 450 451 IslAst IslAst::create(Scop &Scop, const Dependences &D) { 452 IslAst Ast{Scop}; 453 Ast.init(D); 454 return Ast; 455 } 456 457 IslAst::IslAst(IslAst &&O) 458 : S(O.S), Root(O.Root), RunCondition(O.RunCondition), Ctx(O.Ctx) { 459 O.Root = nullptr; 460 O.RunCondition = nullptr; 461 } 462 463 IslAst::~IslAst() { 464 isl_ast_node_free(Root); 465 isl_ast_expr_free(RunCondition); 466 } 467 468 __isl_give isl_ast_node *IslAst::getAst() { return isl_ast_node_copy(Root); } 469 __isl_give isl_ast_expr *IslAst::getRunCondition() { 470 return isl_ast_expr_copy(RunCondition); 471 } 472 473 __isl_give isl_ast_node *IslAstInfo::getAst() { return Ast.getAst(); } 474 __isl_give isl_ast_expr *IslAstInfo::getRunCondition() { 475 return Ast.getRunCondition(); 476 } 477 478 IslAstUserPayload *IslAstInfo::getNodePayload(__isl_keep isl_ast_node *Node) { 479 isl_id *Id = isl_ast_node_get_annotation(Node); 480 if (!Id) 481 return nullptr; 482 IslAstUserPayload *Payload = (IslAstUserPayload *)isl_id_get_user(Id); 483 isl_id_free(Id); 484 return Payload; 485 } 486 487 bool IslAstInfo::isInnermost(__isl_keep isl_ast_node *Node) { 488 IslAstUserPayload *Payload = getNodePayload(Node); 489 return Payload && Payload->IsInnermost; 490 } 491 492 bool IslAstInfo::isParallel(__isl_keep isl_ast_node *Node) { 493 return IslAstInfo::isInnermostParallel(Node) || 494 IslAstInfo::isOutermostParallel(Node); 495 } 496 497 bool IslAstInfo::isInnermostParallel(__isl_keep isl_ast_node *Node) { 498 IslAstUserPayload *Payload = getNodePayload(Node); 499 return Payload && Payload->IsInnermostParallel; 500 } 501 502 bool IslAstInfo::isOutermostParallel(__isl_keep isl_ast_node *Node) { 503 IslAstUserPayload *Payload = getNodePayload(Node); 504 return Payload && Payload->IsOutermostParallel; 505 } 506 507 bool IslAstInfo::isReductionParallel(__isl_keep isl_ast_node *Node) { 508 IslAstUserPayload *Payload = getNodePayload(Node); 509 return Payload && Payload->IsReductionParallel; 510 } 511 512 bool IslAstInfo::isExecutedInParallel(__isl_keep isl_ast_node *Node) { 513 514 if (!PollyParallel) 515 return false; 516 517 // Do not parallelize innermost loops. 518 // 519 // Parallelizing innermost loops is often not profitable, especially if 520 // they have a low number of iterations. 521 // 522 // TODO: Decide this based on the number of loop iterations that will be 523 // executed. This can possibly require run-time checks, which again 524 // raises the question of both run-time check overhead and code size 525 // costs. 526 if (!PollyParallelForce && isInnermost(Node)) 527 return false; 528 529 return isOutermostParallel(Node) && !isReductionParallel(Node); 530 } 531 532 __isl_give isl_union_map * 533 IslAstInfo::getSchedule(__isl_keep isl_ast_node *Node) { 534 IslAstUserPayload *Payload = getNodePayload(Node); 535 return Payload ? isl_ast_build_get_schedule(Payload->Build) : nullptr; 536 } 537 538 __isl_give isl_pw_aff * 539 IslAstInfo::getMinimalDependenceDistance(__isl_keep isl_ast_node *Node) { 540 IslAstUserPayload *Payload = getNodePayload(Node); 541 return Payload ? isl_pw_aff_copy(Payload->MinimalDependenceDistance) 542 : nullptr; 543 } 544 545 IslAstInfo::MemoryAccessSet * 546 IslAstInfo::getBrokenReductions(__isl_keep isl_ast_node *Node) { 547 IslAstUserPayload *Payload = getNodePayload(Node); 548 return Payload ? &Payload->BrokenReductions : nullptr; 549 } 550 551 isl_ast_build *IslAstInfo::getBuild(__isl_keep isl_ast_node *Node) { 552 IslAstUserPayload *Payload = getNodePayload(Node); 553 return Payload ? Payload->Build : nullptr; 554 } 555 556 IslAstInfo IslAstAnalysis::run(Scop &S, ScopAnalysisManager &SAM, 557 ScopStandardAnalysisResults &SAR) { 558 return {S, SAM.getResult<DependenceAnalysis>(S, SAR).getDependences( 559 Dependences::AL_Statement)}; 560 } 561 562 void IslAstInfo::print(raw_ostream &OS) { 563 isl_ast_print_options *Options; 564 isl_ast_node *RootNode = Ast.getAst(); 565 Function &F = S.getFunction(); 566 567 OS << ":: isl ast :: " << F.getName() << " :: " << S.getNameStr() << "\n"; 568 569 if (!RootNode) { 570 OS << ":: isl ast generation and code generation was skipped!\n\n"; 571 OS << ":: This is either because no useful optimizations could be applied " 572 "(use -polly-process-unprofitable to enforce code generation) or " 573 "because earlier passes such as dependence analysis timed out (use " 574 "-polly-dependences-computeout=0 to set dependence analysis timeout " 575 "to infinity)\n\n"; 576 return; 577 } 578 579 isl_ast_expr *RunCondition = Ast.getRunCondition(); 580 char *RtCStr, *AstStr; 581 582 Options = isl_ast_print_options_alloc(S.getIslCtx()); 583 Options = isl_ast_print_options_set_print_for(Options, cbPrintFor, nullptr); 584 585 isl_printer *P = isl_printer_to_str(S.getIslCtx()); 586 P = isl_printer_set_output_format(P, ISL_FORMAT_C); 587 P = isl_printer_print_ast_expr(P, RunCondition); 588 RtCStr = isl_printer_get_str(P); 589 P = isl_printer_flush(P); 590 P = isl_printer_indent(P, 4); 591 P = isl_ast_node_print(RootNode, P, Options); 592 AstStr = isl_printer_get_str(P); 593 594 auto *Schedule = S.getScheduleTree(); 595 596 DEBUG({ 597 dbgs() << S.getContextStr() << "\n"; 598 dbgs() << stringFromIslObj(Schedule); 599 }); 600 OS << "\nif (" << RtCStr << ")\n\n"; 601 OS << AstStr << "\n"; 602 OS << "else\n"; 603 OS << " { /* original code */ }\n\n"; 604 605 free(RtCStr); 606 free(AstStr); 607 608 isl_ast_expr_free(RunCondition); 609 isl_schedule_free(Schedule); 610 isl_ast_node_free(RootNode); 611 isl_printer_free(P); 612 } 613 614 AnalysisKey IslAstAnalysis::Key; 615 PreservedAnalyses IslAstPrinterPass::run(Scop &S, ScopAnalysisManager &SAM, 616 ScopStandardAnalysisResults &SAR, 617 SPMUpdater &U) { 618 619 auto &Ast = SAM.getResult<IslAstAnalysis>(S, SAR); 620 Ast.print(Stream); 621 return PreservedAnalyses::all(); 622 } 623 624 void IslAstInfoWrapperPass::releaseMemory() { Ast.reset(); } 625 626 bool IslAstInfoWrapperPass::runOnScop(Scop &Scop) { 627 const Dependences &D = 628 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); 629 630 Ast.reset(new IslAstInfo(Scop, D)); 631 632 DEBUG(printScop(dbgs(), Scop)); 633 return false; 634 } 635 void IslAstInfoWrapperPass::getAnalysisUsage(AnalysisUsage &AU) const { 636 // Get the Common analysis usage of ScopPasses. 637 ScopPass::getAnalysisUsage(AU); 638 AU.addRequired<ScopInfoRegionPass>(); 639 AU.addRequired<DependenceInfo>(); 640 } 641 642 void IslAstInfoWrapperPass::printScop(raw_ostream &OS, Scop &S) const { 643 if (Ast) 644 Ast->print(OS); 645 } 646 647 char IslAstInfoWrapperPass::ID = 0; 648 649 Pass *polly::createIslAstInfoWrapperPassPass() { 650 return new IslAstInfoWrapperPass(); 651 } 652 653 INITIALIZE_PASS_BEGIN(IslAstInfoWrapperPass, "polly-ast", 654 "Polly - Generate an AST of the SCoP (isl)", false, 655 false); 656 INITIALIZE_PASS_DEPENDENCY(ScopInfoRegionPass); 657 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 658 INITIALIZE_PASS_END(IslAstInfoWrapperPass, "polly-ast", 659 "Polly - Generate an AST from the SCoP (isl)", false, false) 660