[llvm] [DAG] Fold nested add(add(reduce(a), b), add(reduce(c), d)) (PR #115150)

David Green via llvm-commits llvm-commits at lists.llvm.org
Thu Nov 7 00:09:37 PST 2024


================
@@ -1329,6 +1329,38 @@ SDValue DAGCombiner::reassociateReduction(unsigned RedOpc, unsigned Opc,
                        DAG.getNode(Opc, DL, N0.getOperand(0).getValueType(),
                                    N0.getOperand(0), N1.getOperand(0)));
   }
+
+  // Reassociate add(add(vecreduce(a), b), add(vecreduce(c), d)) into
+  // add(vecreduce(add(a, c)), add(b, d)), to combine the reductions into a
+  // single node.
+  if (N0.getOpcode() == Opc && N1.getOpcode() == Opc && N0->hasOneUse() &&
+      N1->hasOneUse()) {
+    SDValue N00 = N0.getOperand(0);
+    SDValue N01 = N0.getOperand(1);
+    if (N00.getOpcode() != RedOpc && N01.getOpcode() == RedOpc)
+      std::swap(N00, N01);
+    if (N00.getOpcode() == RedOpc && N01.getOpcode() != RedOpc &&
+        N00->hasOneUse()) {
----------------
davemgreen wrote:

Oh that does sound like a better approach. Thanks.

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


More information about the llvm-commits mailing list