[llvm] [LoopUnroll] Don't runtime-unroll multi-exit loops with a divergent side exit (PR #224924)

Yaxun Liu via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 04:45:55 PDT 2026


================
@@ -580,13 +581,19 @@ static bool canProfitablyRuntimeUnrollMultiExitLoop(
     assert(LatchBB && "Expected loop to have a latch");
     BasicBlock *NonLatchExitingBlock =
         (ExitingBlocks[0] == LatchBB) ? ExitingBlocks[1] : ExitingBlocks[0];
-    auto BranchProb =
-        llvm::getBranchProbability(NonLatchExitingBlock, OtherExits[0]);
-    // If BranchProbability could not be extracted (returns unknown), then
-    // don't return and do the check for deopt block.
-    if (!BranchProb.isUnknown()) {
-      auto Threshold = TTI->getPredictableBranchThreshold().getCompl();
-      return BranchProb < Threshold;
+    // On divergent targets a rarely-taken branch can still be divergent, and
+    // unrolling duplicates that divergent control flow across every copy. So
+    // branch probability isn't a safe proxy here; fall through to the deopt
+    // check.
+    if (!UI || !UI->hasDivergentTerminator(*NonLatchExitingBlock)) {
----------------
yxsamliu wrote:

One concern: for a divergent deopt exit with known non-rare weights, the old code returned `false`, but this change skips the probability check and may return `true` through the deopt fallback. Could we preserve the non-rare rejection and add a test for this case?

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


More information about the llvm-commits mailing list