Skip to content

Commit 680901e

Browse files
committed
[VPlan] Implement VPHeaderPHIRecipe::computeCost.
Fill out computeCost implementations for various header PHI recipes, matching the legacy cost model for now.
1 parent 83ae171 commit 680901e

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,6 +2050,10 @@ class VPHeaderPHIRecipe : public VPSingleDefRecipe {
20502050
/// Generate the phi nodes.
20512051
void execute(VPTransformState &State) override = 0;
20522052

2053+
/// Return the cost of this header phi recipe.
2054+
InstructionCost computeCost(ElementCount VF,
2055+
VPCostContext &Ctx) const override;
2056+
20532057
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
20542058
/// Print the recipe.
20552059
void print(raw_ostream &O, const Twine &Indent,
@@ -2295,6 +2299,10 @@ struct VPFirstOrderRecurrencePHIRecipe : public VPHeaderPHIRecipe {
22952299

22962300
void execute(VPTransformState &State) override;
22972301

2302+
/// Return the cost of this first-order recurrence phi recipe.
2303+
InstructionCost computeCost(ElementCount VF,
2304+
VPCostContext &Ctx) const override;
2305+
22982306
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
22992307
/// Print the recipe.
23002308
void print(raw_ostream &O, const Twine &Indent,
@@ -3134,6 +3142,13 @@ class VPCanonicalIVPHIRecipe : public VPHeaderPHIRecipe {
31343142
/// canonical, i.e. has the same start and step (of 1) as the canonical IV.
31353143
bool isCanonical(InductionDescriptor::InductionKind Kind, VPValue *Start,
31363144
VPValue *Step) const;
3145+
3146+
/// Return the cost of this VPCanonicalIVPHIRecipe.
3147+
InstructionCost computeCost(ElementCount VF,
3148+
VPCostContext &Ctx) const override {
3149+
// For now, match the behavior of the legacy cost model.
3150+
return 0;
3151+
}
31373152
};
31383153

31393154
/// A recipe for generating the active lane mask for the vector loop that is
@@ -3196,6 +3211,13 @@ class VPEVLBasedIVPHIRecipe : public VPHeaderPHIRecipe {
31963211
/// TODO: investigate if it can share the code with VPCanonicalIVPHIRecipe.
31973212
void execute(VPTransformState &State) override;
31983213

3214+
/// Return the cost of this VPEVLBasedIVPHIRecipe.
3215+
InstructionCost computeCost(ElementCount VF,
3216+
VPCostContext &Ctx) const override {
3217+
// For now, match the behavior of the legacy cost model.
3218+
return 0;
3219+
}
3220+
31993221
/// Returns true if the recipe only uses the first lane of operand \p Op.
32003222
bool onlyFirstLaneUsed(const VPValue *Op) const override {
32013223
assert(is_contained(operands(), Op) &&

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,11 @@ void VPWidenCastRecipe::print(raw_ostream &O, const Twine &Indent,
15891589
}
15901590
#endif
15911591

1592+
InstructionCost VPHeaderPHIRecipe::computeCost(ElementCount VF,
1593+
VPCostContext &Ctx) const {
1594+
return Ctx.TTI.getCFInstrCost(Instruction::PHI, TTI::TCK_RecipThroughput);
1595+
}
1596+
15921597
/// This function adds
15931598
/// (StartIdx * Step, (StartIdx + 1) * Step, (StartIdx + 2) * Step, ...)
15941599
/// to each vector element of Val. The sequence starts at StartIndex.
@@ -3334,6 +3339,23 @@ void VPFirstOrderRecurrencePHIRecipe::execute(VPTransformState &State) {
33343339
State.set(this, Phi);
33353340
}
33363341

3342+
InstructionCost
3343+
VPFirstOrderRecurrencePHIRecipe::computeCost(ElementCount VF,
3344+
VPCostContext &Ctx) const {
3345+
if (VF.isScalable() && VF.getKnownMinValue() == 1)
3346+
return InstructionCost::getInvalid();
3347+
3348+
SmallVector<int> Mask(VF.getKnownMinValue());
3349+
std::iota(Mask.begin(), Mask.end(), VF.getKnownMinValue() - 1);
3350+
Type *VectorTy =
3351+
ToVectorTy(Ctx.Types.inferScalarType(this->getVPSingleValue()), VF);
3352+
3353+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
3354+
return Ctx.TTI.getShuffleCost(TargetTransformInfo::SK_Splice,
3355+
cast<VectorType>(VectorTy), Mask, CostKind,
3356+
VF.getKnownMinValue() - 1);
3357+
}
3358+
33373359
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
33383360
void VPFirstOrderRecurrencePHIRecipe::print(raw_ostream &O, const Twine &Indent,
33393361
VPSlotTracker &SlotTracker) const {

0 commit comments

Comments
 (0)