Skip to content

Commit fa76965

Browse files
committed
[LV] NFC: Make VPPartialReductionRecipe a VPReductionRecipe
1 parent b02f2e8 commit fa76965

File tree

4 files changed

+69
-65
lines changed

4 files changed

+69
-65
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9122,17 +9122,18 @@ VPRecipeBuilder::tryToCreatePartialReduction(Instruction *Reduction,
91229122
ReductionOpcode = Instruction::Add;
91239123
}
91249124

9125+
VPValue *Cond = nullptr;
91259126
if (CM.blockNeedsPredicationForAnyReason(Reduction->getParent())) {
91269127
assert((ReductionOpcode == Instruction::Add ||
91279128
ReductionOpcode == Instruction::Sub) &&
91289129
"Expected an ADD or SUB operation for predicated partial "
91299130
"reductions (because the neutral element in the mask is zero)!");
9130-
VPValue *Mask = getBlockInMask(Reduction->getParent());
9131+
Cond = getBlockInMask(Reduction->getParent());
91319132
VPValue *Zero =
91329133
Plan.getOrAddLiveIn(ConstantInt::get(Reduction->getType(), 0));
9133-
BinOp = Builder.createSelect(Mask, BinOp, Zero, Reduction->getDebugLoc());
9134+
BinOp = Builder.createSelect(Cond, BinOp, Zero, Reduction->getDebugLoc());
91349135
}
9135-
return new VPPartialReductionRecipe(ReductionOpcode, BinOp, Accumulator,
9136+
return new VPPartialReductionRecipe(ReductionOpcode, Accumulator, BinOp, Cond,
91369137
ScaleFactor, Reduction);
91379138
}
91389139

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 59 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,62 +2133,6 @@ class VPReductionPHIRecipe : public VPHeaderPHIRecipe,
21332133
}
21342134
};
21352135

2136-
/// A recipe for forming partial reductions. In the loop, an accumulator and
2137-
/// vector operand are added together and passed to the next iteration as the
2138-
/// next accumulator. After the loop body, the accumulator is reduced to a
2139-
/// scalar value.
2140-
class VPPartialReductionRecipe : public VPSingleDefRecipe {
2141-
unsigned Opcode;
2142-
/// The divisor by which the VF of this recipe's output should be divided
2143-
/// during execution.
2144-
unsigned VFScaleFactor;
2145-
2146-
public:
2147-
VPPartialReductionRecipe(Instruction *ReductionInst, VPValue *Op0,
2148-
VPValue *Op1, unsigned VFScaleFactor)
2149-
: VPPartialReductionRecipe(ReductionInst->getOpcode(), Op0, Op1,
2150-
VFScaleFactor, ReductionInst) {}
2151-
VPPartialReductionRecipe(unsigned Opcode, VPValue *Op0, VPValue *Op1,
2152-
unsigned VFScaleFactor,
2153-
Instruction *ReductionInst = nullptr)
2154-
: VPSingleDefRecipe(VPDef::VPPartialReductionSC,
2155-
ArrayRef<VPValue *>({Op0, Op1}), ReductionInst),
2156-
Opcode(Opcode), VFScaleFactor(VFScaleFactor) {
2157-
[[maybe_unused]] auto *AccumulatorRecipe =
2158-
getOperand(1)->getDefiningRecipe();
2159-
assert((isa<VPReductionPHIRecipe>(AccumulatorRecipe) ||
2160-
isa<VPPartialReductionRecipe>(AccumulatorRecipe)) &&
2161-
"Unexpected operand order for partial reduction recipe");
2162-
}
2163-
~VPPartialReductionRecipe() override = default;
2164-
2165-
VPPartialReductionRecipe *clone() override {
2166-
return new VPPartialReductionRecipe(Opcode, getOperand(0), getOperand(1),
2167-
VFScaleFactor, getUnderlyingInstr());
2168-
}
2169-
2170-
VP_CLASSOF_IMPL(VPDef::VPPartialReductionSC)
2171-
2172-
/// Generate the reduction in the loop.
2173-
void execute(VPTransformState &State) override;
2174-
2175-
/// Return the cost of this VPPartialReductionRecipe.
2176-
InstructionCost computeCost(ElementCount VF,
2177-
VPCostContext &Ctx) const override;
2178-
2179-
/// Get the binary op's opcode.
2180-
unsigned getOpcode() const { return Opcode; }
2181-
2182-
/// Get the factor that the VF of this recipe's output should be scaled by.
2183-
unsigned getVFScaleFactor() const { return VFScaleFactor; }
2184-
2185-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2186-
/// Print the recipe.
2187-
void print(raw_ostream &O, const Twine &Indent,
2188-
VPSlotTracker &SlotTracker) const override;
2189-
#endif
2190-
};
2191-
21922136
/// A recipe for vectorizing a phi-node as a sequence of mask-based select
21932137
/// instructions.
21942138
class VPBlendRecipe : public VPSingleDefRecipe {
@@ -2431,6 +2375,65 @@ class VPReductionRecipe : public VPRecipeWithIRFlags {
24312375
}
24322376
};
24332377

2378+
/// A recipe for forming partial reductions. In the loop, an accumulator and
2379+
/// vector operand are added together and passed to the next iteration as the
2380+
/// next accumulator. After the loop body, the accumulator is reduced to a
2381+
/// scalar value.
2382+
class VPPartialReductionRecipe : public VPReductionRecipe {
2383+
unsigned Opcode;
2384+
2385+
/// The divisor by which the VF of this recipe's output should be divided
2386+
/// during execution.
2387+
unsigned VFScaleFactor;
2388+
2389+
public:
2390+
VPPartialReductionRecipe(Instruction *ReductionInst, VPValue *Op0,
2391+
VPValue *Op1, VPValue *Cond, unsigned VFScaleFactor)
2392+
: VPPartialReductionRecipe(ReductionInst->getOpcode(), Op0, Op1, Cond,
2393+
VFScaleFactor, ReductionInst) {}
2394+
VPPartialReductionRecipe(unsigned Opcode, VPValue *Op0, VPValue *Op1,
2395+
VPValue *Cond, unsigned ScaleFactor,
2396+
Instruction *ReductionInst = nullptr)
2397+
: VPReductionRecipe(VPDef::VPPartialReductionSC, RecurKind::Add,
2398+
FastMathFlags(), ReductionInst,
2399+
ArrayRef<VPValue *>({Op0, Op1}), Cond, false, {}),
2400+
Opcode(Opcode), VFScaleFactor(ScaleFactor) {
2401+
[[maybe_unused]] auto *AccumulatorRecipe =
2402+
getChainOp()->getDefiningRecipe();
2403+
assert((isa<VPReductionPHIRecipe>(AccumulatorRecipe) ||
2404+
isa<VPPartialReductionRecipe>(AccumulatorRecipe)) &&
2405+
"Unexpected operand order for partial reduction recipe");
2406+
}
2407+
~VPPartialReductionRecipe() override = default;
2408+
2409+
VPPartialReductionRecipe *clone() override {
2410+
return new VPPartialReductionRecipe(Opcode, getOperand(0), getOperand(1),
2411+
getCondOp(), VFScaleFactor,
2412+
getUnderlyingInstr());
2413+
}
2414+
2415+
VP_CLASSOF_IMPL(VPDef::VPPartialReductionSC)
2416+
2417+
/// Generate the reduction in the loop.
2418+
void execute(VPTransformState &State) override;
2419+
2420+
/// Return the cost of this VPPartialReductionRecipe.
2421+
InstructionCost computeCost(ElementCount VF,
2422+
VPCostContext &Ctx) const override;
2423+
2424+
/// Get the binary op's opcode.
2425+
unsigned getOpcode() const { return Opcode; }
2426+
2427+
/// Get the factor that the VF of this recipe's output should be scaled by.
2428+
unsigned getVFScaleFactor() const { return VFScaleFactor; }
2429+
2430+
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2431+
/// Print the recipe.
2432+
void print(raw_ostream &O, const Twine &Indent,
2433+
VPSlotTracker &SlotTracker) const override;
2434+
#endif
2435+
};
2436+
24342437
/// A recipe to represent inloop reduction operations with vector-predication
24352438
/// intrinsics, performing a reduction on a vector operand with the explicit
24362439
/// vector length (EVL) into a scalar value, and adding the result to a chain.

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,12 +287,12 @@ InstructionCost
287287
VPPartialReductionRecipe::computeCost(ElementCount VF,
288288
VPCostContext &Ctx) const {
289289
std::optional<unsigned> Opcode = std::nullopt;
290-
VPValue *BinOp = getOperand(0);
290+
VPValue *BinOp = getOperand(1);
291291

292292
// If the partial reduction is predicated, a select will be operand 0 rather
293293
// than the binary op
294294
using namespace llvm::VPlanPatternMatch;
295-
if (match(getOperand(0), m_Select(m_VPValue(), m_VPValue(), m_VPValue())))
295+
if (match(getOperand(1), m_Select(m_VPValue(), m_VPValue(), m_VPValue())))
296296
BinOp = BinOp->getDefiningRecipe()->getOperand(1);
297297

298298
// If BinOp is a negation, use the side effect of match to assign the actual
@@ -337,8 +337,8 @@ void VPPartialReductionRecipe::execute(VPTransformState &State) {
337337
assert(getOpcode() == Instruction::Add &&
338338
"Unhandled partial reduction opcode");
339339

340-
Value *BinOpVal = State.get(getOperand(0));
341-
Value *PhiVal = State.get(getOperand(1));
340+
Value *BinOpVal = State.get(getOperand(1));
341+
Value *PhiVal = State.get(getOperand(0));
342342
assert(PhiVal && BinOpVal && "Phi and Mul must be set");
343343

344344
Type *RetTy = PhiVal->getType();

llvm/test/Transforms/LoopVectorize/AArch64/vplan-printing.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ define i32 @print_partial_reduction(ptr %a, ptr %b) {
3333
; CHECK-NEXT: WIDEN ir<%load.b> = load vp<[[PTR_B]]>
3434
; CHECK-NEXT: WIDEN-CAST ir<%ext.b> = zext ir<%load.b> to i32
3535
; CHECK-NEXT: WIDEN ir<%mul> = mul ir<%ext.b>, ir<%ext.a>
36-
; CHECK-NEXT: PARTIAL-REDUCE ir<[[REDUCE]]> = add ir<%mul>, ir<[[ACC]]>
36+
; CHECK-NEXT: PARTIAL-REDUCE ir<[[REDUCE]]> = add ir<[[ACC]]>, ir<%mul>
3737
; CHECK-NEXT: EMIT vp<[[CAN_IV_NEXT]]> = add nuw vp<[[CAN_IV]]>, vp<[[VFxUF]]>
3838
; CHECK-NEXT: EMIT branch-on-count vp<[[CAN_IV_NEXT]]>, vp<[[VEC_TC]]>
3939
; CHECK-NEXT: No successors
@@ -98,7 +98,7 @@ define i32 @print_partial_reduction(ptr %a, ptr %b) {
9898
; CHECK-NEXT: WIDEN ir<%load.b> = load vp<[[PTR_B]]>
9999
; CHECK-NEXT: WIDEN-CAST ir<%ext.b> = zext ir<%load.b> to i32
100100
; CHECK-NEXT: WIDEN ir<%mul> = mul ir<%ext.b>, ir<%ext.a>
101-
; CHECK-NEXT: PARTIAL-REDUCE ir<%add> = add ir<%mul>, ir<%accum>
101+
; CHECK-NEXT: PARTIAL-REDUCE ir<%add> = add ir<%accum>, ir<%mul>
102102
; CHECK-NEXT: EMIT vp<[[EP_IV_NEXT:%.+]]> = add nuw vp<[[EP_IV]]>, ir<16>
103103
; CHECK-NEXT: EMIT branch-on-count vp<[[EP_IV_NEXT]]>, ir<1024>
104104
; CHECK-NEXT: No successors

0 commit comments

Comments
 (0)