Skip to content

Commit 3fc1eee

Browse files
committed
[VPlan] Implement VPWidenCallRecipe::computeCost (NFCI).
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.
1 parent 533e6bb commit 3fc1eee

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -924,6 +924,46 @@ void VPWidenCallRecipe::execute(VPTransformState &State) {
924924
}
925925
}
926926

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

0 commit comments

Comments
 (0)