[llvm] [SLP] Better track MinBWs between vectorization attempts (PR #224199)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 16 23:10:45 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Ryan Buchner (bababuck)
<details>
<summary>Changes</summary>
Not sure if this change is appropriate, feel free to shoot it down on principle.
When retrying vectorization with smaller VF, we may be able to vectorize further where previously gathering was required. This may expose [s/z]ext instruction that would allow us to optimize via MinBWs. In such cases, ignore AnalyzedMinBWVals and recompute MinBWs anyways.
Assisted By: Codex
---
Full diff: https://github.com/llvm/llvm-project/pull/224199.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (+13-7)
- (modified) llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll (+15-15)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index a0adca7aabad0..01adb4b4ea20e 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -3699,9 +3699,9 @@ class slpvectorizer::BoUpSLP {
mutable SmallDenseMap<std::tuple<Type *, Type *, unsigned>, unsigned>
NumberOfPartsCache;
- /// Values, already been analyzed for mininmal bitwidth and found to be
- /// non-profitable.
- DenseSet<Value *> AnalyzedMinBWVals;
+ /// Values already analyzed for minimal bitwidth and found to be
+ /// non-profitable, mapped to the size of the tree used for the analysis.
+ SmallDenseMap<Value *, unsigned> AnalyzedMinBWVals;
/// A list of values that need to extracted out of the tree.
/// This list holds pairs of (Internal Scalar : External User). External User
@@ -27528,8 +27528,14 @@ void BoUpSLP::computeMinimumValueSizes() {
++NodeIdx;
}
+ auto IsAnalyzedMinBWVal = [&](Value *V) {
+ auto It = AnalyzedMinBWVals.find(V);
+ return It != AnalyzedMinBWVals.end() &&
+ It->second >= VectorizableTree.size();
+ };
+
// Analyzed the reduction already and not profitable - exit.
- if (AnalyzedMinBWVals.contains(VectorizableTree[NodeIdx]->Scalars.front()))
+ if (IsAnalyzedMinBWVal(VectorizableTree[NodeIdx]->Scalars.front()))
return;
SmallVector<unsigned> ToDemote;
@@ -27591,8 +27597,7 @@ void BoUpSLP::computeMinimumValueSizes() {
if (!TreeRootIT)
return 0u;
- if (any_of(E.Scalars,
- [&](Value *V) { return AnalyzedMinBWVals.contains(V); }))
+ if (any_of(E.Scalars, IsAnalyzedMinBWVal))
return 0u;
unsigned NumParts =
@@ -27834,7 +27839,8 @@ void BoUpSLP::computeMinimumValueSizes() {
cast<IntegerType>(TreeRoot.front()->getType()->getScalarType())
->getBitWidth()) {
if (UserIgnoreList)
- AnalyzedMinBWVals.insert_range(TreeRoot);
+ for (Value *V : TreeRoot)
+ AnalyzedMinBWVals[V] = VectorizableTree.size();
NodesToKeepBWs.insert_range(ToDemote);
continue;
}
diff --git a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
index e266733af51c3..0519227aa09e4 100644
--- a/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
+++ b/llvm/test/Transforms/SLPVectorizer/AArch64/revec-reductions.ll
@@ -6,25 +6,25 @@ define <16 x i64> @test(<16 x i8> %x) {
; CHECK-SAME: <16 x i8> [[X:%.*]]) {
; CHECK-NEXT: [[ENTRY:.*:]]
; CHECK-NEXT: [[TMP0:%.*]] = zext <16 x i8> [[X]] to <16 x i64>
-; CHECK-NEXT: [[TMP1:%.*]] = sub <16 x i64> zeroinitializer, [[TMP0]]
+; CHECK-NEXT: [[TMP12:%.*]] = sub <16 x i64> zeroinitializer, [[TMP0]]
; CHECK-NEXT: [[TMP2:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT: [[TMP3:%.*]] = sub <16 x i64> zeroinitializer, [[TMP2]]
-; CHECK-NEXT: [[TMP4:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP1]], <16 x i64> [[TMP3]])
-; CHECK-NEXT: [[TMP5:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT: [[TMP6:%.*]] = sub <16 x i64> zeroinitializer, [[TMP5]]
-; CHECK-NEXT: [[TMP7:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP4]], <16 x i64> [[TMP6]])
-; CHECK-NEXT: [[TMP8:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
-; CHECK-NEXT: [[TMP9:%.*]] = sub <16 x i64> zeroinitializer, [[TMP8]]
-; CHECK-NEXT: [[TMP10:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP7]], <16 x i64> [[TMP9]])
-; CHECK-NEXT: [[TMP11:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
-; CHECK-NEXT: [[TMP12:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP10]], <16 x i64> [[TMP11]])
-; CHECK-NEXT: [[TMP13:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP13:%.*]] = sub <16 x i64> zeroinitializer, [[TMP2]]
; CHECK-NEXT: [[TMP14:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP12]], <16 x i64> [[TMP13]])
-; CHECK-NEXT: [[TMP15:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP5:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
+; CHECK-NEXT: [[TMP15:%.*]] = sub <16 x i64> zeroinitializer, [[TMP5]]
; CHECK-NEXT: [[TMP16:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP14]], <16 x i64> [[TMP15]])
-; CHECK-NEXT: [[TMP17:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP8:%.*]] = zext <16 x i8> zeroinitializer to <16 x i64>
+; CHECK-NEXT: [[TMP17:%.*]] = sub <16 x i64> zeroinitializer, [[TMP8]]
; CHECK-NEXT: [[TMP48:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP16]], <16 x i64> [[TMP17]])
-; CHECK-NEXT: ret <16 x i64> [[TMP48]]
+; CHECK-NEXT: [[TMP11:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP19:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP48]], <16 x i64> [[TMP11]])
+; CHECK-NEXT: [[TMP20:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP21:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP19]], <16 x i64> [[TMP20]])
+; CHECK-NEXT: [[TMP22:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP23:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP21]], <16 x i64> [[TMP22]])
+; CHECK-NEXT: [[TMP24:%.*]] = sub <16 x i64> zeroinitializer, zeroinitializer
+; CHECK-NEXT: [[TMP18:%.*]] = call <16 x i64> @llvm.smin.v16i64(<16 x i64> [[TMP23]], <16 x i64> [[TMP24]])
+; CHECK-NEXT: ret <16 x i64> [[TMP18]]
;
entry:
%0 = zext <16 x i8> %x to <16 x i64>
``````````
</details>
https://github.com/llvm/llvm-project/pull/224199
More information about the llvm-commits
mailing list