[llvm] [LoopUnroll] Rotate loop before unrolling inside of UnrollRuntimeLoopRemainder (PR #148243)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Jul 28 07:23:21 PDT 2025
================
@@ -586,10 +587,31 @@ bool llvm::UnrollRuntimeLoopRemainder(
LLVM_DEBUG(UseEpilogRemainder ? dbgs() << "Using epilog remainder.\n"
: dbgs() << "Using prolog remainder.\n");
+ LoopReminderUnrollResult Result = LoopReminderUnrollResult::Unmodified;
+
+ // Rotate loop if it makes the exit count from the latch computable.
+ BasicBlock *OrigHeader = L->getHeader();
+ BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
+ if (BI && !BI->isUnconditional() &&
+ isa<SCEVCouldNotCompute>(SE->getExitCount(L, L->getLoopLatch())) &&
+ !isa<SCEVCouldNotCompute>(SE->getExitCount(L, OrigHeader))) {
+ LLVM_DEBUG(
+ dbgs() << " Rotating loop to make the exit count computable.\n");
+ SimplifyQuery SQ{OrigHeader->getDataLayout()};
+ SQ.TLI = nullptr;
+ SQ.DT = DT;
+ SQ.AC = AC;
+ if (llvm::LoopRotation(L, LI, TTI, AC, DT, SE, nullptr /*MemorySSAUpdater*/,
----------------
annamthomas wrote:
Why do this here? Specifically, doing this here as the 1st thing or in LoopUnroll caller doesn't reduce the chances of rotation without unrolling much.
We can do this after all the legality and profitability checks of `UnrollRuntimeLoopRemainder`.
Specifically at this part:
```
// Only unroll loops with a computable trip count.
// We calculate the backedge count by using getExitCount on the Latch block,
// which is proven to be the only exiting block in this loop. This is same as
// calculating getBackedgeTakenCount on the loop (which computes SCEV for all
// exiting blocks).
const SCEV *BECountSC = SE->getExitCount(L, Latch);
if (isa<SCEVCouldNotCompute>(BECountSC)) {
LLVM_DEBUG(dbgs() << "Could not compute exit block SCEV\n");
return false;
}
unsigned BEWidth = cast<IntegerType>(BECountSC->getType())->getBitWidth();
// Add 1 since the backedge count doesn't include the first loop iteration.
// (Note that overflow can occur, this is handled explicitly below)
const SCEV *TripCountSC =
SE->getAddExpr(BECountSC, SE->getConstant(BECountSC->getType(), 1));
if (isa<SCEVCouldNotCompute>(TripCountSC)) {
LLVM_DEBUG(dbgs() << "Could not compute trip count SCEV.\n");
return false;
}
```
Do both the checks on `BECountSC` with Header as the block, if the latch BE count fails the check.
However, we still need to redo the previous checks.
https://github.com/llvm/llvm-project/pull/148243
More information about the llvm-commits
mailing list