https://gcc.gnu.org/g:48b4aae5c811190aa3e33ea2adcb4f9fcc44aeae

commit r17-6683-g48b4aae5c811190aa3e33ea2adcb4f9fcc44aeae
Author: Tobias Burnus <[email protected]>
Date:   Wed Nov 25 11:37:42 2026 +0100

    libgomp: Fix OMP_TARGET_OFFLOAD=mandatory with nested ICVs
    
    OMP_TARGET_OFFLOAD=mandatory sets the default-device-icv to INT_MIN
    such that the default-device-icv can either be set to the first
    offload device (i.e. '0') or if no such no-host device exists to
    omp_invalid_device.
    
    However, when the first device call happens inside a nested ICV,
    the initialization via gomp_init_targets_once did not update that
    ICV. Do this now in the omp_get_default_device routine and in the
    gomp_get_default_device function, called during device resolution.
    
    libgomp/ChangeLog:
    
            * icv-device.c (omp_get_default_device): Handle a nonglobal
            ICV with uninitialized default-device-var ICV.
            * target.c (gomp_get_default_device): Likewise.
            (gomp_managed_alloc, gomp_managed_free): Call
            omp_get_default_device instead of getting it from the ICV.
            (gomp_target_init): For !PLUGIN_SUPPORT, set default-device
            ICV to omp_invalid_device for mandatory (i.e. MIN_INT).
            * testsuite/libgomp.c/target-56.c: New test.
            * testsuite/libgomp.c/target-57.c: New test.
            * testsuite/libgomp.c/target-57a.c: New test.

Diff:
---
 libgomp/icv-device.c                     |  9 +++++++--
 libgomp/target.c                         | 24 ++++++++++++++++++------
 libgomp/testsuite/libgomp.c/target-56.c  | 29 +++++++++++++++++++++++++++++
 libgomp/testsuite/libgomp.c/target-57.c  | 30 ++++++++++++++++++++++++++++++
 libgomp/testsuite/libgomp.c/target-57a.c | 31 +++++++++++++++++++++++++++++++
 5 files changed, 115 insertions(+), 8 deletions(-)

diff --git a/libgomp/icv-device.c b/libgomp/icv-device.c
index 07ab0c239232..98445951fb4d 100644
--- a/libgomp/icv-device.c
+++ b/libgomp/icv-device.c
@@ -46,8 +46,13 @@ omp_get_default_device (void)
 {
   struct gomp_task_icv *icv = gomp_icv (false);
   if (icv->default_device_var == INT_MIN)
-    /* This implies OMP_TARGET_OFFLOAD=mandatory.  */
-    gomp_init_targets_once ();
+    {
+      /* This implies OMP_TARGET_OFFLOAD=mandatory.  */
+      gomp_init_targets_once ();
+      /* This can happen when a nested ICV has been created before
+        any device has been initialized.  */
+      icv->default_device_var = gomp_global_icv.default_device_var;
+    }
   return icv->default_device_var;
 }
 
diff --git a/libgomp/target.c b/libgomp/target.c
index 2bd1d573de45..a4293b147908 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -144,6 +144,12 @@ gomp_get_default_device ()
 {
   gomp_init_targets_once ();
   struct gomp_task_icv *icv = gomp_icv (false);
+  /* gomp_init_targets_once sets the global default-device-var ICV, when
+     target-offload-var is 'mandatory'. However, it fails to set any nested
+     ICV. Thus, do it here as fallback.  */
+  if (icv->default_device_var == INT_MIN
+      && gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY)
+    icv->default_device_var = gomp_global_icv.default_device_var;
   return icv->default_device_var;
 }
 
@@ -5109,9 +5115,8 @@ omp_target_free (void *device_ptr, int device_num)
 void *
 gomp_managed_alloc (size_t size)
 {
-  struct gomp_task_icv *icv = gomp_icv (false);
-  struct gomp_device_descr *devicep = resolve_device (icv->default_device_var,
-                                                     false);
+  struct gomp_device_descr *devicep
+    = resolve_device (gomp_get_default_device (), false);
   if (devicep == NULL)
     return NULL;
 
@@ -5129,9 +5134,8 @@ gomp_managed_free (void *device_ptr)
   if (device_ptr == NULL)
     return;
 
-  struct gomp_task_icv *icv = gomp_icv (false);
-  struct gomp_device_descr *devicep = resolve_device (icv->default_device_var,
-                                                     false);
+  struct gomp_device_descr *devicep
+    = resolve_device (gomp_get_default_device (), false);
   if (devicep == NULL)
     gomp_fatal ("attempted to free managed memory at %p, but the default "
                "device is set to the host device", device_ptr);
@@ -6956,5 +6960,13 @@ gomp_target_init (void)
 static void
 gomp_target_init (void)
 {
+  /* This implies OMP_TARGET_OFFLOAD=mandatory.  */
+  if (gomp_global_icv.default_device_var == INT_MIN)
+    {
+       struct gomp_icv_list *none;
+       none = gomp_get_initial_icv_item (GOMP_DEVICE_NUM_FOR_NO_SUFFIX);
+       gomp_global_icv.default_device_var = omp_invalid_device;
+       none->icvs.default_device_var = omp_invalid_device;
+    }
 }
 #endif /* PLUGIN_SUPPORT */
diff --git a/libgomp/testsuite/libgomp.c/target-56.c 
b/libgomp/testsuite/libgomp.c/target-56.c
new file mode 100644
index 000000000000..8d1f107a4443
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-56.c
@@ -0,0 +1,29 @@
+/* Only run this with available non-host devices; in that case, GCC sets
+   the default-device-var to 0.  */
+
+/* { dg-do run { target { offload_device } } } */
+/* { dg-set-target-env-var OMP_TARGET_OFFLOAD "mandatory" } */
+
+/* Was failing before because of the created data environment ICV,
+   the resolution of the ICV value was not visible when the target region
+   was called.  Due to mandatory, it then wrongly claims that no offload
+   device was available.  */
+
+#include <omp.h>
+
+int
+main ()
+{
+  bool host_flag;
+  #pragma omp task shared(host_flag)
+    #pragma omp target map(from: host_flag)
+      host_flag = omp_is_initial_device();
+  #pragma omp taskwait
+
+  /* Due target offload_device, this must be the case;
+     the actual check is that this line is reached at
+     all without aborting due to not resolved 'mandatory'.  */
+  if (host_flag == true)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/target-57.c 
b/libgomp/testsuite/libgomp.c/target-57.c
new file mode 100644
index 000000000000..8cba2062bfe6
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-57.c
@@ -0,0 +1,30 @@
+/* Only run this with available non-host devices; in that case, GCC sets
+   the default-device-var to 0.  */
+
+/* { dg-do run { target { offload_device } } } */
+/* { dg-set-target-env-var OMP_TARGET_OFFLOAD "mandatory" } */
+
+/* Was failing before because of the created data environment ICV,
+   the resolution of the ICV value was not visible when the target region
+   was called.  Due to mandatory, it then wrongly claims that no offload
+   device was available.  */
+
+#include <omp.h>
+
+int
+main ()
+{
+  /* Due 'target offload_device' a device is available, hence,
+     it will return a valid device number - in GCC it is always
+     0 unless otherwise overridden.
+     The following uses a nested ICV due to 'task'.  */
+  #pragma omp task
+    if (omp_get_default_device() != 0)
+    {
+      __builtin_printf ("default-device: %d\n", omp_get_default_device());
+      __builtin_abort ();
+    }
+  #pragma omp taskwait
+
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/target-57a.c 
b/libgomp/testsuite/libgomp.c/target-57a.c
new file mode 100644
index 000000000000..bbba80775bbf
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-57a.c
@@ -0,0 +1,31 @@
+/* Only run this with when there is only the host; in that case, GCC shall
+   set the default-device-var to omp_invalid_device.  */
+
+/* { dg-do run { target { ! offload_device } } } */
+/* { dg-set-target-env-var OMP_TARGET_OFFLOAD "mandatory" } */
+
+/* Was failing before because of the created data environment ICV,
+   the resolution of the ICV value was not visible when the target region
+   was called.  Due to mandatory, it then wrongly claims that no offload
+   device was available.  */
+
+#include <omp.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+  /* No device available. This means 'omp_invalid_device' if the
+     env var was successfullly set - and otherwise '0' as default.  */
+  const char *env = getenv ("OMP_TARGET_OFFLOAD");
+  #pragma omp task
+    if ((env != NULL && omp_get_default_device() != omp_invalid_device)
+       || (env == NULL && omp_get_default_device() != 0))
+      {
+        __builtin_printf ("default-device: %d, env: %s\n", 
omp_get_default_device(), env);
+        __builtin_abort ();
+      }
+  #pragma omp taskwait
+
+  return 0;
+}

Reply via email to