[llvm] Use ScaledThreshold to calculate Unroll count (PR #162247)
Chandana Mudda via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 7 02:12:09 PDT 2025
https://github.com/chandmudda created https://github.com/llvm/llvm-project/pull/162247
Earlier we were using partial threshold to calculate unroll count for loop but for small loops it is giving more unroll count which is degrading the performance.Using Scaled Threshold improves the performance.
>From 77fdbc17bda08b7a14b10a5bbcea28eacdf9e4bf Mon Sep 17 00:00:00 2001
From: Chandana Mudda <quic_csinderi at quicinc.com>
Date: Mon, 6 Oct 2025 22:44:30 -0700
Subject: [PATCH] Use ScaledThreshold to calculate Unroll count
Earlier we were using partial threshold to calculate unroll count
for loop but for small loops it is giving more unroll count which
is degrading the performance.Using Scaled Threshold improves
the performance.
---
llvm/include/llvm/Analysis/TargetTransformInfo.h | 3 +++
llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp | 5 +++--
2 files changed, 6 insertions(+), 2 deletions(-)
diff --git a/llvm/include/llvm/Analysis/TargetTransformInfo.h b/llvm/include/llvm/Analysis/TargetTransformInfo.h
index 5d3b233ed6b6a..748dda15f5e08 100644
--- a/llvm/include/llvm/Analysis/TargetTransformInfo.h
+++ b/llvm/include/llvm/Analysis/TargetTransformInfo.h
@@ -574,6 +574,9 @@ class TargetTransformInfo {
/// The cost threshold for the unrolled loop, like Threshold, but used
/// for partial/runtime unrolling (set to UINT_MAX to disable).
unsigned PartialThreshold;
+ /// The cost threshold for the unrolled loop, like Threshold, but used
+ /// for calculating unroll count for loop.
+ unsigned ScaledThreshold;
/// The cost threshold for the unrolled loop when optimizing for size, like
/// OptSizeThreshold, but used for partial/runtime unrolling (set to
/// UINT_MAX to disable).
diff --git a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
index 2bda9d83236e8..c0576d7cceb15 100644
--- a/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopUnrollPass.cpp
@@ -201,6 +201,7 @@ TargetTransformInfo::UnrollingPreferences llvm::gatherUnrollingPreferences(
UP.MaxPercentThresholdBoost = 400;
UP.OptSizeThreshold = UnrollOptSizeThreshold;
UP.PartialThreshold = 150;
+ UP.ScaledThreshold = 100;
UP.PartialOptSizeThreshold = UnrollOptSizeThreshold;
UP.Count = 0;
UP.DefaultUnrollRuntimeCount = 8;
@@ -884,8 +885,8 @@ shouldPartialUnroll(const unsigned LoopSize, const unsigned TripCount,
if (UP.PartialThreshold != NoThreshold) {
// Reduce unroll count to be modulo of TripCount for partial unrolling.
if (UCE.getUnrolledLoopSize(UP, count) > UP.PartialThreshold)
- count = (std::max(UP.PartialThreshold, UP.BEInsns + 1) - UP.BEInsns) /
- (LoopSize - UP.BEInsns);
+ count = (std::max(UP.ScaledThreshold, UP.BEInsns + 1) - UP.BEInsns) /
+ (LoopSize - UP.BEInsns);
if (count > UP.MaxCount)
count = UP.MaxCount;
while (count != 0 && TripCount % count != 0)
More information about the llvm-commits
mailing list