Skip to content

Commit c29236c

Browse files
committed
When sort/uniqueing exiting regions for processing, unique not by pointer value, but by ID.
1 parent f3c8c5a commit c29236c

File tree

3 files changed

+28
-7
lines changed

3 files changed

+28
-7
lines changed

include/swift/Basic/STLExtras.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,21 @@ void sortUnique(
558558
C.erase(std::unique(C.begin(), C.end()), C.end());
559559
}
560560

561+
/// Sorts and then uniques a container with random access iterators and an erase
562+
/// method that removes a range specified by random access iterators.
563+
template <typename Container, typename Comparator>
564+
void sortUnique(
565+
Container &C,
566+
Comparator Cmp,
567+
typename std::enable_if<
568+
std::is_same<typename std::iterator_traits<
569+
typename Container::iterator>::iterator_category,
570+
std::random_access_iterator_tag>::value,
571+
void>::type * = nullptr) {
572+
std::sort(C.begin(), C.end(), Cmp);
573+
C.erase(std::unique(C.begin(), C.end()), C.end());
574+
}
575+
561576
} // end namespace swift
562577

563578
#endif

lib/SILOptimizer/Analysis/LoopRegionAnalysis.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,13 @@ getExitingRegions(LoopRegionFunctionInfo *LRFI,
526526
// We can have a loop subregion that has multiple exiting edges from the
527527
// current loop. We do not want to visit that loop subregion multiple
528528
// times. So we unique the exiting region list.
529-
sortUnique(ExitingRegions);
529+
//
530+
// In order to make sure we have a deterministic ordering when we visiting
531+
// exiting subregions, we need to sort our exiting regions by ID, not pointer
532+
// value.
533+
sortUnique(ExitingRegions, [](LoopRegion *R1, LoopRegion *R2) -> bool {
534+
return R1->getID() < R2->getID();
535+
});
530536
}
531537

532538
/// For each exiting block:

test/SILOptimizer/loop-region-analysis.sil

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1724,7 +1724,7 @@ bb4:
17241724
// CHECK-NEXT: (succs)
17251725
// CHECK-NEXT: (subregs)
17261726
// CHECK-NEXT: (non-local-succs
1727-
// CHECK-NEXT: (parentindex:1)))
1727+
// CHECK-NEXT: (parentindex:0)))
17281728
// CHECK: (region id:9 kind:loop ucfh:false ucft:false parent:8
17291729
// CHECK-NEXT: (preds
17301730
// CHECK-NEXT: (region id:1))
@@ -1734,7 +1734,7 @@ bb4:
17341734
// CHECK-NEXT: (region id:2)
17351735
// CHECK-NEXT: (region id:3))
17361736
// CHECK-NEXT: (non-local-succs
1737-
// CHECK-NEXT: (parentindex:0)))
1737+
// CHECK-NEXT: (parentindex:1)))
17381738
// CHECK: (region id:3 kind:bb ucfh:false ucft:false parent:9
17391739
// CHECK-NEXT: (preds
17401740
// CHECK-NEXT: (region id:2))
@@ -1823,7 +1823,7 @@ bb6:
18231823
// CHECK-NEXT: (succs)
18241824
// CHECK-NEXT: (subregs)
18251825
// CHECK-NEXT: (non-local-succs
1826-
// CHECK-NEXT: (parentindex:1)))
1826+
// CHECK-NEXT: (parentindex:0)))
18271827
// CHECK: (region id:9 kind:loop ucfh:false ucft:false parent:8
18281828
// CHECK-NEXT: (preds
18291829
// CHECK-NEXT: (region id:1))
@@ -1833,7 +1833,7 @@ bb6:
18331833
// CHECK-NEXT: (region id:2)
18341834
// CHECK-NEXT: (region id:3))
18351835
// CHECK-NEXT: (non-local-succs
1836-
// CHECK-NEXT: (parentindex:0)))
1836+
// CHECK-NEXT: (parentindex:1)))
18371837
// CHECK: (region id:3 kind:bb ucfh:false ucft:false parent:9
18381838
// CHECK-NEXT: (preds
18391839
// CHECK-NEXT: (region id:2))
@@ -2026,7 +2026,7 @@ bb4:
20262026
// CHECK-NEXT: (succs)
20272027
// CHECK-NEXT: (subregs)
20282028
// CHECK-NEXT: (non-local-succs
2029-
// CHECK-NEXT: (parentindex:2)))
2029+
// CHECK-NEXT: (parentindex:1)))
20302030
// CHECK: (region id:12 kind:loop ucfh:false ucft:false parent:11
20312031
// CHECK-NEXT: (preds
20322032
// CHECK-NEXT: (region id:1))
@@ -2036,7 +2036,7 @@ bb4:
20362036
// CHECK-NEXT: (region id:2)
20372037
// CHECK-NEXT: (region id:3))
20382038
// CHECK-NEXT: (non-local-succs
2039-
// CHECK-NEXT: (parentindex:1)))
2039+
// CHECK-NEXT: (parentindex:2)))
20402040
// CHECK: (region id:3 kind:bb ucfh:false ucft:false parent:12
20412041
// CHECK-NEXT: (preds
20422042
// CHECK-NEXT: (region id:2))

0 commit comments

Comments
 (0)