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

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 3 02:29:14 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)
----------------
fhahn wrote:

In terms of terminology, I think runtime unrolling currently requires the latch to have a computable exit count, but not that the whole loop has an exact, computable back edge-taken count.

Rotating the loop will make the exit count from the latch computable, but the whole loop still remains uncountable (i.e. some exit counts cannot be computed exactly).



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


More information about the llvm-commits mailing list