[llvm] [LoopUnroll] Rotate loop to make it countable for runtime unrolling (PR #146540)

via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 9 06:02:17 PDT 2025


================
@@ -484,8 +485,27 @@ llvm::UnrollLoop(Loop *L, UnrollLoopOptions ULO, LoopInfo *LI,
 
   assert(ULO.Count > 0);
 
-  // All these values should be taken only after peeling because they might have
-  // changed.
+  if (ULO.Runtime && SE) {
+    BasicBlock *OrigHeader = L->getHeader();
+    BranchInst *BI = dyn_cast<BranchInst>(OrigHeader->getTerminator());
+    // Rotate loop if it makes it countable (for later unrolling)
+    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 loop countable.\n");
+      SimplifyQuery SQ{OrigHeader->getDataLayout()};
+      SQ.TLI = nullptr;
+      SQ.DT = DT;
+      SQ.AC = AC;
+      llvm::LoopRotation(L, LI, TTI, AC, DT, SE, nullptr /*MemorySSAUpdater*/,
----------------
annamthomas wrote:

> I could also try to move this rotation into the UnrollRuntimeLoopRemainder and then recompute all the resources used in unroll pass, which are currently collected after the rotation, but I don't think that would change much.

Just to clarify, even if this was done, there is no guarantee that unrolling will definitely happen if we were to rotate. There are further checks on the loop within `UnrollRuntimeLoopRemainder` which would need to be rerun on the rotated loop. 

The check where we bail out in runtime unrolling's `UnrollRuntimeLoopRemainder` without rotation is here:
```
   // 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;
    }
 ```

We then use this info `TripCountSC` to do further legality checks on runtime unrolling 

https://github.com/llvm/llvm-project/pull/146540


More information about the llvm-commits mailing list