1 //===- polly/ScheduleTreeTransform.cpp --------------------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // Make changes to isl's schedule tree data structure. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "polly/ScheduleTreeTransform.h" 14 #include "polly/Support/ISLTools.h" 15 #include "polly/Support/ScopHelper.h" 16 #include "llvm/ADT/ArrayRef.h" 17 #include "llvm/ADT/Sequence.h" 18 #include "llvm/ADT/SmallVector.h" 19 #include "llvm/IR/Constants.h" 20 #include "llvm/IR/Metadata.h" 21 #include "llvm/Transforms/Utils/UnrollLoop.h" 22 23 using namespace polly; 24 using namespace llvm; 25 26 namespace { 27 /// Recursively visit all nodes of a schedule tree while allowing changes. 28 /// 29 /// The visit methods return an isl::schedule_node that is used to continue 30 /// visiting the tree. Structural changes such as returning a different node 31 /// will confuse the visitor. 32 template <typename Derived, typename... Args> 33 struct ScheduleNodeRewriter 34 : public RecursiveScheduleTreeVisitor<Derived, isl::schedule_node, 35 Args...> { 36 Derived &getDerived() { return *static_cast<Derived *>(this); } 37 const Derived &getDerived() const { 38 return *static_cast<const Derived *>(this); 39 } 40 41 isl::schedule_node visitNode(const isl::schedule_node &Node, Args... args) { 42 if (!Node.has_children()) 43 return Node; 44 45 isl::schedule_node It = Node.first_child(); 46 while (true) { 47 It = getDerived().visit(It, std::forward<Args>(args)...); 48 if (!It.has_next_sibling()) 49 break; 50 It = It.next_sibling(); 51 } 52 return It.parent(); 53 } 54 }; 55 56 /// Rewrite a schedule tree by reconstructing it bottom-up. 57 /// 58 /// By default, the original schedule tree is reconstructed. To build a 59 /// different tree, redefine visitor methods in a derived class (CRTP). 60 /// 61 /// Note that AST build options are not applied; Setting the isolate[] option 62 /// makes the schedule tree 'anchored' and cannot be modified afterwards. Hence, 63 /// AST build options must be set after the tree has been constructed. 64 template <typename Derived, typename... Args> 65 struct ScheduleTreeRewriter 66 : public RecursiveScheduleTreeVisitor<Derived, isl::schedule, Args...> { 67 Derived &getDerived() { return *static_cast<Derived *>(this); } 68 const Derived &getDerived() const { 69 return *static_cast<const Derived *>(this); 70 } 71 72 isl::schedule visitDomain(isl::schedule_node_domain Node, Args... args) { 73 // Every schedule_tree already has a domain node, no need to add one. 74 return getDerived().visit(Node.first_child(), std::forward<Args>(args)...); 75 } 76 77 isl::schedule visitBand(isl::schedule_node_band Band, Args... args) { 78 isl::multi_union_pw_aff PartialSched = 79 isl::manage(isl_schedule_node_band_get_partial_schedule(Band.get())); 80 isl::schedule NewChild = 81 getDerived().visit(Band.child(0), std::forward<Args>(args)...); 82 isl::schedule_node NewNode = 83 NewChild.insert_partial_schedule(PartialSched).get_root().child(0); 84 85 // Reapply permutability and coincidence attributes. 86 NewNode = isl::manage(isl_schedule_node_band_set_permutable( 87 NewNode.release(), isl_schedule_node_band_get_permutable(Band.get()))); 88 unsigned BandDims = isl_schedule_node_band_n_member(Band.get()); 89 for (unsigned i = 0; i < BandDims; i += 1) 90 NewNode = isl::manage(isl_schedule_node_band_member_set_coincident( 91 NewNode.release(), i, 92 isl_schedule_node_band_member_get_coincident(Band.get(), i))); 93 94 return NewNode.get_schedule(); 95 } 96 97 isl::schedule visitSequence(isl::schedule_node_sequence Sequence, 98 Args... args) { 99 int NumChildren = isl_schedule_node_n_children(Sequence.get()); 100 isl::schedule Result = 101 getDerived().visit(Sequence.child(0), std::forward<Args>(args)...); 102 for (int i = 1; i < NumChildren; i += 1) 103 Result = Result.sequence( 104 getDerived().visit(Sequence.child(i), std::forward<Args>(args)...)); 105 return Result; 106 } 107 108 isl::schedule visitSet(isl::schedule_node_set Set, Args... args) { 109 int NumChildren = isl_schedule_node_n_children(Set.get()); 110 isl::schedule Result = 111 getDerived().visit(Set.child(0), std::forward<Args>(args)...); 112 for (int i = 1; i < NumChildren; i += 1) 113 Result = isl::manage( 114 isl_schedule_set(Result.release(), 115 getDerived() 116 .visit(Set.child(i), std::forward<Args>(args)...) 117 .release())); 118 return Result; 119 } 120 121 isl::schedule visitLeaf(isl::schedule_node_leaf Leaf, Args... args) { 122 return isl::schedule::from_domain(Leaf.get_domain()); 123 } 124 125 isl::schedule visitMark(const isl::schedule_node &Mark, Args... args) { 126 127 isl::id TheMark = Mark.as<isl::schedule_node_mark>().get_id(); 128 isl::schedule_node NewChild = 129 getDerived() 130 .visit(Mark.first_child(), std::forward<Args>(args)...) 131 .get_root() 132 .first_child(); 133 return NewChild.insert_mark(TheMark).get_schedule(); 134 } 135 136 isl::schedule visitExtension(isl::schedule_node_extension Extension, 137 Args... args) { 138 isl::union_map TheExtension = 139 Extension.as<isl::schedule_node_extension>().get_extension(); 140 isl::schedule_node NewChild = getDerived() 141 .visit(Extension.child(0), args...) 142 .get_root() 143 .first_child(); 144 isl::schedule_node NewExtension = 145 isl::schedule_node::from_extension(TheExtension); 146 return NewChild.graft_before(NewExtension).get_schedule(); 147 } 148 149 isl::schedule visitFilter(isl::schedule_node_filter Filter, Args... args) { 150 isl::union_set FilterDomain = 151 Filter.as<isl::schedule_node_filter>().get_filter(); 152 isl::schedule NewSchedule = 153 getDerived().visit(Filter.child(0), std::forward<Args>(args)...); 154 return NewSchedule.intersect_domain(FilterDomain); 155 } 156 157 isl::schedule visitNode(isl::schedule_node Node, Args... args) { 158 llvm_unreachable("Not implemented"); 159 } 160 }; 161 162 /// Rewrite a schedule tree to an equivalent one without extension nodes. 163 /// 164 /// Each visit method takes two additional arguments: 165 /// 166 /// * The new domain the node, which is the inherited domain plus any domains 167 /// added by extension nodes. 168 /// 169 /// * A map of extension domains of all children is returned; it is required by 170 /// band nodes to schedule the additional domains at the same position as the 171 /// extension node would. 172 /// 173 struct ExtensionNodeRewriter 174 : public ScheduleTreeRewriter<ExtensionNodeRewriter, const isl::union_set &, 175 isl::union_map &> { 176 using BaseTy = ScheduleTreeRewriter<ExtensionNodeRewriter, 177 const isl::union_set &, isl::union_map &>; 178 BaseTy &getBase() { return *this; } 179 const BaseTy &getBase() const { return *this; } 180 181 isl::schedule visitSchedule(isl::schedule Schedule) { 182 isl::union_map Extensions; 183 isl::schedule Result = 184 visit(Schedule.get_root(), Schedule.get_domain(), Extensions); 185 assert(!Extensions.is_null() && Extensions.is_empty()); 186 return Result; 187 } 188 189 isl::schedule visitSequence(isl::schedule_node_sequence Sequence, 190 const isl::union_set &Domain, 191 isl::union_map &Extensions) { 192 int NumChildren = isl_schedule_node_n_children(Sequence.get()); 193 isl::schedule NewNode = visit(Sequence.first_child(), Domain, Extensions); 194 for (int i = 1; i < NumChildren; i += 1) { 195 isl::schedule_node OldChild = Sequence.child(i); 196 isl::union_map NewChildExtensions; 197 isl::schedule NewChildNode = visit(OldChild, Domain, NewChildExtensions); 198 NewNode = NewNode.sequence(NewChildNode); 199 Extensions = Extensions.unite(NewChildExtensions); 200 } 201 return NewNode; 202 } 203 204 isl::schedule visitSet(isl::schedule_node_set Set, 205 const isl::union_set &Domain, 206 isl::union_map &Extensions) { 207 int NumChildren = isl_schedule_node_n_children(Set.get()); 208 isl::schedule NewNode = visit(Set.first_child(), Domain, Extensions); 209 for (int i = 1; i < NumChildren; i += 1) { 210 isl::schedule_node OldChild = Set.child(i); 211 isl::union_map NewChildExtensions; 212 isl::schedule NewChildNode = visit(OldChild, Domain, NewChildExtensions); 213 NewNode = isl::manage( 214 isl_schedule_set(NewNode.release(), NewChildNode.release())); 215 Extensions = Extensions.unite(NewChildExtensions); 216 } 217 return NewNode; 218 } 219 220 isl::schedule visitLeaf(isl::schedule_node_leaf Leaf, 221 const isl::union_set &Domain, 222 isl::union_map &Extensions) { 223 Extensions = isl::union_map::empty(Leaf.ctx()); 224 return isl::schedule::from_domain(Domain); 225 } 226 227 isl::schedule visitBand(isl::schedule_node_band OldNode, 228 const isl::union_set &Domain, 229 isl::union_map &OuterExtensions) { 230 isl::schedule_node OldChild = OldNode.first_child(); 231 isl::multi_union_pw_aff PartialSched = 232 isl::manage(isl_schedule_node_band_get_partial_schedule(OldNode.get())); 233 234 isl::union_map NewChildExtensions; 235 isl::schedule NewChild = visit(OldChild, Domain, NewChildExtensions); 236 237 // Add the extensions to the partial schedule. 238 OuterExtensions = isl::union_map::empty(NewChildExtensions.ctx()); 239 isl::union_map NewPartialSchedMap = isl::union_map::from(PartialSched); 240 unsigned BandDims = isl_schedule_node_band_n_member(OldNode.get()); 241 for (isl::map Ext : NewChildExtensions.get_map_list()) { 242 unsigned ExtDims = Ext.domain_tuple_dim().release(); 243 assert(ExtDims >= BandDims); 244 unsigned OuterDims = ExtDims - BandDims; 245 246 isl::map BandSched = 247 Ext.project_out(isl::dim::in, 0, OuterDims).reverse(); 248 NewPartialSchedMap = NewPartialSchedMap.unite(BandSched); 249 250 // There might be more outer bands that have to schedule the extensions. 251 if (OuterDims > 0) { 252 isl::map OuterSched = 253 Ext.project_out(isl::dim::in, OuterDims, BandDims); 254 OuterExtensions = OuterExtensions.unite(OuterSched); 255 } 256 } 257 isl::multi_union_pw_aff NewPartialSchedAsAsMultiUnionPwAff = 258 isl::multi_union_pw_aff::from_union_map(NewPartialSchedMap); 259 isl::schedule_node NewNode = 260 NewChild.insert_partial_schedule(NewPartialSchedAsAsMultiUnionPwAff) 261 .get_root() 262 .child(0); 263 264 // Reapply permutability and coincidence attributes. 265 NewNode = isl::manage(isl_schedule_node_band_set_permutable( 266 NewNode.release(), 267 isl_schedule_node_band_get_permutable(OldNode.get()))); 268 for (unsigned i = 0; i < BandDims; i += 1) { 269 NewNode = isl::manage(isl_schedule_node_band_member_set_coincident( 270 NewNode.release(), i, 271 isl_schedule_node_band_member_get_coincident(OldNode.get(), i))); 272 } 273 274 return NewNode.get_schedule(); 275 } 276 277 isl::schedule visitFilter(isl::schedule_node_filter Filter, 278 const isl::union_set &Domain, 279 isl::union_map &Extensions) { 280 isl::union_set FilterDomain = 281 Filter.as<isl::schedule_node_filter>().get_filter(); 282 isl::union_set NewDomain = Domain.intersect(FilterDomain); 283 284 // A filter is added implicitly if necessary when joining schedule trees. 285 return visit(Filter.first_child(), NewDomain, Extensions); 286 } 287 288 isl::schedule visitExtension(isl::schedule_node_extension Extension, 289 const isl::union_set &Domain, 290 isl::union_map &Extensions) { 291 isl::union_map ExtDomain = 292 Extension.as<isl::schedule_node_extension>().get_extension(); 293 isl::union_set NewDomain = Domain.unite(ExtDomain.range()); 294 isl::union_map ChildExtensions; 295 isl::schedule NewChild = 296 visit(Extension.first_child(), NewDomain, ChildExtensions); 297 Extensions = ChildExtensions.unite(ExtDomain); 298 return NewChild; 299 } 300 }; 301 302 /// Collect all AST build options in any schedule tree band. 303 /// 304 /// ScheduleTreeRewriter cannot apply the schedule tree options. This class 305 /// collects these options to apply them later. 306 struct CollectASTBuildOptions 307 : public RecursiveScheduleTreeVisitor<CollectASTBuildOptions> { 308 using BaseTy = RecursiveScheduleTreeVisitor<CollectASTBuildOptions>; 309 BaseTy &getBase() { return *this; } 310 const BaseTy &getBase() const { return *this; } 311 312 llvm::SmallVector<isl::union_set, 8> ASTBuildOptions; 313 314 void visitBand(isl::schedule_node_band Band) { 315 ASTBuildOptions.push_back( 316 isl::manage(isl_schedule_node_band_get_ast_build_options(Band.get()))); 317 return getBase().visitBand(Band); 318 } 319 }; 320 321 /// Apply AST build options to the bands in a schedule tree. 322 /// 323 /// This rewrites a schedule tree with the AST build options applied. We assume 324 /// that the band nodes are visited in the same order as they were when the 325 /// build options were collected, typically by CollectASTBuildOptions. 326 struct ApplyASTBuildOptions 327 : public ScheduleNodeRewriter<ApplyASTBuildOptions> { 328 using BaseTy = ScheduleNodeRewriter<ApplyASTBuildOptions>; 329 BaseTy &getBase() { return *this; } 330 const BaseTy &getBase() const { return *this; } 331 332 size_t Pos; 333 llvm::ArrayRef<isl::union_set> ASTBuildOptions; 334 335 ApplyASTBuildOptions(llvm::ArrayRef<isl::union_set> ASTBuildOptions) 336 : ASTBuildOptions(ASTBuildOptions) {} 337 338 isl::schedule visitSchedule(isl::schedule Schedule) { 339 Pos = 0; 340 isl::schedule Result = visit(Schedule).get_schedule(); 341 assert(Pos == ASTBuildOptions.size() && 342 "AST build options must match to band nodes"); 343 return Result; 344 } 345 346 isl::schedule_node visitBand(isl::schedule_node_band Band) { 347 isl::schedule_node_band Result = 348 Band.set_ast_build_options(ASTBuildOptions[Pos]); 349 Pos += 1; 350 return getBase().visitBand(Result); 351 } 352 }; 353 354 /// Return whether the schedule contains an extension node. 355 static bool containsExtensionNode(isl::schedule Schedule) { 356 assert(!Schedule.is_null()); 357 358 auto Callback = [](__isl_keep isl_schedule_node *Node, 359 void *User) -> isl_bool { 360 if (isl_schedule_node_get_type(Node) == isl_schedule_node_extension) { 361 // Stop walking the schedule tree. 362 return isl_bool_error; 363 } 364 365 // Continue searching the subtree. 366 return isl_bool_true; 367 }; 368 isl_stat RetVal = isl_schedule_foreach_schedule_node_top_down( 369 Schedule.get(), Callback, nullptr); 370 371 // We assume that the traversal itself does not fail, i.e. the only reason to 372 // return isl_stat_error is that an extension node was found. 373 return RetVal == isl_stat_error; 374 } 375 376 /// Find a named MDNode property in a LoopID. 377 static MDNode *findOptionalNodeOperand(MDNode *LoopMD, StringRef Name) { 378 return dyn_cast_or_null<MDNode>( 379 findMetadataOperand(LoopMD, Name).getValueOr(nullptr)); 380 } 381 382 /// Is this node of type mark? 383 static bool isMark(const isl::schedule_node &Node) { 384 return isl_schedule_node_get_type(Node.get()) == isl_schedule_node_mark; 385 } 386 387 /// Is this node of type band? 388 static bool isBand(const isl::schedule_node &Node) { 389 return isl_schedule_node_get_type(Node.get()) == isl_schedule_node_band; 390 } 391 392 #ifndef NDEBUG 393 /// Is this node a band of a single dimension (i.e. could represent a loop)? 394 static bool isBandWithSingleLoop(const isl::schedule_node &Node) { 395 return isBand(Node) && isl_schedule_node_band_n_member(Node.get()) == 1; 396 } 397 #endif 398 399 static bool isLeaf(const isl::schedule_node &Node) { 400 return isl_schedule_node_get_type(Node.get()) == isl_schedule_node_leaf; 401 } 402 403 /// Create an isl::id representing the output loop after a transformation. 404 static isl::id createGeneratedLoopAttr(isl::ctx Ctx, MDNode *FollowupLoopMD) { 405 // Don't need to id the followup. 406 // TODO: Append llvm.loop.disable_heustistics metadata unless overridden by 407 // user followup-MD 408 if (!FollowupLoopMD) 409 return {}; 410 411 BandAttr *Attr = new BandAttr(); 412 Attr->Metadata = FollowupLoopMD; 413 return getIslLoopAttr(Ctx, Attr); 414 } 415 416 /// A loop consists of a band and an optional marker that wraps it. Return the 417 /// outermost of the two. 418 419 /// That is, either the mark or, if there is not mark, the loop itself. Can 420 /// start with either the mark or the band. 421 static isl::schedule_node moveToBandMark(isl::schedule_node BandOrMark) { 422 if (isBandMark(BandOrMark)) { 423 assert(isBandWithSingleLoop(BandOrMark.child(0))); 424 return BandOrMark; 425 } 426 assert(isBandWithSingleLoop(BandOrMark)); 427 428 isl::schedule_node Mark = BandOrMark.parent(); 429 if (isBandMark(Mark)) 430 return Mark; 431 432 // Band has no loop marker. 433 return BandOrMark; 434 } 435 436 static isl::schedule_node removeMark(isl::schedule_node MarkOrBand, 437 BandAttr *&Attr) { 438 MarkOrBand = moveToBandMark(MarkOrBand); 439 440 isl::schedule_node Band; 441 if (isMark(MarkOrBand)) { 442 Attr = getLoopAttr(MarkOrBand.as<isl::schedule_node_mark>().get_id()); 443 Band = isl::manage(isl_schedule_node_delete(MarkOrBand.release())); 444 } else { 445 Attr = nullptr; 446 Band = MarkOrBand; 447 } 448 449 assert(isBandWithSingleLoop(Band)); 450 return Band; 451 } 452 453 /// Remove the mark that wraps a loop. Return the band representing the loop. 454 static isl::schedule_node removeMark(isl::schedule_node MarkOrBand) { 455 BandAttr *Attr; 456 return removeMark(MarkOrBand, Attr); 457 } 458 459 static isl::schedule_node insertMark(isl::schedule_node Band, isl::id Mark) { 460 assert(isBand(Band)); 461 assert(moveToBandMark(Band).is_equal(Band) && 462 "Don't add a two marks for a band"); 463 464 return Band.insert_mark(Mark).child(0); 465 } 466 467 /// Return the (one-dimensional) set of numbers that are divisible by @p Factor 468 /// with remainder @p Offset. 469 /// 470 /// isDivisibleBySet(Ctx, 4, 0) = { [i] : floord(i,4) = 0 } 471 /// isDivisibleBySet(Ctx, 4, 1) = { [i] : floord(i,4) = 1 } 472 /// 473 static isl::basic_set isDivisibleBySet(isl::ctx &Ctx, long Factor, 474 long Offset) { 475 isl::val ValFactor{Ctx, Factor}; 476 isl::val ValOffset{Ctx, Offset}; 477 478 isl::space Unispace{Ctx, 0, 1}; 479 isl::local_space LUnispace{Unispace}; 480 isl::aff AffFactor{LUnispace, ValFactor}; 481 isl::aff AffOffset{LUnispace, ValOffset}; 482 483 isl::aff Id = isl::aff::var_on_domain(LUnispace, isl::dim::out, 0); 484 isl::aff DivMul = Id.mod(ValFactor); 485 isl::basic_map Divisible = isl::basic_map::from_aff(DivMul); 486 isl::basic_map Modulo = Divisible.fix_val(isl::dim::out, 0, ValOffset); 487 return Modulo.domain(); 488 } 489 490 /// Make the last dimension of Set to take values from 0 to VectorWidth - 1. 491 /// 492 /// @param Set A set, which should be modified. 493 /// @param VectorWidth A parameter, which determines the constraint. 494 static isl::set addExtentConstraints(isl::set Set, int VectorWidth) { 495 unsigned Dims = Set.tuple_dim().release(); 496 isl::space Space = Set.get_space(); 497 isl::local_space LocalSpace = isl::local_space(Space); 498 isl::constraint ExtConstr = isl::constraint::alloc_inequality(LocalSpace); 499 ExtConstr = ExtConstr.set_constant_si(0); 500 ExtConstr = ExtConstr.set_coefficient_si(isl::dim::set, Dims - 1, 1); 501 Set = Set.add_constraint(ExtConstr); 502 ExtConstr = isl::constraint::alloc_inequality(LocalSpace); 503 ExtConstr = ExtConstr.set_constant_si(VectorWidth - 1); 504 ExtConstr = ExtConstr.set_coefficient_si(isl::dim::set, Dims - 1, -1); 505 return Set.add_constraint(ExtConstr); 506 } 507 } // namespace 508 509 bool polly::isBandMark(const isl::schedule_node &Node) { 510 return isMark(Node) && 511 isLoopAttr(Node.as<isl::schedule_node_mark>().get_id()); 512 } 513 514 BandAttr *polly::getBandAttr(isl::schedule_node MarkOrBand) { 515 MarkOrBand = moveToBandMark(MarkOrBand); 516 if (!isMark(MarkOrBand)) 517 return nullptr; 518 519 return getLoopAttr(MarkOrBand.as<isl::schedule_node_mark>().get_id()); 520 } 521 522 isl::schedule polly::hoistExtensionNodes(isl::schedule Sched) { 523 // If there is no extension node in the first place, return the original 524 // schedule tree. 525 if (!containsExtensionNode(Sched)) 526 return Sched; 527 528 // Build options can anchor schedule nodes, such that the schedule tree cannot 529 // be modified anymore. Therefore, apply build options after the tree has been 530 // created. 531 CollectASTBuildOptions Collector; 532 Collector.visit(Sched); 533 534 // Rewrite the schedule tree without extension nodes. 535 ExtensionNodeRewriter Rewriter; 536 isl::schedule NewSched = Rewriter.visitSchedule(Sched); 537 538 // Reapply the AST build options. The rewriter must not change the iteration 539 // order of bands. Any other node type is ignored. 540 ApplyASTBuildOptions Applicator(Collector.ASTBuildOptions); 541 NewSched = Applicator.visitSchedule(NewSched); 542 543 return NewSched; 544 } 545 546 isl::schedule polly::applyFullUnroll(isl::schedule_node BandToUnroll) { 547 isl::ctx Ctx = BandToUnroll.ctx(); 548 549 // Remove the loop's mark, the loop will disappear anyway. 550 BandToUnroll = removeMark(BandToUnroll); 551 assert(isBandWithSingleLoop(BandToUnroll)); 552 553 isl::multi_union_pw_aff PartialSched = isl::manage( 554 isl_schedule_node_band_get_partial_schedule(BandToUnroll.get())); 555 assert(PartialSched.dim(isl::dim::out).release() == 1 && 556 "Can only unroll a single dimension"); 557 isl::union_pw_aff PartialSchedUAff = PartialSched.at(0); 558 559 isl::union_set Domain = BandToUnroll.get_domain(); 560 PartialSchedUAff = PartialSchedUAff.intersect_domain(Domain); 561 isl::union_map PartialSchedUMap = 562 isl::union_map::from(isl::union_pw_multi_aff(PartialSchedUAff)); 563 564 // Enumerator only the scatter elements. 565 isl::union_set ScatterList = PartialSchedUMap.range(); 566 567 // Enumerate all loop iterations. 568 // TODO: Diagnose if not enumerable or depends on a parameter. 569 SmallVector<isl::point, 16> Elts; 570 ScatterList.foreach_point([&Elts](isl::point P) -> isl::stat { 571 Elts.push_back(P); 572 return isl::stat::ok(); 573 }); 574 575 // Don't assume that foreach_point returns in execution order. 576 llvm::sort(Elts, [](isl::point P1, isl::point P2) -> bool { 577 isl::val C1 = P1.get_coordinate_val(isl::dim::set, 0); 578 isl::val C2 = P2.get_coordinate_val(isl::dim::set, 0); 579 return C1.lt(C2); 580 }); 581 582 // Convert the points to a sequence of filters. 583 isl::union_set_list List = isl::union_set_list(Ctx, Elts.size()); 584 for (isl::point P : Elts) { 585 // Determine the domains that map this scatter element. 586 isl::union_set DomainFilter = PartialSchedUMap.intersect_range(P).domain(); 587 588 List = List.add(DomainFilter); 589 } 590 591 // Replace original band with unrolled sequence. 592 isl::schedule_node Body = 593 isl::manage(isl_schedule_node_delete(BandToUnroll.release())); 594 Body = Body.insert_sequence(List); 595 return Body.get_schedule(); 596 } 597 598 isl::schedule polly::applyPartialUnroll(isl::schedule_node BandToUnroll, 599 int Factor) { 600 assert(Factor > 0 && "Positive unroll factor required"); 601 isl::ctx Ctx = BandToUnroll.ctx(); 602 603 // Remove the mark, save the attribute for later use. 604 BandAttr *Attr; 605 BandToUnroll = removeMark(BandToUnroll, Attr); 606 assert(isBandWithSingleLoop(BandToUnroll)); 607 608 isl::multi_union_pw_aff PartialSched = isl::manage( 609 isl_schedule_node_band_get_partial_schedule(BandToUnroll.get())); 610 611 // { Stmt[] -> [x] } 612 isl::union_pw_aff PartialSchedUAff = PartialSched.at(0); 613 614 // Here we assume the schedule stride is one and starts with 0, which is not 615 // necessarily the case. 616 isl::union_pw_aff StridedPartialSchedUAff = 617 isl::union_pw_aff::empty(PartialSchedUAff.get_space()); 618 isl::val ValFactor{Ctx, Factor}; 619 PartialSchedUAff.foreach_pw_aff([&StridedPartialSchedUAff, 620 &ValFactor](isl::pw_aff PwAff) -> isl::stat { 621 isl::space Space = PwAff.get_space(); 622 isl::set Universe = isl::set::universe(Space.domain()); 623 isl::pw_aff AffFactor{Universe, ValFactor}; 624 isl::pw_aff DivSchedAff = PwAff.div(AffFactor).floor().mul(AffFactor); 625 StridedPartialSchedUAff = StridedPartialSchedUAff.union_add(DivSchedAff); 626 return isl::stat::ok(); 627 }); 628 629 isl::union_set_list List = isl::union_set_list(Ctx, Factor); 630 for (auto i : seq<int>(0, Factor)) { 631 // { Stmt[] -> [x] } 632 isl::union_map UMap = 633 isl::union_map::from(isl::union_pw_multi_aff(PartialSchedUAff)); 634 635 // { [x] } 636 isl::basic_set Divisible = isDivisibleBySet(Ctx, Factor, i); 637 638 // { Stmt[] } 639 isl::union_set UnrolledDomain = UMap.intersect_range(Divisible).domain(); 640 641 List = List.add(UnrolledDomain); 642 } 643 644 isl::schedule_node Body = 645 isl::manage(isl_schedule_node_delete(BandToUnroll.copy())); 646 Body = Body.insert_sequence(List); 647 isl::schedule_node NewLoop = 648 Body.insert_partial_schedule(StridedPartialSchedUAff); 649 650 MDNode *FollowupMD = nullptr; 651 if (Attr && Attr->Metadata) 652 FollowupMD = 653 findOptionalNodeOperand(Attr->Metadata, LLVMLoopUnrollFollowupUnrolled); 654 655 isl::id NewBandId = createGeneratedLoopAttr(Ctx, FollowupMD); 656 if (!NewBandId.is_null()) 657 NewLoop = insertMark(NewLoop, NewBandId); 658 659 return NewLoop.get_schedule(); 660 } 661 662 isl::set polly::getPartialTilePrefixes(isl::set ScheduleRange, 663 int VectorWidth) { 664 isl_size Dims = ScheduleRange.tuple_dim().release(); 665 isl::set LoopPrefixes = 666 ScheduleRange.drop_constraints_involving_dims(isl::dim::set, Dims - 1, 1); 667 auto ExtentPrefixes = addExtentConstraints(LoopPrefixes, VectorWidth); 668 isl::set BadPrefixes = ExtentPrefixes.subtract(ScheduleRange); 669 BadPrefixes = BadPrefixes.project_out(isl::dim::set, Dims - 1, 1); 670 LoopPrefixes = LoopPrefixes.project_out(isl::dim::set, Dims - 1, 1); 671 return LoopPrefixes.subtract(BadPrefixes); 672 } 673 674 isl::union_set polly::getIsolateOptions(isl::set IsolateDomain, 675 isl_size OutDimsNum) { 676 isl_size Dims = IsolateDomain.tuple_dim().release(); 677 assert(OutDimsNum <= Dims && 678 "The isl::set IsolateDomain is used to describe the range of schedule " 679 "dimensions values, which should be isolated. Consequently, the " 680 "number of its dimensions should be greater than or equal to the " 681 "number of the schedule dimensions."); 682 isl::map IsolateRelation = isl::map::from_domain(IsolateDomain); 683 IsolateRelation = IsolateRelation.move_dims(isl::dim::out, 0, isl::dim::in, 684 Dims - OutDimsNum, OutDimsNum); 685 isl::set IsolateOption = IsolateRelation.wrap(); 686 isl::id Id = isl::id::alloc(IsolateOption.ctx(), "isolate", nullptr); 687 IsolateOption = IsolateOption.set_tuple_id(Id); 688 return isl::union_set(IsolateOption); 689 } 690 691 isl::union_set polly::getDimOptions(isl::ctx Ctx, const char *Option) { 692 isl::space Space(Ctx, 0, 1); 693 auto DimOption = isl::set::universe(Space); 694 auto Id = isl::id::alloc(Ctx, Option, nullptr); 695 DimOption = DimOption.set_tuple_id(Id); 696 return isl::union_set(DimOption); 697 } 698 699 isl::schedule_node polly::tileNode(isl::schedule_node Node, 700 const char *Identifier, 701 ArrayRef<int> TileSizes, 702 int DefaultTileSize) { 703 auto Space = isl::manage(isl_schedule_node_band_get_space(Node.get())); 704 auto Dims = Space.dim(isl::dim::set); 705 auto Sizes = isl::multi_val::zero(Space); 706 std::string IdentifierString(Identifier); 707 for (auto i : seq<isl_size>(0, Dims.release())) { 708 auto tileSize = 709 i < (isl_size)TileSizes.size() ? TileSizes[i] : DefaultTileSize; 710 Sizes = Sizes.set_val(i, isl::val(Node.ctx(), tileSize)); 711 } 712 auto TileLoopMarkerStr = IdentifierString + " - Tiles"; 713 auto TileLoopMarker = isl::id::alloc(Node.ctx(), TileLoopMarkerStr, nullptr); 714 Node = Node.insert_mark(TileLoopMarker); 715 Node = Node.child(0); 716 Node = 717 isl::manage(isl_schedule_node_band_tile(Node.release(), Sizes.release())); 718 Node = Node.child(0); 719 auto PointLoopMarkerStr = IdentifierString + " - Points"; 720 auto PointLoopMarker = 721 isl::id::alloc(Node.ctx(), PointLoopMarkerStr, nullptr); 722 Node = Node.insert_mark(PointLoopMarker); 723 return Node.child(0); 724 } 725 726 isl::schedule_node polly::applyRegisterTiling(isl::schedule_node Node, 727 ArrayRef<int> TileSizes, 728 int DefaultTileSize) { 729 Node = tileNode(Node, "Register tiling", TileSizes, DefaultTileSize); 730 auto Ctx = Node.ctx(); 731 return Node.as<isl::schedule_node_band>().set_ast_build_options( 732 isl::union_set(Ctx, "{unroll[x]}")); 733 } 734 735 /// Find statements and sub-loops in (possibly nested) sequences. 736 static void 737 collectFussionableStmts(isl::schedule_node Node, 738 SmallVectorImpl<isl::schedule_node> &ScheduleStmts) { 739 if (isBand(Node) || isLeaf(Node)) { 740 ScheduleStmts.push_back(Node); 741 return; 742 } 743 744 if (Node.has_children()) { 745 isl::schedule_node C = Node.first_child(); 746 while (true) { 747 collectFussionableStmts(C, ScheduleStmts); 748 if (!C.has_next_sibling()) 749 break; 750 C = C.next_sibling(); 751 } 752 } 753 } 754 755 isl::schedule polly::applyMaxFission(isl::schedule_node BandToFission) { 756 isl::ctx Ctx = BandToFission.ctx(); 757 BandToFission = removeMark(BandToFission); 758 isl::schedule_node BandBody = BandToFission.child(0); 759 760 SmallVector<isl::schedule_node> FissionableStmts; 761 collectFussionableStmts(BandBody, FissionableStmts); 762 size_t N = FissionableStmts.size(); 763 764 // Collect the domain for each of the statements that will get their own loop. 765 isl::union_set_list DomList = isl::union_set_list(Ctx, N); 766 for (size_t i = 0; i < N; ++i) { 767 isl::schedule_node BodyPart = FissionableStmts[i]; 768 DomList = DomList.add(BodyPart.get_domain()); 769 } 770 771 // Apply the fission by copying the entire loop, but inserting a filter for 772 // the statement domains for each fissioned loop. 773 isl::schedule_node Fissioned = BandToFission.insert_sequence(DomList); 774 775 return Fissioned.get_schedule(); 776 } 777