Skip to content

Commit feff37e

Browse files
committed
!fixup typo and implement cost of BranchOnCond
1 parent 8d0c466 commit feff37e

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -744,17 +744,39 @@ InstructionCost VPInstruction::computeCost(ElementCount VF,
744744
Instruction::Or, cast<VectorType>(VecTy), std::nullopt, Ctx.CostKind);
745745
}
746746
case VPInstruction::BranchOnCount: {
747-
// BranchOnCount will genearte icmp_eq + br instructions and the
748-
// cost of branch will be calculated in VPRegionBlock.
749-
// If the vector loop only executed once, ignore the cost of the cmp.
750747
Type *ValTy = Ctx.Types.inferScalarType(getOperand(0));
748+
749+
// If the vector loop only executed once, ignore the cost.
750+
// TODO: We can remove this after hoist `unrollByUF` and
751+
// `optimizeForVFandUF` which will should optimize BranchOnCount out.
751752
auto TC = dyn_cast_if_present<ConstantInt>(
752753
getParent()->getPlan()->getTripCount()->getUnderlyingValue());
753754
if (TC && VF.isFixed() && TC->getSExtValue() == VF.getFixedValue())
754755
return 0;
756+
757+
// BranchOnCount will generate icmp_eq + br instructions and the
758+
// cost of branch will be calculated in VPRegionBlock.
755759
return Ctx.TTI.getCmpSelInstrCost(Instruction::ICmp, ValTy, nullptr,
756760
CmpInst::ICMP_EQ, Ctx.CostKind);
757761
}
762+
case VPInstruction::BranchOnCond: {
763+
// BranchOnCond will generate `extractelement` when the condition is vector
764+
// type.
765+
VPValue *Op = getOperand(0);
766+
VPRecipeBase *R = Op->getDefiningRecipe();
767+
if (R &&
768+
any_of(R->operands(), [&](VPValue *V) { return !R->usesScalars(V); }) &&
769+
VF.isVector())
770+
return Ctx.TTI.getVectorInstrCost(
771+
Instruction::ExtractElement,
772+
cast<VectorType>(
773+
toVectorTy(Ctx.Types.inferScalarType(getOperand(0)), VF)),
774+
Ctx.CostKind, 0, nullptr, nullptr);
775+
776+
// Otherwise, BranchOnCond is free since the branch cost is already
777+
// calculated by VPBB.
778+
return 0;
779+
}
758780
default:
759781
// TODO: Compute cost other VPInstructions once the legacy cost model has
760782
// been retired.

0 commit comments

Comments
 (0)