Skip to content

Commit f17d60d

Browse files
bjopetstellar
authored andcommitted
Inform pass manager when child loops are deleted
As part of the nontrivial unswitching we could end up removing child loops. This patch add a notification to the pass manager when that happens (using the markLoopAsDeleted callback). Without this there could be stale LoopAccessAnalysis results cached in the analysis manager. Those analysis results are cached based on a Loop* as key. Since the BumpPtrAllocator used to allocate Loop objects could be resetted between different runs of for example the loop-distribute pass (running on different functions), a new Loop object could be created using the same Loop pointer. And then when requiring the LoopAccessAnalysis for the loop we got the stale (corrupt) result from the destroyed loop. Reviewed By: aeubanks Differential Revision: https://reviews.llvm.org/D109257 (fixes PR51754) (cherry-picked from commit 0f0344d)
1 parent f56129f commit f17d60d

File tree

2 files changed

+102
-12
lines changed

2 files changed

+102
-12
lines changed

llvm/lib/Transforms/Scalar/SimpleLoopUnswitch.cpp

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1587,10 +1587,12 @@ deleteDeadClonedBlocks(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
15871587
BB->eraseFromParent();
15881588
}
15891589

1590-
static void deleteDeadBlocksFromLoop(Loop &L,
1591-
SmallVectorImpl<BasicBlock *> &ExitBlocks,
1592-
DominatorTree &DT, LoopInfo &LI,
1593-
MemorySSAUpdater *MSSAU) {
1590+
static void
1591+
deleteDeadBlocksFromLoop(Loop &L,
1592+
SmallVectorImpl<BasicBlock *> &ExitBlocks,
1593+
DominatorTree &DT, LoopInfo &LI,
1594+
MemorySSAUpdater *MSSAU,
1595+
function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
15941596
// Find all the dead blocks tied to this loop, and remove them from their
15951597
// successors.
15961598
SmallSetVector<BasicBlock *, 8> DeadBlockSet;
@@ -1640,6 +1642,7 @@ static void deleteDeadBlocksFromLoop(Loop &L,
16401642
}) &&
16411643
"If the child loop header is dead all blocks in the child loop must "
16421644
"be dead as well!");
1645+
DestroyLoopCB(*ChildL, ChildL->getName());
16431646
LI.destroy(ChildL);
16441647
return true;
16451648
});
@@ -1980,6 +1983,8 @@ static bool rebuildLoopAfterUnswitch(Loop &L, ArrayRef<BasicBlock *> ExitBlocks,
19801983
ParentL->removeChildLoop(llvm::find(*ParentL, &L));
19811984
else
19821985
LI.removeLoop(llvm::find(LI, &L));
1986+
// markLoopAsDeleted for L should be triggered by the caller (it is typically
1987+
// done by using the UnswitchCB callback).
19831988
LI.destroy(&L);
19841989
return false;
19851990
}
@@ -2019,7 +2024,8 @@ static void unswitchNontrivialInvariants(
20192024
SmallVectorImpl<BasicBlock *> &ExitBlocks, IVConditionInfo &PartialIVInfo,
20202025
DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
20212026
function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
2022-
ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2027+
ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2028+
function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
20232029
auto *ParentBB = TI.getParent();
20242030
BranchInst *BI = dyn_cast<BranchInst>(&TI);
20252031
SwitchInst *SI = BI ? nullptr : cast<SwitchInst>(&TI);
@@ -2319,7 +2325,7 @@ static void unswitchNontrivialInvariants(
23192325
// Now that our cloned loops have been built, we can update the original loop.
23202326
// First we delete the dead blocks from it and then we rebuild the loop
23212327
// structure taking these deletions into account.
2322-
deleteDeadBlocksFromLoop(L, ExitBlocks, DT, LI, MSSAU);
2328+
deleteDeadBlocksFromLoop(L, ExitBlocks, DT, LI, MSSAU, DestroyLoopCB);
23232329

23242330
if (MSSAU && VerifyMemorySSA)
23252331
MSSAU->getMemorySSA()->verifyMemorySSA();
@@ -2670,7 +2676,8 @@ static bool unswitchBestCondition(
26702676
Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
26712677
AAResults &AA, TargetTransformInfo &TTI,
26722678
function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
2673-
ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2679+
ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2680+
function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
26742681
// Collect all invariant conditions within this loop (as opposed to an inner
26752682
// loop which would be handled when visiting that inner loop).
26762683
SmallVector<std::pair<Instruction *, TinyPtrVector<Value *>>, 4>
@@ -2958,7 +2965,7 @@ static bool unswitchBestCondition(
29582965
<< "\n");
29592966
unswitchNontrivialInvariants(L, *BestUnswitchTI, BestUnswitchInvariants,
29602967
ExitBlocks, PartialIVInfo, DT, LI, AC,
2961-
UnswitchCB, SE, MSSAU);
2968+
UnswitchCB, SE, MSSAU, DestroyLoopCB);
29622969
return true;
29632970
}
29642971

@@ -2988,7 +2995,8 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
29882995
AAResults &AA, TargetTransformInfo &TTI, bool Trivial,
29892996
bool NonTrivial,
29902997
function_ref<void(bool, bool, ArrayRef<Loop *>)> UnswitchCB,
2991-
ScalarEvolution *SE, MemorySSAUpdater *MSSAU) {
2998+
ScalarEvolution *SE, MemorySSAUpdater *MSSAU,
2999+
function_ref<void(Loop &, StringRef)> DestroyLoopCB) {
29923000
assert(L.isRecursivelyLCSSAForm(DT, LI) &&
29933001
"Loops must be in LCSSA form before unswitching.");
29943002

@@ -3036,7 +3044,8 @@ unswitchLoop(Loop &L, DominatorTree &DT, LoopInfo &LI, AssumptionCache &AC,
30363044

30373045
// Try to unswitch the best invariant condition. We prefer this full unswitch to
30383046
// a partial unswitch when possible below the threshold.
3039-
if (unswitchBestCondition(L, DT, LI, AC, AA, TTI, UnswitchCB, SE, MSSAU))
3047+
if (unswitchBestCondition(L, DT, LI, AC, AA, TTI, UnswitchCB, SE, MSSAU,
3048+
DestroyLoopCB))
30403049
return true;
30413050

30423051
// No other opportunities to unswitch.
@@ -3083,6 +3092,10 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
30833092
U.markLoopAsDeleted(L, LoopName);
30843093
};
30853094

3095+
auto DestroyLoopCB = [&U](Loop &L, StringRef Name) {
3096+
U.markLoopAsDeleted(L, Name);
3097+
};
3098+
30863099
Optional<MemorySSAUpdater> MSSAU;
30873100
if (AR.MSSA) {
30883101
MSSAU = MemorySSAUpdater(AR.MSSA);
@@ -3091,7 +3104,8 @@ PreservedAnalyses SimpleLoopUnswitchPass::run(Loop &L, LoopAnalysisManager &AM,
30913104
}
30923105
if (!unswitchLoop(L, AR.DT, AR.LI, AR.AC, AR.AA, AR.TTI, Trivial, NonTrivial,
30933106
UnswitchCB, &AR.SE,
3094-
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr))
3107+
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
3108+
DestroyLoopCB))
30953109
return PreservedAnalyses::all();
30963110

30973111
if (AR.MSSA && VerifyMemorySSA)
@@ -3179,12 +3193,17 @@ bool SimpleLoopUnswitchLegacyPass::runOnLoop(Loop *L, LPPassManager &LPM) {
31793193
LPM.markLoopAsDeleted(*L);
31803194
};
31813195

3196+
auto DestroyLoopCB = [&LPM](Loop &L, StringRef /* Name */) {
3197+
LPM.markLoopAsDeleted(L);
3198+
};
3199+
31823200
if (MSSA && VerifyMemorySSA)
31833201
MSSA->verifyMemorySSA();
31843202

31853203
bool Changed =
31863204
unswitchLoop(*L, DT, LI, AC, AA, TTI, true, NonTrivial, UnswitchCB, SE,
3187-
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr);
3205+
MSSAU.hasValue() ? MSSAU.getPointer() : nullptr,
3206+
DestroyLoopCB);
31883207

31893208
if (MSSA && VerifyMemorySSA)
31903209
MSSA->verifyMemorySSA();
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
; RUN: opt < %s -enable-loop-distribute -passes='loop-distribute,loop-mssa(simple-loop-unswitch<nontrivial>),loop-distribute' -o /dev/null -S -debug-pass-manager=verbose 2>&1 | FileCheck %s
2+
3+
4+
; Running loop-distribute will result in LoopAccessAnalysis being required and
5+
; cached in the LoopAnalysisManagerFunctionProxy.
6+
;
7+
; CHECK: Running analysis: LoopAccessAnalysis on Loop at depth 2 containing: %loop_a_inner<header><latch><exiting>
8+
9+
10+
; Then simple-loop-unswitch is removing/replacing some loops (resulting in
11+
; Loop objects used as key in the analyses cache is destroyed). So here we
12+
; want to see that any analysis results cached on the destroyed loop is
13+
; cleared. A special case here is that loop_a_inner is destroyed when
14+
; unswitching the parent loop.
15+
;
16+
; The bug solved and verified by this test case was related to the
17+
; SimpleLoopUnswitch not marking the Loop as removed, so we missed clearing
18+
; the analysis caches.
19+
;
20+
; CHECK: Running pass: SimpleLoopUnswitchPass on Loop at depth 1 containing: %loop_begin<header>,%loop_b,%loop_b_inner,%loop_b_inner_exit,%loop_a,%loop_a_inner,%loop_a_inner_exit,%latch<latch><exiting>
21+
; CHECK-NEXT: Clearing all analysis results for: loop_a_inner
22+
23+
24+
; When running loop-distribute the second time we can see that loop_a_inner
25+
; isn't analysed because the loop no longer exists (instead we find a new loop,
26+
; loop_a_inner.us). This kind of verifies that it was correct to remove the
27+
; loop_a_inner related analysis above.
28+
;
29+
; CHECK: Running analysis: LoopAccessAnalysis on Loop at depth 2 containing: %loop_a_inner.us<header><latch><exiting>
30+
31+
32+
define i32 @test6(i1* %ptr, i1 %cond1, i32* %a.ptr, i32* %b.ptr) {
33+
entry:
34+
br label %loop_begin
35+
36+
loop_begin:
37+
%v = load i1, i1* %ptr
38+
br i1 %cond1, label %loop_a, label %loop_b
39+
40+
loop_a:
41+
br label %loop_a_inner
42+
43+
loop_a_inner:
44+
%va = load i1, i1* %ptr
45+
%a = load i32, i32* %a.ptr
46+
br i1 %va, label %loop_a_inner, label %loop_a_inner_exit
47+
48+
loop_a_inner_exit:
49+
%a.lcssa = phi i32 [ %a, %loop_a_inner ]
50+
br label %latch
51+
52+
loop_b:
53+
br label %loop_b_inner
54+
55+
loop_b_inner:
56+
%vb = load i1, i1* %ptr
57+
%b = load i32, i32* %b.ptr
58+
br i1 %vb, label %loop_b_inner, label %loop_b_inner_exit
59+
60+
loop_b_inner_exit:
61+
%b.lcssa = phi i32 [ %b, %loop_b_inner ]
62+
br label %latch
63+
64+
latch:
65+
%ab.phi = phi i32 [ %a.lcssa, %loop_a_inner_exit ], [ %b.lcssa, %loop_b_inner_exit ]
66+
br i1 %v, label %loop_begin, label %loop_exit
67+
68+
loop_exit:
69+
%ab.lcssa = phi i32 [ %ab.phi, %latch ]
70+
ret i32 %ab.lcssa
71+
}

0 commit comments

Comments
 (0)