Skip to content

Commit dc9cfca

Browse files
committed
[LoopInterchange] Hoist isCompuatableLoopNest() in the control flow
The profiling of the LLVM Test-suite reveals that a significant portion, specifically 14,090 out of 139,323, loop nests were identified as non-viable candidates for transformation, leading to the transform exiting from isComputableLoopNest() without any action. More importantly, dependence information was computed for these loop nests before reaching the function isComputableLoopNest(), which does not require DI and relies solely on scalar evolution (SE). To enhance compile-time efficiency, this patch moves the call to isComputableLoopNest() earlier in the control-flow, thereby avoiding unnecessary dependence calculations. The impact of this change is evident on the compile-time-tracker, with the overall geometric mean improvement recorded at 0.11%, while the lencode benchmark gets a more substantial benefit of 0.44%. This improvement can be tracked in the isc-ln-exp-2 branch under my repo.
1 parent f10441a commit dc9cfca

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,26 @@ static bool hasSupportedLoopDepth(SmallVectorImpl<Loop *> &LoopList,
271271
}
272272
return true;
273273
}
274+
275+
static bool isComputableLoopNest(ScalarEvolution *SE, ArrayRef<Loop *> LoopList) {
276+
for (Loop *L : LoopList) {
277+
const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
278+
if (isa<SCEVCouldNotCompute>(ExitCountOuter)) {
279+
LLVM_DEBUG(dbgs() << "Couldn't compute backedge count\n");
280+
return false;
281+
}
282+
if (L->getNumBackEdges() != 1) {
283+
LLVM_DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
284+
return false;
285+
}
286+
if (!L->getExitingBlock()) {
287+
LLVM_DEBUG(dbgs() << "Loop doesn't have unique exit block\n");
288+
return false;
289+
}
290+
}
291+
return true;
292+
}
293+
274294
namespace {
275295

276296
/// LoopInterchangeLegality checks if it is legal to interchange the loop.
@@ -426,25 +446,6 @@ struct LoopInterchange {
426446
return processLoopList(LoopList);
427447
}
428448

429-
bool isComputableLoopNest(ArrayRef<Loop *> LoopList) {
430-
for (Loop *L : LoopList) {
431-
const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
432-
if (isa<SCEVCouldNotCompute>(ExitCountOuter)) {
433-
LLVM_DEBUG(dbgs() << "Couldn't compute backedge count\n");
434-
return false;
435-
}
436-
if (L->getNumBackEdges() != 1) {
437-
LLVM_DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
438-
return false;
439-
}
440-
if (!L->getExitingBlock()) {
441-
LLVM_DEBUG(dbgs() << "Loop doesn't have unique exit block\n");
442-
return false;
443-
}
444-
}
445-
return true;
446-
}
447-
448449
unsigned selectLoopForInterchange(ArrayRef<Loop *> LoopList) {
449450
// TODO: Add a better heuristic to select the loop to be interchanged based
450451
// on the dependence matrix. Currently we select the innermost loop.
@@ -459,10 +460,6 @@ struct LoopInterchange {
459460
"Unsupported depth of loop nest.");
460461

461462
unsigned LoopNestDepth = LoopList.size();
462-
if (!isComputableLoopNest(LoopList)) {
463-
LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
464-
return false;
465-
}
466463

467464
LLVM_DEBUG(dbgs() << "Processing LoopList of size = " << LoopNestDepth
468465
<< "\n");
@@ -1756,10 +1753,17 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
17561753
// Ensure minimum depth of the loop nest to do the interchange.
17571754
if (!hasSupportedLoopDepth(LoopList, ORE))
17581755
return PreservedAnalyses::all();
1756+
1757+
// Ensure computable loop nest.
1758+
if (!isComputableLoopNest(&AR.SE, LoopList)) {
1759+
LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
1760+
return PreservedAnalyses::all();
1761+
}
1762+
17591763
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
17601764
std::unique_ptr<CacheCost> CC =
17611765
CacheCost::getCacheCost(LN.getOutermostLoop(), AR, DI);
1762-
1766+
17631767
if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, CC, &ORE).run(LN))
17641768
return PreservedAnalyses::all();
17651769
U.markLoopNestChanged(true);

0 commit comments

Comments
 (0)