[llvm] [LoopUnroll] Fix misleading runtime unroll debug message (PR #190709)

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 14 08:54:33 PDT 2026


https://github.com/justinfargnoli updated https://github.com/llvm/llvm-project/pull/190709

>From 2299d1692cc4abb3fc3b4ccca7ee25d61217c5b8 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Mon, 6 Apr 2026 23:59:06 +0000
Subject: [PATCH 1/2] [LoopUnroll] Add test for misleading runtime unroll debug
 message

Add a RUNTIME-NOPROFIT test that uses -unroll-partial-threshold=9 to
force the runtime unroll count below 2 for runtime_unroll_simple. This
shows that "Runtime unrolling with count: 1" is printed even though
unrolling does not proceed.
---
 llvm/test/Transforms/LoopUnroll/debug.ll | 18 ++++++++++++++++++
 1 file changed, 18 insertions(+)

diff --git a/llvm/test/Transforms/LoopUnroll/debug.ll b/llvm/test/Transforms/LoopUnroll/debug.ll
index 6b611952c0935..baed9bf590d99 100644
--- a/llvm/test/Transforms/LoopUnroll/debug.ll
+++ b/llvm/test/Transforms/LoopUnroll/debug.ll
@@ -7,6 +7,7 @@
 ; RUN: opt -disable-output -passes=loop-unroll -debug-only=loop-unroll -unroll-full-max-count=2 < %s 2>&1 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=MAX-COUNT
 ; RUN: opt -disable-output -passes=loop-unroll -debug-only=loop-unroll -unroll-allow-partial -unroll-partial-threshold=4 < %s 2>&1 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=PARTIAL-NOPROFIT
 ; RUN: opt -disable-output -passes=loop-unroll -debug-only=loop-unroll -unroll-allow-remainder=false < %s 2>&1 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=PRAGMA-NOREMAINDER
+; RUN: opt -disable-output -passes=loop-unroll -debug-only=loop-unroll -unroll-partial-threshold=9 < %s 2>&1 | FileCheck %s --match-full-lines --strict-whitespace --check-prefix=RUNTIME-NOPROFIT
 
 ; REQUIRES: asserts
 
@@ -567,6 +568,23 @@ exit:
 ; CHECK-NEXT:  Runtime unrolling with count: 8
 ; CHECK-NEXT:  Exiting block %for.body: TripCount=0, TripMultiple=1, BreakoutTrip=1
 ; CHECK:UNROLLING loop %for.body by 8 with run-time trip count!
+;
+; With a low partial threshold, the runtime unroll count is reduced below 2
+; and no unrolling occurs, but a misleading "Runtime unrolling with count"
+; message is still printed.
+;
+; RUNTIME-NOPROFIT-LABEL:Loop Unroll: F[runtime_unroll_simple] Loop %for.body (depth=1)
+; RUNTIME-NOPROFIT-NEXT:Loop Size = 6
+; RUNTIME-NOPROFIT-NEXT: Computing unroll count: TripCount=0, MaxTripCount=2147483647, TripMultiple=1
+; RUNTIME-NOPROFIT-NEXT: Explicit unroll requested: pragma-enable
+; RUNTIME-NOPROFIT-NEXT: Trying pragma unroll...
+; RUNTIME-NOPROFIT-NEXT: Trying full unroll...
+; RUNTIME-NOPROFIT-NEXT: Trying upper-bound unroll...
+; RUNTIME-NOPROFIT-NEXT: Trying loop peeling...
+; RUNTIME-NOPROFIT-NEXT: Trying partial unroll...
+; RUNTIME-NOPROFIT-NEXT: Trying runtime unroll...
+; RUNTIME-NOPROFIT-NEXT:  Runtime unrolling with count: 1
+; RUNTIME-NOPROFIT-NEXT: Not unrolling: no viable strategy found.
 
 define i32 @runtime_unroll_simple(ptr %A, i32 %n) {
 entry:

>From b3135f6bb37dd6c8b6931fa253fd1239b50b00d0 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Tue, 7 Apr 2026 00:00:05 +0000
Subject: [PATCH 2/2] [LoopUnroll] Fix misleading "Runtime unrolling with
 count" debug message

The LLVM_DEBUG print for runtime unrolling fired before the Count < 2
guard, so it could report "Runtime unrolling with count: 1" right before
the count was zeroed out. Move the print into an else branch so it only
fires when unrolling actually proceeds.
---
 llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 5 +++--
 llvm/test/Transforms/LoopUnroll/debug.ll      | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 9fdf7ef1b0a86..2e697f4e4c59c 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -1230,10 +1230,11 @@ void llvm::computeUnrollCount(Loop *L, const TargetTransformInfo &TTI,
   if (MaxTripCount && UP.Count > MaxTripCount)
     UP.Count = MaxTripCount;
 
-  LLVM_DEBUG(dbgs().indent(2)
-             << "Runtime unrolling with count: " << UP.Count << "\n");
   if (UP.Count < 2)
     UP.Count = 0;
+  else
+    LLVM_DEBUG(dbgs().indent(2)
+               << "Runtime unrolling with count: " << UP.Count << "\n");
   return;
 }
 
diff --git a/llvm/test/Transforms/LoopUnroll/debug.ll b/llvm/test/Transforms/LoopUnroll/debug.ll
index baed9bf590d99..146207eb77f06 100644
--- a/llvm/test/Transforms/LoopUnroll/debug.ll
+++ b/llvm/test/Transforms/LoopUnroll/debug.ll
@@ -570,8 +570,8 @@ exit:
 ; CHECK:UNROLLING loop %for.body by 8 with run-time trip count!
 ;
 ; With a low partial threshold, the runtime unroll count is reduced below 2
-; and no unrolling occurs, but a misleading "Runtime unrolling with count"
-; message is still printed.
+; and no unrolling occurs. Verify we don't print a misleading
+; "Runtime unrolling with count" message.
 ;
 ; RUNTIME-NOPROFIT-LABEL:Loop Unroll: F[runtime_unroll_simple] Loop %for.body (depth=1)
 ; RUNTIME-NOPROFIT-NEXT:Loop Size = 6
@@ -583,8 +583,8 @@ exit:
 ; RUNTIME-NOPROFIT-NEXT: Trying loop peeling...
 ; RUNTIME-NOPROFIT-NEXT: Trying partial unroll...
 ; RUNTIME-NOPROFIT-NEXT: Trying runtime unroll...
-; RUNTIME-NOPROFIT-NEXT:  Runtime unrolling with count: 1
-; RUNTIME-NOPROFIT-NEXT: Not unrolling: no viable strategy found.
+; RUNTIME-NOPROFIT-NOT:  Runtime unrolling with count:
+; RUNTIME-NOPROFIT: Not unrolling: no viable strategy found.
 
 define i32 @runtime_unroll_simple(ptr %A, i32 %n) {
 entry:



More information about the llvm-commits mailing list