]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
tree-optimization/103237 - avoid vectorizing unhandled double reductions
authorRichard Biener <rguenther@suse.de>
Mon, 15 Nov 2021 10:37:56 +0000 (11:37 +0100)
committerRichard Biener <rguenther@suse.de>
Mon, 15 Nov 2021 12:07:57 +0000 (13:07 +0100)
Double reductions which have multiple LC PHIs in the inner loop
are not handled correctly during transformation since those PHIs
are not properly classified as reduction.  The following disables
vectorizing them.

2021-11-15  Richard Biener  <rguenther@suse.de>

PR tree-optimization/103237
* tree-vect-loop.c (vect_is_simple_reduction): Fail for
double reductions with multiple inner loop LC PHI nodes.

* gcc.dg/torture/pr103237.c: New testcase.

gcc/testsuite/gcc.dg/torture/pr103237.c [new file with mode: 0644]
gcc/tree-vect-loop.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr103237.c b/gcc/testsuite/gcc.dg/torture/pr103237.c
new file mode 100644 (file)
index 0000000..f2399f9
--- /dev/null
@@ -0,0 +1,24 @@
+/* { dg-do run } */
+/* { dg-additional-options "-ftree-vectorize" } */
+
+int g1;
+unsigned int g2 = -1U;
+static void __attribute__((noipa))
+func_1()
+{
+  int *l_1 = &g1;
+  for (int g3a = 0; g3a != 4; g3a++)
+    for (int l_2 = 0; l_2 <= 3; l_2++)
+      {
+        unsigned int *l_3 = &g2;
+        *l_1 = *l_3 ^= 1;
+      }
+}
+int
+main()
+{
+  func_1();
+  if (g1 != -1)
+    __builtin_abort ();
+  return 0;
+}
index 1cd5dbcb6f7788b467a74b09385c4938525524b3..73efdb96badf151e7ace9826e49dd4cee97ba40e 100644 (file)
@@ -3567,6 +3567,17 @@ vect_is_simple_reduction (loop_vec_info loop_info, stmt_vec_info phi_info,
       return def_stmt_info;
     }
 
+  /* When the inner loop of a double reduction ends up with more than
+     one loop-closed PHI we have failed to classify alternate such
+     PHIs as double reduction, leading to wrong code.  See PR103237.  */
+  if (inner_loop_of_double_reduc && lcphis.length () != 1)
+    {
+      if (dump_enabled_p ())
+       dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
+                        "unhandle double reduction\n");
+      return NULL;
+    }
+
   /* If this isn't a nested cycle or if the nested cycle reduction value
      is used ouside of the inner loop we cannot handle uses of the reduction
      value.  */