Skip to content

Commit 3d797a6

Browse files
fhahnronlieb
authored andcommitted
[VPlan] Implement VPWidenCallRecipe::computeCost (NFCI). (llvm#106047)
Implement cost computation for VPWidenCallRecipe. In some cases, targets use argument info to compute intrinsic costs. If all operands of the call are VPValues with an underlying IR value, use the IR values as arguments. PR: llvm#106731 Change-Id: I9eea7ea07341d8662d5cb0798dcb7151f0fb04e2
1 parent 2873cfd commit 3d797a6

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1565,6 +1565,10 @@ class VPWidenCallRecipe : public VPSingleDefRecipe {
15651565
/// Produce a widened version of the call instruction.
15661566
void execute(VPTransformState &State) override;
15671567

1568+
/// Return the cost of this VPWidenCallRecipe.
1569+
InstructionCost computeCost(ElementCount VF,
1570+
VPCostContext &Ctx) const override;
1571+
15681572
Function *getCalledScalarFunction() const {
15691573
return cast<Function>(getOperand(getNumOperands() - 1)->getLiveInIRValue());
15701574
}

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -925,6 +925,45 @@ void VPWidenCallRecipe::execute(VPTransformState &State) {
925925
}
926926
}
927927

928+
InstructionCost VPWidenCallRecipe::computeCost(ElementCount VF,
929+
VPCostContext &Ctx) const {
930+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
931+
if (Variant) {
932+
return Ctx.TTI.getCallInstrCost(nullptr, Variant->getReturnType(),
933+
Variant->getFunctionType()->params(),
934+
CostKind);
935+
}
936+
937+
FastMathFlags FMF;
938+
// TODO: Manage flags via VPRecipeWithIRFlags.
939+
if (auto *FPMO = dyn_cast_or_null<FPMathOperator>(getUnderlyingValue()))
940+
FMF = FPMO->getFastMathFlags();
941+
942+
// Some backends analyze intrinsic arguments to determine cost. If all
943+
// operands are VPValues with an underlying IR value, use the original IR
944+
// values for cost computations.
945+
SmallVector<const Value *> Arguments;
946+
for (VPValue *Op : operands()) {
947+
auto *V = Op->getUnderlyingValue();
948+
if (!V) {
949+
Arguments.clear();
950+
break;
951+
}
952+
Arguments.push_back(V);
953+
}
954+
955+
Type *RetTy =
956+
ToVectorTy(Ctx.Types.inferScalarType(this->getVPSingleValue()), VF);
957+
SmallVector<Type *> ParamTys;
958+
for (unsigned I = 0; I != getNumOperands(); ++I)
959+
ParamTys.push_back(
960+
ToVectorTy(Ctx.Types.inferScalarType(getOperand(I)), VF));
961+
962+
IntrinsicCostAttributes CostAttrs(VectorIntrinsicID, RetTy, Arguments,
963+
ParamTys, FMF);
964+
return Ctx.TTI.getIntrinsicInstrCost(CostAttrs, CostKind);
965+
}
966+
928967
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
929968
void VPWidenCallRecipe::print(raw_ostream &O, const Twine &Indent,
930969
VPSlotTracker &SlotTracker) const {

0 commit comments

Comments
 (0)