[llvm] [LoopUnrollPass] Indent `LLVM_DEBUG()` messages based on our depth in the `tryToUnrollLoop()` call graph (PR #178945)
Justin Fargnoli via llvm-commits
llvm-commits at lists.llvm.org
Wed Feb 11 09:31:58 PST 2026
https://github.com/justinfargnoli updated https://github.com/llvm/llvm-project/pull/178945
>From 603b59a79f8dea3398a017b3256c87fa2c4ba0a0 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Fri, 30 Jan 2026 19:20:00 +0000
Subject: [PATCH 1/3] [LoopUnrollPass] Indent `LLVM_DEBUG()` messages based on
our depth in the call graph
---
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 75 +++++++++++--------
1 file changed, 43 insertions(+), 32 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 6050650eb937c..7ee138a7432c2 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -463,8 +463,9 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
return Op;
});
UnrolledCost += TTI.getInstructionCost(I, Operands, CostKind);
- LLVM_DEBUG(dbgs() << "Adding cost of instruction (iteration "
- << Iteration << "): ");
+ LLVM_DEBUG(dbgs().indent(3)
+ << "Adding cost of instruction (iteration " << Iteration
+ << "): ");
LLVM_DEBUG(I->dump());
}
@@ -500,7 +501,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
assert(L->isLCSSAForm(DT) &&
"Must have loops in LCSSA form to track live-out values.");
- LLVM_DEBUG(dbgs() << "Starting LoopUnroll profitability analysis...\n");
+ LLVM_DEBUG(dbgs().indent(3)
+ << "Starting LoopUnroll profitability analysis...\n");
TargetTransformInfo::TargetCostKind CostKind =
L->getHeader()->getParent()->hasMinSize() ?
@@ -510,7 +512,7 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// Since the same load will take different values on different iterations,
// we literally have to go through all loop's iterations.
for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) {
- LLVM_DEBUG(dbgs() << " Analyzing iteration " << Iteration << "\n");
+ LLVM_DEBUG(dbgs().indent(3) << "Analyzing iteration " << Iteration << "\n");
// Prepare for the iteration by collecting any simplified entry or backedge
// inputs.
@@ -576,7 +578,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
if (auto *CI = dyn_cast<CallInst>(&I)) {
const Function *Callee = CI->getCalledFunction();
if (!Callee || TTI.isLoweredToCall(Callee)) {
- LLVM_DEBUG(dbgs() << "Can't analyze cost of loop with call\n");
+ LLVM_DEBUG(dbgs().indent(3)
+ << "Can't analyze cost of loop with call\n");
return std::nullopt;
}
}
@@ -588,10 +591,12 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// If unrolled body turns out to be too big, bail out.
if (UnrolledCost > MaxUnrolledLoopSize) {
- LLVM_DEBUG(dbgs() << " Exceeded threshold.. exiting.\n"
- << " UnrolledCost: " << UnrolledCost
- << ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize
- << "\n");
+ LLVM_DEBUG({
+ dbgs().indent(3) << "Exceeded threshold.. exiting.\n";
+ dbgs().indent(3)
+ << "UnrolledCost: " << UnrolledCost
+ << ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize << "\n";
+ });
return std::nullopt;
}
}
@@ -648,8 +653,10 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// If we found no optimization opportunities on the first iteration, we
// won't find them on later ones too.
if (UnrolledCost == RolledDynamicCost) {
- LLVM_DEBUG(dbgs() << " No opportunities found.. exiting.\n"
- << " UnrolledCost: " << UnrolledCost << "\n");
+ LLVM_DEBUG({
+ dbgs().indent(3) << "No opportunities found.. exiting.\n";
+ dbgs().indent(3) << "UnrolledCost: " << UnrolledCost << "\n";
+ });
return std::nullopt;
}
}
@@ -674,9 +681,11 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
"All instructions must have a valid cost, whether the "
"loop is rolled or unrolled.");
- LLVM_DEBUG(dbgs() << "Analysis finished:\n"
- << "UnrolledCost: " << UnrolledCost << ", "
- << "RolledDynamicCost: " << RolledDynamicCost << "\n");
+ LLVM_DEBUG({
+ dbgs().indent(3) << "Analysis finished:\n";
+ dbgs().indent(3) << "UnrolledCost: " << UnrolledCost
+ << ", RolledDynamicCost: " << RolledDynamicCost << "\n";
+ });
return {{unsigned(UnrolledCost.getValue()),
unsigned(RolledDynamicCost.getValue())}};
}
@@ -710,17 +719,18 @@ UnrollCostEstimator::UnrollCostEstimator(
bool UnrollCostEstimator::canUnroll() const {
switch (Convergence) {
case ConvergenceKind::ExtendedLoop:
- LLVM_DEBUG(dbgs() << " Convergence prevents unrolling.\n");
+ LLVM_DEBUG(dbgs().indent(1) << "Convergence prevents unrolling.\n");
return false;
default:
break;
}
if (!LoopSize.isValid()) {
- LLVM_DEBUG(dbgs() << " Invalid loop size prevents unrolling.\n");
+ LLVM_DEBUG(dbgs().indent(1) << "Invalid loop size prevents unrolling.\n");
return false;
}
if (NotDuplicatable) {
- LLVM_DEBUG(dbgs() << " Non-duplicatable blocks prevent unrolling.\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Non-duplicatable blocks prevent unrolling.\n");
return false;
}
return true;
@@ -820,7 +830,7 @@ shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
// INT_MAX, Block full unrolling at a reasonable limit so that the compiler
// doesn't hang trying to unroll the loop. See PR77842
if (TripCount > PragmaUnrollFullMaxIterations) {
- LLVM_DEBUG(dbgs() << "Won't unroll; trip count is too large\n");
+ LLVM_DEBUG(dbgs().indent(2) << "Won't unroll; trip count is too large\n");
return std::nullopt;
}
@@ -873,8 +883,8 @@ shouldPartialUnroll(const unsigned LoopSize, const unsigned TripCount,
return std::nullopt;
if (!UP.Partial) {
- LLVM_DEBUG(dbgs() << " will not try to unroll partially because "
- << "-unroll-allow-partial not given\n");
+ LLVM_DEBUG(dbgs().indent(2) << "will not try to unroll partially because "
+ << "-unroll-allow-partial not given\n");
return 0;
}
unsigned count = UP.Count;
@@ -910,7 +920,8 @@ shouldPartialUnroll(const unsigned LoopSize, const unsigned TripCount,
if (count > UP.MaxCount)
count = UP.MaxCount;
- LLVM_DEBUG(dbgs() << " partially unrolling with count: " << count << "\n");
+ LLVM_DEBUG(dbgs().indent(2)
+ << "partially unrolling with count: " << count << "\n");
return count;
}
@@ -1094,8 +1105,8 @@ bool llvm::computeUnrollCount(
}
UP.Runtime |= PragmaEnableUnroll || PragmaCount > 0 || UserUnrollCount;
if (!UP.Runtime) {
- LLVM_DEBUG(
- dbgs() << " will not try to unroll loop with runtime trip count "
+ LLVM_DEBUG(dbgs().indent(1)
+ << "will not try to unroll loop with runtime trip count "
<< "-unroll-runtime not given\n");
UP.Count = 0;
return false;
@@ -1116,8 +1127,8 @@ bool llvm::computeUnrollCount(
if (!UP.AllowRemainder && UP.Count != 0 && (TripMultiple % UP.Count) != 0) {
while (UP.Count != 0 && TripMultiple % UP.Count != 0)
UP.Count >>= 1;
- LLVM_DEBUG(
- dbgs() << "Remainder loop is restricted (that could architecture "
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Remainder loop is restricted (that could architecture "
"specific or because the loop contains a convergent "
"instruction), so unroll count must divide the trip "
"multiple, "
@@ -1148,8 +1159,8 @@ bool llvm::computeUnrollCount(
if (MaxTripCount && UP.Count > MaxTripCount)
UP.Count = MaxTripCount;
- LLVM_DEBUG(dbgs() << " runtime unrolling with count: " << UP.Count
- << "\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "runtime unrolling with count: " << UP.Count << "\n");
if (UP.Count < 2)
UP.Count = 0;
return ExplicitUnroll;
@@ -1198,13 +1209,13 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
hasUnrollTransformation(L) != TM_ForcedByUser) {
LLVM_DEBUG(
dbgs()
- << " Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
+ << "Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
return LoopUnrollResult::Unmodified;
}
if (!L->isLoopSimplifyForm()) {
LLVM_DEBUG(
- dbgs() << " Not unrolling loop which is not in loop-simplify form.\n");
+ dbgs() << "Not unrolling loop which is not in loop-simplify form.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1232,12 +1243,12 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
UnrollCostEstimator UCE(L, TTI, EphValues, UP.BEInsns);
if (!UCE.canUnroll()) {
- LLVM_DEBUG(dbgs() << " Loop not considered unrollable.\n");
+ LLVM_DEBUG(dbgs() << "Loop not considered unrollable.\n");
return LoopUnrollResult::Unmodified;
}
unsigned LoopSize = UCE.getRolledLoopSize();
- LLVM_DEBUG(dbgs() << " Loop Size = " << LoopSize << "\n");
+ LLVM_DEBUG(dbgs() << "Loop Size = " << LoopSize << "\n");
// When optimizing for size, use LoopSize + 1 as threshold (we use < Threshold
// later), to (fully) unroll loops, if it does not increase code size.
@@ -1245,7 +1256,7 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
UP.Threshold = std::max(UP.Threshold, LoopSize + 1);
if (UCE.NumInlineCandidates != 0) {
- LLVM_DEBUG(dbgs() << " Not unrolling loop with inlinable calls.\n");
+ LLVM_DEBUG(dbgs() << "Not unrolling loop with inlinable calls.\n");
return LoopUnrollResult::Unmodified;
}
>From 8e928f02c0f4231530330c5d7a555e3fc0524cd5 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Mon, 2 Feb 2026 23:15:13 +0000
Subject: [PATCH 2/3] Add test + update indents
---
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 53 +++--
llvm/test/Transforms/LoopUnroll/debug.ll | 204 ++++++++++++++++++
2 files changed, 230 insertions(+), 27 deletions(-)
create mode 100644 llvm/test/Transforms/LoopUnroll/debug.ll
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 7ee138a7432c2..7311a2ab08665 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -463,7 +463,7 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
return Op;
});
UnrolledCost += TTI.getInstructionCost(I, Operands, CostKind);
- LLVM_DEBUG(dbgs().indent(3)
+ LLVM_DEBUG(dbgs().indent(4)
<< "Adding cost of instruction (iteration " << Iteration
<< "): ");
LLVM_DEBUG(I->dump());
@@ -501,7 +501,7 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
assert(L->isLCSSAForm(DT) &&
"Must have loops in LCSSA form to track live-out values.");
- LLVM_DEBUG(dbgs().indent(3)
+ LLVM_DEBUG(dbgs().indent(4)
<< "Starting LoopUnroll profitability analysis...\n");
TargetTransformInfo::TargetCostKind CostKind =
@@ -512,7 +512,7 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// Since the same load will take different values on different iterations,
// we literally have to go through all loop's iterations.
for (unsigned Iteration = 0; Iteration < TripCount; ++Iteration) {
- LLVM_DEBUG(dbgs().indent(3) << "Analyzing iteration " << Iteration << "\n");
+ LLVM_DEBUG(dbgs().indent(4) << "Analyzing iteration " << Iteration << "\n");
// Prepare for the iteration by collecting any simplified entry or backedge
// inputs.
@@ -578,7 +578,7 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
if (auto *CI = dyn_cast<CallInst>(&I)) {
const Function *Callee = CI->getCalledFunction();
if (!Callee || TTI.isLoweredToCall(Callee)) {
- LLVM_DEBUG(dbgs().indent(3)
+ LLVM_DEBUG(dbgs().indent(4)
<< "Can't analyze cost of loop with call\n");
return std::nullopt;
}
@@ -592,8 +592,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// If unrolled body turns out to be too big, bail out.
if (UnrolledCost > MaxUnrolledLoopSize) {
LLVM_DEBUG({
- dbgs().indent(3) << "Exceeded threshold.. exiting.\n";
- dbgs().indent(3)
+ dbgs().indent(4) << "Exceeded threshold.. exiting.\n";
+ dbgs().indent(4)
<< "UnrolledCost: " << UnrolledCost
<< ", MaxUnrolledLoopSize: " << MaxUnrolledLoopSize << "\n";
});
@@ -654,8 +654,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
// won't find them on later ones too.
if (UnrolledCost == RolledDynamicCost) {
LLVM_DEBUG({
- dbgs().indent(3) << "No opportunities found.. exiting.\n";
- dbgs().indent(3) << "UnrolledCost: " << UnrolledCost << "\n";
+ dbgs().indent(4) << "No opportunities found.. exiting.\n";
+ dbgs().indent(4) << "UnrolledCost: " << UnrolledCost << "\n";
});
return std::nullopt;
}
@@ -682,8 +682,8 @@ static std::optional<EstimatedUnrollCost> analyzeLoopUnrollCost(
"loop is rolled or unrolled.");
LLVM_DEBUG({
- dbgs().indent(3) << "Analysis finished:\n";
- dbgs().indent(3) << "UnrolledCost: " << UnrolledCost
+ dbgs().indent(4) << "Analysis finished:\n";
+ dbgs().indent(4) << "UnrolledCost: " << UnrolledCost
<< ", RolledDynamicCost: " << RolledDynamicCost << "\n";
});
return {{unsigned(UnrolledCost.getValue()),
@@ -830,7 +830,7 @@ shouldPragmaUnroll(Loop *L, const PragmaInfo &PInfo,
// INT_MAX, Block full unrolling at a reasonable limit so that the compiler
// doesn't hang trying to unroll the loop. See PR77842
if (TripCount > PragmaUnrollFullMaxIterations) {
- LLVM_DEBUG(dbgs().indent(2) << "Won't unroll; trip count is too large\n");
+ LLVM_DEBUG(dbgs().indent(3) << "Won't unroll; trip count is too large\n");
return std::nullopt;
}
@@ -883,7 +883,7 @@ shouldPartialUnroll(const unsigned LoopSize, const unsigned TripCount,
return std::nullopt;
if (!UP.Partial) {
- LLVM_DEBUG(dbgs().indent(2) << "will not try to unroll partially because "
+ LLVM_DEBUG(dbgs().indent(3) << "will not try to unroll partially because "
<< "-unroll-allow-partial not given\n");
return 0;
}
@@ -920,7 +920,7 @@ shouldPartialUnroll(const unsigned LoopSize, const unsigned TripCount,
if (count > UP.MaxCount)
count = UP.MaxCount;
- LLVM_DEBUG(dbgs().indent(2)
+ LLVM_DEBUG(dbgs().indent(3)
<< "partially unrolling with count: " << count << "\n");
return count;
@@ -1105,7 +1105,7 @@ bool llvm::computeUnrollCount(
}
UP.Runtime |= PragmaEnableUnroll || PragmaCount > 0 || UserUnrollCount;
if (!UP.Runtime) {
- LLVM_DEBUG(dbgs().indent(1)
+ LLVM_DEBUG(dbgs().indent(2)
<< "will not try to unroll loop with runtime trip count "
<< "-unroll-runtime not given\n");
UP.Count = 0;
@@ -1127,7 +1127,7 @@ bool llvm::computeUnrollCount(
if (!UP.AllowRemainder && UP.Count != 0 && (TripMultiple % UP.Count) != 0) {
while (UP.Count != 0 && TripMultiple % UP.Count != 0)
UP.Count >>= 1;
- LLVM_DEBUG(dbgs().indent(1)
+ LLVM_DEBUG(dbgs().indent(2)
<< "Remainder loop is restricted (that could architecture "
"specific or because the loop contains a convergent "
"instruction), so unroll count must divide the trip "
@@ -1159,7 +1159,7 @@ bool llvm::computeUnrollCount(
if (MaxTripCount && UP.Count > MaxTripCount)
UP.Count = MaxTripCount;
- LLVM_DEBUG(dbgs().indent(1)
+ LLVM_DEBUG(dbgs().indent(2)
<< "runtime unrolling with count: " << UP.Count << "\n");
if (UP.Count < 2)
UP.Count = 0;
@@ -1197,8 +1197,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
if (ParentL != nullptr &&
hasUnrollAndJamTransformation(ParentL) == TM_ForcedByUser &&
hasUnrollTransformation(L) != TM_ForcedByUser) {
- LLVM_DEBUG(dbgs() << "Not unrolling loop since parent loop has"
- << " llvm.loop.unroll_and_jam.\n");
+ LLVM_DEBUG(dbgs().indent(1) << "Not unrolling loop since parent loop has"
+ << " llvm.loop.unroll_and_jam.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1207,15 +1207,14 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
// unrolling from interfering with the user requested transformation.
if (hasUnrollAndJamTransformation(L) == TM_ForcedByUser &&
hasUnrollTransformation(L) != TM_ForcedByUser) {
- LLVM_DEBUG(
- dbgs()
- << "Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
return LoopUnrollResult::Unmodified;
}
if (!L->isLoopSimplifyForm()) {
- LLVM_DEBUG(
- dbgs() << "Not unrolling loop which is not in loop-simplify form.\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Not unrolling loop which is not in loop-simplify form.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1243,7 +1242,7 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
UnrollCostEstimator UCE(L, TTI, EphValues, UP.BEInsns);
if (!UCE.canUnroll()) {
- LLVM_DEBUG(dbgs() << "Loop not considered unrollable.\n");
+ LLVM_DEBUG(dbgs().indent(1) << "Loop not considered unrollable.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1256,7 +1255,7 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
UP.Threshold = std::max(UP.Threshold, LoopSize + 1);
if (UCE.NumInlineCandidates != 0) {
- LLVM_DEBUG(dbgs() << "Not unrolling loop with inlinable calls.\n");
+ LLVM_DEBUG(dbgs().indent(1) << "Not unrolling loop with inlinable calls.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1339,8 +1338,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
// Do not attempt partial/runtime unrolling in FullLoopUnrolling
if (OnlyFullUnroll && ((!TripCount && !MaxTripCount) ||
UP.Count < TripCount || UP.Count < MaxTripCount)) {
- LLVM_DEBUG(
- dbgs() << "Not attempting partial/runtime unroll in FullLoopUnroll.\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Not attempting partial/runtime unroll in FullLoopUnroll.\n");
return LoopUnrollResult::Unmodified;
}
diff --git a/llvm/test/Transforms/LoopUnroll/debug.ll b/llvm/test/Transforms/LoopUnroll/debug.ll
new file mode 100644
index 0000000000000..b68b7c2d10e94
--- /dev/null
+++ b/llvm/test/Transforms/LoopUnroll/debug.ll
@@ -0,0 +1,204 @@
+; RUN: opt -disable-output -passes=loop-unroll -debug-only=loop-unroll < %s 2>&1 | FileCheck %s --match-full-lines --strict-whitespace
+
+; REQUIRES: asserts
+
+; CHECK-LABEL:Loop Unroll: F[pragma_full_unroll_unknown_tc] Loop %for.body
+; CHECK-NEXT:Loop Size = 6
+; CHECK-NEXT: will not try to unroll loop with runtime trip count -unroll-runtime not given
+
+define i32 @pragma_full_unroll_unknown_tc(ptr %A, i32 %n) {
+entry:
+ %cmp.entry = icmp sgt i32 %n, 0
+ br i1 %cmp.entry, label %for.body, label %exit
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ %add = add i32 %sum, %load
+ %inc = add i32 %i, 1
+ %cmp = icmp slt i32 %inc, %n
+ br i1 %cmp, label %for.body, label %exit, !llvm.loop !0
+
+exit:
+ %result = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ ret i32 %result
+}
+
+; CHECK-LABEL:Loop Unroll: F[full_unroll_cost_exceeds] Loop %for.body
+; CHECK-NEXT:Loop Size = 6
+; CHECK-NEXT: will not try to unroll partially because -unroll-allow-partial not given
+
+define i32 @full_unroll_cost_exceeds(ptr %A) {
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ %add = add i32 %sum, %load
+ %inc = add i32 %i, 1
+ %cmp = icmp ult i32 %inc, 100
+ br i1 %cmp, label %for.body, label %exit
+
+exit:
+ ret i32 %add
+}
+
+; CHECK-LABEL:Loop Unroll: F[extended_convergence] Loop %for.body
+; CHECK-NEXT: Convergence prevents unrolling.
+; CHECK-NEXT: Loop not considered unrollable.
+
+declare void @convergent_func() convergent
+declare token @llvm.experimental.convergence.anchor()
+
+define i32 @extended_convergence(ptr %A, i32 %n) {
+entry:
+ br label %for.body, !llvm.loop !1
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %tok = call token @llvm.experimental.convergence.anchor()
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ %add = add i32 %sum, %load
+ %inc = add i32 %i, 1
+ %cmp = icmp slt i32 %inc, %n
+ br i1 %cmp, label %for.body, label %exit, !llvm.loop !1
+
+exit:
+ call void @convergent_func() [ "convergencectrl"(token %tok) ]
+ ret i32 %add
+}
+
+; CHECK-LABEL:Loop Unroll: F[noduplicate_prevents_unroll] Loop %for.body
+; CHECK-NEXT: Non-duplicatable blocks prevent unrolling.
+; CHECK-NEXT: Loop not considered unrollable.
+
+declare void @noduplicate_func() noduplicate
+
+define i32 @noduplicate_prevents_unroll(ptr %A) {
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ call void @noduplicate_func()
+ %add = add i32 %sum, %load
+ %inc = add i32 %i, 1
+ %cmp = icmp ult i32 %inc, 8
+ br i1 %cmp, label %for.body, label %exit, !llvm.loop !1
+
+exit:
+ ret i32 %add
+}
+
+; CHECK-LABEL:Loop Unroll: F[indirectbr_loop] Loop %for.body
+; CHECK-NEXT: Not unrolling loop which is not in loop-simplify form.
+
+define i32 @indirectbr_loop(ptr %A, ptr %target) {
+entry:
+ indirectbr ptr %target, [label %for.body, label %exit]
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ %add = add i32 %sum, %load
+ %inc = add i32 %i, 1
+ %cmp = icmp ult i32 %inc, 10
+ br i1 %cmp, label %for.body, label %exit, !llvm.loop !1
+
+exit:
+ %result = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ ret i32 %result
+}
+
+; CHECK-LABEL:Loop Unroll: F[inline_prevents_unroll] Loop %for.body
+; CHECK-NEXT:Loop Size = 8
+; CHECK-NEXT: Not unrolling loop with inlinable calls.
+
+define internal i32 @single_use_helper(i32 %x) {
+ %add = add i32 %x, 42
+ ret i32 %add
+}
+
+define i32 @inline_prevents_unroll(ptr %A) {
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %add, %for.body ]
+ %arrayidx = getelementptr inbounds i32, ptr %A, i32 %i
+ %load = load i32, ptr %arrayidx
+ %helper_result = call i32 @single_use_helper(i32 %load)
+ %add = add i32 %sum, %helper_result
+ %inc = add i32 %i, 1
+ %cmp = icmp ult i32 %inc, 10
+ br i1 %cmp, label %for.body, label %exit, !llvm.loop !2
+
+exit:
+ ret i32 %add
+}
+
+; CHECK-LABEL:Loop Unroll: F[full_unroll_profitability_analysis] Loop %for.body
+; CHECK-NEXT:Loop Size = {{[0-9]+}}
+; CHECK-NEXT: Starting LoopUnroll profitability analysis...
+; CHECK-NEXT: Analyzing iteration 0
+; CHECK-NEXT: Analyzing iteration 1
+; CHECK-NEXT: Analyzing iteration 2
+; CHECK-NEXT: Analyzing iteration 3
+; CHECK-NEXT: Analyzing iteration 4
+; CHECK-NEXT: Analyzing iteration 5
+; CHECK-NEXT: Analyzing iteration 6
+; CHECK-NEXT: Analyzing iteration 7
+; CHECK-NEXT: Analyzing iteration 8
+; CHECK-NEXT: Analyzing iteration 9
+; CHECK: Analysis finished:
+; CHECK-NEXT: UnrolledCost: {{[0-9]+}}, RolledDynamicCost: {{[0-9]+}}
+; CHECK-NEXT: Exiting block %for.body: TripCount=10, TripMultiple=0, BreakoutTrip=0
+; CHECK-NEXT:COMPLETELY UNROLLING loop %for.body with trip count 10!
+
+define i32 @full_unroll_profitability_analysis(ptr %A, ptr %B) {
+entry:
+ br label %for.body
+
+for.body:
+ %i = phi i32 [ 0, %entry ], [ %inc, %for.body ]
+ %sum = phi i32 [ 0, %entry ], [ %result, %for.body ]
+ %idxA = getelementptr inbounds i32, ptr %A, i32 %i
+ %loadA = load i32, ptr %idxA
+ %idxB = getelementptr inbounds i32, ptr %B, i32 %i
+ %loadB = load i32, ptr %idxB
+ %mul1 = mul i32 %loadA, %loadB
+ %add1 = add i32 %mul1, %loadA
+ %mul2 = mul i32 %add1, %loadB
+ %sub1 = sub i32 %mul2, %loadA
+ %add2 = add i32 %sub1, %loadB
+ %mul3 = mul i32 %add2, %loadA
+ %sub2 = sub i32 %mul3, %loadB
+ %xor1 = xor i32 %sub2, %loadA
+ %or1 = or i32 %xor1, %loadB
+ %result = add i32 %sum, %or1
+ %inc = add i32 %i, 1
+ %cmp = icmp ult i32 %inc, 10
+ br i1 %cmp, label %for.body, label %exit
+
+exit:
+ ret i32 %result
+}
+
+!0 = distinct !{!0, !3}
+!1 = distinct !{!1, !4}
+!2 = distinct !{!2, !4}
+!3 = !{!"llvm.loop.unroll.full"}
+!4 = !{!"llvm.loop.unroll.enable"}
>From 8d8cedd5261148df0fb8d03cc0de88856c333e76 Mon Sep 17 00:00:00 2001
From: Justin Fargnoli <jfargnoli at nvidia.com>
Date: Mon, 2 Feb 2026 23:15:59 +0000
Subject: [PATCH 3/3] clang-format
---
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 7311a2ab08665..40d8043d01e02 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -1207,8 +1207,9 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
// unrolling from interfering with the user requested transformation.
if (hasUnrollAndJamTransformation(L) == TM_ForcedByUser &&
hasUnrollTransformation(L) != TM_ForcedByUser) {
- LLVM_DEBUG(dbgs().indent(1)
- << "Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
+ LLVM_DEBUG(
+ dbgs().indent(1)
+ << "Not unrolling loop since it has llvm.loop.unroll_and_jam.\n");
return LoopUnrollResult::Unmodified;
}
@@ -1255,7 +1256,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
UP.Threshold = std::max(UP.Threshold, LoopSize + 1);
if (UCE.NumInlineCandidates != 0) {
- LLVM_DEBUG(dbgs().indent(1) << "Not unrolling loop with inlinable calls.\n");
+ LLVM_DEBUG(dbgs().indent(1)
+ << "Not unrolling loop with inlinable calls.\n");
return LoopUnrollResult::Unmodified;
}
More information about the llvm-commits
mailing list