[llvm] Prevent LoopUnrollPass from doing partial unroll if TripCount and MaxTripCount are unknown (PR #165013)

Ivan Kelarev via llvm-commits llvm-commits at lists.llvm.org
Fri Oct 24 09:52:06 PDT 2025


https://github.com/ivankelarev created https://github.com/llvm/llvm-project/pull/165013

None

>From c4826296e88af1dea43b68b944d2f0ea3b22df1e Mon Sep 17 00:00:00 2001
From: Ivan Kelarev <ivan.kelarev at intel.com>
Date: Fri, 24 Oct 2025 09:49:50 -0700
Subject: [PATCH] Prevent LoopUnrollPass from doing partial unroll if TripCount
 and MaxTripCount are unknown

---
 llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp |  3 +-
 .../LoopUnroll/full-unroll-avoid-partial.ll   | 28 +++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 2bda9d83236e8..802ae4e9c28e3 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -1327,7 +1327,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
   }
 
   // Do not attempt partial/runtime unrolling in FullLoopUnrolling
-  if (OnlyFullUnroll && (UP.Count < TripCount || UP.Count < MaxTripCount)) {
+  if (OnlyFullUnroll && ((!TripCount && !MaxTripCount) ||
+                         UP.Count < TripCount || UP.Count < MaxTripCount)) {
     LLVM_DEBUG(
         dbgs() << "Not attempting partial/runtime unroll in FullLoopUnroll.\n");
     return LoopUnrollResult::Unmodified;
diff --git a/llvm/test/Transforms/LoopUnroll/full-unroll-avoid-partial.ll b/llvm/test/Transforms/LoopUnroll/full-unroll-avoid-partial.ll
index 7f266a754d1bc..55f2c8b8dc19a 100644
--- a/llvm/test/Transforms/LoopUnroll/full-unroll-avoid-partial.ll
+++ b/llvm/test/Transforms/LoopUnroll/full-unroll-avoid-partial.ll
@@ -85,6 +85,34 @@ for.body:                                         ; preds = %for.body.preheader,
   br i1 %exitcond, label %for.body, label %for.cond.cleanup.loopexit, !llvm.loop !3
 }
 
+; LOOP-UNROLL-LABEL: Loop Unroll: F[pragma_unroll_count2] Loop %
+; LOOP-UNROLL-NEXT: Loop Size = 4
+; LOOP-UNROLL-NEXT: Exiting block %: TripCount=0, TripMultiple=1, BreakoutTrip=1
+; LOOP-UNROLL-NEXT: Trying runtime unrolling on Loop:
+; LOOP-UNROLL-NEXT: Loop at depth 1 containing: %2<header><exiting>,%5<latch>
+; LOOP-UNROLL-NEXT: Using epilog remainder.
+; LOOP-UNROLL-NEXT: Loop latch not terminated by a conditional branch.
+; LOOP-UNROLL-NEXT: UNROLLING loop % by 5!
+
+; LOOP-UNROLL-FULL-LABEL: Loop Unroll: F[pragma_unroll_count2] Loop %
+; LOOP-UNROLL-FULL-NEXT: Loop Size = 4
+; LOOP-UNROLL-FULL-NEXT: Not attempting partial/runtime unroll in FullLoopUnroll
+define void @pragma_unroll_count2(i64 %0) {
+  br label %2
+
+2:                                                ; preds = %5, %1
+  %3 = phi i64 [ 0, %1 ], [ %6, %5 ]
+  %4 = icmp ult i64 %3, %0
+  br i1 %4, label %5, label %7
+
+5:                                                ; preds = %2
+  %6 = add i64 %3, 8
+  br label %2, !llvm.loop !3
+
+7:                                                ; preds = %2
+  ret void
+}
+
 ; LOOP-UNROLL: llvm.loop.unroll.disable
 ; LOOP-UNROLL-FULL: llvm.loop.unroll.enable
 !0 = !{!"llvm.loop.unroll.enable"}



More information about the llvm-commits mailing list