[llvm] [SLP] Check legality before performing RT stride analysis (NFC). (PR #220968)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Thu Sep 3 10:57:30 PDT 2026
https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/220968
>From 881df31b1986103bfa81724631100257165d51bb Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Tue, 1 Sep 2026 09:17:24 +0100
Subject: [PATCH 1/2] [SLP] Check legality before performing RT stride analysis
(NFC).
analyzeRtStrideCandidate builds SCEVs for pointers during analysis,
which can be expensive, especially if none of the potential strides are
legal on the target.
This improves compile-time on SLP-heavy workloads, like lencod with
ThinLTO (-0.59% on lencod, -0.06% geomean).
https://llvm-compile-time-tracker.com/compare.php?from=d23b9dbc4e8b25fc673970b27fad53a2e7a7fe60&to=c968e08d341112dbf9c1e3741b423e254d1057ab&stat=instructions:u
Aided by Opus 5.
---
.../Transforms/Vectorize/SLPVectorizer.cpp | 46 +++++++++++++------
1 file changed, 32 insertions(+), 14 deletions(-)
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index 749265b73b118..a90944e36642e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -6465,6 +6465,36 @@ bool BoUpSLP::analyzeRtStrideCandidate(ArrayRef<Value *> PointerOps,
SmallVectorImpl<unsigned> &SortedIndices,
StridedPtrInfo &SPtrInfo,
bool IsLoad) const {
+ const unsigned Sz = PointerOps.size();
+ const unsigned MinProfitableStridedOps =
+ IsLoad ? MinProfitableStridedLoads : MinProfitableStridedStores;
+ if (Sz * getNumElements(BaseTy) < MinProfitableStridedOps)
+ return false;
+
+ // The element type of the strided access is fixed by the number of distinct
+ // constant offsets the pointers fall into, which has to divide their number.
+ auto GetStridedTy = [&](unsigned NumOffsets) -> FixedVectorType * {
+ Type *NewScalarTy = BaseTy;
+ if (NumOffsets > 1 || BaseTy->isVectorTy())
+ NewScalarTy = Type::getIntNTy(
+ SE->getContext(),
+ DL->getTypeSizeInBits(BaseTy).getFixedValue() * NumOffsets);
+ return dyn_cast<FixedVectorType>(
+ getWidenedType(NewScalarTy, Sz / NumOffsets));
+ };
+ auto IsLegalStridedTy = [&](FixedVectorType *StridedTy) {
+ return StridedTy && TTI->isTypeLegal(StridedTy) &&
+ TTI->isLegalStridedLoadStore(StridedTy, CommonAlignment);
+ };
+ // Determining the offsets below builds SCEVs for every pointer, which is
+ // expensive. Bail out if the target does not support any strided accesses at
+ // all.
+ if (none_of(seq_inclusive<unsigned>(1, Sz), [&](unsigned NumOffsets) {
+ return Sz % NumOffsets == 0 &&
+ IsLegalStridedTy(GetStridedTy(NumOffsets));
+ }))
+ return false;
+
// If each value in `PointerOps` is of the form `%x + Offset` where `Offset`
// is constant, we partition `PointerOps` sequence into subsequences of
// pointers with the same offset. For each offset we record values from
@@ -6507,9 +6537,7 @@ bool BoUpSLP::analyzeRtStrideCandidate(ArrayRef<Value *> PointerOps,
// Quick detour: at this point we can say what the type of strided load would
// be if all the checks pass. Check if this type is legal for the target.
- const unsigned Sz = PointerOps.size();
unsigned VecSz = Sz;
- Type *NewScalarTy = BaseTy;
if (NumOffsets > 1) {
if (Sz % NumOffsets != 0)
return false;
@@ -6519,18 +6547,8 @@ bool BoUpSLP::analyzeRtStrideCandidate(ArrayRef<Value *> PointerOps,
if (StrideMultiples.size() != VecSz)
return false;
- if (NumOffsets > 1 || BaseTy->isVectorTy())
- NewScalarTy = Type::getIntNTy(
- SE->getContext(),
- DL->getTypeSizeInBits(BaseTy).getFixedValue() * NumOffsets);
- auto *StridedLoadTy =
- cast<FixedVectorType>(getWidenedType(NewScalarTy, VecSz));
- unsigned MinProfitableStridedOps =
- IsLoad ? MinProfitableStridedLoads : MinProfitableStridedStores;
- const unsigned BaseTyNumElts = getNumElements(BaseTy);
- if (Sz * BaseTyNumElts < MinProfitableStridedOps ||
- !TTI->isTypeLegal(StridedLoadTy) ||
- !TTI->isLegalStridedLoadStore(StridedLoadTy, CommonAlignment))
+ auto *StridedLoadTy = GetStridedTy(NumOffsets);
+ if (!IsLegalStridedTy(StridedLoadTy))
return false;
// Check if the offsets are contiguous and that each group has the required
>From 0da665a7fa574162fa6e7ae3f3d3a62338a4bfae Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 3 Sep 2026 18:56:57 +0100
Subject: [PATCH 2/2] !fixup switch to seq, thanks
---
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 a90944e36642e..108b8a52d54bc 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -6489,7 +6489,7 @@ bool BoUpSLP::analyzeRtStrideCandidate(ArrayRef<Value *> PointerOps,
// Determining the offsets below builds SCEVs for every pointer, which is
// expensive. Bail out if the target does not support any strided accesses at
// all.
- if (none_of(seq_inclusive<unsigned>(1, Sz), [&](unsigned NumOffsets) {
+ if (none_of(seq<unsigned>(1, Sz), [&](unsigned NumOffsets) {
return Sz % NumOffsets == 0 &&
IsLegalStridedTy(GetStridedTy(NumOffsets));
}))
More information about the llvm-commits
mailing list