1 //===- Schedule.cpp - Calculate an optimized schedule ---------------------===// 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 // This pass generates an entirey new schedule tree from the data dependences 11 // and iteration domains. The new schedule tree is computed in two steps: 12 // 13 // 1) The isl scheduling optimizer is run 14 // 15 // The isl scheduling optimizer creates a new schedule tree that maximizes 16 // parallelism and tileability and minimizes data-dependence distances. The 17 // algorithm used is a modified version of the ``Pluto'' algorithm: 18 // 19 // U. Bondhugula, A. Hartono, J. Ramanujam, and P. Sadayappan. 20 // A Practical Automatic Polyhedral Parallelizer and Locality Optimizer. 21 // In Proceedings of the 2008 ACM SIGPLAN Conference On Programming Language 22 // Design and Implementation, PLDI ’08, pages 101–113. ACM, 2008. 23 // 24 // 2) A set of post-scheduling transformations is applied on the schedule tree. 25 // 26 // These optimizations include: 27 // 28 // - Tiling of the innermost tilable bands 29 // - Prevectorization - The coice of a possible outer loop that is strip-mined 30 // to the innermost level to enable inner-loop 31 // vectorization. 32 // - Some optimizations for spatial locality are also planned. 33 // 34 // For a detailed description of the schedule tree itself please see section 6 35 // of: 36 // 37 // Polyhedral AST generation is more than scanning polyhedra 38 // Tobias Grosser, Sven Verdoolaege, Albert Cohen 39 // ACM Transations on Programming Languages and Systems (TOPLAS), 40 // 37(4), July 2015 41 // http://www.grosser.es/#pub-polyhedral-AST-generation 42 // 43 // This publication also contains a detailed discussion of the different options 44 // for polyhedral loop unrolling, full/partial tile separation and other uses 45 // of the schedule tree. 46 // 47 //===----------------------------------------------------------------------===// 48 49 #include "polly/ScheduleOptimizer.h" 50 #include "polly/CodeGen/CodeGeneration.h" 51 #include "polly/DependenceInfo.h" 52 #include "polly/LinkAllPasses.h" 53 #include "polly/Options.h" 54 #include "polly/ScopInfo.h" 55 #include "polly/Support/GICHelper.h" 56 #include "llvm/Support/Debug.h" 57 #include "isl/aff.h" 58 #include "isl/band.h" 59 #include "isl/constraint.h" 60 #include "isl/map.h" 61 #include "isl/options.h" 62 #include "isl/printer.h" 63 #include "isl/schedule.h" 64 #include "isl/schedule_node.h" 65 #include "isl/space.h" 66 #include "isl/union_map.h" 67 #include "isl/union_set.h" 68 69 using namespace llvm; 70 using namespace polly; 71 72 #define DEBUG_TYPE "polly-opt-isl" 73 74 static cl::opt<std::string> 75 OptimizeDeps("polly-opt-optimize-only", 76 cl::desc("Only a certain kind of dependences (all/raw)"), 77 cl::Hidden, cl::init("all"), cl::ZeroOrMore, 78 cl::cat(PollyCategory)); 79 80 static cl::opt<std::string> 81 SimplifyDeps("polly-opt-simplify-deps", 82 cl::desc("Dependences should be simplified (yes/no)"), 83 cl::Hidden, cl::init("yes"), cl::ZeroOrMore, 84 cl::cat(PollyCategory)); 85 86 static cl::opt<int> MaxConstantTerm( 87 "polly-opt-max-constant-term", 88 cl::desc("The maximal constant term allowed (-1 is unlimited)"), cl::Hidden, 89 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); 90 91 static cl::opt<int> MaxCoefficient( 92 "polly-opt-max-coefficient", 93 cl::desc("The maximal coefficient allowed (-1 is unlimited)"), cl::Hidden, 94 cl::init(20), cl::ZeroOrMore, cl::cat(PollyCategory)); 95 96 static cl::opt<std::string> FusionStrategy( 97 "polly-opt-fusion", cl::desc("The fusion strategy to choose (min/max)"), 98 cl::Hidden, cl::init("min"), cl::ZeroOrMore, cl::cat(PollyCategory)); 99 100 static cl::opt<std::string> 101 MaximizeBandDepth("polly-opt-maximize-bands", 102 cl::desc("Maximize the band depth (yes/no)"), cl::Hidden, 103 cl::init("yes"), cl::ZeroOrMore, cl::cat(PollyCategory)); 104 105 static cl::opt<int> PrevectorWidth( 106 "polly-prevect-width", 107 cl::desc( 108 "The number of loop iterations to strip-mine for pre-vectorization"), 109 cl::Hidden, cl::init(4), cl::ZeroOrMore, cl::cat(PollyCategory)); 110 111 static cl::opt<bool> FirstLevelTiling("polly-tiling", 112 cl::desc("Enable loop tiling"), 113 cl::init(true), cl::ZeroOrMore, 114 cl::cat(PollyCategory)); 115 116 static cl::opt<int> FirstLevelDefaultTileSize( 117 "polly-default-tile-size", 118 cl::desc("The default tile size (if not enough were provided by" 119 " --polly-tile-sizes)"), 120 cl::Hidden, cl::init(32), cl::ZeroOrMore, cl::cat(PollyCategory)); 121 122 static cl::list<int> FirstLevelTileSizes( 123 "polly-tile-sizes", cl::desc("A tile size for each loop dimension, filled " 124 "with --polly-default-tile-size"), 125 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, cl::cat(PollyCategory)); 126 127 static cl::opt<bool> 128 SecondLevelTiling("polly-2nd-level-tiling", 129 cl::desc("Enable a 2nd level loop of loop tiling"), 130 cl::init(false), cl::ZeroOrMore, cl::cat(PollyCategory)); 131 132 static cl::opt<int> SecondLevelDefaultTileSize( 133 "polly-2nd-level-default-tile-size", 134 cl::desc("The default 2nd-level tile size (if not enough were provided by" 135 " --polly-2nd-level-tile-sizes)"), 136 cl::Hidden, cl::init(16), cl::ZeroOrMore, cl::cat(PollyCategory)); 137 138 static cl::list<int> 139 SecondLevelTileSizes("polly-2nd-level-tile-sizes", 140 cl::desc("A tile size for each loop dimension, filled " 141 "with --polly-default-tile-size"), 142 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, 143 cl::cat(PollyCategory)); 144 145 static cl::opt<bool> RegisterTiling("polly-register-tiling", 146 cl::desc("Enable register tiling"), 147 cl::init(false), cl::ZeroOrMore, 148 cl::cat(PollyCategory)); 149 150 static cl::opt<int> RegisterDefaultTileSize( 151 "polly-register-tiling-default-tile-size", 152 cl::desc("The default register tile size (if not enough were provided by" 153 " --polly-register-tile-sizes)"), 154 cl::Hidden, cl::init(2), cl::ZeroOrMore, cl::cat(PollyCategory)); 155 156 static cl::list<int> 157 RegisterTileSizes("polly-register-tile-sizes", 158 cl::desc("A tile size for each loop dimension, filled " 159 "with --polly-register-tile-size"), 160 cl::Hidden, cl::ZeroOrMore, cl::CommaSeparated, 161 cl::cat(PollyCategory)); 162 163 /// @brief Create an isl_union_set, which describes the isolate option based 164 /// on IsoalteDomain. 165 /// 166 /// @param IsolateDomain An isl_set whose last dimension is the only one that 167 /// should belong to the current band node. 168 static __isl_give isl_union_set * 169 getIsolateOptions(__isl_take isl_set *IsolateDomain) { 170 auto Dims = isl_set_dim(IsolateDomain, isl_dim_set); 171 auto *IsolateRelation = isl_map_from_domain(IsolateDomain); 172 IsolateRelation = isl_map_move_dims(IsolateRelation, isl_dim_out, 0, 173 isl_dim_in, Dims - 1, 1); 174 auto *IsolateOption = isl_map_wrap(IsolateRelation); 175 auto *Id = isl_id_alloc(isl_set_get_ctx(IsolateOption), "isolate", NULL); 176 return isl_union_set_from_set(isl_set_set_tuple_id(IsolateOption, Id)); 177 } 178 179 /// @brief Create an isl_union_set, which describes the atomic option for the 180 /// dimension of the current node. 181 /// 182 /// It may help to reduce the size of generated code. 183 /// 184 /// @param Ctx An isl_ctx, which is used to create the isl_union_set. 185 static __isl_give isl_union_set *getAtomicOptions(__isl_take isl_ctx *Ctx) { 186 auto *Space = isl_space_set_alloc(Ctx, 0, 1); 187 auto *AtomicOption = isl_set_universe(Space); 188 auto *Id = isl_id_alloc(Ctx, "atomic", NULL); 189 return isl_union_set_from_set(isl_set_set_tuple_id(AtomicOption, Id)); 190 } 191 192 /// @brief Make the last dimension of Set to take values 193 /// from 0 to VectorWidth - 1. 194 /// 195 /// @param Set A set, which should be modified. 196 /// @param VectorWidth A parameter, which determines the constraint. 197 static __isl_give isl_set *addExtentConstraints(__isl_take isl_set *Set, 198 int VectorWidth) { 199 auto Dims = isl_set_dim(Set, isl_dim_set); 200 auto Space = isl_set_get_space(Set); 201 auto *LocalSpace = isl_local_space_from_space(Space); 202 auto *ExtConstr = 203 isl_constraint_alloc_inequality(isl_local_space_copy(LocalSpace)); 204 ExtConstr = isl_constraint_set_constant_si(ExtConstr, 0); 205 ExtConstr = 206 isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, 1); 207 Set = isl_set_add_constraint(Set, ExtConstr); 208 ExtConstr = isl_constraint_alloc_inequality(LocalSpace); 209 ExtConstr = isl_constraint_set_constant_si(ExtConstr, VectorWidth - 1); 210 ExtConstr = 211 isl_constraint_set_coefficient_si(ExtConstr, isl_dim_set, Dims - 1, -1); 212 return isl_set_add_constraint(Set, ExtConstr); 213 } 214 215 /// @brief Build the desired set of partial tile prefixes. 216 /// 217 /// We build a set of partial tile prefixes, which are prefixes of the vector 218 /// loop that have exactly VectorWidth iterations. 219 /// 220 /// 1. Get all prefixes of the vector loop. 221 /// 2. Extend it to a set, which has exactly VectorWidth iterations for 222 /// any prefix from the set that was built on the previous step. 223 /// 3. Subtract loop domain from it, project out the vector loop dimension and 224 /// get a set of prefixes, which don’t have exactly VectorWidth iterations. 225 /// 4. Subtract it from all prefixes of the vector loop and get the desired 226 /// set. 227 /// 228 /// @param ScheduleRange A range of a map, which describes a prefix schedule 229 /// relation. 230 static __isl_give isl_set * 231 getPartialTilePrefixes(__isl_take isl_set *ScheduleRange, int VectorWidth) { 232 auto Dims = isl_set_dim(ScheduleRange, isl_dim_set); 233 auto *LoopPrefixes = isl_set_project_out(isl_set_copy(ScheduleRange), 234 isl_dim_set, Dims - 1, 1); 235 auto *ExtentPrefixes = 236 isl_set_add_dims(isl_set_copy(LoopPrefixes), isl_dim_set, 1); 237 ExtentPrefixes = addExtentConstraints(ExtentPrefixes, VectorWidth); 238 auto *BadPrefixes = isl_set_subtract(ExtentPrefixes, ScheduleRange); 239 BadPrefixes = isl_set_project_out(BadPrefixes, isl_dim_set, Dims - 1, 1); 240 return isl_set_subtract(LoopPrefixes, BadPrefixes); 241 } 242 243 __isl_give isl_schedule_node *ScheduleTreeOptimizer::isolateFullPartialTiles( 244 __isl_take isl_schedule_node *Node, int VectorWidth) { 245 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); 246 Node = isl_schedule_node_child(Node, 0); 247 Node = isl_schedule_node_child(Node, 0); 248 auto *SchedRelUMap = isl_schedule_node_get_prefix_schedule_relation(Node); 249 auto *ScheduleRelation = isl_map_from_union_map(SchedRelUMap); 250 auto *ScheduleRange = isl_map_range(ScheduleRelation); 251 auto *IsolateDomain = getPartialTilePrefixes(ScheduleRange, VectorWidth); 252 auto *AtomicOption = getAtomicOptions(isl_set_get_ctx(IsolateDomain)); 253 auto *IsolateOption = getIsolateOptions(IsolateDomain); 254 Node = isl_schedule_node_parent(Node); 255 Node = isl_schedule_node_parent(Node); 256 auto *Options = isl_union_set_union(IsolateOption, AtomicOption); 257 Node = isl_schedule_node_band_set_ast_build_options(Node, Options); 258 return Node; 259 } 260 261 __isl_give isl_schedule_node * 262 ScheduleTreeOptimizer::prevectSchedBand(__isl_take isl_schedule_node *Node, 263 unsigned DimToVectorize, 264 int VectorWidth) { 265 assert(isl_schedule_node_get_type(Node) == isl_schedule_node_band); 266 267 auto Space = isl_schedule_node_band_get_space(Node); 268 auto ScheduleDimensions = isl_space_dim(Space, isl_dim_set); 269 isl_space_free(Space); 270 assert(DimToVectorize < ScheduleDimensions); 271 272 if (DimToVectorize > 0) { 273 Node = isl_schedule_node_band_split(Node, DimToVectorize); 274 Node = isl_schedule_node_child(Node, 0); 275 } 276 if (DimToVectorize < ScheduleDimensions - 1) 277 Node = isl_schedule_node_band_split(Node, 1); 278 Space = isl_schedule_node_band_get_space(Node); 279 auto Sizes = isl_multi_val_zero(Space); 280 auto Ctx = isl_schedule_node_get_ctx(Node); 281 Sizes = 282 isl_multi_val_set_val(Sizes, 0, isl_val_int_from_si(Ctx, VectorWidth)); 283 Node = isl_schedule_node_band_tile(Node, Sizes); 284 Node = isolateFullPartialTiles(Node, VectorWidth); 285 Node = isl_schedule_node_child(Node, 0); 286 // Make sure the "trivially vectorizable loop" is not unrolled. Otherwise, 287 // we will have troubles to match it in the backend. 288 Node = isl_schedule_node_band_set_ast_build_options( 289 Node, isl_union_set_read_from_str(Ctx, "{ unroll[x]: 1 = 0 }")); 290 Node = isl_schedule_node_band_sink(Node); 291 Node = isl_schedule_node_child(Node, 0); 292 if (isl_schedule_node_get_type(Node) == isl_schedule_node_leaf) 293 Node = isl_schedule_node_parent(Node); 294 isl_id *LoopMarker = isl_id_alloc(Ctx, "SIMD", nullptr); 295 Node = isl_schedule_node_insert_mark(Node, LoopMarker); 296 return Node; 297 } 298 299 __isl_give isl_schedule_node * 300 ScheduleTreeOptimizer::tileNode(__isl_take isl_schedule_node *Node, 301 const char *Identifier, ArrayRef<int> TileSizes, 302 int DefaultTileSize) { 303 auto Ctx = isl_schedule_node_get_ctx(Node); 304 auto Space = isl_schedule_node_band_get_space(Node); 305 auto Dims = isl_space_dim(Space, isl_dim_set); 306 auto Sizes = isl_multi_val_zero(Space); 307 std::string IdentifierString(Identifier); 308 for (unsigned i = 0; i < Dims; i++) { 309 auto tileSize = i < TileSizes.size() ? TileSizes[i] : DefaultTileSize; 310 Sizes = isl_multi_val_set_val(Sizes, i, isl_val_int_from_si(Ctx, tileSize)); 311 } 312 auto TileLoopMarkerStr = IdentifierString + " - Tiles"; 313 isl_id *TileLoopMarker = 314 isl_id_alloc(Ctx, TileLoopMarkerStr.c_str(), nullptr); 315 Node = isl_schedule_node_insert_mark(Node, TileLoopMarker); 316 Node = isl_schedule_node_child(Node, 0); 317 Node = isl_schedule_node_band_tile(Node, Sizes); 318 Node = isl_schedule_node_child(Node, 0); 319 auto PointLoopMarkerStr = IdentifierString + " - Points"; 320 isl_id *PointLoopMarker = 321 isl_id_alloc(Ctx, PointLoopMarkerStr.c_str(), nullptr); 322 Node = isl_schedule_node_insert_mark(Node, PointLoopMarker); 323 Node = isl_schedule_node_child(Node, 0); 324 return Node; 325 } 326 327 bool ScheduleTreeOptimizer::isTileableBandNode( 328 __isl_keep isl_schedule_node *Node) { 329 if (isl_schedule_node_get_type(Node) != isl_schedule_node_band) 330 return false; 331 332 if (isl_schedule_node_n_children(Node) != 1) 333 return false; 334 335 if (!isl_schedule_node_band_get_permutable(Node)) 336 return false; 337 338 auto Space = isl_schedule_node_band_get_space(Node); 339 auto Dims = isl_space_dim(Space, isl_dim_set); 340 isl_space_free(Space); 341 342 if (Dims <= 1) 343 return false; 344 345 auto Child = isl_schedule_node_get_child(Node, 0); 346 auto Type = isl_schedule_node_get_type(Child); 347 isl_schedule_node_free(Child); 348 349 if (Type != isl_schedule_node_leaf) 350 return false; 351 352 return true; 353 } 354 355 __isl_give isl_schedule_node * 356 ScheduleTreeOptimizer::optimizeBand(__isl_take isl_schedule_node *Node, 357 void *User) { 358 if (!isTileableBandNode(Node)) 359 return Node; 360 361 if (FirstLevelTiling) 362 Node = tileNode(Node, "1st level tiling", FirstLevelTileSizes, 363 FirstLevelDefaultTileSize); 364 365 if (SecondLevelTiling) 366 Node = tileNode(Node, "2nd level tiling", SecondLevelTileSizes, 367 SecondLevelDefaultTileSize); 368 369 if (RegisterTiling) { 370 auto *Ctx = isl_schedule_node_get_ctx(Node); 371 Node = tileNode(Node, "Register tiling", RegisterTileSizes, 372 RegisterDefaultTileSize); 373 Node = isl_schedule_node_band_set_ast_build_options( 374 Node, isl_union_set_read_from_str(Ctx, "{unroll[x]}")); 375 } 376 377 if (PollyVectorizerChoice == VECTORIZER_NONE) 378 return Node; 379 380 auto Space = isl_schedule_node_band_get_space(Node); 381 auto Dims = isl_space_dim(Space, isl_dim_set); 382 isl_space_free(Space); 383 384 for (int i = Dims - 1; i >= 0; i--) 385 if (isl_schedule_node_band_member_get_coincident(Node, i)) { 386 Node = prevectSchedBand(Node, i, PrevectorWidth); 387 break; 388 } 389 390 return Node; 391 } 392 393 __isl_give isl_schedule * 394 ScheduleTreeOptimizer::optimizeSchedule(__isl_take isl_schedule *Schedule) { 395 isl_schedule_node *Root = isl_schedule_get_root(Schedule); 396 Root = optimizeScheduleNode(Root); 397 isl_schedule_free(Schedule); 398 auto S = isl_schedule_node_get_schedule(Root); 399 isl_schedule_node_free(Root); 400 return S; 401 } 402 403 __isl_give isl_schedule_node *ScheduleTreeOptimizer::optimizeScheduleNode( 404 __isl_take isl_schedule_node *Node) { 405 Node = isl_schedule_node_map_descendant_bottom_up(Node, optimizeBand, NULL); 406 return Node; 407 } 408 409 bool ScheduleTreeOptimizer::isProfitableSchedule( 410 Scop &S, __isl_keep isl_union_map *NewSchedule) { 411 // To understand if the schedule has been optimized we check if the schedule 412 // has changed at all. 413 // TODO: We can improve this by tracking if any necessarily beneficial 414 // transformations have been performed. This can e.g. be tiling, loop 415 // interchange, or ...) We can track this either at the place where the 416 // transformation has been performed or, in case of automatic ILP based 417 // optimizations, by comparing (yet to be defined) performance metrics 418 // before/after the scheduling optimizer 419 // (e.g., #stride-one accesses) 420 isl_union_map *OldSchedule = S.getSchedule(); 421 bool changed = !isl_union_map_is_equal(OldSchedule, NewSchedule); 422 isl_union_map_free(OldSchedule); 423 return changed; 424 } 425 426 namespace { 427 class IslScheduleOptimizer : public ScopPass { 428 public: 429 static char ID; 430 explicit IslScheduleOptimizer() : ScopPass(ID) { LastSchedule = nullptr; } 431 432 ~IslScheduleOptimizer() { isl_schedule_free(LastSchedule); } 433 434 /// @brief Optimize the schedule of the SCoP @p S. 435 bool runOnScop(Scop &S) override; 436 437 /// @brief Print the new schedule for the SCoP @p S. 438 void printScop(raw_ostream &OS, Scop &S) const override; 439 440 /// @brief Register all analyses and transformation required. 441 void getAnalysisUsage(AnalysisUsage &AU) const override; 442 443 /// @brief Release the internal memory. 444 void releaseMemory() override { 445 isl_schedule_free(LastSchedule); 446 LastSchedule = nullptr; 447 } 448 449 private: 450 isl_schedule *LastSchedule; 451 }; 452 } 453 454 char IslScheduleOptimizer::ID = 0; 455 456 bool IslScheduleOptimizer::runOnScop(Scop &S) { 457 458 // Skip empty SCoPs but still allow code generation as it will delete the 459 // loops present but not needed. 460 if (S.getSize() == 0) { 461 S.markAsOptimized(); 462 return false; 463 } 464 465 const Dependences &D = 466 getAnalysis<DependenceInfo>().getDependences(Dependences::AL_Statement); 467 468 if (!D.hasValidDependences()) 469 return false; 470 471 isl_schedule_free(LastSchedule); 472 LastSchedule = nullptr; 473 474 // Build input data. 475 int ValidityKinds = 476 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; 477 int ProximityKinds; 478 479 if (OptimizeDeps == "all") 480 ProximityKinds = 481 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; 482 else if (OptimizeDeps == "raw") 483 ProximityKinds = Dependences::TYPE_RAW; 484 else { 485 errs() << "Do not know how to optimize for '" << OptimizeDeps << "'" 486 << " Falling back to optimizing all dependences.\n"; 487 ProximityKinds = 488 Dependences::TYPE_RAW | Dependences::TYPE_WAR | Dependences::TYPE_WAW; 489 } 490 491 isl_union_set *Domain = S.getDomains(); 492 493 if (!Domain) 494 return false; 495 496 isl_union_map *Validity = D.getDependences(ValidityKinds); 497 isl_union_map *Proximity = D.getDependences(ProximityKinds); 498 499 // Simplify the dependences by removing the constraints introduced by the 500 // domains. This can speed up the scheduling time significantly, as large 501 // constant coefficients will be removed from the dependences. The 502 // introduction of some additional dependences reduces the possible 503 // transformations, but in most cases, such transformation do not seem to be 504 // interesting anyway. In some cases this option may stop the scheduler to 505 // find any schedule. 506 if (SimplifyDeps == "yes") { 507 Validity = isl_union_map_gist_domain(Validity, isl_union_set_copy(Domain)); 508 Validity = isl_union_map_gist_range(Validity, isl_union_set_copy(Domain)); 509 Proximity = 510 isl_union_map_gist_domain(Proximity, isl_union_set_copy(Domain)); 511 Proximity = isl_union_map_gist_range(Proximity, isl_union_set_copy(Domain)); 512 } else if (SimplifyDeps != "no") { 513 errs() << "warning: Option -polly-opt-simplify-deps should either be 'yes' " 514 "or 'no'. Falling back to default: 'yes'\n"; 515 } 516 517 DEBUG(dbgs() << "\n\nCompute schedule from: "); 518 DEBUG(dbgs() << "Domain := " << stringFromIslObj(Domain) << ";\n"); 519 DEBUG(dbgs() << "Proximity := " << stringFromIslObj(Proximity) << ";\n"); 520 DEBUG(dbgs() << "Validity := " << stringFromIslObj(Validity) << ";\n"); 521 522 unsigned IslSerializeSCCs; 523 524 if (FusionStrategy == "max") { 525 IslSerializeSCCs = 0; 526 } else if (FusionStrategy == "min") { 527 IslSerializeSCCs = 1; 528 } else { 529 errs() << "warning: Unknown fusion strategy. Falling back to maximal " 530 "fusion.\n"; 531 IslSerializeSCCs = 0; 532 } 533 534 int IslMaximizeBands; 535 536 if (MaximizeBandDepth == "yes") { 537 IslMaximizeBands = 1; 538 } else if (MaximizeBandDepth == "no") { 539 IslMaximizeBands = 0; 540 } else { 541 errs() << "warning: Option -polly-opt-maximize-bands should either be 'yes'" 542 " or 'no'. Falling back to default: 'yes'\n"; 543 IslMaximizeBands = 1; 544 } 545 546 isl_options_set_schedule_serialize_sccs(S.getIslCtx(), IslSerializeSCCs); 547 isl_options_set_schedule_maximize_band_depth(S.getIslCtx(), IslMaximizeBands); 548 isl_options_set_schedule_max_constant_term(S.getIslCtx(), MaxConstantTerm); 549 isl_options_set_schedule_max_coefficient(S.getIslCtx(), MaxCoefficient); 550 isl_options_set_tile_scale_tile_loops(S.getIslCtx(), 0); 551 552 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_CONTINUE); 553 554 isl_schedule_constraints *ScheduleConstraints; 555 ScheduleConstraints = isl_schedule_constraints_on_domain(Domain); 556 ScheduleConstraints = 557 isl_schedule_constraints_set_proximity(ScheduleConstraints, Proximity); 558 ScheduleConstraints = isl_schedule_constraints_set_validity( 559 ScheduleConstraints, isl_union_map_copy(Validity)); 560 ScheduleConstraints = 561 isl_schedule_constraints_set_coincidence(ScheduleConstraints, Validity); 562 isl_schedule *Schedule; 563 Schedule = isl_schedule_constraints_compute_schedule(ScheduleConstraints); 564 isl_options_set_on_error(S.getIslCtx(), ISL_ON_ERROR_ABORT); 565 566 // In cases the scheduler is not able to optimize the code, we just do not 567 // touch the schedule. 568 if (!Schedule) 569 return false; 570 571 DEBUG({ 572 auto *P = isl_printer_to_str(S.getIslCtx()); 573 P = isl_printer_set_yaml_style(P, ISL_YAML_STYLE_BLOCK); 574 P = isl_printer_print_schedule(P, Schedule); 575 dbgs() << "NewScheduleTree: \n" << isl_printer_get_str(P) << "\n"; 576 isl_printer_free(P); 577 }); 578 579 isl_schedule *NewSchedule = ScheduleTreeOptimizer::optimizeSchedule(Schedule); 580 isl_union_map *NewScheduleMap = isl_schedule_get_map(NewSchedule); 581 582 if (!ScheduleTreeOptimizer::isProfitableSchedule(S, NewScheduleMap)) { 583 isl_union_map_free(NewScheduleMap); 584 isl_schedule_free(NewSchedule); 585 return false; 586 } 587 588 S.setScheduleTree(NewSchedule); 589 S.markAsOptimized(); 590 591 isl_union_map_free(NewScheduleMap); 592 return false; 593 } 594 595 void IslScheduleOptimizer::printScop(raw_ostream &OS, Scop &) const { 596 isl_printer *p; 597 char *ScheduleStr; 598 599 OS << "Calculated schedule:\n"; 600 601 if (!LastSchedule) { 602 OS << "n/a\n"; 603 return; 604 } 605 606 p = isl_printer_to_str(isl_schedule_get_ctx(LastSchedule)); 607 p = isl_printer_print_schedule(p, LastSchedule); 608 ScheduleStr = isl_printer_get_str(p); 609 isl_printer_free(p); 610 611 OS << ScheduleStr << "\n"; 612 } 613 614 void IslScheduleOptimizer::getAnalysisUsage(AnalysisUsage &AU) const { 615 ScopPass::getAnalysisUsage(AU); 616 AU.addRequired<DependenceInfo>(); 617 } 618 619 Pass *polly::createIslScheduleOptimizerPass() { 620 return new IslScheduleOptimizer(); 621 } 622 623 INITIALIZE_PASS_BEGIN(IslScheduleOptimizer, "polly-opt-isl", 624 "Polly - Optimize schedule of SCoP", false, false); 625 INITIALIZE_PASS_DEPENDENCY(DependenceInfo); 626 INITIALIZE_PASS_DEPENDENCY(ScopInfo); 627 INITIALIZE_PASS_END(IslScheduleOptimizer, "polly-opt-isl", 628 "Polly - Optimize schedule of SCoP", false, false) 629