public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: gcc-patches <gcc-patches@gcc.gnu.org>,
	fortran <fortran@gcc.gnu.org>, Jakub Jelinek <jakub@redhat.com>
Subject: [Patch] OpenMP: Fix 'omp declare target' handling for vars [PR99509]
Date: Wed, 10 Mar 2021 15:20:42 +0100	[thread overview]
Message-ID: <29dc3864-050b-a83d-4b64-3982e233bd4f@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 668 bytes --]

The C/C++ FE sets for an 'omp declare target' ... 'omp end declare target'
the attribute 'omp declare target implicit'.

That's later processed (for C++) in decl.c - which remove that attribute
and either keeps and explicit 'omp declare target' or 'omp declare target link'
attribute.

Unfortunately, adding 'omp declare target' comes too late as the varpool
has been generated.

I am not sure what's the best way, but the following seems to work.

OK for mainline?

Tobias

-----------------
Mentor Graphics (Deutschland) GmbH, Arnulfstrasse 201, 80634 München Registergericht München HRB 106955, Geschäftsführer: Thomas Heurung, Frank Thürauf

[-- Attachment #2: omp-decl-tgt.diff --]
[-- Type: text/x-patch, Size: 2107 bytes --]

OpenMP: Fix 'omp declare target' handling for vars [PR99509]

C/C++ use 'omp declare target implicit' when they call
varpool_node::get_create; make sure that a later
'omp declare target' (but not 'omp declare target link')
is honored.

gcc/ChangeLog:

	PR c++/99509
	* varpool.c (varpool_node::get_create): Re-check whether
	'omp declare target'.

libgomp/ChangeLog:

	PR c++/99509
	* testsuite/libgomp.c-c++-common/declare_target-1.c: New test.

 gcc/varpool.c                                      | 18 +++++++++++++++++-
 .../libgomp.c-c++-common/declare_target-1.c        | 22 ++++++++++++++++++++++
 2 files changed, 39 insertions(+), 1 deletion(-)

diff --git a/gcc/varpool.c b/gcc/varpool.c
index 4830df5..5ae6778 100644
--- a/gcc/varpool.c
+++ b/gcc/varpool.c
@@ -145,7 +145,23 @@ varpool_node::get_create (tree decl)
   varpool_node *node = varpool_node::get (decl);
   gcc_checking_assert (VAR_P (decl));
   if (node)
-    return node;
+    {
+      /* This happens with C/C++ when 'omp declare target implicit' is set
+	 which is only later replaced by 'omp declate target' or '... link'.  */
+      if (!node->offloadable
+	  && (flag_openacc || flag_openmp)
+	  && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
+	{
+	  node->offloadable = 1;
+	  if (ENABLE_OFFLOADING && !DECL_EXTERNAL (decl))
+	    {
+	      g->have_offload = true;
+	      if (!in_lto_p)
+		vec_safe_push (offload_vars, decl);
+	    }
+	}
+      return node;
+    }
 
   node = varpool_node::create_empty ();
   node->decl = decl;
diff --git a/libgomp/testsuite/libgomp.c-c++-common/declare_target-1.c b/libgomp/testsuite/libgomp.c-c++-common/declare_target-1.c
new file mode 100644
index 0000000..c5670df
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/declare_target-1.c
@@ -0,0 +1,22 @@
+/* PR c++/99509  */
+
+#pragma omp declare target
+int data[] = {5};
+#pragma omp end declare target
+
+static inline int
+foo (int idx)
+{
+  return data[idx];
+}
+
+int
+main ()
+{
+  int i = -1;
+  #pragma omp target map(from:i)
+    i = foo(0);
+  if (i != 5)
+    __builtin_abort ();
+  return 0;
+}

             reply	other threads:[~2021-03-10 14:20 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-10 14:20 Tobias Burnus [this message]
2021-03-12 12:20 ` Jakub Jelinek
2021-03-15  8:17   ` Tobias Burnus
2021-03-15  8:20     ` Jakub Jelinek

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=29dc3864-050b-a83d-4b64-3982e233bd4f@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /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).