Skip to content

Reland "[LoopVectorizer] Add support for chaining partial reductions #120272" #124282

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
merged 7 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 48 additions & 29 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8684,12 +8684,12 @@ VPReplicateRecipe *VPRecipeBuilder::handleReplication(Instruction *I,
/// are valid so recipes can be formed later.
void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
// Find all possible partial reductions.
SmallVector<std::pair<PartialReductionChain, unsigned>, 1>
SmallVector<std::pair<PartialReductionChain, unsigned>>
PartialReductionChains;
for (const auto &[Phi, RdxDesc] : Legal->getReductionVars())
if (std::optional<std::pair<PartialReductionChain, unsigned>> Pair =
getScaledReduction(Phi, RdxDesc, Range))
PartialReductionChains.push_back(*Pair);
for (const auto &[Phi, RdxDesc] : Legal->getReductionVars()) {
getScaledReductions(Phi, RdxDesc.getLoopExitInstr(), Range,
PartialReductionChains);
}

// A partial reduction is invalid if any of its extends are used by
// something that isn't another partial reduction. This is because the
Expand Down Expand Up @@ -8717,39 +8717,54 @@ void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
}
}

std::optional<std::pair<PartialReductionChain, unsigned>>
VPRecipeBuilder::getScaledReduction(PHINode *PHI,
const RecurrenceDescriptor &Rdx,
VFRange &Range) {
bool VPRecipeBuilder::getScaledReductions(
Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains) {

if (!CM.TheLoop->contains(RdxExitInstr))
return false;

// TODO: Allow scaling reductions when predicating. The select at
// the end of the loop chooses between the phi value and most recent
// reduction result, both of which have different VFs to the active lane
// mask when scaling.
if (CM.blockNeedsPredicationForAnyReason(Rdx.getLoopExitInstr()->getParent()))
return std::nullopt;
if (CM.blockNeedsPredicationForAnyReason(RdxExitInstr->getParent()))
return false;

auto *Update = dyn_cast<BinaryOperator>(Rdx.getLoopExitInstr());
auto *Update = dyn_cast<BinaryOperator>(RdxExitInstr);
if (!Update)
return std::nullopt;
return false;

Value *Op = Update->getOperand(0);
Value *PhiOp = Update->getOperand(1);
if (Op == PHI) {
Op = Update->getOperand(1);
PhiOp = Update->getOperand(0);
if (Op == PHI)
std::swap(Op, PhiOp);

// Try and get a scaled reduction from the first non-phi operand.
// If one is found, we use the discovered reduction instruction in
// place of the accumulator for costing.
if (auto *OpInst = dyn_cast<Instruction>(Op)) {
if (getScaledReductions(PHI, OpInst, Range, Chains)) {
PHI = Chains.rbegin()->first.Reduction;

Op = Update->getOperand(0);
PhiOp = Update->getOperand(1);
if (Op == PHI)
std::swap(Op, PhiOp);
}
}
if (PhiOp != PHI)
return std::nullopt;
return false;

auto *BinOp = dyn_cast<BinaryOperator>(Op);
if (!BinOp || !BinOp->hasOneUse())
return std::nullopt;
return false;

using namespace llvm::PatternMatch;
Value *A, *B;
if (!match(BinOp->getOperand(0), m_ZExtOrSExt(m_Value(A))) ||
!match(BinOp->getOperand(1), m_ZExtOrSExt(m_Value(B))))
return std::nullopt;
return false;

Instruction *ExtA = cast<Instruction>(BinOp->getOperand(0));
Instruction *ExtB = cast<Instruction>(BinOp->getOperand(1));
Expand All @@ -8759,7 +8774,7 @@ VPRecipeBuilder::getScaledReduction(PHINode *PHI,
TTI::PartialReductionExtendKind OpBExtend =
TargetTransformInfo::getPartialReductionExtendKind(ExtB);

PartialReductionChain Chain(Rdx.getLoopExitInstr(), ExtA, ExtB, BinOp);
PartialReductionChain Chain(RdxExitInstr, ExtA, ExtB, BinOp);

unsigned TargetScaleFactor =
PHI->getType()->getPrimitiveSizeInBits().getKnownScalarFactor(
Expand All @@ -8773,10 +8788,12 @@ VPRecipeBuilder::getScaledReduction(PHINode *PHI,
std::make_optional(BinOp->getOpcode()));
return Cost.isValid();
},
Range))
return std::make_pair(Chain, TargetScaleFactor);
Range)) {
Chains.push_back(std::make_pair(Chain, TargetScaleFactor));
return true;
}

return std::nullopt;
return false;
}

VPRecipeBase *
Expand Down Expand Up @@ -8871,12 +8888,14 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
"Unexpected number of operands for partial reduction");

VPValue *BinOp = Operands[0];
VPValue *Phi = Operands[1];
if (isa<VPReductionPHIRecipe>(BinOp->getDefiningRecipe()))
std::swap(BinOp, Phi);

return new VPPartialReductionRecipe(Reduction->getOpcode(), BinOp, Phi,
Reduction);
VPValue *Accumulator = Operands[1];
VPRecipeBase *BinOpRecipe = BinOp->getDefiningRecipe();
if (isa<VPReductionPHIRecipe>(BinOpRecipe) ||
isa<VPPartialReductionRecipe>(BinOpRecipe))
std::swap(BinOp, Accumulator);

return new VPPartialReductionRecipe(Reduction->getOpcode(), BinOp,
Accumulator, Reduction);
}

void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
Expand Down
16 changes: 10 additions & 6 deletions llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,16 @@ class VPRecipeBuilder {

/// Examines reduction operations to see if the target can use a cheaper
/// operation with a wider per-iteration input VF and narrower PHI VF.
/// Returns null if no scaled reduction was found, otherwise a pair with a
/// struct containing reduction information and the scaling factor between the
/// number of elements in the input and output.
std::optional<std::pair<PartialReductionChain, unsigned>>
getScaledReduction(PHINode *PHI, const RecurrenceDescriptor &Rdx,
VFRange &Range);
/// Each element within Chains is a pair with a struct containing reduction
/// information and the scaling factor between the number of elements in
/// the input and output.
/// Recursively calls itself to identify chained scaled reductions.
/// Returns true if this invocation added an entry to Chains, otherwise false.
/// i.e. returns false in the case that a subcall adds an entry to Chains,
/// but the top-level call does not.
bool getScaledReductions(
Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains);

public:
VPRecipeBuilder(VPlan &Plan, Loop *OrigLoop, const TargetLibraryInfo *TLI,
Expand Down
5 changes: 4 additions & 1 deletion llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2456,7 +2456,10 @@ class VPPartialReductionRecipe : public VPSingleDefRecipe {
: VPSingleDefRecipe(VPDef::VPPartialReductionSC,
ArrayRef<VPValue *>({Op0, Op1}), ReductionInst),
Opcode(Opcode) {
assert(isa<VPReductionPHIRecipe>(getOperand(1)->getDefiningRecipe()) &&
[[maybe_unused]] auto *AccumulatorRecipe =
getOperand(1)->getDefiningRecipe();
assert((isa<VPReductionPHIRecipe>(AccumulatorRecipe) ||
isa<VPPartialReductionRecipe>(AccumulatorRecipe)) &&
"Unexpected operand order for partial reduction recipe");
}
~VPPartialReductionRecipe() override = default;
Expand Down
Loading
Loading