[llvm] Unroll loops remarks (PR #177374)
Ahmad Yasin via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 22 06:54:40 PST 2026
https://github.com/ayasin-a created https://github.com/llvm/llvm-project/pull/177374
Adding Optimization Remark when runtime loop unrolling is missed in the generic LoopUnroll pass. Maybe be enabled with `-Rpass-missed=loop-unroll`
The idea is to provide actionable data for developers and users. For that, added a SkipReason to TargetTransformInfo and filled reasons by `getAppleRuntimeUnrollPreferences()` for now.
>From be9ba2e2eaeba462f55375db437dc54dc96254b2 Mon Sep 17 00:00:00 2001
From: Ahmad Yasin <ahmad.yasin at apple.com>
Date: Thu, 22 Jan 2026 16:42:48 +0200
Subject: [PATCH 1/2] Optimization Remark when runtime unrolling got missed
---
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 11 ++++++++++-
1 file changed, 10 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index f7d2258e1c283..631b8363cef9a 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -1095,6 +1095,14 @@ bool llvm::computeUnrollCount(
LLVM_DEBUG(
dbgs() << " will not try to unroll loop with runtime trip count "
<< "-unroll-runtime not given\n");
+ if (ORE) {
+ ORE->emit([&]() {
+ auto Remark = OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeUnrollSkipped",
+ L->getStartLoc(), L->getHeader())
+ << "runtime unrolling was skipped";
+ return Remark;
+ });
+ }
UP.Count = 0;
return false;
}
@@ -1171,7 +1179,8 @@ tryToUnrollLoop(Loop *L, DominatorTree &DT, LoopInfo *LI, ScalarEvolution &SE,
LLVM_DEBUG(dbgs() << "Loop Unroll: F["
<< L->getHeader()->getParent()->getName() << "] Loop %"
- << L->getHeader()->getName() << "\n");
+ << L->getHeader()->getName() << " OnlyFullUnroll="
+ << OnlyFullUnroll << "\n");
TransformationMode TM = hasUnrollTransformation(L);
if (TM & TM_Disable)
return LoopUnrollResult::Unmodified;
>From 3d8f4e0c78f47a2fda535aa0b26c525562099667 Mon Sep 17 00:00:00 2001
From: Ahmad Yasin <ahmad.yasin at apple.com>
Date: Thu, 22 Jan 2026 16:47:51 +0200
Subject: [PATCH 2/2] Detail reasons by getAppleRuntimeUnrollPreferences for
the RuntimeUnrollSkipped Opt Remark
---
.../llvm/Analysis/TargetTransformInfo.h | 3 ++
.../AArch64/AArch64TargetTransformInfo.cpp | 43 +++++++++++++++----
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 2 +
3 files changed, 40 insertions(+), 8 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 9186419715cc4..96dbff68819b2 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -638,6 +638,9 @@ class TargetTransformInfo {
/// Fall back to the generic logic to determine whether multi-exit unrolling
/// is profitable if set to false.
bool RuntimeUnrollMultiExit;
+ /// Optional reason why runtime unrolling was not enabled by the target.
+ /// Used to provide detailed diagnostics in optimization remarks.
+ std::string RuntimeSkipReason;
};
/// Get target-customized preferences for the generic loop unrolling
diff --git a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
index 3042251cf754d..b75b811eabdf4 100644
--- a/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
+++ b/llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp
@@ -4886,24 +4886,42 @@ getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
// likely with complex control flow). Note that the heuristics here may be
// overly conservative and we err on the side of avoiding runtime unrolling
// rather than unroll excessively. They are all subject to further refinement.
- if (!L->isInnermost() || L->getNumBlocks() > 8)
+ if (!L->isInnermost() || L->getNumBlocks() > 8) {
+ UP.RuntimeSkipReason = !L->isInnermost()
+ ? "Apple: not an innermost loop"
+ : "Apple: loop has too many blocks (" +
+ std::to_string(L->getNumBlocks()) + " > 8)";
return;
+ }
// Loops with multiple exits are handled by common code.
- if (!L->getExitBlock())
+ if (!L->getExitBlock()) {
+ UP.RuntimeSkipReason = "Apple: loop has multiple exits";
return;
+ }
const SCEV *BTC = SE.getSymbolicMaxBackedgeTakenCount(L);
if (isa<SCEVConstant>(BTC) || isa<SCEVCouldNotCompute>(BTC) ||
(SE.getSmallConstantMaxTripCount(L) > 0 &&
- SE.getSmallConstantMaxTripCount(L) <= 32))
+ SE.getSmallConstantMaxTripCount(L) <= 32)) {
+ UP.RuntimeSkipReason = isa<SCEVConstant>(BTC)
+ ? "Apple: loop has constant trip count"
+ : isa<SCEVCouldNotCompute>(BTC)
+ ? "Apple: could not compute backedge-taken count"
+ : "Apple: loop has small max trip count (" +
+ std::to_string(SE.getSmallConstantMaxTripCount(L)) + " <= 32)";
return;
+ }
- if (findStringMetadataForLoop(L, "llvm.loop.isvectorized"))
+ if (findStringMetadataForLoop(L, "llvm.loop.isvectorized")) {
+ UP.RuntimeSkipReason = "Apple: loop already vectorized";
return;
+ }
- if (SE.getSymbolicMaxBackedgeTakenCount(L) != SE.getBackedgeTakenCount(L))
+ if (SE.getSymbolicMaxBackedgeTakenCount(L) != SE.getBackedgeTakenCount(L)) {
+ UP.RuntimeSkipReason = "Apple: symbolic max backedge-taken count differs from backedge-taken count";
return;
+ }
// Limit to loops with trip counts that are cheap to expand.
UP.SCEVExpansionBudget = 1;
@@ -4916,8 +4934,11 @@ getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
// Estimate the size of the loop.
unsigned Size;
unsigned Width = 10;
- if (!isLoopSizeWithinBudget(L, TTI, Width, &Size))
+ if (!isLoopSizeWithinBudget(L, TTI, Width, &Size)) {
+ UP.RuntimeSkipReason = "Apple: loop size exceeds budget (" +
+ std::to_string(Width) + ")";
return;
+ }
SmallPtrSet<Value *, 8> LoadedValuesPlus;
SmallVector<StoreInst *> Stores;
@@ -4960,8 +4981,12 @@ getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
if (BestUC == 1 || none_of(Stores, [&LoadedValuesPlus](StoreInst *SI) {
return LoadedValuesPlus.contains(SI->getOperand(0));
- }))
+ })) {
+ UP.RuntimeSkipReason = (BestUC == 1)
+ ? "Apple: no beneficial unroll count found (BestUC=1)"
+ : "Apple: no load/store dependencies found that would benefit from unrolling";
return;
+ }
UP.Runtime = true;
UP.DefaultUnrollRuntimeCount = BestUC;
@@ -4975,8 +5000,10 @@ getAppleRuntimeUnrollPreferences(Loop *L, ScalarEvolution &SE,
SmallVector<BasicBlock *> Preds(predecessors(Latch));
if (!Term || !Term->isConditional() || Preds.size() == 1 ||
!llvm::is_contained(Preds, Header) ||
- none_of(Preds, [L](BasicBlock *Pred) { return L->contains(Pred); }))
+ none_of(Preds, [L](BasicBlock *Pred) { return L->contains(Pred); })) {
+ UP.RuntimeSkipReason = "Apple: loop structure unsuitable for early-continue optimization";
return;
+ }
std::function<bool(Instruction *, unsigned)> DependsOnLoopLoad =
[&](Instruction *I, unsigned Depth) -> bool {
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 631b8363cef9a..e63d2558f0fe7 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -1100,6 +1100,8 @@ bool llvm::computeUnrollCount(
auto Remark = OptimizationRemarkMissed(DEBUG_TYPE, "RuntimeUnrollSkipped",
L->getStartLoc(), L->getHeader())
<< "runtime unrolling was skipped";
+ if (!UP.RuntimeSkipReason.empty())
+ Remark << ": " << UP.RuntimeSkipReason;
return Remark;
});
}
More information about the llvm-commits
mailing list