]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/105053 - fix reduction chain epilogue generation
authorRichard Biener <rguenther@suse.de>
Fri, 25 Mar 2022 13:31:25 +0000 (14:31 +0100)
committerRichard Biener <rguenther@suse.de>
Thu, 7 Apr 2022 09:11:10 +0000 (11:11 +0200)
When we optimize permutations in a reduction chain we have to
be careful to select the correct live-out stmt, otherwise the
reduction result will be unused and the retained scalar code will
execute only the number of vector iterations.

2022-03-25  Richard Biener  <rguenther@suse.de>

PR tree-optimization/105053
* tree-vect-loop.c (vect_create_epilog_for_reduction): Pick
the correct live-out stmt for a reduction chain.

* g++.dg/vect/pr105053.cc: New testcase.

gcc/testsuite/g++.dg/vect/pr105053.cc [new file with mode: 0644]
gcc/tree-vect-loop.c

diff --git a/gcc/testsuite/g++.dg/vect/pr105053.cc b/gcc/testsuite/g++.dg/vect/pr105053.cc
new file mode 100644 (file)
index 0000000..6deef84
--- /dev/null
@@ -0,0 +1,25 @@
+// { dg-require-effective-target c++11 }
+// { dg-require-effective-target int32plus }
+
+#include <vector>
+#include <tuple>
+#include <algorithm>
+
+int main()
+{
+  const int n = 4;
+  std::vector<std::tuple<int,int,double>> vec
+      = { { 1597201307, 1817606674, 0. },
+            { 1380347796, 1721941769, 0.},
+            {837975613, 1032707773, 0.},
+            {1173654292, 2020064272, 0.} } ;
+  int sup1 = 0;
+  for(int i=0;i<n;++i)
+    sup1=std::max(sup1,std::max(std::get<0>(vec[i]),std::get<1>(vec[i])));
+  int sup2 = 0;
+  for(int i=0;i<n;++i)
+    sup2=std::max(std::max(sup2,std::get<0>(vec[i])),std::get<1>(vec[i]));
+  if (sup1 != sup2)
+    std::abort ();
+  return 0;
+}
index 468b9aed68cda9c939cebc1196151d210ee25b33..968087a9008b5e1c9c73a34facaf04a7e5fbfef4 100644 (file)
@@ -6000,8 +6000,14 @@ vect_create_epilog_for_reduction (loop_vec_info loop_vinfo,
      the loop exit phi node.  */
   if (REDUC_GROUP_FIRST_ELEMENT (stmt_info))
     {
-      stmt_vec_info dest_stmt_info
-       = vect_orig_stmt (SLP_TREE_SCALAR_STMTS (slp_node)[group_size - 1]);
+      /* The last statement in the reduction chain produces the live-out
+        value.  Note SLP optimization can shuffle scalar stmts to
+        optimize permutations so we have to search for the last stmt.  */
+      stmt_vec_info dest_stmt_info;
+      FOR_EACH_VEC_ELT (SLP_TREE_SCALAR_STMTS (slp_node), k, dest_stmt_info)
+       if (!REDUC_GROUP_NEXT_ELEMENT (dest_stmt_info))
+         break;
+      dest_stmt_info = vect_orig_stmt (dest_stmt_info);
       scalar_dest = gimple_assign_lhs (dest_stmt_info->stmt);
       group_size = 1;
     }