public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/omp/gcc-13] libgomp: Fix OMP_TARGET_OFFLOAD=mandatory
@ 2023-06-16 15:53 Tobias Burnus
  0 siblings, 0 replies; only message in thread
From: Tobias Burnus @ 2023-06-16 15:53 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:aa827ff02ec8622aa3327cc1379538ffb81f20db

commit aa827ff02ec8622aa3327cc1379538ffb81f20db
Author: Tobias Burnus <tobias@codesourcery.com>
Date:   Fri Jun 16 17:52:40 2023 +0200

    libgomp: Fix OMP_TARGET_OFFLOAD=mandatory
    
    It turned out that gomp_init_targets_once() was not run when directly
    calling 'omp target' or 'omp target (enter/exit) data' causing an
    abort with OMP_TARGET_OFFLOAD=mandatory wrongly claiming that no
    device is available. It was called a tiny bit later but few lines too
    late for updating the default-device-var.
    
    libgomp/ChangeLog:
    
            * target.c (resolve_device): Call gomp_get_num_devices early to ensure
            gomp_init_targets_once was called before using default-device-var.
            * testsuite/libgomp.c/target-55.c: New test.
            * testsuite/libgomp.c/target-55a.c: New test.
    
    (cherry picked from commit 8216ca85037be9f4d5c20540522a22a4a93b660e)

Diff:
---
 libgomp/ChangeLog.omp                    | 10 ++++++++++
 libgomp/target.c                         | 10 +++++++---
 libgomp/testsuite/libgomp.c/target-55.c  | 20 ++++++++++++++++++++
 libgomp/testsuite/libgomp.c/target-55a.c | 23 +++++++++++++++++++++++
 4 files changed, 60 insertions(+), 3 deletions(-)

diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp
index 73b35705d4b..8e02ad9f916 100644
--- a/libgomp/ChangeLog.omp
+++ b/libgomp/ChangeLog.omp
@@ -1,3 +1,13 @@
+2023-06-16  Tobias Burnus  <tobias@codesourcery.com>
+
+	Backported from mainline:
+	2023-06-16  Tobias Burnus  <tobias@codesourcery.com>
+
+	* target.c (resolve_device): Call gomp_get_num_devices early to ensure
+	gomp_init_targets_once was called before using default-device-var.
+	* testsuite/libgomp.c/target-55.c: New test.
+	* testsuite/libgomp.c/target-55a.c: New test.
+
 2023-06-15  Tobias Burnus  <tobias@codesourcery.com>
 
 	Backported from mainline:
diff --git a/libgomp/target.c b/libgomp/target.c
index f8c992cf609..77e03fcf5b7 100644
--- a/libgomp/target.c
+++ b/libgomp/target.c
@@ -206,6 +206,10 @@ gomp_get_num_devices (void)
 static struct gomp_device_descr *
 resolve_device (int device_id, bool remapped)
 {
+  /* Get number of devices and thus ensure that 'gomp_init_targets_once' was
+     called, which must be done before using default_device_var.  */
+  int num_devices = gomp_get_num_devices ();
+
   if (remapped && device_id == GOMP_DEVICE_ICV)
     {
       struct gomp_task_icv *icv = gomp_icv (false);
@@ -219,7 +223,7 @@ resolve_device (int device_id, bool remapped)
 				 : omp_initial_device))
 	return NULL;
       if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
-	  && gomp_get_num_devices () == 0)
+	  && num_devices == 0)
 	gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
 		    "but only the host device is available");
       else if (device_id == omp_invalid_device)
@@ -230,10 +234,10 @@ resolve_device (int device_id, bool remapped)
 
       return NULL;
     }
-  else if (device_id >= gomp_get_num_devices ())
+  else if (device_id >= num_devices)
     {
       if (gomp_target_offload_var == GOMP_TARGET_OFFLOAD_MANDATORY
-	  && device_id != num_devices_openmp)
+	  && device_id != num_devices)
 	gomp_fatal ("OMP_TARGET_OFFLOAD is set to MANDATORY, "
 		    "but device not found");
 
diff --git a/libgomp/testsuite/libgomp.c/target-55.c b/libgomp/testsuite/libgomp.c/target-55.c
new file mode 100644
index 00000000000..1314b3c6963
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-55.c
@@ -0,0 +1,20 @@
+/* { dg-do run { target { offload_device } } } */
+/* { dg-set-target-env-var OMP_TARGET_OFFLOAD "mandatory" } */
+
+/* Should pass - see target-55a.c for !offload_device */
+
+/* Check OMP_TARGET_OFFLOAD - it shall run on systems with offloading
+   devices available and fail otherwise.  Note that this did always
+   fail - as the device handling wasn't initialized before doing the
+   mandatory checking.  */
+
+int
+main ()
+{
+  int x = 1;
+  #pragma omp target map(tofrom: x)
+    x = 5;
+  if (x != 5)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/target-55a.c b/libgomp/testsuite/libgomp.c/target-55a.c
new file mode 100644
index 00000000000..53978c3f405
--- /dev/null
+++ b/libgomp/testsuite/libgomp.c/target-55a.c
@@ -0,0 +1,23 @@
+/* { dg-do run { target { ! offload_device } } } */
+/* { dg-set-target-env-var OMP_TARGET_OFFLOAD "mandatory" } */
+
+/* Should fail - see target-55a.c for offload_device */
+
+/* { dg-shouldfail "omp_invalid_device" } */
+/* { dg-output ".*libgomp: OMP_TARGET_OFFLOAD is set to MANDATORY, but only the host device is available.*" } */
+
+/* Check OMP_TARGET_OFFLOAD - it shall run on systems with offloading
+   devices available and fail otherwise.  Note that this did always
+   fail - as the device handling wasn't initialized before doing the
+   mandatory checking.  */
+
+int
+main ()
+{
+  int x = 1;
+  #pragma omp target map(tofrom: x)
+    x = 5;
+  if (x != 5)
+    __builtin_abort ();
+  return 0;
+}

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-16 15:53 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-16 15:53 [gcc/devel/omp/gcc-13] libgomp: Fix OMP_TARGET_OFFLOAD=mandatory Tobias Burnus

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).