]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libgomp: Fix up creation of artificial teams
authorJakub Jelinek <jakub@redhat.com>
Mon, 17 Oct 2022 14:55:46 +0000 (16:55 +0200)
committerTobias Burnus <tobias@codesourcery.com>
Mon, 17 Oct 2022 14:55:46 +0000 (16:55 +0200)
When not in explicit parallel/target/teams construct, we in some cases create
an artificial parallel with a single thread (either to handle target nowait
or for task reduction purposes).  In those cases, it handled again artificially
created implicit task (created by gomp_new_icv for cases where we needed to write
to some ICVs), but as the testcases show, didn't take into account possibility
of this being done from explicit task(s).  The code would destroy/free the previous
task and replace it with the new implicit task.  If task is an explicit task
(when teams is NULL, all explicit tasks behave like if (0)), it is a pointer to
a local stack variable, so freeing it doesn't work, and additionally we shouldn't
lose the explicit tasks - the new implicit task should instead replace the
ancestor task which is the first implicit one.

2022-10-12  Jakub Jelinek  <jakub@redhat.com>

* task.c (gomp_create_artificial_team): Fix up handling of invocations
from within explicit task.
* target.c (GOMP_target_ext): Likewise.
* testsuite/libgomp.c/task-7.c: New test.
* testsuite/libgomp.c/task-8.c: New test.
* testsuite/libgomp.c-c++-common/task-reduction-17.c: New test.
* testsuite/libgomp.c-c++-common/task-reduction-18.c: New test.

(cherry picked from commit a58a965eb73253759f6a3e1c7380392557da89c8)

libgomp/ChangeLog.omp
libgomp/target.c
libgomp/task.c
libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c [new file with mode: 0644]
libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c [new file with mode: 0644]
libgomp/testsuite/libgomp.c/task-7.c [new file with mode: 0644]
libgomp/testsuite/libgomp.c/task-8.c [new file with mode: 0644]

index 048314eb1be07dc08f00772bbbbbb19334fec402..432938871fa9e6d41cc73884e26c5b67bfeca08a 100644 (file)
@@ -1,3 +1,16 @@
+2022-10-17  Tobias Burnus  <tobias@codesourcery.com>
+
+       Backport from mainline:
+       2022-10-12  Jakub Jelinek  <jakub@redhat.com>
+
+       * task.c (gomp_create_artificial_team): Fix up handling of invocations
+       from within explicit task.
+       * target.c (GOMP_target_ext): Likewise.
+       * testsuite/libgomp.c/task-7.c: New test.
+       * testsuite/libgomp.c/task-8.c: New test.
+       * testsuite/libgomp.c-c++-common/task-reduction-17.c: New test.
+       * testsuite/libgomp.c-c++-common/task-reduction-18.c: New test.
+
 2022-10-17  Thomas Schwinge  <thomas@codesourcery.com>
 
        Backported from master:
index 613f4f7ae251ab96f2a60a7b5df18c03f1cd5577..2a3a5fa4e4f4f6999d5dfd27509d510582b21960 100644 (file)
@@ -3003,6 +3003,7 @@ GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
        {
          struct gomp_team *team = gomp_new_team (1);
          struct gomp_task *task = thr->task;
+         struct gomp_task **implicit_task = &task;
          struct gomp_task_icv *icv = task ? &task->icv : &gomp_global_icv;
          team->prev_ts = thr->ts;
          thr->ts.team = team;
@@ -3015,15 +3016,23 @@ GOMP_target_ext (int device, void (*fn) (void *), size_t mapnum,
          thr->ts.static_trip = 0;
          thr->task = &team->implicit_task[0];
          gomp_init_task (thr->task, NULL, icv);
-         if (task)
+         while (*implicit_task
+                && (*implicit_task)->kind != GOMP_TASK_IMPLICIT)
+           implicit_task = &(*implicit_task)->parent;
+         if (*implicit_task)
            {
-             thr->task = task;
+             thr->task = *implicit_task;
              gomp_end_task ();
-             free (task);
+             free (*implicit_task);
              thr->task = &team->implicit_task[0];
            }
          else
            pthread_setspecific (gomp_thread_destructor, thr);
+         if (implicit_task != &task)
+           {
+             *implicit_task = thr->task;
+             thr->task = task;
+           }
        }
       if (thr->ts.team
          && !thr->task->final_task)
index 30cd046df2a6549c2b7ce7aef468cd77d2a212fd..7766535d1aa233e0125f9712dfc6cdc0b6513d9b 100644 (file)
@@ -2465,6 +2465,7 @@ gomp_create_artificial_team (void)
   struct gomp_task_icv *icv;
   struct gomp_team *team = gomp_new_team (1);
   struct gomp_task *task = thr->task;
+  struct gomp_task **implicit_task = &task;
   icv = task ? &task->icv : &gomp_global_icv;
   team->prev_ts = thr->ts;
   thr->ts.team = team;
@@ -2477,17 +2478,25 @@ gomp_create_artificial_team (void)
   thr->ts.static_trip = 0;
   thr->task = &team->implicit_task[0];
   gomp_init_task (thr->task, NULL, icv);
-  if (task)
+  while (*implicit_task
+        && (*implicit_task)->kind != GOMP_TASK_IMPLICIT)
+    implicit_task = &(*implicit_task)->parent;
+  if (*implicit_task)
     {
-      thr->task = task;
+      thr->task = *implicit_task;
       gomp_end_task ();
-      free (task);
+      free (*implicit_task);
       thr->task = &team->implicit_task[0];
     }
 #ifdef LIBGOMP_USE_PTHREADS
   else
     pthread_setspecific (gomp_thread_destructor, thr);
 #endif
+  if (implicit_task != &task)
+    {
+      *implicit_task = thr->task;
+      thr->task = task;
+    }
 }
 
 /* The format of data is:
diff --git a/libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c b/libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c
new file mode 100644 (file)
index 0000000..4a8d1e8
--- /dev/null
@@ -0,0 +1,36 @@
+/* { dg-do run } */
+
+#include <omp.h>
+#include <stdlib.h>
+
+int a;
+
+int
+main ()
+{
+  #pragma omp task final (1)
+  {
+    if (!omp_in_final ())
+      abort ();
+    #pragma omp task
+    {
+      if (!omp_in_final ())
+       abort ();
+      #pragma omp taskgroup task_reduction (+: a)
+      {
+       if (!omp_in_final ())
+         abort ();
+       #pragma omp task in_reduction (+: a)
+       {
+         ++a;
+         if (!omp_in_final ())
+           abort ();
+       }
+      }
+      if (!omp_in_final ())
+       abort ();
+      #pragma omp taskwait
+    }
+  }
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c b/libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c
new file mode 100644 (file)
index 0000000..483f440
--- /dev/null
@@ -0,0 +1,17 @@
+/* { dg-do run } */
+
+int a;
+
+int
+main ()
+{
+  #pragma omp task
+  {
+    #pragma omp taskgroup task_reduction (+: a)
+    {
+      #pragma omp task in_reduction (+: a)
+      ++a;
+    }
+  }
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/task-7.c b/libgomp/testsuite/libgomp.c/task-7.c
new file mode 100644 (file)
index 0000000..0307575
--- /dev/null
@@ -0,0 +1,26 @@
+/* { dg-do run } */
+
+#include <omp.h>
+#include <stdlib.h>
+
+int
+main ()
+{
+  #pragma omp task final (1)
+  {
+    if (!omp_in_final ())
+      abort ();
+    #pragma omp task
+    {
+      if (!omp_in_final ())
+       abort ();
+      #pragma omp target nowait
+      if (omp_in_final ())
+       abort ();
+      if (!omp_in_final ())
+       abort ();
+      #pragma omp taskwait
+    }
+  }
+  return 0;
+}
diff --git a/libgomp/testsuite/libgomp.c/task-8.c b/libgomp/testsuite/libgomp.c/task-8.c
new file mode 100644 (file)
index 0000000..f03aef6
--- /dev/null
@@ -0,0 +1,14 @@
+/* { dg-do run } */
+
+int
+main ()
+{
+  int i = 0;
+  #pragma omp task
+  {
+    #pragma omp target nowait private (i)
+    i = 1;
+    #pragma omp taskwait
+  }
+  return 0;
+}