public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jakub Jelinek <jakub@redhat.com>
To: Richard Biener <rguenther@suse.de>
Cc: gcc-patches@gcc.gnu.org
Subject: [PATCH] Handle "omp simd array" accesses in ifcvt_memrefs_wont_trap
Date: Fri, 31 May 2019 09:43:00 -0000	[thread overview]
Message-ID: <20190531093323.GF19695@tucnak> (raw)

Hi!

As a preparation for lastprivate(conditional:) on #pragma omp simd, I need
if-conversion to handle "omp simd array" accesses.  These are safe, no
matter whether written or read, each simd lane accesses their own element,
after the vectorization it is all just a single vector read or store or RMW
cycle, it will never trap (it is an automatic variable), it will never be
out of bounds (the index is guaranteed to be within the bounds of the array,
as the array is sized to the vectorization factor and index is the simd lane
number within that vectorization factor).

Tested on x86_64-linux with vect.exp, ok for trunk if it passes full
bootstrap/regtest?

2019-05-31  Jakub Jelinek  <jakub@redhat.com>

	* tree-if-conv.c: Include attribs.h.
	(ifcvt_memrefs_wont_trap): Return true for ARRAY_REF access to
	"omp simd array" variable with .GOMP_SIMD_LANE as index.

	* gcc.dg/vect/vect-cond-12.c: New test.

--- gcc/tree-if-conv.c.jj	2019-05-14 21:37:32.932400472 +0200
+++ gcc/tree-if-conv.c	2019-05-31 11:13:13.558732900 +0200
@@ -120,6 +120,7 @@ along with GCC; see the file COPYING3.
 #include "fold-const.h"
 #include "tree-ssa-sccvn.h"
 #include "tree-cfgcleanup.h"
+#include "attribs.h"
 
 /* Only handle PHIs with no more arguments unless we are asked to by
    simd pragma.  */
@@ -897,6 +898,32 @@ ifcvt_memrefs_wont_trap (gimple *stmt, v
   if (DR_W_UNCONDITIONALLY (*master_dr))
     return true;
 
+  /* OpenMP simd lane accesses to omp simd array variables are always
+     within bounds and can be handled as if it was unconditional.  */
+  tree base_var = get_base_address (base);
+  if (VAR_P (base_var)
+      && lookup_attribute ("omp simd array", DECL_ATTRIBUTES (base_var)))
+    {
+      tree ref = DR_REF (a);
+      struct loop *loop = loop_containing_stmt (stmt);
+      if (loop->simduid
+	  && TREE_CODE (ref) == ARRAY_REF
+	  && TREE_CODE (TREE_OPERAND (ref, 1)) == SSA_NAME)
+	{
+	  gimple *def = SSA_NAME_DEF_STMT (TREE_OPERAND (ref, 1));
+	  if (is_gimple_call (def)
+	      && gimple_call_internal_p (def)
+	      && (gimple_call_internal_fn (def) == IFN_GOMP_SIMD_LANE))
+	    {
+	      tree arg = gimple_call_arg (def, 0);
+	      gcc_assert (TREE_CODE (arg) == SSA_NAME);
+	      arg = SSA_NAME_VAR (arg);
+	      if (arg == loop->simduid)
+		return true;
+	    }
+	}
+    }
+
   /* If a is unconditionally accessed then ...
 
      Even a is conditional access, we can treat it as an unconditional
--- gcc/testsuite/gcc.dg/vect/vect-cond-12.c.jj	2019-05-31 11:25:33.203577504 +0200
+++ gcc/testsuite/gcc.dg/vect/vect-cond-12.c	2019-05-31 11:26:58.616174115 +0200
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-additional-options "-fopenmp-simd" } */
+/* { dg-final { scan-tree-dump-times "vectorized 1 loops" 1 "vect" { target vect_condition } } } */
+
+int x;
+
+void
+foo (int *a)
+{
+  #pragma omp simd lastprivate (x)
+  for (int i = 0; i < 1024; ++i)
+    if (a[i])
+      x = i;
+}

	Jakub

             reply	other threads:[~2019-05-31  9:33 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-31  9:43 Jakub Jelinek [this message]
2019-05-31  9:44 ` Richard Biener
2019-05-31 10:35   ` Jakub Jelinek
2019-05-31 11:32     ` Richard Biener

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=20190531093323.GF19695@tucnak \
    --to=jakub@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=rguenther@suse.de \
    /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).