[llvm] [LoopUnroll] Allow runtime unroll with remainder for uniform trip count in convergent loops (PR #192819)

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Mon Apr 20 13:57:52 PDT 2026


================
@@ -1372,9 +1386,15 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
   // is unsafe -- it adds a control-flow dependency to the convergent
   // operation.  Therefore restrict remainder loop (try unrolling without).
   //
-  // TODO: This is somewhat conservative; we could allow the remainder if the
-  // trip count is uniform.
-  UP.AllowRemainder &= UCE.ConvergenceAllowsRuntime;
+  // However, if the trip count is uniform across all threads, all threads
+  // will take the same path through the prelude, so the remainder is safe.
+  bool TripCountIsUniform = false;
+  if (!UCE.ConvergenceAllowsRuntime && UI) {
+    if (const SCEV *BTC = SE.getBackedgeTakenCount(L);
+        !isa<SCEVCouldNotCompute>(BTC))
+      TripCountIsUniform = isSCEVUniform(BTC, *UI);
+  }
+  UP.AllowRemainder &= UCE.ConvergenceAllowsRuntime || TripCountIsUniform;
----------------
justinfargnoli wrote:

Could we move this to the initialization of `ConvergenceAllowsRuntime`?

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


More information about the llvm-commits mailing list