[llvm] [LoopUnroll] Fix misleading runtime unroll debug message (PR #190709)
Justin Fargnoli via llvm-commits
llvm-commits at lists.llvm.org
Mon Apr 6 16:47:38 PDT 2026
https://github.com/justinfargnoli created https://github.com/llvm/llvm-project/pull/190709
## Summary
- Move the "Runtime unrolling with count:" `LLVM_DEBUG` print after the `Count < 2` guard so it doesn't fire when the count is about to be zeroed out
- Without this fix, the debug output can show "Runtime unrolling with count: 1" immediately before the count is set to 0 — making it appear that unrolling will proceed when it won't
- Add a test (`RUNTIME-NOPROFIT`) that uses a low partial threshold to force the runtime unroll count below 2, verifying the misleading message no longer appears
## Test plan
- [x] `llvm-lit llvm/test/Transforms/LoopUnroll/debug.ll` passes
>From 2689d04cd456dcbb19a7f0d462ee238faa7e9c59 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Mon, 6 Apr 2026 23:44:48 +0000
Subject: [PATCH] [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 | 18 ++++++++++++++++++
2 files changed, 21 insertions(+), 2 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 6b611952c0935..146207eb77f06 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. 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
+; 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-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