Skip to content

Commit 460b92d

Browse files
committed
!fixup typo and implement cost of BranchOnCond
1 parent 050b588 commit 460b92d

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -836,20 +836,42 @@ bool VPInstruction::onlyFirstPartUsed(const VPValue *Op) const {
836836

837837
InstructionCost VPInstruction::computeCost(ElementCount VF,
838838
VPCostContext &Ctx) const {
839-
Type *ValTy = Ctx.Types.inferScalarType(getOperand(0));
840839

841840
switch (getOpcode()) {
842841
case VPInstruction::BranchOnCount: {
843-
// BranchOnCount will genearte icmp_eq + br instructions and the
844-
// cost of branch will be calculated in VPRegionBlock.
845-
// If the vector loop only executed once, ignore the cost of the cmp.
842+
Type *ValTy = Ctx.Types.inferScalarType(getOperand(0));
843+
844+
// If the vector loop only executed once, ignore the cost.
845+
// TODO: We can remove this after hoist `unrollByUF` and
846+
// `optimizeForVFandUF` which will should optimize BranchOnCount out.
846847
auto TC = dyn_cast_if_present<ConstantInt>(
847848
getParent()->getPlan()->getTripCount()->getUnderlyingValue());
848849
if (TC && VF.isFixed() && TC->getSExtValue() == VF.getFixedValue())
849850
return 0;
851+
852+
// BranchOnCount will generate icmp_eq + br instructions and the
853+
// cost of branch will be calculated in VPRegionBlock.
850854
return Ctx.TTI.getCmpSelInstrCost(Instruction::ICmp, ValTy, nullptr,
851855
CmpInst::ICMP_EQ, Ctx.CostKind);
852856
}
857+
case VPInstruction::BranchOnCond: {
858+
// BranchOnCond will generate `extractelement` when the condition is vector
859+
// type.
860+
VPValue *Op = getOperand(0);
861+
VPRecipeBase *R = Op->getDefiningRecipe();
862+
if (R &&
863+
any_of(R->operands(), [&](VPValue *V) { return !R->usesScalars(V); }) &&
864+
VF.isVector())
865+
return Ctx.TTI.getVectorInstrCost(
866+
Instruction::ExtractElement,
867+
cast<VectorType>(
868+
toVectorTy(Ctx.Types.inferScalarType(getOperand(0)), VF)),
869+
Ctx.CostKind, 0, nullptr, nullptr);
870+
871+
// Otherwise, BranchOnCond is free since the branch cost is already
872+
// calculated by VPBB.
873+
return 0;
874+
}
853875
default:
854876
// TODO: Compute accurate cost after retiring the legacy cost model.
855877
return 0;

0 commit comments

Comments
 (0)