public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Richard Biener <rguenther@suse.de>
To: gcc-patches@gcc.gnu.org
Subject: [PATCH] tree-optimization/100778 - fix placement of trapping vectorized ops
Date: Thu, 1 Jul 2021 12:31:15 +0200 (CEST)	[thread overview]
Message-ID: <6n97rrso-7q82-s5n-71s1-os9n4q2or2so@fhfr.qr> (raw)

This avoids placing possibly trapping vectorized operations where
the corresponding scalar operation was possibly not executed.

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

2021-01-07  Richard Biener  <rguenther@suse.de>

	PR tree-optimization/100778
	* tree-vect-slp.c (vect_schedule_slp_node): Do not place trapping
	vectorized ops ahead of their scalar BB.

	* gcc.dg/torture/pr100778.c: New testcase.
---
 gcc/testsuite/gcc.dg/torture/pr100778.c | 31 +++++++++++++++++++++++++
 gcc/tree-vect-slp.c                     | 15 ++++++++++++
 2 files changed, 46 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/torture/pr100778.c

diff --git a/gcc/testsuite/gcc.dg/torture/pr100778.c b/gcc/testsuite/gcc.dg/torture/pr100778.c
new file mode 100644
index 00000000000..7997f2f2bcd
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/torture/pr100778.c
@@ -0,0 +1,31 @@
+/* { dg-do run { target *-*-*gnu* } } */
+/* { dg-additional-options "-fno-tree-sink -fno-math-errno -ftree-vectorize -D_GNU_SOURCE" } */
+/* { dg-require-effective-target fenv_exceptions } */
+
+#include <fenv.h>
+
+double a[2];
+void __attribute__((noipa)) foo ()
+{
+  double x = a[0];
+  double y = a[1];
+  double norm = __builtin_sqrt (x*x + y*y);
+  if (norm > 1.)
+    {
+      x = x / norm;
+      y = y / norm;
+    }
+  a[0] = x;
+  a[1] = y;
+}
+
+int main()
+{
+  feenableexcept (FE_INVALID);
+  a[0] = 0.;
+  a[1] = 0.;
+  foo ();
+  if (a[0] != 0. || a[1] != 0.)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/gcc/tree-vect-slp.c b/gcc/tree-vect-slp.c
index 966b281ffae..f08797c2bc0 100644
--- a/gcc/tree-vect-slp.c
+++ b/gcc/tree-vect-slp.c
@@ -7100,6 +7100,21 @@ vect_schedule_slp_node (vec_info *vinfo,
 	  gcc_assert (seen_vector_def);
 	  si = gsi_after_labels (as_a <bb_vec_info> (vinfo)->bbs[0]);
 	}
+      else if (is_a <bb_vec_info> (vinfo)
+	       && gimple_bb (last_stmt) != gimple_bb (stmt_info->stmt)
+	       && gimple_could_trap_p (stmt_info->stmt))
+	{
+	  /* We've constrained possibly trapping operations to all come
+	     from the same basic-block, if vectorized defs would allow earlier
+	     scheduling still force vectorized stmts to the original block.
+	     This is only necessary for BB vectorization since for loop vect
+	     all operations are in a single BB and scalar stmt based
+	     placement doesn't play well with epilogue vectorization.  */
+	  gcc_assert (dominated_by_p (CDI_DOMINATORS,
+				      gimple_bb (stmt_info->stmt),
+				      gimple_bb (last_stmt)));
+	  si = gsi_after_labels (gimple_bb (stmt_info->stmt));
+	}
       else if (is_a <gphi *> (last_stmt))
 	si = gsi_after_labels (gimple_bb (last_stmt));
       else
-- 
2.26.2

                 reply	other threads:[~2021-07-01 10:31 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=6n97rrso-7q82-s5n-71s1-os9n4q2or2so@fhfr.qr \
    --to=rguenther@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).