Skip to content

[LoopVectorizer] Add support for chaining partial reductions #120272

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 8 commits into from
Jan 23, 2025
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64TargetTransformInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ class AArch64TTIImpl : public BasicTTIImplBase<AArch64TTIImpl> {
InstructionCost Invalid = InstructionCost::getInvalid();
InstructionCost Cost(TTI::TCC_Basic);

if (Opcode != Instruction::Add)
if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
return Invalid;

if (InputTypeA != InputTypeB)
Expand Down
55 changes: 38 additions & 17 deletions llvm/lib/Transforms/Vectorize/LoopVectorize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8790,12 +8790,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()) {
if (auto SR = getScaledReduction(Phi, RdxDesc.getLoopExitInstr(), Range))
PartialReductionChains.append(*SR);
}

// 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 @@ -8823,26 +8823,41 @@ void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
}
}

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

if (!CM.TheLoop->contains(RdxExitInstr))
return std::nullopt;

// 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()))
if (CM.blockNeedsPredicationForAnyReason(RdxExitInstr->getParent()))
return std::nullopt;

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

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);

SmallVector<std::pair<PartialReductionChain, unsigned>> Chains;

if (auto *OpInst = dyn_cast<Instruction>(Op)) {
if (auto SR0 = getScaledReduction(PHI, OpInst, Range)) {
Chains.append(*SR0);
PHI = SR0->rbegin()->first.Reduction;

Op = Update->getOperand(0);
PhiOp = Update->getOperand(1);
if (Op == PHI)
std::swap(Op, PhiOp);
}
}
if (PhiOp != PHI)
return std::nullopt;
Expand All @@ -8860,12 +8875,16 @@ VPRecipeBuilder::getScaledReduction(PHINode *PHI,
Instruction *ExtA = cast<Instruction>(BinOp->getOperand(0));
Instruction *ExtB = cast<Instruction>(BinOp->getOperand(1));

// Check that the extends extend from the same type.
if (A->getType() != B->getType())
return std::nullopt;

TTI::PartialReductionExtendKind OpAExtend =
TargetTransformInfo::getPartialReductionExtendKind(ExtA);
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 @@ -8880,9 +8899,9 @@ VPRecipeBuilder::getScaledReduction(PHINode *PHI,
return Cost.isValid();
},
Range))
return std::make_pair(Chain, TargetScaleFactor);
Chains.push_back(std::make_pair(Chain, TargetScaleFactor));

return std::nullopt;
return Chains;
}

VPRecipeBase *
Expand Down Expand Up @@ -8979,7 +8998,9 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,

VPValue *BinOp = Operands[0];
VPValue *Phi = Operands[1];
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be called Accumulator or something like that since that's now more accurate.

if (isa<VPReductionPHIRecipe>(BinOp->getDefiningRecipe()))
VPRecipeBase *BinOpRecipe = BinOp->getDefiningRecipe();
if (isa<VPReductionPHIRecipe>(BinOpRecipe) ||
isa<VPPartialReductionRecipe>(BinOpRecipe))
std::swap(BinOp, Phi);

return new VPPartialReductionRecipe(Reduction->getOpcode(), BinOp, Phi,
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ class VPRecipeBuilder {
/// 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,
std::optional<SmallVector<std::pair<PartialReductionChain, unsigned>>>
getScaledReduction(Instruction *PHI, Instruction *RdxExitInstr,
VFRange &Range);

public:
Expand Down
7 changes: 5 additions & 2 deletions llvm/lib/Transforms/Vectorize/VPlan.h
Original file line number Diff line number Diff line change
Expand Up @@ -2453,13 +2453,16 @@ class VPPartialReductionRecipe : public VPSingleDefRecipe {
: VPSingleDefRecipe(VPDef::VPPartialReductionSC,
ArrayRef<VPValue *>({Op0, Op1}), ReductionInst),
Opcode(Opcode) {
assert(isa<VPReductionPHIRecipe>(getOperand(1)->getDefiningRecipe()) &&
auto *DefiningRecipe = getOperand(1)->getDefiningRecipe();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps AccumulatorRecipe would be more descriptive.

assert((isa<VPReductionPHIRecipe>(DefiningRecipe) ||
isa<VPPartialReductionRecipe>(DefiningRecipe)) &&
"Unexpected operand order for partial reduction recipe");
}
~VPPartialReductionRecipe() override = default;

VPPartialReductionRecipe *clone() override {
return new VPPartialReductionRecipe(Opcode, getOperand(0), getOperand(1));
return new VPPartialReductionRecipe(Opcode, getOperand(0), getOperand(1),
getUnderlyingInstr());
}

VP_CLASSOF_IMPL(VPDef::VPPartialReductionSC)
Expand Down
15 changes: 12 additions & 3 deletions llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,22 @@ void VPPartialReductionRecipe::execute(VPTransformState &State) {
State.setDebugLocFrom(getDebugLoc());
auto &Builder = State.Builder;

assert(getOpcode() == Instruction::Add &&
"Unhandled partial reduction opcode");

Value *BinOpVal = State.get(getOperand(0));
Value *PhiVal = State.get(getOperand(1));
assert(PhiVal && BinOpVal && "Phi and Mul must be set");

auto Opcode = getOpcode();

// Currently we don't have a partial_reduce_sub intrinsic,
// so mimic the behaviour by negating the second operand
if (Opcode == Instruction::Sub) {
BinOpVal = Builder.CreateSub(Constant::getNullValue(BinOpVal->getType()),
BinOpVal);
Opcode = Instruction::Add;
}

assert(Opcode == Instruction::Add && "Unhandled partial reduction opcode");

Type *RetTy = PhiVal->getType();

CallInst *V = Builder.CreateIntrinsic(
Expand Down
Loading
Loading