[llvm] [DAGCombiner] Reassociate chains of vector reductions (PR #206471)

Pengcheng Wang via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 2 01:42:14 PDT 2026


================
@@ -1401,6 +1401,43 @@ SDValue DAGCombiner::reassociateReduction(unsigned RedOpc, unsigned Opc,
     SDValue Op2 = DAG.getNode(Opc, DL, VT, B, D);
     return DAG.getNode(Opc, DL, VT, Red, Op2);
   }
+
+  // Reassociate a reduction chain so two reductions become adjacent and the
+  // folds above can merge them:
+  //   op(vecreduce(X), op(vecreduce(Y), Z))
+  //     -> op(vecreduce(op(X, Y)), Z)
+  // Applied to fixpoint by the combiner worklist, this collapses an
+  // arbitrarily long chain of reductions (such as the left-leaning chain SLP
+  // emits) into a single reduction.
+  auto FoldReductionChain = [&](SDValue Red0, SDValue Chain) -> SDValue {
+    SDValue X, Y, Z, RedY;
+    if (!sd_match(Red0, m_OneUse(m_UnaryOp(RedOpc, m_Value(X)))) ||
+        !sd_match(Chain, m_OneUse(m_c_BinOp(
+                             Opc,
+                             m_AllOf(m_OneUse(m_UnaryOp(RedOpc, m_Value(Y))),
+                                     m_Value(RedY)),
----------------
wangpc-pp wrote:

We may need to rewrite the above code as well.

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


More information about the llvm-commits mailing list