Hi Tobias, Arsen,

The handling of iterator maps in libgomp is mostly inside gomp_map_vars*, where 
the header is processed and the correct data addresses are created and returned.

For host-fallback and shared-memory device cases, due to gomp_map_vars() 
skipped and not called there, we need to add in such handling before the 
child/task function is called (similar to the firstprivate copying we have 
right now)

Tested on x86_64-linux + nvptx offloading without regressions.
Okay for mainline?

This patch was assisted-by Opus 5.5

Thanks,
Chung-Lin

libgomp/ChangeLog

        * target.c (adjust_iterator_maps): New function.
        (GOMP_target_ext, gomp_target_task_fn): Call it before host
        fallback and before passing HOSTADDRS to a shared-memory device.
        * testsuite/libgomp.c-c++-common/target-map-iterators-10.c: New 
testcase.

diff --git a/libgomp/target.c b/libgomp/target.c
index 2bd1d573de4..779cf554f30 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -3229,6 +3229,18 @@ gomp_unload_device (struct gomp_device_descr *devicep)
     }
 }
 
+/* Replace the address of each iterator map (SIZES[I] == SIZE_MAX) in
+   HOSTADDRS by that of its first element, as gomp_map_vars would.  */
+
+static void
+adjust_iterator_maps (size_t mapnum, void **hostaddrs, size_t *sizes)
+{
+  for (size_t i = 0; i < mapnum; i++)
+    if (sizes[i] == SIZE_MAX)
+      hostaddrs[i] = (*(uintptr_t *) hostaddrs[i]
+                     ? ((void **) hostaddrs[i])[1] : NULL);
+}
+
 /* Host fallback for GOMP_target{,_ext} routines.  */
 
 static void
@@ -3743,6 +3755,7 @@ GOMP_target_ext (int device, void (*fn) (void *), size_t 
mapnum,
                                      tgt_align, tgt_size);
            }
        }
+      adjust_iterator_maps (mapnum, hostaddrs, sizes);
       gomp_target_fallback (fn, hostaddrs, devicep, new_args);
       return;
     }
@@ -3795,6 +3808,7 @@ GOMP_target_ext (int device, void (*fn) (void *), size_t 
mapnum,
            }
        }
       tgt_vars = NULL;
+      adjust_iterator_maps (mapnum, hostaddrs, sizes);
       devicep->session.set_tvt_func (session, hostaddrs);
     }
   else
@@ -4941,6 +4955,7 @@ gomp_target_task_fn (void *data)
          || (devicep->can_run_func && !devicep->can_run_func (fn_addr)))
        {
          ttask->state = GOMP_TARGET_TASK_FALLBACK;
+         adjust_iterator_maps (ttask->mapnum, ttask->hostaddrs, ttask->sizes);
          gomp_target_fallback (ttask->fn, ttask->hostaddrs, devicep,
                                ttask->args);
          return false;
@@ -4959,6 +4974,7 @@ gomp_target_task_fn (void *data)
       if (devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)
        {
          ttask->tgt = NULL;
+         adjust_iterator_maps (ttask->mapnum, ttask->hostaddrs, ttask->sizes);
          devicep->session.set_tvt_func (session, ttask->hostaddrs);
        }
       else
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-map-iterators-10.c 
b/libgomp/testsuite/libgomp.c-c++-common/target-map-iterators-10.c
new file mode 100644
index 00000000000..9cf70b820c0
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c-c++-common/target-map-iterators-10.c
@@ -0,0 +1,78 @@
+/* { dg-do run } */
+
+/* Host fallback of 'target' with an iterator map: the region must see the
+   mapped elements.  'if (0)' forces host fallback on every configuration;
+   the region in F also covers the deferred (nowait) target task.  */
+
+#include <stdlib.h>
+
+#define DIM1 4
+#define DIM2 3
+
+__attribute__((noipa)) int
+f (int **x)
+{
+  int y;
+  #pragma omp target if (0) nowait map(iterator(i=0:DIM1), to: x[i][:DIM2]) \
+                    map(from: y)
+  {
+    y = 0;
+    for (int i = 0; i < DIM1; i++)
+      y += x[i][1];
+  }
+  #pragma omp taskwait
+  return y;
+}
+
+int
+main (void)
+{
+  int *x[DIM1], data[DIM1][DIM2];
+  int y;
+
+  for (int i = 0; i < DIM1; i++)
+    {
+      x[i] = data[i];
+      for (int j = 0; j < DIM2; j++)
+       data[i][j] = i * 10 + j;
+    }
+
+  /* All elements.  */
+  #pragma omp target if (0) map(iterator(i=0:DIM1), to: x[i][:DIM2]) \
+                    map(from: y)
+  {
+    y = 0;
+    for (int i = 0; i < DIM1; i++)
+      for (int j = 0; j < DIM2; j++)
+       y += x[i][j];
+  }
+  if (y != 4 * (0 + 1 + 2) + 3 * (0 + 10 + 20 + 30))
+    abort ();
+
+  /* Every other element, written back.  */
+  #pragma omp target if (0) map(iterator(i=0:DIM1:2), tofrom: x[i][:DIM2])
+  {
+    x[0][0] += 100;
+    x[2][0] += 100;
+  }
+  if (data[0][0] != 100 || data[2][0] != 120 || data[1][0] != 10)
+    abort ();
+
+  /* Control: the same without an iterator.  */
+  #pragma omp target if (0) map(tofrom: x[0][:DIM2], x[2][:DIM2])
+  {
+    x[0][0] += 100;
+    x[2][0] += 100;
+  }
+  if (data[0][0] != 200 || data[2][0] != 220)
+    abort ();
+
+  /* Deferred target task (the parallel region gives it a team).  */
+  #pragma omp parallel num_threads (2)
+  #pragma omp single
+  y = f (x);
+  if (y != 1 + 11 + 21 + 31)
+    abort ();
+
+  return 0;
+}

Reply via email to