From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 2153) id C39613858011; Fri, 4 Nov 2022 08:31:09 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C39613858011 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1667550669; bh=ra8Kd2r8a74WkJNB5EJRo2jU0ZtQszoVUxI6u4/Mimc=; h=From:To:Subject:Date:From; b=GBYygJJbkB9VLRk+iM/q4aXfBhFuBss+6+xz70vUYc+hen/mRrAt8A2LnIZTsUxI7 AHIj2cNKmMgzcjnhbo+XAQBo876yySV/0C+AOS254y40RCwD5lzmWZJkpoyuSddSdK mo3WG3MrOwGEH9eQQa0n/ritqUDTpWmkG+n25pNU= MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Jakub Jelinek To: gcc-cvs@gcc.gnu.org Subject: [gcc r11-10361] libgomp: Fix up creation of artificial teams X-Act-Checkin: gcc X-Git-Author: Jakub Jelinek X-Git-Refname: refs/heads/releases/gcc-11 X-Git-Oldrev: 8693cafd8975364ef9b409c18ddfc233e66345d8 X-Git-Newrev: a2423e832d936ab50a2bf1a6b2f0dfb811d0be50 Message-Id: <20221104083109.C39613858011@sourceware.org> Date: Fri, 4 Nov 2022 08:31:09 +0000 (GMT) List-Id: https://gcc.gnu.org/g:a2423e832d936ab50a2bf1a6b2f0dfb811d0be50 commit r11-10361-ga2423e832d936ab50a2bf1a6b2f0dfb811d0be50 Author: Jakub Jelinek Date: Wed Oct 12 17:54:08 2022 +0200 libgomp: Fix up creation of artificial teams 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 * 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) Diff: --- libgomp/target.c | 15 +++++++-- libgomp/task.c | 15 +++++++-- .../libgomp.c-c++-common/task-reduction-17.c | 36 ++++++++++++++++++++++ .../libgomp.c-c++-common/task-reduction-18.c | 17 ++++++++++ libgomp/testsuite/libgomp.c/task-7.c | 26 ++++++++++++++++ libgomp/testsuite/libgomp.c/task-8.c | 14 +++++++++ 6 files changed, 117 insertions(+), 6 deletions(-) diff --git a/libgomp/target.c b/libgomp/target.c index 4a4e1f80745..98a791770e7 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -2192,6 +2192,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; @@ -2204,15 +2205,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) diff --git a/libgomp/task.c b/libgomp/task.c index f5d26873d5b..80274e950be 100644 --- a/libgomp/task.c +++ b/libgomp/task.c @@ -2202,6 +2202,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; @@ -2214,17 +2215,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 index 00000000000..4a8d1e8bb73 --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c @@ -0,0 +1,36 @@ +/* { dg-do run } */ + +#include +#include + +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 index 00000000000..483f4406f6f --- /dev/null +++ b/libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c @@ -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 index 00000000000..0307575f978 --- /dev/null +++ b/libgomp/testsuite/libgomp.c/task-7.c @@ -0,0 +1,26 @@ +/* { dg-do run } */ + +#include +#include + +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 index 00000000000..f03aef6a030 --- /dev/null +++ b/libgomp/testsuite/libgomp.c/task-8.c @@ -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; +}