Skip to content

Commit fad4645

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 749372b commit fad4645

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
@@ -276,6 +276,26 @@ static bool hasSupportedLoopDepth(SmallVectorImpl<Loop *> &LoopList,
276276
}
277277
return true;
278278
}
279+
280+
static bool isComputableLoopNest(ScalarEvolution *SE, ArrayRef<Loop *> LoopList) {
281+
for (Loop *L : LoopList) {
282+
const SCEV *ExitCountOuter = SE->getBackedgeTakenCount(L);
283+
if (isa<SCEVCouldNotCompute>(ExitCountOuter)) {
284+
LLVM_DEBUG(dbgs() << "Couldn't compute backedge count\n");
285+
return false;
286+
}
287+
if (L->getNumBackEdges() != 1) {
288+
LLVM_DEBUG(dbgs() << "NumBackEdges is not equal to 1\n");
289+
return false;
290+
}
291+
if (!L->getExitingBlock()) {
292+
LLVM_DEBUG(dbgs() << "Loop doesn't have unique exit block\n");
293+
return false;
294+
}
295+
}
296+
return true;
297+
}
298+
279299
namespace {
280300

281301
/// LoopInterchangeLegality checks if it is legal to interchange the loop.
@@ -431,25 +451,6 @@ struct LoopInterchange {
431451
return processLoopList(LoopList);
432452
}
433453

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

466467
unsigned LoopNestDepth = LoopList.size();
467-
if (!isComputableLoopNest(LoopList)) {
468-
LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
469-
return false;
470-
}
471468

472469
LLVM_DEBUG(dbgs() << "Processing LoopList of size = " << LoopNestDepth
473470
<< "\n");
@@ -1761,10 +1758,17 @@ PreservedAnalyses LoopInterchangePass::run(LoopNest &LN,
17611758
// Ensure minimum depth of the loop nest to do the interchange.
17621759
if (!hasSupportedLoopDepth(LoopList, ORE))
17631760
return PreservedAnalyses::all();
1761+
1762+
// Ensure computable loop nest.
1763+
if (!isComputableLoopNest(&AR.SE, LoopList)) {
1764+
LLVM_DEBUG(dbgs() << "Not valid loop candidate for interchange\n");
1765+
return PreservedAnalyses::all();
1766+
}
1767+
17641768
DependenceInfo DI(&F, &AR.AA, &AR.SE, &AR.LI);
17651769
std::unique_ptr<CacheCost> CC =
17661770
CacheCost::getCacheCost(LN.getOutermostLoop(), AR, DI);
1767-
1771+
17681772
if (!LoopInterchange(&AR.SE, &AR.LI, &DI, &AR.DT, CC, &ORE).run(LN))
17691773
return PreservedAnalyses::all();
17701774
U.markLoopNestChanged(true);

0 commit comments

Comments
 (0)