Hi!

On 2019-12-11T18:22:00+0100, I wrote:
> On 2019-10-29T12:15:01+0000, Julian Brown <jul...@codesourcery.com> wrote:
>> I've removed the special-case handling
>> of pointers in the enter/exit data code, and combined the
>> gomp_acc_remove_pointer code (which now iterated over mappings
>> one-at-a-time anyway) with the loop iterating over mappings in the
>> new goacc_exit_data_internal function. It was a bit nonsensical to have
>> the "exit data" code split over two files, as before.
>
> Yes, I like that very much, and we shall tackle that next intermediate
> step

> One thing:
>
>>             libgomp/
>
>>             * oacc-parallel.c (find_pointer): Remove function.
>>             (find_group_last, goacc_enter_data_internal,
>>             goacc_exit_data_internal): New functions.
>>             (GOACC_enter_exit_data): Use goacc_enter_data_internal and
>>             goacc_exit_data_internal helper functions.
>
> It makes much sense to move all that into 'libgomp/oacc-mem.c', and as a
> preparational step, see attached "[OpenACC] Consolidate
> 'GOACC_enter_exit_data' and its helper functions in
> 'libgomp/oacc-mem.c'", committed to trunk in r279233.

Working incrementally towards the goal of unifying all that mapping
handling code, I did some refactoring ("No functional changes"): see the
attached "[OpenACC] Refactor 'present_create_copy' into
'goacc_enter_data'", "[OpenACC] Refactor 'delete_copyout' into
'goacc_exit_data'", "[OpenACC] Refactor 'GOACC_enter_exit_data' to call
'goacc_enter_data', 'goacc_exit_data'", "[OpenACC] Refactor
'goacc_remove_pointer' interface", "[OpenACC] Refactor 'goacc_enter_data'
so that it can be called from 'goacc_insert_pointer', "not present"
case", "[OpenACC] Refactor 'goacc_enter_data' so that it can be called
from 'goacc_insert_pointer', "present" case, and simplify"; committed to
trunk in r279535, r279536, r279537, r279538, r279539, r279540.


Grüße
 Thomas


From ab6f9acf81772264a8564f834b9c5d1b5b70213e Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:01:51 +0000
Subject: [PATCH 1/6] [OpenACC] Refactor 'present_create_copy' into
 'goacc_enter_data'

Every caller passes in 'FLAG_PRESENT', 'FLAG_CREATE'.  Change the remaining
'FLAG_COPY' into the usual map kind.

No functional changes.

	libgomp/
	* oacc-mem.c (present_create_copy): Refactor into...
	(goacc_enter_data): ... this.  Adjust all users.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279535 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  3 +++
 libgomp/oacc-mem.c | 37 ++++++++++---------------------------
 2 files changed, 13 insertions(+), 27 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index 541a2c7610c..a5d6b51df5f 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,8 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (present_create_copy): Refactor into...
+	(goacc_enter_data): ... this.  Adjust all users.
+
 	* target.c (gomp_unmap_vars_internal): Add a safeguard to
 	'gomp_remove_var'.
 
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index 32bf3656029..68b78b3f42f 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -492,12 +492,13 @@ acc_unmap_data (void *h)
     }
 }
 
-#define FLAG_PRESENT (1 << 0)
-#define FLAG_CREATE (1 << 1)
-#define FLAG_COPY (1 << 2)
+
+/* Enter a dynamic mapping.
+
+   Return the device pointer.  */
 
 static void *
-present_create_copy (unsigned f, void *h, size_t s, int async)
+goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
 {
   void *d;
   splay_tree_key n;
@@ -530,12 +531,6 @@ present_create_copy (unsigned f, void *h, size_t s, int async)
       /* Present. */
       d = (void *) (n->tgt->tgt_start + n->tgt_offset + h - n->host_start);
 
-      if (!(f & FLAG_PRESENT))
-        {
-	  gomp_mutex_unlock (&acc_dev->lock);
-          gomp_fatal ("[%p,+%d] already mapped to [%p,+%d]",
-        	      (void *)h, (int)s, (void *)d, (int)s);
-	}
       if ((h + s) > (void *)n->host_end)
 	{
 	  gomp_mutex_unlock (&acc_dev->lock);
@@ -549,29 +544,18 @@ present_create_copy (unsigned f, void *h, size_t s, int async)
 
       gomp_mutex_unlock (&acc_dev->lock);
     }
-  else if (!(f & FLAG_CREATE))
-    {
-      gomp_mutex_unlock (&acc_dev->lock);
-      gomp_fatal ("[%p,+%d] not mapped", (void *)h, (int)s);
-    }
   else
     {
       struct target_mem_desc *tgt;
       size_t mapnum = 1;
-      unsigned short kinds;
       void *hostaddrs = h;
 
-      if (f & FLAG_COPY)
-	kinds = GOMP_MAP_TO;
-      else
-	kinds = GOMP_MAP_ALLOC;
-
       gomp_mutex_unlock (&acc_dev->lock);
 
       goacc_aq aq = get_goacc_asyncqueue (async);
 
       tgt = gomp_map_vars_async (acc_dev, aq, mapnum, &hostaddrs, NULL, &s,
-				 &kinds, true, GOMP_MAP_VARS_ENTER_DATA);
+				 &kind, true, GOMP_MAP_VARS_ENTER_DATA);
       assert (tgt);
       n = tgt->list[0].key;
       assert (n->refcount == 1);
@@ -593,13 +577,13 @@ present_create_copy (unsigned f, void *h, size_t s, int async)
 void *
 acc_create (void *h, size_t s)
 {
-  return present_create_copy (FLAG_PRESENT | FLAG_CREATE, h, s, acc_async_sync);
+  return goacc_enter_data (h, s, GOMP_MAP_ALLOC, acc_async_sync);
 }
 
 void
 acc_create_async (void *h, size_t s, int async)
 {
-  present_create_copy (FLAG_PRESENT | FLAG_CREATE, h, s, async);
+  goacc_enter_data (h, s, GOMP_MAP_ALLOC, async);
 }
 
 /* acc_present_or_create used to be what acc_create is now.  */
@@ -624,14 +608,13 @@ acc_pcreate (void *h, size_t s)
 void *
 acc_copyin (void *h, size_t s)
 {
-  return present_create_copy (FLAG_PRESENT | FLAG_CREATE | FLAG_COPY, h, s,
-			      acc_async_sync);
+  return goacc_enter_data (h, s, GOMP_MAP_TO, acc_async_sync);
 }
 
 void
 acc_copyin_async (void *h, size_t s, int async)
 {
-  present_create_copy (FLAG_PRESENT | FLAG_CREATE | FLAG_COPY, h, s, async);
+  goacc_enter_data (h, s, GOMP_MAP_TO, async);
 }
 
 /* acc_present_or_copyin used to be what acc_copyin is now.  */
-- 
2.17.1

From b78fea6a4d1e27e4f57060fd10d5fe7dc583954e Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:02:00 +0000
Subject: [PATCH 2/6] [OpenACC] Refactor 'delete_copyout' into
 'goacc_exit_data'

Change 'FLAG_COPYOUT', 'FLAG_FINALIZE' into the usual map kind.

No functional changes.

	libgomp/
	* oacc-mem.c (delete_copyout): Refactor into...
	(goacc_exit_data): ... this.  Adjust all users.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279536 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  3 +++
 libgomp/oacc-mem.c | 33 +++++++++++++++++++--------------
 2 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index a5d6b51df5f..683bb1ee283 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,8 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (delete_copyout): Refactor into...
+	(goacc_exit_data): ... this.  Adjust all users.
+
 	* oacc-mem.c (present_create_copy): Refactor into...
 	(goacc_enter_data): ... this.  Adjust all users.
 
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index 68b78b3f42f..76356ce9280 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -636,15 +636,17 @@ acc_pcopyin (void *h, size_t s)
 }
 #endif
 
-#define FLAG_COPYOUT  (1 << 0)
-#define FLAG_FINALIZE (1 << 1)
+
+/* Exit a dynamic mapping.  */
 
 static void
-delete_copyout (unsigned f, void *h, size_t s, int async, const char *libfnname)
+goacc_exit_data (void *h, size_t s, unsigned short kind, int async)
 {
   /* No need to call lazy open, as the data must already have been
      mapped.  */
 
+  kind &= 0xff;
+
   struct goacc_thread *thr = goacc_thread ();
   struct gomp_device_descr *acc_dev = thr->dev;
 
@@ -683,7 +685,9 @@ delete_copyout (unsigned f, void *h, size_t s, int async, const char *libfnname)
       gomp_fatal ("Dynamic reference counting assert fail\n");
     }
 
-  if (f & FLAG_FINALIZE)
+  bool finalize = (kind == GOMP_MAP_DELETE
+		   || kind == GOMP_MAP_FORCE_FROM);
+  if (finalize)
     {
       if (n->refcount != REFCOUNT_INFINITY)
 	n->refcount -= n->dynamic_refcount;
@@ -700,7 +704,9 @@ delete_copyout (unsigned f, void *h, size_t s, int async, const char *libfnname)
     {
       goacc_aq aq = get_goacc_asyncqueue (async);
 
-      if (f & FLAG_COPYOUT)
+      bool copyout = (kind == GOMP_MAP_FROM
+		      || kind == GOMP_MAP_FORCE_FROM);
+      if (copyout)
 	{
 	  void *d = (void *) (n->tgt->tgt_start + n->tgt_offset
 			      + (uintptr_t) h - n->host_start);
@@ -733,50 +739,49 @@ delete_copyout (unsigned f, void *h, size_t s, int async, const char *libfnname)
 void
 acc_delete (void *h , size_t s)
 {
-  delete_copyout (0, h, s, acc_async_sync, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_RELEASE, acc_async_sync);
 }
 
 void
 acc_delete_async (void *h , size_t s, int async)
 {
-  delete_copyout (0, h, s, async, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_RELEASE, async);
 }
 
 void
 acc_delete_finalize (void *h , size_t s)
 {
-  delete_copyout (FLAG_FINALIZE, h, s, acc_async_sync, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_DELETE, acc_async_sync);
 }
 
 void
 acc_delete_finalize_async (void *h , size_t s, int async)
 {
-  delete_copyout (FLAG_FINALIZE, h, s, async, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_DELETE, async);
 }
 
 void
 acc_copyout (void *h, size_t s)
 {
-  delete_copyout (FLAG_COPYOUT, h, s, acc_async_sync, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_FROM, acc_async_sync);
 }
 
 void
 acc_copyout_async (void *h, size_t s, int async)
 {
-  delete_copyout (FLAG_COPYOUT, h, s, async, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_FROM, async);
 }
 
 void
 acc_copyout_finalize (void *h, size_t s)
 {
-  delete_copyout (FLAG_COPYOUT | FLAG_FINALIZE, h, s, acc_async_sync,
-		  __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_FORCE_FROM, acc_async_sync);
 }
 
 void
 acc_copyout_finalize_async (void *h, size_t s, int async)
 {
-  delete_copyout (FLAG_COPYOUT | FLAG_FINALIZE, h, s, async, __FUNCTION__);
+  goacc_exit_data (h, s, GOMP_MAP_FORCE_FROM, async);
 }
 
 static void
-- 
2.17.1

From b85b17075417eefc5cf68ca1558056794e9f2a4a Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:02:10 +0000
Subject: [PATCH 3/6] [OpenACC] Refactor 'GOACC_enter_exit_data' to call
 'goacc_enter_data', 'goacc_exit_data'

No functional changes.

	libgomp/
	* oacc-mem.c (GOACC_enter_exit_data): Refactor code to call
	'goacc_enter_data', 'goacc_exit_data'.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279537 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  3 +++
 libgomp/oacc-mem.c | 24 +++++++-----------------
 2 files changed, 10 insertions(+), 17 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index 683bb1ee283..9bb22e932ec 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,8 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (GOACC_enter_exit_data): Refactor code to call
+	'goacc_enter_data', 'goacc_exit_data'.
+
 	* oacc-mem.c (delete_copyout): Refactor into...
 	(goacc_exit_data): ... this.  Adjust all users.
 
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index 76356ce9280..6509bd78821 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -1158,28 +1158,26 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
     {
       for (i = 0; i < mapnum; i++)
 	{
-	  unsigned char kind = kinds[i] & 0xff;
-
 	  /* Scan for pointers and PSETs.  */
 	  int pointer = find_pointer (i, mapnum, kinds);
 
 	  if (!pointer)
 	    {
+	      unsigned char kind = kinds[i] & 0xff;
 	      switch (kind)
 		{
 		case GOMP_MAP_ALLOC:
 		case GOMP_MAP_FORCE_ALLOC:
-		  acc_create_async (hostaddrs[i], sizes[i], async);
-		  break;
 		case GOMP_MAP_TO:
 		case GOMP_MAP_FORCE_TO:
-		  acc_copyin_async (hostaddrs[i], sizes[i], async);
 		  break;
 		default:
 		  gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
 			      kind);
 		  break;
 		}
+
+	      goacc_enter_data (hostaddrs[i], sizes[i], kinds[i], async);
 	    }
 	  else
 	    {
@@ -1198,9 +1196,6 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
       {
 	unsigned char kind = kinds[i] & 0xff;
 
-	bool finalize = (kind == GOMP_MAP_DELETE
-			 || kind == GOMP_MAP_FORCE_FROM);
-
 	int pointer = find_pointer (i, mapnum, kinds);
 
 	if (!pointer)
@@ -1209,26 +1204,21 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
 	      {
 	      case GOMP_MAP_RELEASE:
 	      case GOMP_MAP_DELETE:
-		if (finalize)
-		  acc_delete_finalize_async (hostaddrs[i], sizes[i], async);
-		else
-		  acc_delete_async (hostaddrs[i], sizes[i], async);
-		break;
 	      case GOMP_MAP_FROM:
 	      case GOMP_MAP_FORCE_FROM:
-		if (finalize)
-		  acc_copyout_finalize_async (hostaddrs[i], sizes[i], async);
-		else
-		  acc_copyout_async (hostaddrs[i], sizes[i], async);
 		break;
 	      default:
 		gomp_fatal (">>>> GOACC_enter_exit_data UNHANDLED kind 0x%.2x",
 			    kind);
 		break;
 	      }
+
+	    goacc_exit_data (hostaddrs[i], sizes[i], kinds[i], async);
 	  }
 	else
 	  {
+	    bool finalize = (kind == GOMP_MAP_DELETE
+			     || kind == GOMP_MAP_FORCE_FROM);
 	    bool copyfrom = (kind == GOMP_MAP_FORCE_FROM
 			     || kind == GOMP_MAP_FROM);
 	    goacc_remove_pointer (hostaddrs[i], sizes[i], copyfrom, async,
-- 
2.17.1

From c2378cdf6075f9b5cdfc7b6530e3a1e38b94dc85 Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:02:18 +0000
Subject: [PATCH 4/6] [OpenACC] Refactor 'goacc_remove_pointer' interface

No functional changes.

	libgomp/
	* oacc-mem.c (goacc_remove_pointer): Refactor interface.  Adjust
	all users.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279538 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  3 +++
 libgomp/oacc-mem.c | 26 ++++++++++++--------------
 2 files changed, 15 insertions(+), 14 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index 9bb22e932ec..19381999281 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,8 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (goacc_remove_pointer): Refactor interface.  Adjust
+	all users.
+
 	* oacc-mem.c (GOACC_enter_exit_data): Refactor code to call
 	'goacc_enter_data', 'goacc_exit_data'.
 
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index 6509bd78821..d08eb8b4b4f 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -873,8 +873,8 @@ acc_update_self_async (void *h, size_t s, int async)
 /* Special handling for 'GOMP_MAP_POINTER', 'GOMP_MAP_TO_PSET'.
 
    Only the first mapping is considered in reference counting; the following
-   ones implicitly follow suit.  Similarly, 'copyout' ('force_copyfrom') is
-   done only for the first mapping.  */
+   ones implicitly follow suit.  Similarly, 'copyout' is done only for the
+   first mapping.  */
 
 static void
 goacc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
@@ -925,9 +925,10 @@ goacc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
 }
 
 static void
-goacc_remove_pointer (void *h, size_t s, bool force_copyfrom, int async,
-		      int finalize)
+goacc_remove_pointer (void *h, size_t s, unsigned short kind, int async)
 {
+  kind &= 0xff;
+
   struct goacc_thread *thr = goacc_thread ();
   struct gomp_device_descr *acc_dev = thr->dev;
   splay_tree_key n;
@@ -958,6 +959,8 @@ goacc_remove_pointer (void *h, size_t s, bool force_copyfrom, int async,
       gomp_fatal ("Dynamic reference counting assert fail\n");
     }
 
+  bool finalize = (kind == GOMP_MAP_DELETE
+		   || kind == GOMP_MAP_FORCE_FROM);
   if (finalize)
     {
       n->refcount -= n->dynamic_refcount;
@@ -973,11 +976,12 @@ goacc_remove_pointer (void *h, size_t s, bool force_copyfrom, int async,
     {
       goacc_aq aq = get_goacc_asyncqueue (async);
 
-      if (force_copyfrom)
+      bool copyout = (kind == GOMP_MAP_FROM
+		      || kind == GOMP_MAP_FORCE_FROM);
+      if (copyout)
 	{
 	  void *d = (void *) (t->tgt_start + n->tgt_offset
 			      + (uintptr_t) h - n->host_start);
-
 	  gomp_copy_dev2host (acc_dev, aq, h, d, s);
 	}
 
@@ -1194,12 +1198,11 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
   else
     for (i = 0; i < mapnum; ++i)
       {
-	unsigned char kind = kinds[i] & 0xff;
-
 	int pointer = find_pointer (i, mapnum, kinds);
 
 	if (!pointer)
 	  {
+	    unsigned char kind = kinds[i] & 0xff;
 	    switch (kind)
 	      {
 	      case GOMP_MAP_RELEASE:
@@ -1217,12 +1220,7 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
 	  }
 	else
 	  {
-	    bool finalize = (kind == GOMP_MAP_DELETE
-			     || kind == GOMP_MAP_FORCE_FROM);
-	    bool copyfrom = (kind == GOMP_MAP_FORCE_FROM
-			     || kind == GOMP_MAP_FROM);
-	    goacc_remove_pointer (hostaddrs[i], sizes[i], copyfrom, async,
-				  finalize);
+	    goacc_remove_pointer (hostaddrs[i], sizes[i], kinds[i], async);
 	    /* See the above comment.  */
 	    i += pointer - 1;
 	  }
-- 
2.17.1

From 928cc6c2d946fa112934209a1cf105050cdf01e1 Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:02:27 +0000
Subject: [PATCH 5/6] [OpenACC] Refactor 'goacc_enter_data' so that it can be
 called from 'goacc_insert_pointer', "not present" case

No functional changes.

	libgomp/
	* oacc-mem.c (goacc_enter_data): Refactor, so that it can be
	called...
	(goacc_insert_pointer): ... from here, "not present" case.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279539 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  4 +++
 libgomp/oacc-mem.c | 65 ++++++++++++++++++++++++++--------------------
 2 files changed, 41 insertions(+), 28 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index 19381999281..7cf85682e7b 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,9 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (goacc_enter_data): Refactor, so that it can be
+	called...
+	(goacc_insert_pointer): ... from here, "not present" case.
+
 	* oacc-mem.c (goacc_remove_pointer): Refactor interface.  Adjust
 	all users.
 
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index d08eb8b4b4f..3806e2d69a0 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -493,18 +493,26 @@ acc_unmap_data (void *h)
 }
 
 
-/* Enter a dynamic mapping.
+/* Enter dynamic mappings.
 
-   Return the device pointer.  */
+   The handling for MAPNUM bigger than one is special handling for
+   'GOMP_MAP_POINTER', 'GOMP_MAP_TO_PSET'.  For these, only the first mapping
+   is considered in reference counting; the following ones implicitly follow
+   suit.
+
+   If there's just one mapping, return the device pointer.  */
 
 static void *
-goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
+goacc_enter_data (size_t mapnum, void **hostaddrs, size_t *sizes, void *kinds,
+		  int async)
 {
   void *d;
   splay_tree_key n;
 
-  if (!h || !s)
-    gomp_fatal ("[%p,+%d] is a bad range", (void *)h, (int)s);
+  assert (mapnum > 0);
+  if (mapnum == 1
+      && (!hostaddrs[0] || !sizes[0]))
+    gomp_fatal ("[%p,+%d] is a bad range", hostaddrs[0], (int) sizes[0]);
 
   goacc_lazy_initialize ();
 
@@ -512,7 +520,12 @@ goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
   struct gomp_device_descr *acc_dev = thr->dev;
 
   if (acc_dev->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
-    return h;
+    {
+      if (mapnum == 1)
+	return hostaddrs[0];
+      else
+	return /* n/a */ (void *) -1;
+    }
 
   acc_prof_info prof_info;
   acc_api_info api_info;
@@ -525,9 +538,13 @@ goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
 
   gomp_mutex_lock (&acc_dev->lock);
 
-  n = lookup_host (acc_dev, h, s);
+  n = lookup_host (acc_dev, hostaddrs[0], sizes[0]);
   if (n)
     {
+      assert (mapnum == 1);
+      void *h = hostaddrs[0];
+      size_t s = sizes[0];
+
       /* Present. */
       d = (void *) (n->tgt->tgt_start + n->tgt_offset + h - n->host_start);
 
@@ -546,16 +563,13 @@ goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
     }
   else
     {
-      struct target_mem_desc *tgt;
-      size_t mapnum = 1;
-      void *hostaddrs = h;
-
       gomp_mutex_unlock (&acc_dev->lock);
 
       goacc_aq aq = get_goacc_asyncqueue (async);
 
-      tgt = gomp_map_vars_async (acc_dev, aq, mapnum, &hostaddrs, NULL, &s,
-				 &kind, true, GOMP_MAP_VARS_ENTER_DATA);
+      struct target_mem_desc *tgt
+	= gomp_map_vars_async (acc_dev, aq, mapnum, hostaddrs, NULL, sizes,
+			       kinds, true, GOMP_MAP_VARS_ENTER_DATA);
       assert (tgt);
       n = tgt->list[0].key;
       assert (n->refcount == 1);
@@ -577,13 +591,15 @@ goacc_enter_data (void *h, size_t s, unsigned short kind, int async)
 void *
 acc_create (void *h, size_t s)
 {
-  return goacc_enter_data (h, s, GOMP_MAP_ALLOC, acc_async_sync);
+  unsigned short kinds[1] = { GOMP_MAP_ALLOC };
+  return goacc_enter_data (1, &h, &s, &kinds, acc_async_sync);
 }
 
 void
 acc_create_async (void *h, size_t s, int async)
 {
-  goacc_enter_data (h, s, GOMP_MAP_ALLOC, async);
+  unsigned short kinds[1] = { GOMP_MAP_ALLOC };
+  goacc_enter_data (1, &h, &s, &kinds, async);
 }
 
 /* acc_present_or_create used to be what acc_create is now.  */
@@ -608,13 +624,15 @@ acc_pcreate (void *h, size_t s)
 void *
 acc_copyin (void *h, size_t s)
 {
-  return goacc_enter_data (h, s, GOMP_MAP_TO, acc_async_sync);
+  unsigned short kinds[1] = { GOMP_MAP_TO };
+  return goacc_enter_data (1, &h, &s, &kinds, acc_async_sync);
 }
 
 void
 acc_copyin_async (void *h, size_t s, int async)
 {
-  goacc_enter_data (h, s, GOMP_MAP_TO, async);
+  unsigned short kinds[1] = { GOMP_MAP_TO };
+  goacc_enter_data (1, &h, &s, &kinds, async);
 }
 
 /* acc_present_or_copyin used to be what acc_copyin is now.  */
@@ -912,16 +930,7 @@ goacc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
       gomp_fatal ("Dynamic refcount incrementing failed for pointer/pset");
     }
 
-  gomp_debug (0, "  %s: prepare mappings\n", __FUNCTION__);
-  goacc_aq aq = get_goacc_asyncqueue (async);
-  tgt = gomp_map_vars_async (acc_dev, aq, mapnum, hostaddrs,
-			     NULL, sizes, kinds, true, GOMP_MAP_VARS_ENTER_DATA);
-  assert (tgt);
-  splay_tree_key n = tgt->list[0].key;
-  assert (n->refcount == 1);
-  assert (n->dynamic_refcount == 0);
-  n->dynamic_refcount++;
-  gomp_debug (0, "  %s: mappings prepared\n", __FUNCTION__);
+  goacc_enter_data (mapnum, hostaddrs, sizes, kinds, async);
 }
 
 static void
@@ -1181,7 +1190,7 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
 		  break;
 		}
 
-	      goacc_enter_data (hostaddrs[i], sizes[i], kinds[i], async);
+	      goacc_enter_data (1, &hostaddrs[i], &sizes[i], &kinds[i], async);
 	    }
 	  else
 	    {
-- 
2.17.1

From 61d76017df60eed3142ef43a415a0ec0757a1da0 Mon Sep 17 00:00:00 2001
From: tschwinge <tschwinge@138bc75d-0d04-0410-961f-82ee72b054a4>
Date: Wed, 18 Dec 2019 17:02:37 +0000
Subject: [PATCH 6/6] [OpenACC] Refactor 'goacc_enter_data' so that it can be
 called from 'goacc_insert_pointer', "present" case, and simplify

No functional changes.

	libgomp/
	* oacc-mem.c (goacc_enter_data): Refactor, so that it can be
	called...
	(goacc_insert_pointer): ... from here, "present" case.
	(goacc_insert_pointer): Inline function into...
	(GOACC_enter_exit_data): ... here, and simplify.

git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/trunk@279540 138bc75d-0d04-0410-961f-82ee72b054a4
---
 libgomp/ChangeLog  |  6 +++
 libgomp/oacc-mem.c | 92 ++++++++++++++++++++--------------------------
 2 files changed, 46 insertions(+), 52 deletions(-)

diff --git a/libgomp/ChangeLog b/libgomp/ChangeLog
index 7cf85682e7b..779f276ca99 100644
--- a/libgomp/ChangeLog
+++ b/libgomp/ChangeLog
@@ -1,5 +1,11 @@
 2019-12-18  Thomas Schwinge  <tho...@codesourcery.com>
 
+	* oacc-mem.c (goacc_enter_data): Refactor, so that it can be
+	called...
+	(goacc_insert_pointer): ... from here, "present" case.
+	(goacc_insert_pointer): Inline function into...
+	(GOACC_enter_exit_data): ... here, and simplify.
+
 	* oacc-mem.c (goacc_enter_data): Refactor, so that it can be
 	called...
 	(goacc_insert_pointer): ... from here, "not present" case.
diff --git a/libgomp/oacc-mem.c b/libgomp/oacc-mem.c
index 3806e2d69a0..15eb17b846e 100644
--- a/libgomp/oacc-mem.c
+++ b/libgomp/oacc-mem.c
@@ -513,6 +513,9 @@ goacc_enter_data (size_t mapnum, void **hostaddrs, size_t *sizes, void *kinds,
   if (mapnum == 1
       && (!hostaddrs[0] || !sizes[0]))
     gomp_fatal ("[%p,+%d] is a bad range", hostaddrs[0], (int) sizes[0]);
+  else if (mapnum > 1
+	   && !hostaddrs[0])
+    return /* n/a */ (void *) -1;
 
   goacc_lazy_initialize ();
 
@@ -539,9 +542,8 @@ goacc_enter_data (size_t mapnum, void **hostaddrs, size_t *sizes, void *kinds,
   gomp_mutex_lock (&acc_dev->lock);
 
   n = lookup_host (acc_dev, hostaddrs[0], sizes[0]);
-  if (n)
+  if (n && mapnum == 1)
     {
-      assert (mapnum == 1);
       void *h = hostaddrs[0];
       size_t s = sizes[0];
 
@@ -561,6 +563,32 @@ goacc_enter_data (size_t mapnum, void **hostaddrs, size_t *sizes, void *kinds,
 
       gomp_mutex_unlock (&acc_dev->lock);
     }
+  else if (n && mapnum > 1)
+    {
+      d = /* n/a */ (void *) -1;
+
+      assert (n->refcount != REFCOUNT_INFINITY
+	      && n->refcount != REFCOUNT_LINK);
+
+      bool processed = false;
+
+      struct target_mem_desc *tgt = n->tgt;
+      for (size_t i = 0; i < tgt->list_count; i++)
+	if (tgt->list[i].key == n)
+	  {
+	    for (size_t j = 0; j < mapnum; j++)
+	      if (i + j < tgt->list_count && tgt->list[i + j].key)
+		{
+		  tgt->list[i + j].key->refcount++;
+		  tgt->list[i + j].key->dynamic_refcount++;
+		}
+	    processed = true;
+	  }
+
+      gomp_mutex_unlock (&acc_dev->lock);
+      if (!processed)
+	gomp_fatal ("dynamic refcount incrementing failed for pointer/pset");
+    }
   else
     {
       gomp_mutex_unlock (&acc_dev->lock);
@@ -894,45 +922,6 @@ acc_update_self_async (void *h, size_t s, int async)
    ones implicitly follow suit.  Similarly, 'copyout' is done only for the
    first mapping.  */
 
-static void
-goacc_insert_pointer (size_t mapnum, void **hostaddrs, size_t *sizes,
-		      void *kinds, int async)
-{
-  struct target_mem_desc *tgt;
-  struct goacc_thread *thr = goacc_thread ();
-  struct gomp_device_descr *acc_dev = thr->dev;
-
-  if (*hostaddrs == NULL)
-    return;
-
-  if (acc_is_present (*hostaddrs, *sizes))
-    {
-      splay_tree_key n;
-      gomp_mutex_lock (&acc_dev->lock);
-      n = lookup_host (acc_dev, *hostaddrs, *sizes);
-      assert (n->refcount != REFCOUNT_INFINITY
-	      && n->refcount != REFCOUNT_LINK);
-      gomp_mutex_unlock (&acc_dev->lock);
-
-      tgt = n->tgt;
-      for (size_t i = 0; i < tgt->list_count; i++)
-	if (tgt->list[i].key == n)
-	  {
-	    for (size_t j = 0; j < mapnum; j++)
-	      if (i + j < tgt->list_count && tgt->list[i + j].key)
-		{
-		  tgt->list[i + j].key->refcount++;
-		  tgt->list[i + j].key->dynamic_refcount++;
-		}
-	    return;
-	  }
-      /* Should not reach here.  */
-      gomp_fatal ("Dynamic refcount incrementing failed for pointer/pset");
-    }
-
-  goacc_enter_data (mapnum, hostaddrs, sizes, kinds, async);
-}
-
 static void
 goacc_remove_pointer (void *h, size_t s, unsigned short kind, int async)
 {
@@ -1190,18 +1179,17 @@ GOACC_enter_exit_data (int flags_m, size_t mapnum, void **hostaddrs,
 		  break;
 		}
 
-	      goacc_enter_data (1, &hostaddrs[i], &sizes[i], &kinds[i], async);
-	    }
-	  else
-	    {
-	      goacc_insert_pointer (pointer, &hostaddrs[i], &sizes[i], &kinds[i],
-				    async);
-	      /* Increment 'i' by two because OpenACC requires fortran
-		 arrays to be contiguous, so each PSET is associated with
-		 one of MAP_FORCE_ALLOC/MAP_FORCE_PRESET/MAP_FORCE_TO, and
-		 one MAP_POINTER.  */
-	      i += pointer - 1;
+	      /* We actually have one mapping.  */
+	      pointer = 1;
 	    }
+
+	  goacc_enter_data (pointer, &hostaddrs[i], &sizes[i], &kinds[i],
+			    async);
+	  /* If applicable, increment 'i' further; OpenACC requires fortran
+	     arrays to be contiguous, so each PSET is associated with
+	     one of MAP_FORCE_ALLOC/MAP_FORCE_PRESET/MAP_FORCE_TO, and
+	     one MAP_POINTER.  */
+	  i += pointer - 1;
 	}
     }
   else
-- 
2.17.1

Attachment: signature.asc
Description: PGP signature

Reply via email to