[llvm] [VPlan] Move call widening decision to VPlan. (NFCI) (PR #195518)
Ramkumar Ramachandra via llvm-commits
llvm-commits at lists.llvm.org
Tue May 12 07:10:26 PDT 2026
================
@@ -6518,3 +6518,179 @@ void VPlanTransforms::makeScalarizationDecisions(VPlan &Plan, VFRange &Range) {
}
}
}
+
+/// Returns true if \p Info's parameter kinds are compatible with \p Args.
+static bool areVFParamsOk(const VFInfo &Info, ArrayRef<VPValue *> Args,
+ PredicatedScalarEvolution &PSE, const Loop *L) {
+ return all_of(Info.Shape.Parameters, [&](VFParameter Param) {
+ switch (Param.ParamKind) {
+ case VFParamKind::Vector:
+ case VFParamKind::GlobalPredicate:
+ return true;
+ case VFParamKind::OMP_Uniform:
+ return PSE.getSE()->isLoopInvariant(
+ vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L), L);
+ case VFParamKind::OMP_Linear:
+ return match(vputils::getSCEVExprForVPValue(Args[Param.ParamPos], PSE, L),
+ m_scev_AffineAddRec(
+ m_SCEV(), m_scev_SpecificSInt(Param.LinearStepOrPos),
+ m_SpecificLoop(L)));
+ default:
+ return false;
+ }
+ });
+}
+
+/// Find a vector variant of \p CI for \p VF, respecting \p MaskRequired.
+/// Returns the variant function, or nullptr. Masked variants are assumed to
+/// take the mask as a trailing parameter.
+static Function *findVectorVariant(CallInst *CI, ArrayRef<VPValue *> Args,
+ ElementCount VF, bool MaskRequired,
+ PredicatedScalarEvolution &PSE,
+ const Loop *L) {
+ if (CI->isNoBuiltin())
+ return nullptr;
+ auto Mappings = VFDatabase::getMappings(*CI);
+ const auto *It = find_if(Mappings, [&](const VFInfo &Info) {
+ return Info.Shape.VF == VF && (!MaskRequired || Info.isMasked()) &&
+ areVFParamsOk(Info, Args, PSE, L);
+ });
+ if (It == Mappings.end())
+ return nullptr;
+ return CI->getModule()->getFunction(It->VectorName);
+}
+
+namespace {
+/// The outcome of choosing how to widen a call at a given VF.
+struct CallWideningDecision {
+ using KindTy = VPCostContext::CallWideningKind;
+ CallWideningDecision(KindTy Kind, Function *Variant = nullptr)
+ : Kind(Kind), Variant(Variant) {}
+ KindTy Kind;
+ /// Set when Kind == VectorVariant.
+ Function *Variant;
+
+ bool operator==(const CallWideningDecision &Other) const {
+ return Kind == Other.Kind && Variant == Other.Variant;
+ }
+};
+} // namespace
+
+/// Pick the cheapest widening for the call \p VPI at \p VF among scalarization,
+/// vector intrinsic, and vector library variant.
+static CallWideningDecision decideCallWidening(VPInstruction &VPI,
+ ArrayRef<VPValue *> Ops,
+ ElementCount VF,
+ VPCostContext &CostCtx) {
+ auto *CI = cast<CallInst>(VPI.getUnderlyingInstr());
+
+ // Scalar VFs and calls forced or known to scalarize always replicate.
+ if (VF.isScalar() || CostCtx.willBeScalarized(CI, VF))
+ return CallWideningDecision::KindTy::Scalarize;
+
+ auto *CalledFn = cast<Function>(
+ VPI.getOperand(VPI.getNumOperandsWithoutMask() - 1)->getLiveInIRValue());
+ Type *ResultTy = CostCtx.Types.inferScalarType(&VPI);
----------------
artagnon wrote:
Hm, not sure how inferScalarTypes would work on a VPInstruction? Aren't there independent cases for VPInstruction and VPWidenCall?
https://github.com/llvm/llvm-project/pull/195518
More information about the llvm-commits
mailing list