Skip to content

Commit c7f986c

Browse files
Address comments. Add RUN line to test files. Change a comment.
Remove Mask as operand in Partial Reduction Recipe. Instead just mask the input when creating the recipe.
1 parent 78fe997 commit c7f986c

File tree

5 files changed

+2107
-23
lines changed

5 files changed

+2107
-23
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8919,8 +8919,13 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
89198919
std::swap(BinOp, Accumulator);
89208920

89218921
VPValue *Mask = getBlockInMask(Reduction->getParent());
8922-
8923-
return new VPPartialReductionRecipe(Reduction->getOpcode(), BinOp, Accumulator, Mask, Reduction);
8922+
if (Mask) {
8923+
VPValue *Zero =
8924+
Plan.getOrAddLiveIn(ConstantInt::get(Reduction->getType(), 0));
8925+
BinOp = Builder.createSelect(Mask, BinOp, Zero, Reduction->getDebugLoc());
8926+
}
8927+
return new VPPartialReductionRecipe(Reduction->getOpcode(), BinOp,
8928+
Accumulator, Reduction);
89248929
}
89258930

89268931
void LoopVectorizationPlanner::buildVPlansWithVPRecipes(ElementCount MinVF,
@@ -9728,11 +9733,9 @@ void LoopVectorizationPlanner::adjustRecipesForReductions(
97289733
// beginning of the dedicated latch block.
97299734
auto *OrigExitingVPV = PhiR->getBackedgeValue();
97309735
auto *NewExitingVPV = PhiR->getBackedgeValue();
9731-
// Don't add selects here for partial reductions because the phi and partial
9732-
// reduction values have less vector elements than Cond. But, each operand
9733-
// in a select instruction needs to have the same number of vector elements,
9734-
// so the compiler would crash. Instead, a select, with the active lane
9735-
// mask, is applied to the inputs to the partial reduction.
9736+
// Don't output selects for partial reductions because they have an output
9737+
// with fewer lanes than the VF. So the operands of the select would have
9738+
// different numbers of lanes. Partial reductions mask the input instead.
97369739
if (!PhiR->isInLoop() && CM.foldTailByMasking() &&
97379740
!isa<VPPartialReductionRecipe>(OrigExitingVPV->getDefiningRecipe())) {
97389741
VPValue *Cond = RecipeBuilder.getBlockInMask(OrigLoop->getHeader());

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2111,11 +2111,10 @@ class VPPartialReductionRecipe : public VPSingleDefRecipe {
21112111

21122112
public:
21132113
VPPartialReductionRecipe(Instruction *ReductionInst, VPValue *Op0,
2114-
VPValue *Op1, VPValue *Mask = nullptr)
2115-
: VPPartialReductionRecipe(ReductionInst->getOpcode(), Op0, Op1, Mask,
2114+
VPValue *Op1)
2115+
: VPPartialReductionRecipe(ReductionInst->getOpcode(), Op0, Op1,
21162116
ReductionInst) {}
21172117
VPPartialReductionRecipe(unsigned Opcode, VPValue *Op0, VPValue *Op1,
2118-
VPValue *Mask = nullptr,
21192118
Instruction *ReductionInst = nullptr)
21202119
: VPSingleDefRecipe(VPDef::VPPartialReductionSC,
21212120
ArrayRef<VPValue *>({Op0, Op1}), ReductionInst),
@@ -2125,18 +2124,12 @@ class VPPartialReductionRecipe : public VPSingleDefRecipe {
21252124
assert((isa<VPReductionPHIRecipe>(AccumulatorRecipe) ||
21262125
isa<VPPartialReductionRecipe>(AccumulatorRecipe)) &&
21272126
"Unexpected operand order for partial reduction recipe");
2128-
if (Mask)
2129-
addOperand(Mask);
21302127
}
21312128
~VPPartialReductionRecipe() override = default;
21322129

21332130
VPPartialReductionRecipe *clone() override {
21342131
return new VPPartialReductionRecipe(Opcode, getOperand(0), getOperand(1),
2135-
getMask(), getUnderlyingInstr());
2136-
}
2137-
2138-
VPValue *getMask() const {
2139-
return getNumOperands() == 3 ? getOperand(2) : nullptr;
2132+
getUnderlyingInstr());
21402133
}
21412134

21422135
VP_CLASSOF_IMPL(VPDef::VPPartialReductionSC)

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,13 @@ VPPartialReductionRecipe::computeCost(ElementCount VF,
285285
VPCostContext &Ctx) const {
286286
std::optional<unsigned> Opcode = std::nullopt;
287287
VPRecipeBase *BinOpR = getOperand(0)->getDefiningRecipe();
288+
289+
// If the partial reduction is predicated, a select will be operand 0 rather
290+
// than the binary op
291+
using namespace llvm::VPlanPatternMatch;
292+
if (match(getOperand(0), m_Select(m_VPValue(), m_VPValue(), m_VPValue())))
293+
BinOpR = BinOpR->getOperand(1)->getDefiningRecipe();
294+
288295
if (auto *WidenR = dyn_cast<VPWidenRecipe>(BinOpR))
289296
Opcode = std::make_optional(WidenR->getOpcode());
290297

@@ -329,12 +336,6 @@ void VPPartialReductionRecipe::execute(VPTransformState &State) {
329336

330337
Type *RetTy = PhiVal->getType();
331338

332-
VPValue *Mask = getMask();
333-
if (Mask) {
334-
Value *MaskVal = State.get(Mask);
335-
Value *Zero = ConstantInt::get(BinOpVal->getType(), 0);
336-
BinOpVal = Builder.CreateSelect(MaskVal, BinOpVal, Zero);
337-
}
338339
CallInst *V = Builder.CreateIntrinsic(
339340
RetTy, Intrinsic::experimental_vector_partial_reduce_add,
340341
{PhiVal, BinOpVal}, nullptr, "partial.reduce");

0 commit comments

Comments
 (0)