@@ -636,20 +636,36 @@ LogicalResult mlir::tryToPromoteMemorySlots(
636
636
// lazily and cached to avoid expensive recomputation.
637
637
BlockIndexCache blockIndexCache;
638
638
639
- for (PromotableAllocationOpInterface allocator : allocators) {
640
- for (MemorySlot slot : allocator.getPromotableSlots ()) {
641
- if (slot.ptr .use_empty ())
642
- continue ;
643
-
644
- MemorySlotPromotionAnalyzer analyzer (slot, dominance, dataLayout);
645
- std::optional<MemorySlotPromotionInfo> info = analyzer.computeInfo ();
646
- if (info) {
647
- MemorySlotPromoter (slot, allocator, builder, dominance, dataLayout,
648
- std::move (*info), statistics, blockIndexCache)
649
- .promoteSlot ();
650
- promotedAny = true ;
639
+ SmallVector<PromotableAllocationOpInterface> workList (allocators.begin (),
640
+ allocators.end ());
641
+
642
+ SmallVector<PromotableAllocationOpInterface> newWorkList;
643
+ newWorkList.reserve (workList.size ());
644
+ while (true ) {
645
+ for (PromotableAllocationOpInterface allocator : workList) {
646
+ for (MemorySlot slot : allocator.getPromotableSlots ()) {
647
+ if (slot.ptr .use_empty ())
648
+ continue ;
649
+
650
+ MemorySlotPromotionAnalyzer analyzer (slot, dominance, dataLayout);
651
+ std::optional<MemorySlotPromotionInfo> info = analyzer.computeInfo ();
652
+ if (info) {
653
+ MemorySlotPromoter (slot, allocator, builder, dominance, dataLayout,
654
+ std::move (*info), statistics, blockIndexCache)
655
+ .promoteSlot ();
656
+ promotedAny = true ;
657
+ continue ;
658
+ }
659
+ newWorkList.push_back (allocator);
651
660
}
652
661
}
662
+ if (workList.size () == newWorkList.size ())
663
+ break ;
664
+
665
+ // Swap the vector's backing memory and clear the entries in newWorkList
666
+ // afterwards. This ensures that additional heap allocations can be avoided.
667
+ workList.swap (newWorkList);
668
+ newWorkList.clear ();
653
669
}
654
670
655
671
return success (promotedAny);
@@ -677,22 +693,16 @@ struct Mem2Reg : impl::Mem2RegBase<Mem2Reg> {
677
693
678
694
OpBuilder builder (®ion.front (), region.front ().begin ());
679
695
680
- // Promoting a slot can allow for further promotion of other slots,
681
- // promotion is tried until no promotion succeeds.
682
- while (true ) {
683
- SmallVector<PromotableAllocationOpInterface> allocators;
684
- // Build a list of allocators to attempt to promote the slots of.
685
- region.walk ([&](PromotableAllocationOpInterface allocator) {
686
- allocators.emplace_back (allocator);
687
- });
688
-
689
- // Attempt promoting until no promotion succeeds.
690
- if (failed (tryToPromoteMemorySlots (allocators, builder, dataLayout,
691
- dominance, statistics)))
692
- break ;
696
+ SmallVector<PromotableAllocationOpInterface> allocators;
697
+ // Build a list of allocators to attempt to promote the slots of.
698
+ region.walk ([&](PromotableAllocationOpInterface allocator) {
699
+ allocators.emplace_back (allocator);
700
+ });
693
701
702
+ // Attempt promoting as many of the slots as possible.
703
+ if (succeeded (tryToPromoteMemorySlots (allocators, builder, dataLayout,
704
+ dominance, statistics)))
694
705
changed = true ;
695
- }
696
706
}
697
707
if (!changed)
698
708
markAllAnalysesPreserved ();
0 commit comments