Skip to content

Commit c840d0f

Browse files
committed
[LV] Add support for partial reductions without a binary op
Consider IR such as this: for.body: %iv = phi i64 [ 0, %entry ], [ %iv.next, %for.body ] %accum = phi i32 [ 0, %entry ], [ %add, %for.body ] %gep.a = getelementptr i8, ptr %a, i64 %iv %load.a = load i8, ptr %gep.a, align 1 %ext.a = zext i8 %load.a to i32 %add = add i32 %ext.a, %accum %iv.next = add i64 %iv, 1 %exitcond.not = icmp eq i64 %iv.next, 1025 br i1 %exitcond.not, label %for.exit, label %for.body Conceptually we can vectorise this using partial reductions too, although the current loop vectoriser implementation requires the accumulation of a multiply. For AArch64 this is easily done with a udot or sdot with an identity operand, i.e. a vector of (i16 1). In order to do this I had to teach getScaledReductions that the accumulated value may come from a unary op, hence there is only one extension to consider. Similarly, I updated the vplan and AArch64 TTI cost model to understand the possible unary op.
1 parent a7605f0 commit c840d0f

File tree

7 files changed

+204
-159
lines changed

7 files changed

+204
-159
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5024,11 +5024,21 @@ InstructionCost AArch64TTIImpl::getPartialReductionCost(
50245024

50255025
// Sub opcodes currently only occur in chained cases.
50265026
// Independent partial reduction subtractions are still costed as an add
5027-
if (Opcode != Instruction::Add && Opcode != Instruction::Sub)
5027+
if ((Opcode != Instruction::Add && Opcode != Instruction::Sub) ||
5028+
OpAExtend == TTI::PR_None)
50285029
return Invalid;
50295030

5030-
if (InputTypeA != InputTypeB)
5031+
// We only support multiply binary operations for now, and for muls we
5032+
// require the types being extended to be the same.
5033+
// NOTE: For muls AArch64 supports lowering mixed extensions to a usdot but
5034+
// only if the i8mm or sve/streaming features are available.
5035+
if (BinOp && (*BinOp != Instruction::Mul || InputTypeA != InputTypeB ||
5036+
OpBExtend == TTI::PR_None ||
5037+
(OpAExtend != OpBExtend && !ST->hasMatMulInt8() &&
5038+
!ST->isSVEorStreamingSVEAvailable())))
50315039
return Invalid;
5040+
assert((BinOp || (OpBExtend == TTI::PR_None && !InputTypeB)) &&
5041+
"Unexpected values for OpBExtend or InputTypeB");
50325042

50335043
EVT InputEVT = EVT::getEVT(InputTypeA);
50345044
EVT AccumEVT = EVT::getEVT(AccumType);
@@ -5046,6 +5056,7 @@ InstructionCost AArch64TTIImpl::getPartialReductionCost(
50465056
if (VFMinValue == Scale)
50475057
return Invalid;
50485058
}
5059+
50495060
if (VF.isFixed() &&
50505061
(!ST->isNeonAvailable() || !ST->hasDotProd() || AccumEVT == MVT::i64))
50515062
return Invalid;
@@ -5075,16 +5086,6 @@ InstructionCost AArch64TTIImpl::getPartialReductionCost(
50755086
} else
50765087
return Invalid;
50775088

5078-
// AArch64 supports lowering mixed extensions to a usdot but only if the
5079-
// i8mm or sve/streaming features are available.
5080-
if (OpAExtend == TTI::PR_None || OpBExtend == TTI::PR_None ||
5081-
(OpAExtend != OpBExtend && !ST->hasMatMulInt8() &&
5082-
!ST->isSVEorStreamingSVEAvailable()))
5083-
return Invalid;
5084-
5085-
if (!BinOp || *BinOp != Instruction::Mul)
5086-
return Invalid;
5087-
50885089
return Cost;
50895090
}
50905091

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 56 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8749,15 +8749,15 @@ void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
87498749
// something that isn't another partial reduction. This is because the
87508750
// extends are intended to be lowered along with the reduction itself.
87518751

8752-
// Build up a set of partial reduction bin ops for efficient use checking.
8753-
SmallSet<User *, 4> PartialReductionBinOps;
8752+
// Build up a set of partial reduction ops for efficient use checking.
8753+
SmallSet<User *, 4> PartialReductionOps;
87548754
for (const auto &[PartialRdx, _] : PartialReductionChains)
8755-
PartialReductionBinOps.insert(PartialRdx.BinOp);
8755+
PartialReductionOps.insert(PartialRdx.ExtendUser);
87568756

87578757
auto ExtendIsOnlyUsedByPartialReductions =
8758-
[&PartialReductionBinOps](Instruction *Extend) {
8758+
[&PartialReductionOps](Instruction *Extend) {
87598759
return all_of(Extend->users(), [&](const User *U) {
8760-
return PartialReductionBinOps.contains(U);
8760+
return PartialReductionOps.contains(U);
87618761
});
87628762
};
87638763

@@ -8766,15 +8766,14 @@ void VPRecipeBuilder::collectScaledReductions(VFRange &Range) {
87668766
for (auto Pair : PartialReductionChains) {
87678767
PartialReductionChain Chain = Pair.first;
87688768
if (ExtendIsOnlyUsedByPartialReductions(Chain.ExtendA) &&
8769-
ExtendIsOnlyUsedByPartialReductions(Chain.ExtendB))
8769+
(!Chain.ExtendB || ExtendIsOnlyUsedByPartialReductions(Chain.ExtendB)))
87708770
ScaledReductionMap.insert(std::make_pair(Chain.Reduction, Pair.second));
87718771
}
87728772
}
87738773

87748774
bool VPRecipeBuilder::getScaledReductions(
87758775
Instruction *PHI, Instruction *RdxExitInstr, VFRange &Range,
87768776
SmallVectorImpl<std::pair<PartialReductionChain, unsigned>> &Chains) {
8777-
87788777
if (!CM.TheLoop->contains(RdxExitInstr))
87798778
return false;
87808779

@@ -8803,40 +8802,69 @@ bool VPRecipeBuilder::getScaledReductions(
88038802
if (PhiOp != PHI)
88048803
return false;
88058804

8806-
auto *BinOp = dyn_cast<BinaryOperator>(Op);
8807-
if (!BinOp || !BinOp->hasOneUse())
8808-
return false;
8809-
88108805
using namespace llvm::PatternMatch;
8811-
// Use the side-effect of match to replace BinOp only if the pattern is
8812-
// matched, we don't care at this point whether it actually matched.
8813-
match(BinOp, m_Neg(m_BinOp(BinOp)));
88148806

8815-
Value *A, *B;
8816-
if (!match(BinOp->getOperand(0), m_ZExtOrSExt(m_Value(A))) ||
8817-
!match(BinOp->getOperand(1), m_ZExtOrSExt(m_Value(B))))
8818-
return false;
8807+
// If the update is a binary operator, check both of its operands to see if
8808+
// they are extends. Otherwise, see if the update comes directly from an
8809+
// extend.
8810+
Instruction *Exts[2] = {nullptr};
8811+
BinaryOperator *ExtendUser = dyn_cast<BinaryOperator>(Op);
8812+
std::optional<unsigned> BinOpc;
8813+
Type *ExtOpTypes[2] = {nullptr};
8814+
8815+
auto collectExtInfo = [&Exts, &ExtOpTypes](SmallVectorImpl<Value *> &Ops) -> bool {
8816+
unsigned I = 0;
8817+
for (Value *OpI : Ops) {
8818+
Value *ExtOp;
8819+
if (!match(OpI, m_ZExtOrSExt(m_Value(ExtOp))))
8820+
return false;
8821+
Exts[I] = cast<Instruction>(OpI);
8822+
ExtOpTypes[I] = ExtOp->getType();
8823+
I++;
8824+
}
8825+
return true;
8826+
};
8827+
8828+
if (ExtendUser) {
8829+
if (!ExtendUser->hasOneUse())
8830+
return false;
8831+
8832+
// Use the side-effect of match to replace BinOp only if the pattern is
8833+
// matched, we don't care at this point whether it actually matched.
8834+
match(ExtendUser, m_Neg(m_BinOp(ExtendUser)));
88198835

8820-
Instruction *ExtA = cast<Instruction>(BinOp->getOperand(0));
8821-
Instruction *ExtB = cast<Instruction>(BinOp->getOperand(1));
8836+
SmallVector<Value *> Ops(ExtendUser->operands());
8837+
if (!collectExtInfo(Ops))
8838+
return false;
8839+
8840+
BinOpc = std::make_optional(ExtendUser->getOpcode());
8841+
} else if (match(Update, m_Add(m_Value(), m_Value()))) {
8842+
// We already know the operands for Update are Op and PhiOp.
8843+
SmallVector<Value *> Ops({Op});
8844+
if (!collectExtInfo(Ops))
8845+
return false;
8846+
8847+
ExtendUser = Update;
8848+
BinOpc = std::nullopt;
8849+
} else
8850+
return false;
88228851

88238852
TTI::PartialReductionExtendKind OpAExtend =
8824-
TargetTransformInfo::getPartialReductionExtendKind(ExtA);
8853+
TargetTransformInfo::getPartialReductionExtendKind(Exts[0]);
88258854
TTI::PartialReductionExtendKind OpBExtend =
8826-
TargetTransformInfo::getPartialReductionExtendKind(ExtB);
8827-
8828-
PartialReductionChain Chain(RdxExitInstr, ExtA, ExtB, BinOp);
8855+
Exts[1] ? TargetTransformInfo::getPartialReductionExtendKind(Exts[1])
8856+
: TargetTransformInfo::PR_None;
8857+
PartialReductionChain Chain(RdxExitInstr, Exts[0], Exts[1], ExtendUser);
88298858

88308859
unsigned TargetScaleFactor =
88318860
PHI->getType()->getPrimitiveSizeInBits().getKnownScalarFactor(
8832-
A->getType()->getPrimitiveSizeInBits());
8861+
ExtOpTypes[0]->getPrimitiveSizeInBits());
88338862

88348863
if (LoopVectorizationPlanner::getDecisionAndClampRange(
88358864
[&](ElementCount VF) {
88368865
InstructionCost Cost = TTI->getPartialReductionCost(
8837-
Update->getOpcode(), A->getType(), B->getType(), PHI->getType(),
8838-
VF, OpAExtend, OpBExtend,
8839-
std::make_optional(BinOp->getOpcode()));
8866+
Update->getOpcode(), ExtOpTypes[0], ExtOpTypes[1],
8867+
PHI->getType(), VF, OpAExtend, OpBExtend, BinOpc);
88408868
return Cost.isValid();
88418869
},
88428870
Range)) {

llvm/lib/Transforms/Vectorize/VPRecipeBuilder.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,18 +30,18 @@ struct VFRange;
3030
/// accumulator).
3131
struct PartialReductionChain {
3232
PartialReductionChain(Instruction *Reduction, Instruction *ExtendA,
33-
Instruction *ExtendB, Instruction *BinOp)
34-
: Reduction(Reduction), ExtendA(ExtendA), ExtendB(ExtendB), BinOp(BinOp) {
35-
}
33+
Instruction *ExtendB, Instruction *ExtendUser)
34+
: Reduction(Reduction), ExtendA(ExtendA), ExtendB(ExtendB),
35+
ExtendUser(ExtendUser) {}
3636
/// The top-level binary operation that forms the reduction to a scalar
3737
/// after the loop body.
3838
Instruction *Reduction;
3939
/// The extension of each of the inner binary operation's operands.
4040
Instruction *ExtendA;
4141
Instruction *ExtendB;
4242

43-
/// The binary operation using the extends that is then reduced.
44-
Instruction *BinOp;
43+
/// The user of the extend that is then reduced.
44+
Instruction *ExtendUser;
4545
};
4646

4747
/// Helper class to create VPRecipies from IR instructions.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -281,31 +281,18 @@ bool VPRecipeBase::isPhi() const {
281281
InstructionCost
282282
VPPartialReductionRecipe::computeCost(ElementCount VF,
283283
VPCostContext &Ctx) const {
284-
std::optional<unsigned> Opcode = std::nullopt;
285-
VPValue *BinOp = getOperand(0);
284+
// If the input operand is an extend then use the opcode for this recipe.
285+
std::optional<unsigned> Opcode;
286+
VPValue *Op = getOperand(0);
287+
VPRecipeBase *OpR = Op->getDefiningRecipe();
286288

287289
// If the partial reduction is predicated, a select will be operand 0 rather
288290
// than the binary op
289291
using namespace llvm::VPlanPatternMatch;
290-
if (match(getOperand(0), m_Select(m_VPValue(), m_VPValue(), m_VPValue())))
291-
BinOp = BinOp->getDefiningRecipe()->getOperand(1);
292-
293-
// If BinOp is a negation, use the side effect of match to assign the actual
294-
// binary operation to BinOp
295-
match(BinOp, m_Binary<Instruction::Sub>(m_SpecificInt(0), m_VPValue(BinOp)));
296-
VPRecipeBase *BinOpR = BinOp->getDefiningRecipe();
297-
298-
if (auto *WidenR = dyn_cast<VPWidenRecipe>(BinOpR))
299-
Opcode = std::make_optional(WidenR->getOpcode());
300-
301-
VPRecipeBase *ExtAR = BinOpR->getOperand(0)->getDefiningRecipe();
302-
VPRecipeBase *ExtBR = BinOpR->getOperand(1)->getDefiningRecipe();
303-
304-
auto *PhiType = Ctx.Types.inferScalarType(getOperand(1));
305-
auto *InputTypeA = Ctx.Types.inferScalarType(ExtAR ? ExtAR->getOperand(0)
306-
: BinOpR->getOperand(0));
307-
auto *InputTypeB = Ctx.Types.inferScalarType(ExtBR ? ExtBR->getOperand(0)
308-
: BinOpR->getOperand(1));
292+
if (match(getOperand(0), m_Select(m_VPValue(), m_VPValue(), m_VPValue()))) {
293+
Op = OpR->getOperand(1);
294+
OpR = Op->getDefiningRecipe();
295+
}
309296

310297
auto GetExtendKind = [](VPRecipeBase *R) {
311298
// The extend could come from outside the plan.
@@ -321,9 +308,38 @@ VPPartialReductionRecipe::computeCost(ElementCount VF,
321308
return TargetTransformInfo::PR_None;
322309
};
323310

311+
Type *InputTypeA, *InputTypeB;
312+
TTI::PartialReductionExtendKind ExtAType, ExtBType;
313+
314+
// The input may come straight from a zext or sext.
315+
if (isa<VPWidenCastRecipe>(OpR)) {
316+
Opcode = std::nullopt;
317+
InputTypeA = Ctx.Types.inferScalarType(OpR->getOperand(0));
318+
InputTypeB = nullptr;
319+
ExtAType = GetExtendKind(OpR);
320+
ExtBType = TargetTransformInfo::PR_None;
321+
} else {
322+
// If BinOp is a negation, use the side effect of match to assign the actual
323+
// binary operation to BinOp
324+
match(Op, m_Binary<Instruction::Sub>(m_SpecificInt(0), m_VPValue(Op)));
325+
OpR = Op->getDefiningRecipe();
326+
Opcode = std::make_optional(cast<VPWidenRecipe>(OpR)->getOpcode());
327+
328+
VPRecipeBase *ExtAR = OpR->getOperand(0)->getDefiningRecipe();
329+
VPRecipeBase *ExtBR = OpR->getOperand(1)->getDefiningRecipe();
330+
331+
InputTypeA = Ctx.Types.inferScalarType(ExtAR ? ExtAR->getOperand(0)
332+
: OpR->getOperand(0));
333+
InputTypeB = Ctx.Types.inferScalarType(ExtBR ? ExtBR->getOperand(0)
334+
: OpR->getOperand(1));
335+
ExtAType = GetExtendKind(ExtAR);
336+
ExtBType = GetExtendKind(ExtBR);
337+
}
338+
339+
auto *PhiType = Ctx.Types.inferScalarType(getOperand(1));
324340
return Ctx.TTI.getPartialReductionCost(getOpcode(), InputTypeA, InputTypeB,
325-
PhiType, VF, GetExtendKind(ExtAR),
326-
GetExtendKind(ExtBR), Opcode);
341+
PhiType, VF, ExtAType, ExtBType,
342+
Opcode);
327343
}
328344

329345
void VPPartialReductionRecipe::execute(VPTransformState &State) {

0 commit comments

Comments
 (0)