[llvm] [SLP][NFC] Refactor vectorizeStores::RangeSizes (PR #177241)
Ryan Buchner via llvm-commits
llvm-commits at lists.llvm.org
Tue Jan 27 10:57:25 PST 2026
https://github.com/bababuck updated https://github.com/llvm/llvm-project/pull/177241
>From 6eff7a1b1c70954543f46554de336f7f302e5243 Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Wed, 14 Jan 2026 15:01:36 -0800
Subject: [PATCH 1/6] [SLP] Simplify isVectorized() and isNotVectorized()
macros
When we set RangeSizes to 0 during vectorization, we always set both
first and second 0 concurrently, so in this case it's fine to just check
the first always.
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 31 +++++++------------
1 file changed, 11 insertions(+), 20 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index cbd9303a07e89..dc78bd91fb6c0 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24296,13 +24296,11 @@ bool SLPVectorizerPass::vectorizeStores(
// The `slice` and `drop_front` interfaces are convenient
const auto RangeSizes = MutableArrayRef(RangeSizesStorage);
DenseMap<Value *, std::pair<unsigned, unsigned>> NonSchedulable;
- auto IsNotVectorized = [](bool First,
- const std::pair<unsigned, unsigned> &P) {
- return First ? P.first > 0 : P.second > 0;
+ auto IsNotVectorized = [](const std::pair<unsigned, unsigned> &P) {
+ return P.first > 0;
};
- auto IsVectorized = [](bool First,
- const std::pair<unsigned, unsigned> &P) {
- return First ? P.first == 0 : P.second == 0;
+ auto IsVectorized = [](const std::pair<unsigned, unsigned> &P) {
+ return P.first == 0;
};
auto VFIsProfitable = [](bool First, unsigned Size,
const std::pair<unsigned, unsigned> &P) {
@@ -24318,18 +24316,15 @@ bool SLPVectorizerPass::vectorizeStores(
bool AnyProfitableGraph = false;
for (unsigned VF : CandidateVFs) {
AnyProfitableGraph = false;
- unsigned FirstUnvecStore =
- std::distance(RangeSizes.begin(),
- find_if(RangeSizes, std::bind(IsNotVectorized,
- VF >= MaxRegVF, _1)));
+ unsigned FirstUnvecStore = std::distance(
+ RangeSizes.begin(), find_if(RangeSizes, IsNotVectorized));
// Form slices of size VF starting from FirstUnvecStore and try to
// vectorize them.
while (FirstUnvecStore < End) {
unsigned FirstVecStore = std::distance(
RangeSizes.begin(),
- find_if(RangeSizes.drop_front(FirstUnvecStore),
- std::bind(IsVectorized, VF >= MaxRegVF, _1)));
+ find_if(RangeSizes.drop_front(FirstUnvecStore), IsVectorized));
unsigned MaxSliceEnd = FirstVecStore >= End ? End : FirstVecStore;
for (unsigned SliceStartIdx = FirstUnvecStore;
SliceStartIdx + VF <= MaxSliceEnd;) {
@@ -24437,16 +24432,13 @@ bool SLPVectorizerPass::vectorizeStores(
AnyProfitableGraph = true;
FirstUnvecStore = std::distance(
RangeSizes.begin(),
- find_if(RangeSizes.drop_front(MaxSliceEnd),
- std::bind(IsNotVectorized, VF >= MaxRegVF, _1)));
+ find_if(RangeSizes.drop_front(MaxSliceEnd), IsNotVectorized));
}
if (!AnyProfitableGraph && VF >= MaxRegVF && has_single_bit(VF))
break;
}
// All values vectorized - exit.
- if (all_of(RangeSizes, [](const std::pair<unsigned, unsigned> &P) {
- return P.first == 0 && P.second == 0;
- }))
+ if (all_of(RangeSizes, IsVectorized))
break;
// Check if tried all attempts or no need for the last attempts at all.
if (Repeat >= MaxAttempts ||
@@ -24457,9 +24449,8 @@ bool SLPVectorizerPass::vectorizeStores(
Operands.size(),
static_cast<unsigned>(
End -
- std::distance(
- RangeSizes.begin(),
- find_if(RangeSizes, std::bind(IsNotVectorized, true, _1))) +
+ std::distance(RangeSizes.begin(),
+ find_if(RangeSizes, IsNotVectorized)) +
1));
unsigned VF = bit_ceil(CandidateVFs.front()) * 2;
unsigned Limit =
>From 732df15552ed9e9f84e23e598626fe27ff16c14e Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Wed, 21 Jan 2026 09:46:26 -0800
Subject: [PATCH 2/6] [SLP] Should be the change to fix #177068
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 6 +++---
1 file changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index dc78bd91fb6c0..923f2ec1e6fe3 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24329,7 +24329,7 @@ bool SLPVectorizerPass::vectorizeStores(
for (unsigned SliceStartIdx = FirstUnvecStore;
SliceStartIdx + VF <= MaxSliceEnd;) {
if (!checkTreeSizes(RangeSizes.slice(SliceStartIdx, VF),
- VF >= MaxRegVF)) {
+ VF > MaxRegVF)) {
++SliceStartIdx;
continue;
}
@@ -24397,7 +24397,7 @@ bool SLPVectorizerPass::vectorizeStores(
}
if (VF > 2 && Res &&
!all_of(RangeSizes.slice(SliceStartIdx, VF),
- std::bind(VFIsProfitable, VF >= MaxRegVF, TreeSize,
+ std::bind(VFIsProfitable, VF > MaxRegVF, TreeSize,
_1))) {
SliceStartIdx += VF;
continue;
@@ -24416,7 +24416,7 @@ bool SLPVectorizerPass::vectorizeStores(
if (TreeSize > 1) {
for (std::pair<unsigned, unsigned> &P :
RangeSizes.slice(SliceStartIdx, VF)) {
- if (VF >= MaxRegVF)
+ if (VF > MaxRegVF)
P.second = std::max(P.second, TreeSize);
else
P.first = std::max(P.first, TreeSize);
>From c2a00ff5e50ceabeedb5c6b2a4f0d05145adabfd Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Tue, 20 Jan 2026 12:36:03 -0800
Subject: [PATCH 3/6] [SLP][NFC] Refactor vectorizeStores::RangeSizes to
clarify usage
When testing out different VF's within vectorizeStores, we iterate in the order
`MaxVF, MaxVF / 2, ..., MinVF, MaxVF * 2, ...`
where:
- `MaxVF <= MaxRegVF`
- We only proceed past MinVF in cases where `MaxVF == MaxRegVF` (since `MaxVF < MaxRegVF`
only if `# stores < MaxRegVF`)
Prior to this, RangeSizes contains a pair, and we access the first/second element
depending on if VF < MaxRegVF.
In the main `while` loop, we iterated though CandidateVFs multiple times
- In the first iteration, we handle VFs where VF <= MaxRegVF
- In subsequent iterations, we handles VFs were VF > MaxRegVF
The behavior of rangeSizes prior to this change:
- if the element at index `Idx` is vectorized, we update RangeSizes[Idx].first = RangeSizes[Idx].second = 0
- if we find a new unprofitable tree, we attempt to update RangeSizes[Idx].second if VF > MaxRegVF, otherwise
RangeSizes[Idx].first
- FirstSizeSame, VFIsProfitable, and CheckTreeSizes use RangeSizes[Idx].first if VF > MaxRegVF, otherwise
RangeSizes[Idx].second
- After iterating through all elements in CandidateVFs, we then update RangeSizes[Idx].first with RangeSizes[Idx].second
In the first iteration of the `while` loop, RangeSizes[...].first is used to store updated values temporarily while we access elements from second.
In future iterations (where now `VF > MaxRegVF`), we update RangeSizes[...].second and access out of RangeSizes[...].first. The values from `second` are propogated to `first` at the end of the loop.
So effectively, we are just using the second part of the pair to cache updates while we iterate through the remainder of the CandidateVFs for a given loop. This commit updates that so that `second` is explicitly used as a cache and `first` is used for checks.
In theory this should speed up compilation time by removing a few if statements, but I imagine the amount is negligible.
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 31 ++++++++-----------
1 file changed, 13 insertions(+), 18 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 923f2ec1e6fe3..5ae6426a0cd65 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24075,13 +24075,12 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
}
/// Checks if the quadratic mean deviation is less than 90% of the mean size.
-static bool checkTreeSizes(ArrayRef<std::pair<unsigned, unsigned>> Sizes,
- bool First) {
+static bool checkTreeSizes(ArrayRef<std::pair<unsigned, unsigned>> Sizes) {
unsigned Num = 0;
uint64_t Sum = std::accumulate(
Sizes.begin(), Sizes.end(), static_cast<uint64_t>(0),
[&](uint64_t V, const std::pair<unsigned, unsigned> &Val) {
- unsigned Size = First ? Val.first : Val.second;
+ unsigned Size = Val.first;
if (Size == 1)
return V;
++Num;
@@ -24095,7 +24094,7 @@ static bool checkTreeSizes(ArrayRef<std::pair<unsigned, unsigned>> Sizes,
uint64_t Dev = std::accumulate(
Sizes.begin(), Sizes.end(), static_cast<uint64_t>(0),
[&](uint64_t V, const std::pair<unsigned, unsigned> &Val) {
- unsigned P = First ? Val.first : Val.second;
+ unsigned P = Val.first;
if (P == 1)
return V;
return V + (P - Mean) * (P - Mean);
@@ -24291,6 +24290,9 @@ bool SLPVectorizerPass::vectorizeStores(
unsigned End = Operands.size();
unsigned Repeat = 0;
constexpr unsigned MaxAttempts = 4;
+ // first: the best TreeSize from all prior loops over CandidateVFs, gets
+ // updated after looping through CandidateVFs second: the best TreeSize
+ // from all prior loops including the current one
llvm::SmallVector<std::pair<unsigned, unsigned>> RangeSizesStorage(
Operands.size(), {1, 1});
// The `slice` and `drop_front` interfaces are convenient
@@ -24302,9 +24304,9 @@ bool SLPVectorizerPass::vectorizeStores(
auto IsVectorized = [](const std::pair<unsigned, unsigned> &P) {
return P.first == 0;
};
- auto VFIsProfitable = [](bool First, unsigned Size,
+ auto VFIsProfitable = [](unsigned Size,
const std::pair<unsigned, unsigned> &P) {
- return First ? Size >= P.first : Size >= P.second;
+ return Size >= P.first;
};
auto FirstSizeSame = [](unsigned Size,
const std::pair<unsigned, unsigned> &P) {
@@ -24328,8 +24330,7 @@ bool SLPVectorizerPass::vectorizeStores(
unsigned MaxSliceEnd = FirstVecStore >= End ? End : FirstVecStore;
for (unsigned SliceStartIdx = FirstUnvecStore;
SliceStartIdx + VF <= MaxSliceEnd;) {
- if (!checkTreeSizes(RangeSizes.slice(SliceStartIdx, VF),
- VF > MaxRegVF)) {
+ if (!checkTreeSizes(RangeSizes.slice(SliceStartIdx, VF))) {
++SliceStartIdx;
continue;
}
@@ -24397,8 +24398,7 @@ bool SLPVectorizerPass::vectorizeStores(
}
if (VF > 2 && Res &&
!all_of(RangeSizes.slice(SliceStartIdx, VF),
- std::bind(VFIsProfitable, VF > MaxRegVF, TreeSize,
- _1))) {
+ std::bind(VFIsProfitable, TreeSize, _1))) {
SliceStartIdx += VF;
continue;
}
@@ -24413,15 +24413,10 @@ bool SLPVectorizerPass::vectorizeStores(
++SliceStartIdx;
continue;
}
- if (TreeSize > 1) {
+ if (TreeSize > 1)
for (std::pair<unsigned, unsigned> &P :
- RangeSizes.slice(SliceStartIdx, VF)) {
- if (VF > MaxRegVF)
- P.second = std::max(P.second, TreeSize);
- else
- P.first = std::max(P.first, TreeSize);
- }
- }
+ RangeSizes.slice(SliceStartIdx, VF))
+ P.second = std::max(P.second, TreeSize);
++SliceStartIdx;
AnyProfitableGraph = true;
}
>From 7e5333ec23de03fd283d64dbfba04b5a77d38a57 Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Mon, 26 Jan 2026 13:09:33 -0800
Subject: [PATCH 4/6] [SLP] Move RangeSizes from second to first after MaxRegVF
iteration
Preseves the exact behavior from before the MR.
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 5 +++++
1 file changed, 5 insertions(+)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 5ae6426a0cd65..0b01109c2ad6a 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24431,6 +24431,11 @@ bool SLPVectorizerPass::vectorizeStores(
}
if (!AnyProfitableGraph && VF >= MaxRegVF && has_single_bit(VF))
break;
+ // For the MaxRegVF, save RangeSizes to limit compile time
+ if (VF == MaxRegVF)
+ for (std::pair<unsigned, unsigned> &P : RangeSizes)
+ if (P.first != 0)
+ P.first = std::max(P.second, P.first);
}
// All values vectorized - exit.
if (all_of(RangeSizes, IsVectorized))
>From 2064612ac246d96c2c548d4b27c23a2856d0ef1d Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Mon, 26 Jan 2026 13:11:58 -0800
Subject: [PATCH 5/6] [SLP] Fix formatting
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 0b01109c2ad6a..5f112da3b118e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24291,8 +24291,9 @@ bool SLPVectorizerPass::vectorizeStores(
unsigned Repeat = 0;
constexpr unsigned MaxAttempts = 4;
// first: the best TreeSize from all prior loops over CandidateVFs, gets
- // updated after looping through CandidateVFs second: the best TreeSize
- // from all prior loops including the current one
+ // updated after looping through CandidateVFs
+ // second: the best TreeSize from all prior loops including the current
+ // one
llvm::SmallVector<std::pair<unsigned, unsigned>> RangeSizesStorage(
Operands.size(), {1, 1});
// The `slice` and `drop_front` interfaces are convenient
>From 1a6f0336f9a04cfa4136ee6675b14ae70e17a76c Mon Sep 17 00:00:00 2001
From: bababuck <rbuchner at qti.qualcomm.com>
Date: Mon, 26 Jan 2026 16:36:54 -0800
Subject: [PATCH 6/6] [SLP] Fix typo
---
llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 5f112da3b118e..81c6d33c1f5bf 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -24432,7 +24432,7 @@ bool SLPVectorizerPass::vectorizeStores(
}
if (!AnyProfitableGraph && VF >= MaxRegVF && has_single_bit(VF))
break;
- // For the MaxRegVF, save RangeSizes to limit compile time
+ // For the MaxRegVF case, save RangeSizes to limit compile time
if (VF == MaxRegVF)
for (std::pair<unsigned, unsigned> &P : RangeSizes)
if (P.first != 0)
More information about the llvm-commits
mailing list