Skip to content

Commit 889bbf6

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 df3bc54 commit 889bbf6

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");
@@ -1755,10 +1752,17 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
17551752
// Ensure minimum depth of the loop nest to do the interchange.
17561753
if (!hasSupportedLoopDepth(LoopList, ORE))
17571754
return PreservedAnalyses::all();
1755+
1756+
// Ensure computable loop nest.
1757+
if (!isComputableLoopNest(&AR.SE, LoopList)) {
1758+
LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
1759+
return PreservedAnalyses::all();
1760+
}
1761+
17581762
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
17591763
std::unique_ptr<CacheCost> CC =
17601764
CacheCost::getCacheCost(LN.getOutermostLoop(), AR, DI);
1761-
1765+
17621766
if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, CC, &ORE).run(LN))
17631767
return PreservedAnalyses::all();
17641768
U.markLoopNestChanged(true);

0 commit comments

Comments
 (0)