[llvm] [LoopUnroll] Use branch probability in multi-exit loop unrolling (PR #164799)

Joel E. Denny via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 20 08:14:22 PST 2026


Marek =?utf-8?q?Sedláček?= <mr.mareksedlacek at gmail.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/164799 at github.com>


================
@@ -557,24 +557,42 @@ static bool canProfitablyRuntimeUnrollMultiExitLoop(
   // We avoid unrolling loops that have more than two exiting blocks. This
   // limits the total number of branches in the unrolled loop to be atmost
   // the unroll factor (since one of the exiting blocks is the latch block).
+  
+  // Allow unrolling of loops with no non latch exit blocks.
+  if (OtherExits.size() == 0)
+    return true;
+  
   SmallVector<BasicBlock*, 4> ExitingBlocks;
   L->getExitingBlocks(ExitingBlocks);
-  if (ExitingBlocks.size() > 2)
+  if (ExitingBlocks.size() > 2 || OtherExits.size() != 1)
     return false;
 
-  // Allow unrolling of loops with no non latch exit blocks.
-  if (OtherExits.size() == 0)
+  // When UnrollRuntimeOtherExitPredictable is specified, we assume the other
+  // exit branch is predictable even if it has no deoptimize call.
+  if (UnrollRuntimeOtherExitPredictable)
     return true;
 
   // The second heuristic is that L has one exit other than the latchexit and
-  // that exit is a deoptimize block. We know that deoptimize blocks are rarely
-  // taken, which also implies the branch leading to the deoptimize block is
-  // highly predictable. When UnrollRuntimeOtherExitPredictable is specified, we
-  // assume the other exit branch is predictable even if it has no deoptimize
-  // call.
+  // that exit is highly predictable.
----------------
jdenny-ornl wrote:

I agree it's the existing wording.  I'm just hoping we can clarify it while we're here because I stumbled over it.  Thanks for considering the suggestion.

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


More information about the llvm-commits mailing list