Skip to content

Commit 5527585

Browse files
committed
[VPlan] Compute cost for most opcodes in VPWidenRecipe (NFCI).
1 parent 4120907 commit 5527585

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7279,7 +7279,8 @@ InstructionCost LoopVectorizationPlanner::cost(VPlan &Plan,
72797279
ElementCount VF) const {
72807280
InstructionCost Cost = 0;
72817281
LLVMContext &LLVMCtx = OrigLoop->getHeader()->getContext();
7282-
VPCostContext CostCtx(CM.TTI, Legal->getWidestInductionType(), LLVMCtx, CM);
7282+
VPCostContext CostCtx(CM.TTI, *CM.TLI, Legal->getWidestInductionType(),
7283+
LLVMCtx, CM);
72837284

72847285
// Cost modeling for inductions is inaccurate in the legacy cost model
72857286
// compared to the recipes that are generated. To match here initially during

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -736,14 +736,16 @@ class VPLiveOut : public VPUser {
736736
/// Struct to hold various analysis needed for cost computations.
737737
struct VPCostContext {
738738
const TargetTransformInfo &TTI;
739+
const TargetLibraryInfo &TLI;
739740
VPTypeAnalysis Types;
740741
LLVMContext &LLVMCtx;
741742
LoopVectorizationCostModel &CM;
742743
SmallPtrSet<Instruction *, 8> SkipCostComputation;
743744

744-
VPCostContext(const TargetTransformInfo &TTI, Type *CanIVTy,
745-
LLVMContext &LLVMCtx, LoopVectorizationCostModel &CM)
746-
: TTI(TTI), Types(CanIVTy, LLVMCtx), LLVMCtx(LLVMCtx), CM(CM) {}
745+
VPCostContext(const TargetTransformInfo &TTI, const TargetLibraryInfo &TLI,
746+
Type *CanIVTy, LLVMContext &LLVMCtx,
747+
LoopVectorizationCostModel &CM)
748+
: TTI(TTI), TLI(TLI), Types(CanIVTy, LLVMCtx), LLVMCtx(LLVMCtx), CM(CM) {}
747749

748750
/// Return the cost for \p UI with \p VF using the legacy cost model as
749751
/// fallback until computing the cost of all recipes migrates to VPlan.
@@ -862,7 +864,8 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock>,
862864
protected:
863865
/// Compute the cost of this recipe using the legacy cost model and the
864866
/// underlying instructions.
865-
InstructionCost computeCost(ElementCount VF, VPCostContext &Ctx) const;
867+
virtual InstructionCost computeCost(ElementCount VF,
868+
VPCostContext &Ctx) const;
866869
};
867870

868871
// Helper macro to define common classof implementations for recipes.
@@ -1423,6 +1426,10 @@ class VPWidenRecipe : public VPRecipeWithIRFlags {
14231426
/// Produce widened copies of all Ingredients.
14241427
void execute(VPTransformState &State) override;
14251428

1429+
/// Return the cost of this VPWidenRecipe.
1430+
InstructionCost computeCost(ElementCount VF,
1431+
VPCostContext &Ctx) const override;
1432+
14261433
unsigned getOpcode() const { return Opcode; }
14271434

14281435
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1137,6 +1137,80 @@ void VPWidenRecipe::execute(VPTransformState &State) {
11371137
#endif
11381138
}
11391139

1140+
InstructionCost VPWidenRecipe::computeCost(ElementCount VF,
1141+
VPCostContext &Ctx) const {
1142+
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
1143+
switch (Opcode) {
1144+
case Instruction::FNeg: {
1145+
Type *VectorTy =
1146+
ToVectorTy(Ctx.Types.inferScalarType(this->getVPSingleValue()), VF);
1147+
return Ctx.TTI.getArithmeticInstrCost(
1148+
Opcode, VectorTy, CostKind,
1149+
{TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
1150+
{TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None});
1151+
}
1152+
1153+
case Instruction::UDiv:
1154+
case Instruction::SDiv:
1155+
case Instruction::SRem:
1156+
case Instruction::URem:
1157+
// More complex computation, let the legacy cost-model handle this for now.
1158+
return Ctx.getLegacyCost(cast<Instruction>(getUnderlyingValue()), VF);
1159+
case Instruction::Add:
1160+
case Instruction::FAdd:
1161+
case Instruction::Sub:
1162+
case Instruction::FSub:
1163+
case Instruction::Mul:
1164+
case Instruction::FMul:
1165+
case Instruction::FDiv:
1166+
case Instruction::FRem:
1167+
case Instruction::Shl:
1168+
case Instruction::LShr:
1169+
case Instruction::AShr:
1170+
case Instruction::And:
1171+
case Instruction::Or:
1172+
case Instruction::Xor: {
1173+
VPValue *Op2 = getOperand(1);
1174+
// Certain instructions can be cheaper to vectorize if they have a constant
1175+
// second vector operand. One example of this are shifts on x86.
1176+
TargetTransformInfo::OperandValueInfo Op2Info = {
1177+
TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None};
1178+
if (Op2->isLiveIn())
1179+
Op2Info = Ctx.TTI.getOperandInfo(Op2->getLiveInIRValue());
1180+
1181+
if (Op2Info.Kind == TargetTransformInfo::OK_AnyValue &&
1182+
getOperand(1)->isDefinedOutsideVectorRegions())
1183+
Op2Info.Kind = TargetTransformInfo::OK_UniformValue;
1184+
Type *VectorTy =
1185+
ToVectorTy(Ctx.Types.inferScalarType(this->getVPSingleValue()), VF);
1186+
Instruction *CtxI = dyn_cast_or_null<Instruction>(getUnderlyingValue());
1187+
1188+
SmallVector<const Value *, 4> Operands;
1189+
if (CtxI)
1190+
Operands.append(CtxI->value_op_begin(), CtxI->value_op_end());
1191+
return Ctx.TTI.getArithmeticInstrCost(
1192+
Opcode, VectorTy, CostKind,
1193+
{TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
1194+
Op2Info, Operands, CtxI, &Ctx.TLI);
1195+
}
1196+
case Instruction::Freeze: {
1197+
// This opcode is unknown. Assume that it is the same as 'mul'.
1198+
Type *VectorTy =
1199+
ToVectorTy(Ctx.Types.inferScalarType(this->getVPSingleValue()), VF);
1200+
return Ctx.TTI.getArithmeticInstrCost(Instruction::Mul, VectorTy, CostKind);
1201+
}
1202+
case Instruction::ICmp:
1203+
case Instruction::FCmp: {
1204+
Instruction *CtxI = dyn_cast_or_null<Instruction>(getUnderlyingValue());
1205+
Type *VectorTy = ToVectorTy(Ctx.Types.inferScalarType(getOperand(0)), VF);
1206+
return Ctx.TTI.getCmpSelInstrCost(Opcode, VectorTy, nullptr, getPredicate(),
1207+
CostKind, CtxI);
1208+
}
1209+
default:
1210+
llvm_unreachable("Unsupported opcode for instruction");
1211+
}
1212+
}
1213+
11401214
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
11411215
void VPWidenRecipe::print(raw_ostream &O, const Twine &Indent,
11421216
VPSlotTracker &SlotTracker) const {

0 commit comments

Comments
 (0)