public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] tree-optimization/97838 - fix SLP leaf detection
@ 2020-11-16 11:11 Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2020-11-16 11:11 UTC (permalink / raw)
  To: gcc-patches

This properly handles reduction PHI nodes with unrepresented
initial value as leaf in the SLP graph.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2020-11-16  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/97838
	* tree-vect-slp.c (vect_slp_build_vertices): Properly handle
	not backwards reachable cycles.
	(vect_optimize_slp): Check a node is leaf before marking it
	visited.

	* gcc.dg/vect/pr97838.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr97838.c | 11 +++++++++++
 gcc/tree-vect-slp.c                 | 22 +++++++++++++++++-----
 2 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr97838.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr97838.c b/gcc/testsuite/gcc.dg/vect/pr97838.c
new file mode 100644
index 00000000000..06ec0358f6e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr97838.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+
+int a, b, c, d;
+
+void f() {
+  while (c++) {
+    int e = -1;
+    d = a ? e / a : e;
+    b ^= ~d;
+  }
+}
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index e4c2aa480e5..b98d5db9f76 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -2629,8 +2629,18 @@ vect_slp_build_vertices (vec_info *info, vec<slp_tree> &vertices,
   unsigned i;
   slp_instance instance;
   FOR_EACH_VEC_ELT (info->slp_instances, i, instance)
-    vect_slp_build_vertices (visited, SLP_INSTANCE_TREE (instance), vertices,
-			     leafs);
+    {
+      unsigned n_v = vertices.length ();
+      unsigned n_l = leafs.length ();
+      vect_slp_build_vertices (visited, SLP_INSTANCE_TREE (instance), vertices,
+			       leafs);
+      /* If we added vertices but no entries to the reverse graph we've
+	 added a cycle that is not backwards-reachable.   Push the entry
+	 to mimic as leaf then.  */
+      if (vertices.length () > n_v
+	  && leafs.length () == n_l)
+	leafs.safe_push (SLP_INSTANCE_TREE (instance)->vertex);
+    }
 }
 
 /* Apply (reverse) bijectite PERM to VEC.  */
@@ -2724,9 +2734,11 @@ vect_optimize_slp (vec_info *vinfo)
 	  || SLP_TREE_DEF_TYPE (node) == vect_constant_def)
 	continue;
 
-      /* Loads are the only thing generating permutes and leafs do not
-	 change across iterations.  */
-      bitmap_set_bit (n_visited, idx);
+      /* Leafs do not change across iterations.  Note leafs also double
+	 as entries to the reverse graph.  */
+      if (!slpg->vertices[idx].succ)
+	bitmap_set_bit (n_visited, idx);
+      /* Loads are the only thing generating permutes.  */
       if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
 	continue;
 
-- 
2.26.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] tree-optimization/97838 - fix SLP leaf detection
@ 2020-11-16 14:20 Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2020-11-16 14:20 UTC (permalink / raw)
  To: gcc-patches

This properly handles reduction PHI nodes with unrepresented
initial value as leaf in the SLP graph.

Bootstrapped and tested on x86_64-unknown-linux-gnu, pushed.

2020-11-16  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/97838
	* tree-vect-slp.c (vect_slp_build_vertices): Properly handle
	not backwards reachable cycles.
	(vect_optimize_slp): Check a node is leaf before marking it
	visited.

	* gcc.dg/vect/pr97838.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr97838.c | 11 +++++++++++
 gcc/tree-vect-slp.c                 | 22 +++++++++++++++++-----
 2 files changed, 28 insertions(+), 5 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr97838.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr97838.c b/gcc/testsuite/gcc.dg/vect/pr97838.c
new file mode 100644
index 00000000000..06ec0358f6e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr97838.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+
+int a, b, c, d;
+
+void f() {
+  while (c++) {
+    int e = -1;
+    d = a ? e / a : e;
+    b ^= ~d;
+  }
+}
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index e4c2aa480e5..b98d5db9f76 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -2629,8 +2629,18 @@ vect_slp_build_vertices (vec_info *info, vec<slp_tree> &vertices,
   unsigned i;
   slp_instance instance;
   FOR_EACH_VEC_ELT (info->slp_instances, i, instance)
-    vect_slp_build_vertices (visited, SLP_INSTANCE_TREE (instance), vertices,
-			     leafs);
+    {
+      unsigned n_v = vertices.length ();
+      unsigned n_l = leafs.length ();
+      vect_slp_build_vertices (visited, SLP_INSTANCE_TREE (instance), vertices,
+			       leafs);
+      /* If we added vertices but no entries to the reverse graph we've
+	 added a cycle that is not backwards-reachable.   Push the entry
+	 to mimic as leaf then.  */
+      if (vertices.length () > n_v
+	  && leafs.length () == n_l)
+	leafs.safe_push (SLP_INSTANCE_TREE (instance)->vertex);
+    }
 }
 
 /* Apply (reverse) bijectite PERM to VEC.  */
@@ -2724,9 +2734,11 @@ vect_optimize_slp (vec_info *vinfo)
 	  || SLP_TREE_DEF_TYPE (node) == vect_constant_def)
 	continue;
 
-      /* Loads are the only thing generating permutes and leafs do not
-	 change across iterations.  */
-      bitmap_set_bit (n_visited, idx);
+      /* Leafs do not change across iterations.  Note leafs also double
+	 as entries to the reverse graph.  */
+      if (!slpg->vertices[idx].succ)
+	bitmap_set_bit (n_visited, idx);
+      /* Loads are the only thing generating permutes.  */
       if (!SLP_TREE_LOAD_PERMUTATION (node).exists ())
 	continue;
 
-- 
2.26.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

* [PATCH] tree-optimization/97838 - fix SLP leaf detection
@ 2020-11-16  8:34 Richard Biener
  0 siblings, 0 replies; 3+ messages in thread
From: Richard Biener @ 2020-11-16  8:34 UTC (permalink / raw)
  To: gcc-patches

This properly handles reduction PHI nodes with unrepresented
initial value as leaf in the SLP graph.

Bootstrap & regtest running on x86_64-unknown-linux-gnu.

2020-11-16  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/97838
	* tree-vect-slp.c (vect_slp_build_vertices): Properly handle
	a NULL child as leaf.

	* gcc.dg/vect/pr97838.c: New testcase.
---
 gcc/testsuite/gcc.dg/vect/pr97838.c | 11 +++++++++++
 gcc/tree-vect-slp.c                 |  7 ++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gcc.dg/vect/pr97838.c

diff --git a/gcc/testsuite/gcc.dg/vect/pr97838.c b/gcc/testsuite/gcc.dg/vect/pr97838.c
new file mode 100644
index 00000000000..06ec0358f6e
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/vect/pr97838.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+
+int a, b, c, d;
+
+void f() {
+  while (c++) {
+    int e = -1;
+    d = a ? e / a : e;
+    b ^= ~d;
+  }
+}
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index e4c2aa480e5..0fb463f4bd0 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -2609,13 +2609,18 @@ vect_slp_build_vertices (hash_set<slp_tree> &visited, slp_tree node,
   vertices.safe_push (node);
 
   bool leaf = true;
+  bool force_leaf = false;
   FOR_EACH_VEC_ELT (SLP_TREE_CHILDREN (node), i, child)
     if (child)
       {
 	leaf = false;
 	vect_slp_build_vertices (visited, child, vertices, leafs);
       }
-  if (leaf)
+    else
+      /* A NULL SLP_TREE_CHILDREN entry is a not represented initial
+	 value of reductions which means the reduction itself is leaf.  */
+      force_leaf = true;
+  if (leaf || force_leaf)
     leafs.safe_push (node->vertex);
 }
 
-- 
2.26.2

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-11-16 14:20 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-16 11:11 [PATCH] tree-optimization/97838 - fix SLP leaf detection Richard Biener
  -- strict thread matches above, loose matches on Subject: below --
2020-11-16 14:20 Richard Biener
2020-11-16  8:34 Richard Biener

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).