[llvm-branch-commits] [llvm] [SLP][modularisation][NFC] Move getReductionInstr/getAggregateSize to SLPReductionUtils (PR #222613)
via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Thu Sep 10 05:37:32 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Madhur Amilkanthwar (madhur13490)
<details>
<summary>Changes</summary>
Move the BoUpSLP-independent helpers getReductionInstr and getAggregateSize
out of SLPVectorizer.cpp into the self-contained
SLPVectorizer/SLPReductionUtils.{h,cpp} module.
Part of the SLPVectorizer.cpp modularization effort:
https://discourse.llvm.org/t/modularizing-slpvectorizer-cpp/90922
---
Full diff: https://github.com/llvm/llvm-project/pull/222613.diff
3 Files Affected:
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp (-79)
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.cpp (+77)
- (modified) llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.h (+16)
``````````diff
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
index ee9ad7fc43973..19fabd2ab9e65 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp
@@ -32338,34 +32338,6 @@ class HorizontalReduction {
static RecurKind getRdxKind(Value *V) {
return HorizontalReduction::getRdxKind(V);
}
-static std::optional<unsigned> getAggregateSize(Instruction *InsertInst) {
- if (auto *IE = dyn_cast<InsertElementInst>(InsertInst))
- return cast<FixedVectorType>(IE->getType())->getNumElements();
-
- unsigned AggregateSize = 1;
- auto *IV = cast<InsertValueInst>(InsertInst);
- Type *CurrentType = IV->getType();
- do {
- if (auto *ST = dyn_cast<StructType>(CurrentType)) {
- for (auto *Elt : ST->elements())
- if (Elt != ST->getElementType(0)) // check homogeneity
- return std::nullopt;
- AggregateSize *= ST->getNumElements();
- CurrentType = ST->getElementType(0);
- } else if (auto *AT = dyn_cast<ArrayType>(CurrentType)) {
- AggregateSize *= AT->getNumElements();
- CurrentType = AT->getElementType();
- } else if (auto *VT = dyn_cast<FixedVectorType>(CurrentType)) {
- AggregateSize *= VT->getNumElements();
- return AggregateSize;
- } else if (CurrentType->isSingleValueType()) {
- return AggregateSize;
- } else {
- return std::nullopt;
- }
- } while (true);
-}
-
static void findBuildAggregateRec(Instruction *LastInsertInst,
TargetTransformInfo *TTI,
SmallVectorImpl<Value *> &BuildVectorOpds,
@@ -32433,57 +32405,6 @@ static bool findBuildAggregate(Instruction *LastInsertInst,
return false;
}
-/// Try and get a reduction instruction from a phi node.
-///
-/// Given a phi node \p P in a block \p ParentBB, consider possible reductions
-/// if they come from either \p ParentBB or a containing loop latch.
-///
-/// \returns A candidate reduction value if possible, or \code nullptr \endcode
-/// if not possible.
-static Instruction *getReductionInstr(const DominatorTree *DT, PHINode *P,
- BasicBlock *ParentBB, LoopInfo *LI) {
- // There are situations where the reduction value is not dominated by the
- // reduction phi. Vectorizing such cases has been reported to cause
- // miscompiles. See PR25787.
- auto DominatedReduxValue = [&](Value *R) {
- return isa<Instruction>(R) &&
- DT->dominates(P->getParent(), cast<Instruction>(R)->getParent());
- };
-
- Instruction *Rdx = nullptr;
-
- // Return the incoming value if it comes from the same BB as the phi node.
- if (P->getIncomingBlock(0) == ParentBB) {
- Rdx = dyn_cast<Instruction>(P->getIncomingValue(0));
- } else if (P->getIncomingBlock(1) == ParentBB) {
- Rdx = dyn_cast<Instruction>(P->getIncomingValue(1));
- }
-
- if (Rdx && DominatedReduxValue(Rdx))
- return Rdx;
-
- // Otherwise, check whether we have a loop latch to look at.
- Loop *BBL = LI->getLoopFor(ParentBB);
- if (!BBL)
- return nullptr;
- BasicBlock *BBLatch = BBL->getLoopLatch();
- if (!BBLatch)
- return nullptr;
-
- // There is a loop latch, return the incoming value if it comes from
- // that. This reduction pattern occasionally turns up.
- if (P->getIncomingBlock(0) == BBLatch) {
- Rdx = dyn_cast<Instruction>(P->getIncomingValue(0));
- } else if (P->getIncomingBlock(1) == BBLatch) {
- Rdx = dyn_cast<Instruction>(P->getIncomingValue(1));
- }
-
- if (Rdx && DominatedReduxValue(Rdx))
- return Rdx;
-
- return nullptr;
-}
-
/// We could have an initial reduction that is not an add.
/// r *= v1 + v2 + v3 + v4
/// In such a case start looking for a tree rooted in the first '+'.
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.cpp b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.cpp
index 94080e4e28ebb..2b018dc3de173 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.cpp
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.cpp
@@ -8,10 +8,15 @@
#include "SLPReductionUtils.h"
+#include "llvm/Analysis/LoopInfo.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/Dominators.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/PatternMatch.h"
+#include <optional>
+
using namespace llvm;
using namespace llvm::PatternMatch;
@@ -54,4 +59,76 @@ bool isReductionCandidate(Instruction *I) {
return IsBinop || IsSelect;
}
+std::optional<unsigned> getAggregateSize(Instruction *InsertInst) {
+ if (auto *IE = dyn_cast<InsertElementInst>(InsertInst))
+ return cast<FixedVectorType>(IE->getType())->getNumElements();
+
+ unsigned AggregateSize = 1;
+ auto *IV = cast<InsertValueInst>(InsertInst);
+ Type *CurrentType = IV->getType();
+ do {
+ if (auto *ST = dyn_cast<StructType>(CurrentType)) {
+ for (auto *Elt : ST->elements())
+ if (Elt != ST->getElementType(0)) // check homogeneity
+ return std::nullopt;
+ AggregateSize *= ST->getNumElements();
+ CurrentType = ST->getElementType(0);
+ } else if (auto *AT = dyn_cast<ArrayType>(CurrentType)) {
+ AggregateSize *= AT->getNumElements();
+ CurrentType = AT->getElementType();
+ } else if (auto *VT = dyn_cast<FixedVectorType>(CurrentType)) {
+ AggregateSize *= VT->getNumElements();
+ return AggregateSize;
+ } else if (CurrentType->isSingleValueType()) {
+ return AggregateSize;
+ } else {
+ return std::nullopt;
+ }
+ } while (true);
+}
+
+Instruction *getReductionInstr(const DominatorTree *DT, PHINode *P,
+ BasicBlock *ParentBB, LoopInfo *LI) {
+ // There are situations where the reduction value is not dominated by the
+ // reduction phi. Vectorizing such cases has been reported to cause
+ // miscompiles. See PR25787.
+ auto DominatedReduxValue = [&](Value *R) {
+ return isa<Instruction>(R) &&
+ DT->dominates(P->getParent(), cast<Instruction>(R)->getParent());
+ };
+
+ Instruction *Rdx = nullptr;
+
+ // Return the incoming value if it comes from the same BB as the phi node.
+ if (P->getIncomingBlock(0) == ParentBB) {
+ Rdx = dyn_cast<Instruction>(P->getIncomingValue(0));
+ } else if (P->getIncomingBlock(1) == ParentBB) {
+ Rdx = dyn_cast<Instruction>(P->getIncomingValue(1));
+ }
+
+ if (Rdx && DominatedReduxValue(Rdx))
+ return Rdx;
+
+ // Otherwise, check whether we have a loop latch to look at.
+ Loop *BBL = LI->getLoopFor(ParentBB);
+ if (!BBL)
+ return nullptr;
+ BasicBlock *BBLatch = BBL->getLoopLatch();
+ if (!BBLatch)
+ return nullptr;
+
+ // There is a loop latch, return the incoming value if it comes from
+ // that. This reduction pattern occasionally turns up.
+ if (P->getIncomingBlock(0) == BBLatch) {
+ Rdx = dyn_cast<Instruction>(P->getIncomingValue(0));
+ } else if (P->getIncomingBlock(1) == BBLatch) {
+ Rdx = dyn_cast<Instruction>(P->getIncomingValue(1));
+ }
+
+ if (Rdx && DominatedReduxValue(Rdx))
+ return Rdx;
+
+ return nullptr;
+}
+
} // namespace llvm::slpvectorizer
diff --git a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.h b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.h
index 1ba4af797b628..fea85c3431772 100644
--- a/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.h
+++ b/llvm/lib/Transforms/Vectorize/SLPVectorizer/SLPReductionUtils.h
@@ -15,8 +15,13 @@
#ifndef LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPREDUCTIONUTILS_H
#define LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPREDUCTIONUTILS_H
+#include <optional>
+
namespace llvm {
+class BasicBlock;
+class DominatorTree;
class Instruction;
+class LoopInfo;
class PHINode;
} // namespace llvm
@@ -30,6 +35,17 @@ Instruction *getNonPhiOperand(Instruction *I, PHINode *Phi);
/// vectorization.
bool isReductionCandidate(Instruction *I);
+/// \returns the number of elements of the homogeneous aggregate built by
+/// \p InsertInst (insertelement or insertvalue), or std::nullopt if it is not
+/// a homogeneous aggregate.
+std::optional<unsigned> getAggregateSize(Instruction *InsertInst);
+
+/// Try to get a reduction instruction from phi node \p P in block \p ParentBB,
+/// considering incoming values from \p ParentBB or the containing loop latch.
+/// \returns a candidate reduction value, or nullptr if none.
+Instruction *getReductionInstr(const DominatorTree *DT, PHINode *P,
+ BasicBlock *ParentBB, LoopInfo *LI);
+
} // namespace llvm::slpvectorizer
#endif // LLVM_LIB_TRANSFORMS_VECTORIZE_SLPVECTORIZER_SLPREDUCTIONUTILS_H
``````````
</details>
https://github.com/llvm/llvm-project/pull/222613
More information about the llvm-branch-commits
mailing list