[llvm] [VPlan] First step towards VPlan cost modeling (LegacyCM in CostCtx) (PR #92555)
via llvm-commits
llvm-commits at lists.llvm.org
Sun May 26 10:01:42 PDT 2024
================
@@ -730,6 +731,89 @@ void VPRegionBlock::execute(VPTransformState *State) {
State->Instance.reset();
}
+static InstructionCost computeCostForRecipe(VPRecipeBase *R, ElementCount VF,
+ VPCostContext &Ctx) {
+ if (auto *S = dyn_cast<VPSingleDefRecipe>(R)) {
+ auto *UI = dyn_cast_or_null<Instruction>(S->getUnderlyingValue());
+ if (UI && Ctx.skipCostComputation(UI))
+ return 0;
+ }
+
+ InstructionCost RecipeCost = R->computeCost(VF, Ctx);
+ if (ForceTargetInstructionCost.getNumOccurrences() > 0 &&
+ RecipeCost.isValid())
+ RecipeCost = InstructionCost(ForceTargetInstructionCost);
+
+ LLVM_DEBUG({
+ dbgs() << "Cost of " << RecipeCost << " for VF " << VF << ": ";
+ R->dump();
+ });
+ return RecipeCost;
+}
+
+InstructionCost VPBasicBlock::computeCost(ElementCount VF, VPCostContext &Ctx) {
+ InstructionCost Cost = 0;
+ for (VPRecipeBase &R : *this)
+ Cost += computeCostForRecipe(&R, VF, Ctx);
+ return Cost;
+}
+
+InstructionCost VPRegionBlock::computeCost(ElementCount VF,
+ VPCostContext &Ctx) {
+ InstructionCost Cost = 0;
+ if (!isReplicator()) {
+ for (VPBlockBase *Block : vp_depth_first_shallow(getEntry()))
+ Cost += Block->computeCost(VF, Ctx);
+ return Cost;
+ }
+
+ // Compute the cost of a replicate region. Replicating isn't supported for
+ // scalable vectors, return an invalid cost for them.
+ if (VF.isScalable())
+ return InstructionCost::getInvalid();
+
+ // First compute the cost of the conditionally executed recipes, followed by
+ // account for the branching cost, except if the mask is a header mask or
+ // uniform condition.
+ using namespace llvm::VPlanPatternMatch;
+ VPBasicBlock *Then = cast<VPBasicBlock>(getEntry()->getSuccessors()[0]);
+ for (VPRecipeBase &R : *Then)
+ Cost += computeCostForRecipe(&R, VF, Ctx);
+
+ // Note the cost estimates below closely match the current legacy cost model.
+ auto *BOM = cast<VPBranchOnMaskRecipe>(&getEntryBasicBlock()->front());
+ VPValue *Cond = BOM->getOperand(0);
+
+ // Check if Cond is a uniform compare or a header mask and don't account for
+ // branching costs. A uniform condition correspondings to a single branch per
+ // VF, and the header mask will always be true except in the last iteration.
+ VPValue *Op;
+ bool IsHeaderMaskOrUniformCond =
+ vputils::isUniformBoolean(Cond) || isa<VPActiveLaneMaskPHIRecipe>(Cond) ||
+ match(Cond, m_ActiveLaneMask(m_VPValue(), m_VPValue())) ||
+ (match(Cond, m_Binary<Instruction::ICmp>(m_VPValue(), m_VPValue(Op))) &&
+ Op == getPlan()->getOrCreateBackedgeTakenCount());
+ if (IsHeaderMaskOrUniformCond)
+ return Cost;
+
+ // For the scalar case, we may not always execute the original predicated
+ // block, Thus, scale the block's cost by the probability of executing it.
+ // blockNeedsPredication from Legal is used so as to not include all blocks in
----------------
ayalz wrote:
blockNeedsPredication is no longer used here, which only checks if VF is scalar.
https://github.com/llvm/llvm-project/pull/92555
More information about the llvm-commits
mailing list