-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[SLP] Emit reduction instead of 2 extracts + scalar op, when vectorizing operands #147583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alexey-bataev
merged 4 commits into
main
from
users/alexey-bataev/spr/slp-emit-reduction-instead-of-2-extracts-scalar-op-when-vectorizing-operands
Jul 9, 2025
+245
−129
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21676,58 +21676,6 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R, | |
return Changed; | ||
} | ||
|
||
bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) { | ||
if (!I) | ||
return false; | ||
|
||
if (!isa<BinaryOperator, CmpInst>(I) || isa<VectorType>(I->getType())) | ||
return false; | ||
|
||
Value *P = I->getParent(); | ||
|
||
// Vectorize in current basic block only. | ||
auto *Op0 = dyn_cast<Instruction>(I->getOperand(0)); | ||
auto *Op1 = dyn_cast<Instruction>(I->getOperand(1)); | ||
if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P || | ||
R.isDeleted(Op0) || R.isDeleted(Op1)) | ||
return false; | ||
|
||
// First collect all possible candidates | ||
SmallVector<std::pair<Value *, Value *>, 4> Candidates; | ||
Candidates.emplace_back(Op0, Op1); | ||
|
||
auto *A = dyn_cast<BinaryOperator>(Op0); | ||
auto *B = dyn_cast<BinaryOperator>(Op1); | ||
// Try to skip B. | ||
if (A && B && B->hasOneUse()) { | ||
auto *B0 = dyn_cast<BinaryOperator>(B->getOperand(0)); | ||
auto *B1 = dyn_cast<BinaryOperator>(B->getOperand(1)); | ||
if (B0 && B0->getParent() == P && !R.isDeleted(B0)) | ||
Candidates.emplace_back(A, B0); | ||
if (B1 && B1->getParent() == P && !R.isDeleted(B1)) | ||
Candidates.emplace_back(A, B1); | ||
} | ||
// Try to skip A. | ||
if (B && A && A->hasOneUse()) { | ||
auto *A0 = dyn_cast<BinaryOperator>(A->getOperand(0)); | ||
auto *A1 = dyn_cast<BinaryOperator>(A->getOperand(1)); | ||
if (A0 && A0->getParent() == P && !R.isDeleted(A0)) | ||
Candidates.emplace_back(A0, B); | ||
if (A1 && A1->getParent() == P && !R.isDeleted(A1)) | ||
Candidates.emplace_back(A1, B); | ||
} | ||
|
||
if (Candidates.size() == 1) | ||
return tryToVectorizeList({Op0, Op1}, R); | ||
|
||
// We have multiple options. Try to pick the single best. | ||
std::optional<int> BestCandidate = R.findBestRootPair(Candidates); | ||
if (!BestCandidate) | ||
return false; | ||
return tryToVectorizeList( | ||
{Candidates[*BestCandidate].first, Candidates[*BestCandidate].second}, R); | ||
} | ||
|
||
namespace { | ||
|
||
/// Model horizontal reductions. | ||
|
@@ -21770,6 +21718,8 @@ class HorizontalReduction { | |
/// Checks if the optimization of original scalar identity operations on | ||
/// matched horizontal reductions is enabled and allowed. | ||
bool IsSupportedHorRdxIdentityOp = false; | ||
/// The minimum number of the reduced values. | ||
const unsigned ReductionLimit = VectorizeNonPowerOf2 ? 3 : 4; | ||
/// Contains vector values for reduction including their scale factor and | ||
/// signedness. | ||
SmallVector<std::tuple<Value *, unsigned, bool>> VectorValuesAndScales; | ||
|
@@ -22068,6 +22018,24 @@ class HorizontalReduction { | |
|
||
public: | ||
HorizontalReduction() = default; | ||
HorizontalReduction(Instruction *I, ArrayRef<Value *> Ops) | ||
: ReductionRoot(I), ReductionLimit(2) { | ||
RdxKind = HorizontalReduction::getRdxKind(I); | ||
ReductionOps.emplace_back().push_back(I); | ||
ReducedVals.emplace_back().assign(Ops.begin(), Ops.end()); | ||
for (Value *V : Ops) | ||
ReducedValsToOps[V].push_back(I); | ||
} | ||
|
||
bool matchReductionForOperands() const { | ||
// Analyze "regular" integer/FP types for reductions - no target-specific | ||
// types or pointers. | ||
assert(ReductionRoot && "Reduction root is not set!"); | ||
if (!isVectorizable(RdxKind, cast<Instruction>(ReductionRoot))) | ||
return false; | ||
|
||
return true; | ||
} | ||
|
||
/// Try to find a reduction tree. | ||
bool matchAssociativeReduction(BoUpSLP &R, Instruction *Root, | ||
|
@@ -22235,7 +22203,6 @@ class HorizontalReduction { | |
/// Attempt to vectorize the tree found by matchAssociativeReduction. | ||
Value *tryToReduce(BoUpSLP &V, const DataLayout &DL, TargetTransformInfo *TTI, | ||
const TargetLibraryInfo &TLI, AssumptionCache *AC) { | ||
const unsigned ReductionLimit = VectorizeNonPowerOf2 ? 3 : 4; | ||
constexpr unsigned RegMaxNumber = 4; | ||
constexpr unsigned RedValsMaxNumber = 128; | ||
// If there are a sufficient number of reduction values, reduce | ||
|
@@ -23740,6 +23707,103 @@ bool SLPVectorizerPass::vectorizeHorReduction( | |
return Res; | ||
} | ||
|
||
bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) { | ||
if (!I) | ||
return false; | ||
|
||
if (!isa<BinaryOperator, CmpInst>(I) || isa<VectorType>(I->getType())) | ||
return false; | ||
|
||
Value *P = I->getParent(); | ||
|
||
// Vectorize in current basic block only. | ||
auto *Op0 = dyn_cast<Instruction>(I->getOperand(0)); | ||
auto *Op1 = dyn_cast<Instruction>(I->getOperand(1)); | ||
if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P || | ||
R.isDeleted(Op0) || R.isDeleted(Op1)) | ||
return false; | ||
|
||
// First collect all possible candidates | ||
SmallVector<std::pair<Value *, Value *>, 4> Candidates; | ||
Candidates.emplace_back(Op0, Op1); | ||
|
||
auto *A = dyn_cast<BinaryOperator>(Op0); | ||
auto *B = dyn_cast<BinaryOperator>(Op1); | ||
// Try to skip B. | ||
if (A && B && B->hasOneUse()) { | ||
auto *B0 = dyn_cast<BinaryOperator>(B->getOperand(0)); | ||
auto *B1 = dyn_cast<BinaryOperator>(B->getOperand(1)); | ||
if (B0 && B0->getParent() == P && !R.isDeleted(B0)) | ||
Candidates.emplace_back(A, B0); | ||
if (B1 && B1->getParent() == P && !R.isDeleted(B1)) | ||
Candidates.emplace_back(A, B1); | ||
} | ||
// Try to skip A. | ||
if (B && A && A->hasOneUse()) { | ||
auto *A0 = dyn_cast<BinaryOperator>(A->getOperand(0)); | ||
auto *A1 = dyn_cast<BinaryOperator>(A->getOperand(1)); | ||
if (A0 && A0->getParent() == P && !R.isDeleted(A0)) | ||
Candidates.emplace_back(A0, B); | ||
if (A1 && A1->getParent() == P && !R.isDeleted(A1)) | ||
Candidates.emplace_back(A1, B); | ||
} | ||
|
||
auto TryToReduce = [this, &R, &TTI = *TTI](Instruction *Inst, | ||
ArrayRef<Value *> Ops) { | ||
if (!isReductionCandidate(Inst)) | ||
return false; | ||
Type *Ty = Inst->getType(); | ||
if (!isValidElementType(Ty) || Ty->isPointerTy()) | ||
return false; | ||
HorizontalReduction HorRdx(Inst, Ops); | ||
if (!HorRdx.matchReductionForOperands()) | ||
return false; | ||
// Check the cost of operations. | ||
VectorType *VecTy = getWidenedType(Ty, Ops.size()); | ||
constexpr TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput; | ||
InstructionCost ScalarCost = | ||
TTI.getScalarizationOverhead( | ||
VecTy, APInt::getAllOnes(getNumElements(VecTy)), /*Insert=*/false, | ||
/*Extract=*/true, CostKind) + | ||
TTI.getInstructionCost(Inst, CostKind); | ||
InstructionCost RedCost; | ||
switch (::getRdxKind(Inst)) { | ||
case RecurKind::Add: | ||
case RecurKind::Mul: | ||
case RecurKind::Or: | ||
case RecurKind::And: | ||
case RecurKind::Xor: | ||
case RecurKind::FAdd: | ||
case RecurKind::FMul: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't the integer min/max kinds be included here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not vectorize it here for now, just binaryoperations and compares |
||
FastMathFlags FMF; | ||
if (auto *FPCI = dyn_cast<FPMathOperator>(Inst)) | ||
FMF = FPCI->getFastMathFlags(); | ||
RedCost = TTI.getArithmeticReductionCost(Inst->getOpcode(), VecTy, FMF, | ||
CostKind); | ||
break; | ||
} | ||
default: | ||
return false; | ||
} | ||
if (RedCost >= ScalarCost) | ||
return false; | ||
|
||
return HorRdx.tryToReduce(R, *DL, &TTI, *TLI, AC) != nullptr; | ||
}; | ||
if (Candidates.size() == 1) | ||
return TryToReduce(I, {Op0, Op1}) || tryToVectorizeList({Op0, Op1}, R); | ||
|
||
// We have multiple options. Try to pick the single best. | ||
std::optional<int> BestCandidate = R.findBestRootPair(Candidates); | ||
if (!BestCandidate) | ||
return false; | ||
return TryToReduce(I, {Candidates[*BestCandidate].first, | ||
Candidates[*BestCandidate].second}) || | ||
tryToVectorizeList({Candidates[*BestCandidate].first, | ||
Candidates[*BestCandidate].second}, | ||
R); | ||
} | ||
|
||
bool SLPVectorizerPass::vectorizeRootInstruction(PHINode *P, Instruction *Root, | ||
BasicBlock *BB, BoUpSLP &R) { | ||
SmallVector<WeakTrackingVH> PostponedInsts; | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
please can you move this as a NFC and the rebase? I can't tell if there's any changes to the implementation