]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
vect: Fix ncopies when costing SLP reductions [PR116901]
authorRichard Sandiford <richard.sandiford@arm.com>
Wed, 12 Mar 2025 09:40:10 +0000 (09:40 +0000)
committerRichard Sandiford <richard.sandiford@arm.com>
Wed, 12 Mar 2025 09:40:10 +0000 (09:40 +0000)
pr110625_[24].c started failing after r15-1329-gd66b820f392aa9a7,
which switched to single def-use cycles for single-lane SLP.
The problem is that we only costed one vector accumulator
operation for an N-vector cycle.

The problem seems to have been latent, and meant that we also
only costed one FADDA for reduc_strict_4.c and reduc_strict_5.c,
even though they need 4 and 6 FADDAs respectively.

I'm not sure why:

   if ((double_reduc || reduction_type != TREE_CODE_REDUCTION)
       && ncopies > 1)

was previously only necessary for non-SLP, but the patch preserves
that for safety.

gcc/
PR tree-optimization/116901
* tree-vect-loop.cc (vectorizable_reduction): Set ncopies to
SLP_TREE_NUMBER_OF_VEC_STMTS for SLP.

gcc/testsuite/
PR tree-optimization/116901
* gcc.target/aarch64/sve/reduc_strict_4.c: Turn off costing.
* gcc.target/aarch64/sve/reduc_strict_5.c: Likewise.

gcc/testsuite/gcc.target/aarch64/sve/reduc_strict_4.c
gcc/testsuite/gcc.target/aarch64/sve/reduc_strict_5.c
gcc/tree-vect-loop.cc

index 9a12edad42ecbe8b595771fd94982cc8aa65c5e6..8dad5ee601663886909160b1de9a55e0b5bbf497 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -ftree-vectorize" } */
+/* { dg-options "-O2 -ftree-vectorize -fno-vect-cost-model" } */
 
 double mat[100][8];
 
index 7c3068fe87ad36e4f691093e25d9665a0ab49165..9e117812d340ae4f12d07e117533510b83edf031 100644 (file)
@@ -1,5 +1,5 @@
 /* { dg-do compile } */
-/* { dg-options "-O2 -ftree-vectorize" } */
+/* { dg-options "-O2 -ftree-vectorize -fno-vect-cost-model" } */
 
 double mat[100][12];
 
index 52533623cab93ad47a8ca436961e1b655c1ca7c7..9413dcef702597ab27165e676546b190e2bd36ba 100644 (file)
@@ -8180,7 +8180,7 @@ vectorizable_reduction (loop_vec_info loop_vinfo,
     return false;
 
   if (slp_node)
-    ncopies = 1;
+    ncopies = SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node);
   else
     ncopies = vect_get_num_copies (loop_vinfo, vectype_in);
 
@@ -8288,7 +8288,7 @@ vectorizable_reduction (loop_vec_info loop_vinfo,
        || reduction_type == CONST_COND_REDUCTION
        || reduction_type == EXTRACT_LAST_REDUCTION)
       && slp_node
-      && SLP_TREE_NUMBER_OF_VEC_STMTS (slp_node) > 1)
+      && ncopies > 1)
     {
       if (dump_enabled_p ())
        dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
@@ -8297,6 +8297,7 @@ vectorizable_reduction (loop_vec_info loop_vinfo,
     }
 
   if ((double_reduc || reduction_type != TREE_CODE_REDUCTION)
+      && !slp_node
       && ncopies > 1)
     {
       if (dump_enabled_p ())
@@ -8523,11 +8524,10 @@ vectorizable_reduction (loop_vec_info loop_vinfo,
    participating.  When unrolling we want each unrolled iteration to have its
    own reduction accumulator since one of the main goals of unrolling a
    reduction is to reduce the aggregate loop-carried latency.  */
-  if ((ncopies > 1
-       || (slp_node
-          && !REDUC_GROUP_FIRST_ELEMENT (stmt_info)
-          && SLP_TREE_LANES (slp_node) == 1
-          && vect_get_num_copies (loop_vinfo, vectype_in) > 1))
+  if (ncopies > 1
+      && (!slp_node
+         || (!REDUC_GROUP_FIRST_ELEMENT (stmt_info)
+             && SLP_TREE_LANES (slp_node) == 1))
       && (STMT_VINFO_RELEVANT (stmt_info) <= vect_used_only_live)
       && reduc_chain_length == 1
       && loop_vinfo->suggested_unroll_factor == 1)