From patchwork Wed Oct 12 16:51:23 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakub Jelinek X-Patchwork-Id: 1961 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp2709834wrs; Wed, 12 Oct 2022 09:52:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4kgXoBg6lDyDUD79uXGLcyeCIfAAenTe+fLzEI509zaogChS6ABHH43KuJppmV2cCM0Bu+ X-Received: by 2002:a05:6402:280a:b0:45c:a9f5:7154 with SMTP id h10-20020a056402280a00b0045ca9f57154mr3949423ede.405.1665593559400; Wed, 12 Oct 2022 09:52:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1665593559; cv=none; d=google.com; s=arc-20160816; b=o6yiBbevWSMoMK9mVUkDntpNaKX/ayISCa7i2z86v0qQzic4P6OzOmc+Qe91v+/XAU egDAIwb7nzcl6SOBjFsQ+j6VzWdaT+YsYVts9XnHNclClKoLGf6UQiOHr2bp6AqU5TbB m3GejhNPN7V1d8yA9LKHoMIYSXVioWrIcg9bGDylu8mIbxWxnjsYaOEBAgq3ZapsrC3o eOqMCSQqOOK0Ov3qxI8zyqAa9sv56UrRJNa/5ldTgr7/tL+Py7IC/bgILeiNIprnXPzU 4lXD2ZbMiQegNfq7tOeQnUo099NIQbCW2Nuzdlpfo+mZV3o3tw4T/uFVrH6IOSY/D6Ik x/OQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=7s5gshqBfguz5oZRKhG4qKFM5C0IfGPeh8AuvwOY2bw=; b=uyx2NjGsLHnlBowa14v0IDYCSbZYouYqhH8ytkDasmgQo9Pzhn2c5X9viJlch7kzRz nkg0I9VXzucYUIXvN33n5Jf5CBQdUheBTk6NETefLgzWagzTOmdyuZj3Txk7FXsGtj85 DnmCirMyF7ulBaf6KXjmphjgwwG/cXbzHkxjwwTcA0TNhdt81H9rEn47fKUQeDlZ4RUc 9ucTFISFW+JvmdMsi/tC4vLt4pK4kwAUt4dbNCdiT1ug2mAFISm+0i21vyyLb00ik3PN c1msIQOBMs/hHtGSotsEU2gJnn931ZzxpF4ty2In1Kj4e/5lloz9LiLEkwSeWtPM7Zj0 l52g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CwFm2igY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id b13-20020a056402278d00b00459348cc7d0si17932158ede.588.2022.10.12.09.52.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 09:52:39 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=CwFm2igY; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 74AE33857B92 for ; Wed, 12 Oct 2022 16:52:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 74AE33857B92 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1665593557; bh=7s5gshqBfguz5oZRKhG4qKFM5C0IfGPeh8AuvwOY2bw=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=CwFm2igYtR/Cn121Mbwri/qEY/DfyPuYxGVJwm78nsPG60aBuqqRRrPU9MZINZGzG E2KicuyHND7BUlEYYOZ4HJK2Zeg8mSJTYwO4sdVi2MsHb1IuaH0VGjZojlROoFBlGO RPMt8uhp6eztwrMy8uQFo+3Eqseol95HYbuC/3PY= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 268E33858D38 for ; Wed, 12 Oct 2022 16:51:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 268E33858D38 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-110-gBzixPmAPhmM0udMpS_VAw-1; Wed, 12 Oct 2022 12:51:29 -0400 X-MC-Unique: gBzixPmAPhmM0udMpS_VAw-1 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id F1A3D811E81; Wed, 12 Oct 2022 16:51:28 +0000 (UTC) Received: from tucnak.zalov.cz (unknown [10.39.192.55]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B16BD2087448; Wed, 12 Oct 2022 16:51:28 +0000 (UTC) Received: from tucnak.zalov.cz (localhost [127.0.0.1]) by tucnak.zalov.cz (8.17.1/8.17.1) with ESMTPS id 29CGpOp21109837 (version=TLSv1.3 cipher=TLS_AES_256_GCM_SHA384 bits=256 verify=NOT); Wed, 12 Oct 2022 18:51:25 +0200 Received: (from jakub@localhost) by tucnak.zalov.cz (8.17.1/8.17.1/Submit) id 29CGpNaF1109836; Wed, 12 Oct 2022 18:51:23 +0200 Date: Wed, 12 Oct 2022 18:51:23 +0200 To: Tobias Burnus Subject: [committed] libgomp: Fix up creation of artificial teams Message-ID: MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline X-Spam-Status: No, score=-3.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jakub Jelinek via Gcc-patches From: Jakub Jelinek Reply-To: Jakub Jelinek Cc: gcc-patches@gcc.gnu.org Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1746501432066396691?= X-GMAIL-MSGID: =?utf-8?q?1746501432066396691?= Hi! 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. Regtested on x86_64-linux and i686-linux, committed to trunk. Will consider it for backporting later. 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. Jakub --- libgomp/task.c.jj 2022-05-25 11:10:32.543261788 +0200 +++ libgomp/task.c 2022-10-12 16:49:03.342493229 +0200 @@ -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: --- libgomp/target.c.jj 2022-09-09 18:44:27.157255847 +0200 +++ libgomp/target.c 2022-10-12 16:43:33.531002003 +0200 @@ -2813,6 +2813,7 @@ GOMP_target_ext (int device, void (*fn) { 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; @@ -2825,15 +2826,23 @@ GOMP_target_ext (int device, void (*fn) 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) --- libgomp/testsuite/libgomp.c/task-7.c.jj 2022-10-12 15:41:25.488494296 +0200 +++ libgomp/testsuite/libgomp.c/task-7.c 2022-10-12 16:03:32.881612519 +0200 @@ -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; +} --- libgomp/testsuite/libgomp.c/task-8.c.jj 2022-10-12 16:06:38.889097404 +0200 +++ libgomp/testsuite/libgomp.c/task-8.c 2022-10-12 16:07:19.568545781 +0200 @@ -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; +} --- libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c.jj 2022-10-12 17:03:31.760628526 +0200 +++ libgomp/testsuite/libgomp.c-c++-common/task-reduction-17.c 2022-10-12 17:08:09.225839917 +0200 @@ -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; +} --- libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c.jj 2022-10-12 17:08:36.737464280 +0200 +++ libgomp/testsuite/libgomp.c-c++-common/task-reduction-18.c 2022-10-12 17:09:40.776589861 +0200 @@ -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; +}