diff --git a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp index dd913f935bac..5e7f6523cd86 100644 --- a/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp +++ b/llvm/lib/Transforms/Vectorize/LoopVectorize.cpp @@ -5832,14 +5832,22 @@ void LoopVectorizationCostModel::setVectorizedCallDecision(ElementCount VF) { // assumed to be vectors, so we need to extract individual elements from // there, execute VF scalar calls, and then gather the result into the // vector return value. - InstructionCost ScalarCallCost = - TTI.getCallInstrCost(ScalarFunc, ScalarRetTy, ScalarTys, CostKind); + if (VF.isFixed()) { + InstructionCost ScalarCallCost = + TTI.getCallInstrCost(ScalarFunc, ScalarRetTy, ScalarTys, CostKind); - // Compute costs of unpacking argument values for the scalar calls and - // packing the return values to a vector. - InstructionCost ScalarizationCost = getScalarizationOverhead(CI, VF); + // Compute costs of unpacking argument values for the scalar calls and + // packing the return values to a vector. + InstructionCost ScalarizationCost = getScalarizationOverhead(CI, VF); + ScalarCost = ScalarCallCost * VF.getKnownMinValue() + ScalarizationCost; + } else { + // There is no point attempting to calculate the scalar cost for a + // scalable VF as we know it will be Invalid. + assert(!getScalarizationOverhead(CI, VF).isValid() && + "Unexpected valid cost for scalarizing scalable vectors"); + ScalarCost = InstructionCost::getInvalid(); + } - ScalarCost = ScalarCallCost * VF.getKnownMinValue() + ScalarizationCost; // Honor ForcedScalars and UniformAfterVectorization decisions. // TODO: For calls, it might still be more profitable to widen. Use // VPlan-based cost model to compare different options.