Skip to content

Commit e3129fb

Browse files
committed
[LoopFlatten] Mark inner loop as deleted
If a loop is flattened, the inner loop is removed and the LPM should be informed of this fact, so it can invalidate associated analyses. To support this, we relax an assertion in LPMUpdater to allow invalidating non-top-level loops when running in LoopNestMode, as the pass does not know how exactly it will get scheduled. Differential Revision: https://reviews.llvm.org/D111350
1 parent d1aaef4 commit e3129fb

File tree

2 files changed

+10
-10
lines changed

2 files changed

+10
-10
lines changed

llvm/include/llvm/Transforms/Scalar/LoopPassManager.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,6 @@ class LPMUpdater {
281281
/// state, this routine will mark that the current loop should be skipped by
282282
/// the rest of the pass management infrastructure.
283283
void markLoopAsDeleted(Loop &L, llvm::StringRef Name) {
284-
assert((!LoopNestMode || CurrentL == &L) &&
285-
"L should be a top-level loop in loop-nest mode.");
286284
LAM.clear(L, Name);
287285
assert((&L == CurrentL || CurrentL->contains(&L)) &&
288286
"Cannot delete a loop outside of the "

llvm/lib/Transforms/Scalar/LoopFlatten.cpp

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ static bool CanFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
606606

607607
static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
608608
ScalarEvolution *SE, AssumptionCache *AC,
609-
const TargetTransformInfo *TTI) {
609+
const TargetTransformInfo *TTI, LPMUpdater *U) {
610610
Function *F = FI.OuterLoop->getHeader()->getParent();
611611
LLVM_DEBUG(dbgs() << "Checks all passed, doing the transformation\n");
612612
{
@@ -662,6 +662,8 @@ static bool DoFlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
662662
// deleted, and any information that have about the outer loop invalidated.
663663
SE->forgetLoop(FI.OuterLoop);
664664
SE->forgetLoop(FI.InnerLoop);
665+
if (U)
666+
U->markLoopAsDeleted(*FI.InnerLoop, FI.InnerLoop->getName());
665667
LI->erase(FI.InnerLoop);
666668

667669
// Increment statistic value.
@@ -737,7 +739,7 @@ static bool CanWidenIV(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
737739

738740
static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
739741
ScalarEvolution *SE, AssumptionCache *AC,
740-
const TargetTransformInfo *TTI) {
742+
const TargetTransformInfo *TTI, LPMUpdater *U) {
741743
LLVM_DEBUG(
742744
dbgs() << "Loop flattening running on outer loop "
743745
<< FI.OuterLoop->getHeader()->getName() << " and inner loop "
@@ -766,7 +768,7 @@ static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
766768

767769
// If we have widened and can perform the transformation, do that here.
768770
if (CanFlatten)
769-
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
771+
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
770772

771773
// Otherwise, if we haven't widened the IV, check if the new iteration
772774
// variable might overflow. In this case, we need to version the loop, and
@@ -784,18 +786,18 @@ static bool FlattenLoopPair(FlattenInfo &FI, DominatorTree *DT, LoopInfo *LI,
784786
}
785787

786788
LLVM_DEBUG(dbgs() << "Multiply cannot overflow, modifying loop in-place\n");
787-
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI);
789+
return DoFlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
788790
}
789791

790792
bool Flatten(LoopNest &LN, DominatorTree *DT, LoopInfo *LI, ScalarEvolution *SE,
791-
AssumptionCache *AC, TargetTransformInfo *TTI) {
793+
AssumptionCache *AC, TargetTransformInfo *TTI, LPMUpdater *U) {
792794
bool Changed = false;
793795
for (Loop *InnerLoop : LN.getLoops()) {
794796
auto *OuterLoop = InnerLoop->getParentLoop();
795797
if (!OuterLoop)
796798
continue;
797799
FlattenInfo FI(OuterLoop, InnerLoop);
798-
Changed |= FlattenLoopPair(FI, DT, LI, SE, AC, TTI);
800+
Changed |= FlattenLoopPair(FI, DT, LI, SE, AC, TTI, U);
799801
}
800802
return Changed;
801803
}
@@ -810,7 +812,7 @@ PreservedAnalyses LoopFlattenPass::run(LoopNest &LN, LoopAnalysisManager &LAM,
810812
// in simplified form, and also needs LCSSA. Running
811813
// this pass will simplify all loops that contain inner loops,
812814
// regardless of whether anything ends up being flattened.
813-
Changed |= Flatten(LN, &AR.DT, &AR.LI, &AR.SE, &AR.AC, &AR.TTI);
815+
Changed |= Flatten(LN, &AR.DT, &AR.LI, &AR.SE, &AR.AC, &AR.TTI, &U);
814816

815817
if (!Changed)
816818
return PreservedAnalyses::all();
@@ -860,7 +862,7 @@ bool LoopFlattenLegacyPass::runOnFunction(Function &F) {
860862
bool Changed = false;
861863
for (Loop *L : *LI) {
862864
auto LN = LoopNest::getLoopNest(*L, *SE);
863-
Changed |= Flatten(*LN, DT, LI, SE, AC, TTI);
865+
Changed |= Flatten(*LN, DT, LI, SE, AC, TTI, nullptr);
864866
}
865867
return Changed;
866868
}

0 commit comments

Comments
 (0)