]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
openmp: Fix up handling of target constructs in offloaded routines [PR100573]
authorJakub Jelinek <jakub@redhat.com>
Wed, 26 May 2021 13:15:19 +0000 (15:15 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Wed, 26 May 2021 13:15:43 +0000 (15:15 +0200)
OpenMP Nesting of Regions restrictions say:
- If a target update, target data, target enter data, or target exit data
construct is encountered during execution of a target region, the behavior is unspecified.
- If a target construct is encountered during execution of a target region and a device
clause in which the ancestor device-modifier appears is not present on the construct, the
behavior is unspecified.
That wording is about the dynamic (runtime) behavior, not about lexical nesting,
so while it is UB if omp target * is encountered in the target region, we need to make
it compile and link (for lexical nesting of target * inside of target we actually
emit a warning).

To make this work, I had to do multiple changes.
One was to mark .omp_data_{sizes,kinds}.* variables when static as "omp declare target".
Another one was to add stub GOMP_target* entrypoints to nvptx and gcn libgomp.a.
The entrypoint functions shouldn't be called or passed in the offload regions,
otherwise
libgomp: cuLaunchKernel error: too many resources requested for launch
was reported; fixed by changing those arguments of calls to GOMP_target_ext
to NULL.
And we didn't mark the entrypoints "omp target entrypoint" when the caller
has been "omp declare target".

2021-05-26  Jakub Jelinek  <jakub@redhat.com>

PR libgomp/100573
gcc/
* omp-low.c: Include omp-offload.h.
(create_omp_child_function): If current_function_decl has
"omp declare target" attribute and is_gimple_omp_offloaded,
remove that attribute from the copy of attribute list and
add "omp target entrypoint" attribute instead.
(lower_omp_target): Mark .omp_data_sizes.* and .omp_data_kinds.*
variables for offloading if in omp_maybe_offloaded_ctx.
* omp-offload.c (pass_omp_target_link::execute): Nullify second
argument to GOMP_target_data_ext in offloaded code.
libgomp/
* config/nvptx/target.c (GOMP_target_ext, GOMP_target_data_ext,
GOMP_target_end_data, GOMP_target_update_ext,
GOMP_target_enter_exit_data): New dummy entrypoints.
* config/gcn/target.c (GOMP_target_ext, GOMP_target_data_ext,
GOMP_target_end_data, GOMP_target_update_ext,
GOMP_target_enter_exit_data): Likewise.
* testsuite/libgomp.c-c++-common/for-3.c (DO_PRAGMA, OMPTEAMS,
OMPFROM, OMPTO): Define.
(main): Remove #pragma omp target teams around all the tests.
* testsuite/libgomp.c-c++-common/target-41.c: New test.
* testsuite/libgomp.c-c++-common/target-42.c: New test.

(cherry picked from commit 95d67762171f83277a5700b270c0d1e2756f83f4)

gcc/ChangeLog.omp
gcc/omp-low.c
gcc/omp-offload.c
libgomp/ChangeLog.omp
libgomp/config/gcn/target.c
libgomp/config/nvptx/target.c
libgomp/testsuite/libgomp.c-c++-common/for-3.c
libgomp/testsuite/libgomp.c-c++-common/target-41.c [new file with mode: 0644]
libgomp/testsuite/libgomp.c-c++-common/target-42.c [new file with mode: 0644]

index dbe8e898dc8b0cc471800df1988ab0ad8d71ddc7..6ed2cd7adf531d7265d8fc104cbebd3acf9167d1 100644 (file)
@@ -1,3 +1,19 @@
+2021-05-26  Tobias Burnus  <tobias@codesourcery.com>
+
+       Backported from master:
+       2021-05-26  Jakub Jelinek  <jakub@redhat.com>
+
+       PR libgomp/100573
+       * omp-low.c: Include omp-offload.h.
+       (create_omp_child_function): If current_function_decl has
+       "omp declare target" attribute and is_gimple_omp_offloaded,
+       remove that attribute from the copy of attribute list and
+       add "omp target entrypoint" attribute instead.
+       (lower_omp_target): Mark .omp_data_sizes.* and .omp_data_kinds.*
+       variables for offloading if in omp_maybe_offloaded_ctx.
+       * omp-offload.c (pass_omp_target_link::execute): Nullify second
+       argument to GOMP_target_data_ext in offloaded code.
+
 2021-05-23  Tobias Burnus  <tobias@codesourcery.com>
 
        Backported from master:
        GOMP_MAP_DECLARE_ALLOCATE and GOMP_MAP_DECLARE_DEALLOCATE.
 
 2018-10-04  Cesar Philippidis  <cesar@codesourcery.com>
-            Julian Brown  <julian@codesourcery.com>
+           Julian Brown  <julian@codesourcery.com>
 
        * omp-low.c (scan_sharing_clauses): Update handling of OpenACC declare
        create, declare copyin and declare deviceptr to have local lifetimes.
index a5ae1091aa26115e353ccf528b5ca9454f7ff560..9302d6ab03ecfa1dbcad8d42b9da49d3b2e736a1 100644 (file)
@@ -59,6 +59,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gimple-pretty-print.h"
 #include "stringpool.h"
 #include "attribs.h"
+#include "omp-offload.h"
 
 /* Lowering of OMP parallel and workshare constructs proceeds in two
    phases.  The first phase scans the function looking for OMP statements
@@ -2123,16 +2124,25 @@ create_omp_child_function (omp_context *ctx, bool task_copy)
        g->have_offload = true;
     }
 
-  if (cgraph_node::get_create (decl)->offloadable
-      && !lookup_attribute ("omp declare target",
-                           DECL_ATTRIBUTES (current_function_decl)))
+  if (cgraph_node::get_create (decl)->offloadable)
     {
       const char *target_attr = (is_gimple_omp_offloaded (ctx->stmt)
                                 ? "omp target entrypoint"
                                 : "omp declare target");
-      DECL_ATTRIBUTES (decl)
-       = tree_cons (get_identifier (target_attr),
-                    NULL_TREE, DECL_ATTRIBUTES (decl));
+      if (lookup_attribute ("omp declare target",
+                           DECL_ATTRIBUTES (current_function_decl)))
+       {
+         if (is_gimple_omp_offloaded (ctx->stmt))
+           DECL_ATTRIBUTES (decl)
+             = remove_attribute ("omp declare target",
+                                 copy_list (DECL_ATTRIBUTES (decl)));
+         else
+           target_attr = NULL;
+       }
+      if (target_attr)
+       DECL_ATTRIBUTES (decl)
+         = tree_cons (get_identifier (target_attr),
+                      NULL_TREE, DECL_ATTRIBUTES (decl));
     }
 
   t = build_decl (DECL_SOURCE_LOCATION (decl),
@@ -13387,6 +13397,23 @@ lower_omp_target (gimple_stmt_iterator *gsi_p, omp_context *ctx)
                                 gimple_build_assign (TREE_VEC_ELT (t, i),
                                                      clobber));
          }
+       else if (omp_maybe_offloaded_ctx (ctx->outer))
+         {
+           tree id = get_identifier ("omp declare target");
+           tree decl = TREE_VEC_ELT (t, i);
+           DECL_ATTRIBUTES (decl)
+             = tree_cons (id, NULL_TREE, DECL_ATTRIBUTES (decl));
+           varpool_node *node = varpool_node::get (decl);
+           if (node)
+             {
+               node->offloadable = 1;
+               if (ENABLE_OFFLOADING)
+                 {
+                   g->have_offload = true;
+                   vec_safe_push (offload_vars, t);
+                 }
+             }
+         }
 
       tree clobber = build_clobber (ctx->record_type);
       gimple_seq_add_stmt (&olist, gimple_build_assign (ctx->sender_decl,
index 16ef6aacd42c20af0a7d9ecf69344cc36cd4e60b..b4592594ee4943b442dd30bc77909c0c6aab99e5 100644 (file)
@@ -2978,8 +2978,16 @@ pass_omp_target_link::execute (function *fun)
     {
       gimple_stmt_iterator gsi;
       for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
-       if (walk_gimple_stmt (&gsi, NULL, find_link_var_op, NULL))
-         gimple_regimplify_operands (gsi_stmt (gsi), &gsi);
+       {
+         if (gimple_call_builtin_p (gsi_stmt (gsi), BUILT_IN_GOMP_TARGET))
+           {
+             /* Nullify the second argument of __builtin_GOMP_target_ext.  */
+             gimple_call_set_arg (gsi_stmt (gsi), 1, null_pointer_node);
+             update_stmt (gsi_stmt (gsi));
+           }
+         if (walk_gimple_stmt (&gsi, NULL, find_link_var_op, NULL))
+           gimple_regimplify_operands (gsi_stmt (gsi), &gsi);
+       }
     }
 
   return 0;
index 1ee4760c06bdc660d39e8611653a406eaa0aeb76..87b2e27f39cdd47c3f1283a21c65e421b527be25 100644 (file)
@@ -1,3 +1,21 @@
+2021-05-26  Tobias Burnus  <tobias@codesourcery.com>
+
+       Backported from master:
+       2021-05-26  Jakub Jelinek  <jakub@redhat.com>
+
+       PR libgomp/100573
+       * config/nvptx/target.c (GOMP_target_ext, GOMP_target_data_ext,
+       GOMP_target_end_data, GOMP_target_update_ext,
+       GOMP_target_enter_exit_data): New dummy entrypoints.
+       * config/gcn/target.c (GOMP_target_ext, GOMP_target_data_ext,
+       GOMP_target_end_data, GOMP_target_update_ext,
+       GOMP_target_enter_exit_data): Likewise.
+       * testsuite/libgomp.c-c++-common/for-3.c (DO_PRAGMA, OMPTEAMS,
+       OMPFROM, OMPTO): Define.
+       (main): Remove #pragma omp target teams around all the tests.
+       * testsuite/libgomp.c-c++-common/target-41.c: New test.
+       * testsuite/libgomp.c-c++-common/target-42.c: New test.
+
 2021-05-25  Tobias Burnus  <tobias@codesourcery.com>
 
        Backported from master:
index 4016b7bb2404bd045c156310bcfbc4f2ff5e0c4e..a93ecc90d44f3878cb9a8764bd499c3800b5d8db 100644 (file)
@@ -65,3 +65,68 @@ omp_pause_resource_all (omp_pause_resource_t kind)
 
 ialias (omp_pause_resource)
 ialias (omp_pause_resource_all)
+
+void
+GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
+                void **hostaddrs, size_t *sizes, unsigned short *kinds,
+                unsigned int flags, void **depend, void **args)
+{
+  (void) device;
+  (void) fn;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  (void) args;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_data_ext (int device, size_t mapnum, void **hostaddrs,
+                     size_t *sizes, unsigned short *kinds)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_end_data (void)
+{
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_update_ext (int device, size_t mapnum, void **hostaddrs,
+                       size_t *sizes, unsigned short *kinds,
+                       unsigned int flags, void **depend)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
+                            size_t *sizes, unsigned short *kinds,
+                            unsigned int flags, void **depend)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  __builtin_unreachable ();
+}
index 1410577079085cd32f75149df892cda1031fe512..e4140e482961333d0daf278220e3c65a716606b2 100644 (file)
@@ -65,3 +65,68 @@ omp_pause_resource_all (omp_pause_resource_t kind)
 
 ialias (omp_pause_resource)
 ialias (omp_pause_resource_all)
+
+void
+GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
+                void **hostaddrs, size_t *sizes, unsigned short *kinds,
+                unsigned int flags, void **depend, void **args)
+{
+  (void) device;
+  (void) fn;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  (void) args;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_data_ext (int device, size_t mapnum, void **hostaddrs,
+                     size_t *sizes, unsigned short *kinds)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_end_data (void)
+{
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_update_ext (int device, size_t mapnum, void **hostaddrs,
+                       size_t *sizes, unsigned short *kinds,
+                       unsigned int flags, void **depend)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  __builtin_unreachable ();
+}
+
+void
+GOMP_target_enter_exit_data (int device, size_t mapnum, void **hostaddrs,
+                            size_t *sizes, unsigned short *kinds,
+                            unsigned int flags, void **depend)
+{
+  (void) device;
+  (void) mapnum;
+  (void) hostaddrs;
+  (void) sizes;
+  (void) kinds;
+  (void) flags;
+  (void) depend;
+  __builtin_unreachable ();
+}
index 173ce8ecc1364eec50b8df34cbdbaa260a6b2400..285f8e9bd4de95153e4af9fcfa26957c1b7dbbe5 100644 (file)
@@ -9,6 +9,11 @@ void abort ();
 #define M(x, y, z) O(x, y, z)
 #define O(x, y, z) x ## _ ## y ## _ ## z
 
+#define DO_PRAGMA(x) _Pragma (#x)
+#define OMPTEAMS DO_PRAGMA (omp target teams)
+#define OMPFROM(v) DO_PRAGMA (omp target update from(v))
+#define OMPTO(v) DO_PRAGMA (omp target update to(v))
+
 #pragma omp declare target
 
 #define F distribute
@@ -81,33 +86,30 @@ int
 main ()
 {
   int err = 0;
-  #pragma omp target teams reduction(|:err)
-    {
-      err |= test_d_normal ();
-      err |= test_d_ds128_normal ();
-      err |= test_ds_normal ();
-      err |= test_ds_ds128_normal ();
-      err |= test_dpf_static ();
-      err |= test_dpf_static32 ();
-      err |= test_dpf_auto ();
-      err |= test_dpf_guided32 ();
-      err |= test_dpf_runtime ();
-      err |= test_dpf_ds128_static ();
-      err |= test_dpf_ds128_static32 ();
-      err |= test_dpf_ds128_auto ();
-      err |= test_dpf_ds128_guided32 ();
-      err |= test_dpf_ds128_runtime ();
-      err |= test_dpfs_static ();
-      err |= test_dpfs_static32 ();
-      err |= test_dpfs_auto ();
-      err |= test_dpfs_guided32 ();
-      err |= test_dpfs_runtime ();
-      err |= test_dpfs_ds128_static ();
-      err |= test_dpfs_ds128_static32 ();
-      err |= test_dpfs_ds128_auto ();
-      err |= test_dpfs_ds128_guided32 ();
-      err |= test_dpfs_ds128_runtime ();
-    }
+  err |= test_d_normal ();
+  err |= test_d_ds128_normal ();
+  err |= test_ds_normal ();
+  err |= test_ds_ds128_normal ();
+  err |= test_dpf_static ();
+  err |= test_dpf_static32 ();
+  err |= test_dpf_auto ();
+  err |= test_dpf_guided32 ();
+  err |= test_dpf_runtime ();
+  err |= test_dpf_ds128_static ();
+  err |= test_dpf_ds128_static32 ();
+  err |= test_dpf_ds128_auto ();
+  err |= test_dpf_ds128_guided32 ();
+  err |= test_dpf_ds128_runtime ();
+  err |= test_dpfs_static ();
+  err |= test_dpfs_static32 ();
+  err |= test_dpfs_auto ();
+  err |= test_dpfs_guided32 ();
+  err |= test_dpfs_runtime ();
+  err |= test_dpfs_ds128_static ();
+  err |= test_dpfs_ds128_static32 ();
+  err |= test_dpfs_ds128_auto ();
+  err |= test_dpfs_ds128_guided32 ();
+  err |= test_dpfs_ds128_runtime ();
   if (err)
     abort ();
   return 0;
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-41.c b/libgomp/testsuite/libgomp.c-c++-common/target-41.c
new file mode 100644 (file)
index 0000000..3aca19a
--- /dev/null
@@ -0,0 +1,28 @@
+/* PR libgomp/100573 */
+
+int
+foo (int a)
+{
+  if (a == 0)
+    {
+      int c;
+      a++;
+      #pragma omp target map(tofrom:a)
+      a = foo (a);
+      #pragma omp target data map(tofrom:a)
+      c = a != 2;
+      if (c)
+       return -1;
+      #pragma omp target enter data map(to:a)
+      #pragma omp target exit data map(from:a)
+    }
+  return a + 1;
+}
+
+int
+main ()
+{
+  if (foo (0) != 3)
+    __builtin_abort ();
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/target-42.c b/libgomp/testsuite/libgomp.c-c++-common/target-42.c
new file mode 100644 (file)
index 0000000..a334f47
--- /dev/null
@@ -0,0 +1,26 @@
+/* PR libgomp/100573 */
+
+int
+foo (int a)
+{
+  #pragma omp target firstprivate(a)
+  if (a == 0)
+    {
+      a++;
+      #pragma omp target map(tofrom:a)         /* { dg-warning "'target' construct inside of 'target' region" } */
+      a = foo (a);
+      #pragma omp target data map(tofrom:a)    /* { dg-warning "'target data' construct inside of 'target' region" } */
+      a++;
+      #pragma omp target enter data map(to:a)  /* { dg-warning "'target enter data' construct inside of 'target' region" } */
+      #pragma omp target exit data map(from:a) /* { dg-warning "'target exit data' construct inside of 'target' region" } */
+    }
+  return a + 1;
+}
+
+int
+main ()
+{
+  if (foo (1) != 2)
+    __builtin_abort ();
+  return 0;
+}