]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization - Require size > 1 for SLP reduction subgroups
authorZhongyao Chen <chenzhongyao.hit@gmail.com>
Wed, 14 Jan 2026 11:38:02 +0000 (11:38 +0000)
committerZhongyao Chen <chen.zhongyao@zte.com.cn>
Thu, 11 Jun 2026 12:33:38 +0000 (12:33 +0000)
The SLP reduction subgroup analysis can succeed for size-1 groups,
but this leads to poor code generation.  Size-1 cases should fall
back to single-lane reduction instead.

Handle size-1 groups by returning false from the analysis function,
and add a loop exit check to avoid unnecessary processing.

PR tree-optimization/123343

gcc/ChangeLog:

* tree-vect-slp.cc (vect_analyze_slp_reduction_group): Return
false for group_size <= 1 at entry.
(vect_analyze_slp_reductions): Add loop exit check for
group_size <= 1.

gcc/testsuite/ChangeLog:

* gcc.dg/vect/slp-reduc-var.c: New testcase.

Signed-off-by: Zhongyao Chen <chen.zhongyao@zte.com.cn>
gcc/testsuite/gcc.dg/vect/slp-reduc-var.c [new file with mode: 0644]
gcc/tree-vect-slp.cc

diff --git a/gcc/testsuite/gcc.dg/vect/slp-reduc-var.c b/gcc/testsuite/gcc.dg/vect/slp-reduc-var.c
new file mode 100644 (file)
index 0000000..bb322b1
--- /dev/null
@@ -0,0 +1,25 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-O3 --param vect-epilogues-nomask=0" } */
+#include <stdint-gcc.h>
+
+uint64_t
+x264_pixel_var_8x8 (uint8_t *pix, int i_stride)
+{
+  uint32_t sum = 0, sqr = 0;
+  for (int y = 0; y < 8; y++)
+    {
+      for (int x = 0; x < 8; x++)
+       {
+         sum += pix[x];
+         sqr += pix[x] * pix[x];
+       }
+      pix += i_stride;
+    }
+  return sum + ((uint64_t)sqr << 32);
+}
+
+/* Verify that size-1 reductions fall back to single-lane reduction chains.  */
+/* { dg-final { scan-tree-dump "Starting SLP discovery of reduction chain" "vect" } } */
+/* { dg-final { scan-tree-dump-not "SLP discovery of reduction chain failed" "vect" } } */
+/* { dg-final { scan-tree-dump-not "SLP discovery of size 2 reduction group" "vect" } } */
+/* { dg-final { scan-tree-dump-not "SLP discovery of size 1 reduction group" "vect" } } */
index 90c183c8811bf902a0cfa16b89bf28889e541d1c..2250f6f74a1ae63b0b9e42559955c0ab77ff3135 100644 (file)
@@ -4852,8 +4852,11 @@ vect_analyze_slp_reduction_group (loop_vec_info loop_vinfo,
                                  unsigned max_tree_size, unsigned *limit,
                                  bool *matches)
 {
-  /* Try to form a reduction group.  */
+  /* Try to form a reduction group.  Size-1 groups are not suitable
+     for SLP reduction and should fall back to single-lane reduction.  */
   unsigned int group_size = scalar_stmts.length ();
+  if (group_size <= 1)
+    return false;
   if (!matches)
     matches = XALLOCAVEC (bool, group_size);
   poly_uint64 max_nunits = 1;
@@ -5003,6 +5006,8 @@ vect_analyze_slp_reductions (loop_vec_info loop_vinfo,
              }
          scalar_stmts.truncate (j);
          group_size = scalar_stmts.length ();
+         if (group_size <= 1)
+           break;
          if (vect_analyze_slp_reduction_group (loop_vinfo, scalar_stmts,
                                                bst_map, max_tree_size, limit,
                                                matches))