[llvm] [SelectionDAG] Legalize non-divisible scalable vector widening (PR #218777)

Paul Walker via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 26 03:53:12 PDT 2026


================
@@ -9242,6 +9242,32 @@ SDValue DAGTypeLegalizer::ModifyToType(SDValue InOp, EVT NVT,
   if (InEC.hasKnownScalarFactor(WidenEC))
     return DAG.getExtractSubvector(dl, NVT, InOp, 0);
 
+  if (InVT.isScalableVector()) {
+    // Split the input and widened vector into the largest equal-sized scalable
+    // subvectors. The common factor lets us preserve the source lanes and pad
+    // with subvectors, avoiding per-lane operations on scalable vectors.
+    // Worst case scenario (GCD == 1) we fall back to single operations.
+    unsigned InNumElts = InVT.getVectorMinNumElements();
+    unsigned NewNumElts = NVT.getVectorMinNumElements();
+    unsigned CommonFactor = std::gcd(InNumElts, NewNumElts);
+    EVT PartVT = EVT::getVectorVT(*DAG.getContext(), NVT.getVectorElementType(),
+                                  ElementCount::getScalable(CommonFactor));
+
+    SmallVector<SDValue, 16> Ops;
+    unsigned NumSourceParts = InNumElts / CommonFactor;
+    for (unsigned I = 0; I != NumSourceParts; ++I)
+      Ops.push_back(
+          DAG.getExtractSubvector(dl, PartVT, InOp, I * CommonFactor));
+
+    unsigned NumResultParts = NewNumElts / CommonFactor;
+    if (NumSourceParts != NumResultParts) {
+      SDValue FillVal = FillWithZeroes ? DAG.getConstant(0, dl, PartVT)
+                                       : DAG.getPOISON(PartVT);
+      Ops.append(NumResultParts - NumSourceParts, FillVal);
+    }
----------------
paulwalker-arm wrote:

The first loop assumes "NewNumElts >= InNumElts", which given the context means the main entry if statement is missing `NVT.bitsGT(InVT)` and the `if (NumSourceParts != NumResultParts)` test is redundant.

Alternatively doing: `unsigned NumSourceParts = std::min(InNumElts, NewNumElts) / CommonFactor` means the algorithm can support narrowing as well?

https://github.com/llvm/llvm-project/pull/218777


More information about the llvm-commits mailing list