[llvm] 30e9cba - [VPlan] Move logic to compute scalarization overhead to cost helper(NFC)
Florian Hahn via llvm-commits
llvm-commits at lists.llvm.org
Sat Sep 13 12:43:58 PDT 2025
Author: Florian Hahn
Date: 2025-09-13T20:41:44+01:00
New Revision: 30e9cbacab5b474de89992851f126fff300c1ab7
URL: https://github.com/llvm/llvm-project/commit/30e9cbacab5b474de89992851f126fff300c1ab7
DIFF: https://github.com/llvm/llvm-project/commit/30e9cbacab5b474de89992851f126fff300c1ab7.diff
LOG: [VPlan] Move logic to compute scalarization overhead to cost helper(NFC)
Extract the logic to compute the scalarization overhead to a helper for
easy re-use in the future.
Added:
Modified:
llvm/lib/Transforms/Vectorize/VPlan.cpp
llvm/lib/Transforms/Vectorize/VPlanHelpers.h
llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/Vectorize/VPlan.cpp b/llvm/lib/Transforms/Vectorize/VPlan.cpp
index e3244623ee968..30a3a01ddd949 100644
--- a/llvm/lib/Transforms/Vectorize/VPlan.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlan.cpp
@@ -1747,3 +1747,33 @@ VPCostContext::getOperandInfo(VPValue *V) const {
return TTI::getOperandInfo(V->getLiveInIRValue());
}
+
+InstructionCost VPCostContext::getScalarizationOverhead(
+ Type *ResultTy, ArrayRef<const VPValue *> Operands, ElementCount VF) {
+ if (VF.isScalar())
+ return 0;
+
+ InstructionCost ScalarizationCost = 0;
+ // Compute the cost of scalarizing the result if needed.
+ if (!ResultTy->isVoidTy()) {
+ for (Type *VectorTy :
+ to_vector(getContainedTypes(toVectorizedTy(ResultTy, VF)))) {
+ ScalarizationCost += TTI.getScalarizationOverhead(
+ cast<VectorType>(VectorTy), APInt::getAllOnes(VF.getFixedValue()),
+ /*Insert=*/true,
+ /*Extract=*/false, CostKind);
+ }
+ }
+ // Compute the cost of scalarizing the operands, skipping ones that do not
+ // require extraction/scalarization and do not incur any overhead.
+ SmallPtrSet<const VPValue *, 4> UniqueOperands;
+ SmallVector<Type *> Tys;
+ for (auto *Op : Operands) {
+ if (Op->isLiveIn() || isa<VPReplicateRecipe, VPPredInstPHIRecipe>(Op) ||
+ !UniqueOperands.insert(Op).second)
+ continue;
+ Tys.push_back(toVectorizedTy(Types.inferScalarType(Op), VF));
+ }
+ return ScalarizationCost +
+ TTI.getOperandsScalarizationOverhead(Tys, CostKind);
+}
diff --git a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
index 5ad2ac6b61e05..fe59774b7c838 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
+++ b/llvm/lib/Transforms/Vectorize/VPlanHelpers.h
@@ -371,6 +371,13 @@ struct VPCostContext {
/// legacy cost model for \p VF. Only used to check for additional VPlan
/// simplifications.
bool isLegacyUniformAfterVectorization(Instruction *I, ElementCount VF) const;
+
+ /// Estimate the overhead of scalarizing a recipe with result type \p ResultTy
+ /// and \p Operands with \p VF. This is a convenience wrapper for the
+ /// type-based getScalarizationOverhead API.
+ InstructionCost getScalarizationOverhead(Type *ResultTy,
+ ArrayRef<const VPValue *> Operands,
+ ElementCount VF);
};
/// This class can be used to assign names to VPValues. For VPValues without
diff --git a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
index 11846f863a3fa..c6273074778d1 100644
--- a/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
+++ b/llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp
@@ -3132,33 +3132,8 @@ InstructionCost VPReplicateRecipe::computeCost(ElementCount VF,
if (VF.isScalable())
return InstructionCost::getInvalid();
- // Compute the cost of scalarizing the result and operands if needed.
- InstructionCost ScalarizationCost = 0;
- if (VF.isVector()) {
- if (!ResultTy->isVoidTy()) {
- for (Type *VectorTy :
- to_vector(getContainedTypes(toVectorizedTy(ResultTy, VF)))) {
- ScalarizationCost += Ctx.TTI.getScalarizationOverhead(
- cast<VectorType>(VectorTy), APInt::getAllOnes(VF.getFixedValue()),
- /*Insert=*/true,
- /*Extract=*/false, Ctx.CostKind);
- }
- }
- // Skip operands that do not require extraction/scalarization and do not
- // incur any overhead.
- SmallPtrSet<const VPValue *, 4> UniqueOperands;
- Tys.clear();
- for (auto *Op : ArgOps) {
- if (Op->isLiveIn() || isa<VPReplicateRecipe, VPPredInstPHIRecipe>(Op) ||
- !UniqueOperands.insert(Op).second)
- continue;
- Tys.push_back(toVectorizedTy(Ctx.Types.inferScalarType(Op), VF));
- }
- ScalarizationCost +=
- Ctx.TTI.getOperandsScalarizationOverhead(Tys, Ctx.CostKind);
- }
-
- return ScalarCallCost * VF.getFixedValue() + ScalarizationCost;
+ return ScalarCallCost * VF.getFixedValue() +
+ Ctx.getScalarizationOverhead(ResultTy, ArgOps, VF);
}
case Instruction::Add:
case Instruction::Sub:
More information about the llvm-commits
mailing list