[llvm] [DAGCombiner] Spill dynamic insertelt chain in one go (PR #162368)

Princeton Ferro via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 8 12:53:12 PDT 2025


================
@@ -23445,6 +23445,65 @@ SDValue DAGCombiner::visitINSERT_VECTOR_ELT(SDNode *N) {
     // inselt undef, InVal, EltNo --> build_vector < InVal, InVal, ... >
     if (InVec.isUndef() && TLI.shouldSplatInsEltVarIndex(VT))
       return DAG.getSplat(VT, DL, InVal);
+
+    if (TLI.getTypeAction(*DAG.getContext(), VT) ==
+        TargetLowering::TypeSplitVector) {
+      // For dynamic insertelts, the type legalizer will spill the entire
+      // vector. For a chain of dynamic insertelts, this can be really
+      // inefficient and bad for compile time. If each insertelt is only fed
+      // into the next, the vector is write-only across this chain, and we can
+      // just spill once.
+      SmallVector<SDNode *> Seq{N};
+      while (true) {
+        SDValue InVec = Seq.back()->getOperand(0);
+        if (InVec.getOpcode() != ISD::INSERT_VECTOR_ELT ||
+            isa<ConstantSDNode>(InVec.getOperand(2)))
+          break;
+        Seq.push_back(InVec.getNode());
+      }
+
+      // Only care about chains, otherwise this instruction can be handled by
+      // the type legalizer just fine.
+      if (Seq.size() > 1) {
+        // In cases where the vector is illegal it will be broken down into
+        // parts and stored in parts - we should use the alignment for the
+        // smallest part.
+        Align SmallestAlign = DAG.getReducedAlign(VT, /*UseABI=*/false);
+        SDValue StackPtr =
+            DAG.CreateStackTemporary(VT.getStoreSize(), SmallestAlign);
----------------
Prince781 wrote:

> Or is the vector type legalizer code in a worse state than the LegalizeDAG handling for this?

Sorry, I'm not sure I understand this point.

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


More information about the llvm-commits mailing list