From patchwork Thu Feb 2 14:13:58 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Tobias Burnus X-Patchwork-Id: 51942 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp262157wrn; Thu, 2 Feb 2023 06:14:37 -0800 (PST) X-Google-Smtp-Source: AK7set917yJlQGSganPGXdsdCswsz3nsHBDEsrjQaDMqyQsVi0eiJ1x4bWNWvzCcnlhaOB1Ke3k+ X-Received: by 2002:a17:906:3716:b0:878:4497:380e with SMTP id d22-20020a170906371600b008784497380emr6225327ejc.4.1675347277672; Thu, 02 Feb 2023 06:14:37 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1675347277; cv=none; d=google.com; s=arc-20160816; b=t2mSBIcNrTpSYEtKUdfXLtYhy5agTca39XOtuvKEj8cfWcOojQsBhKCmtPlsP+A6Kv NMo139XarB7enIvR4YMi1gNrhPA04Q9PD1h176juL63F4Apz8z5QDhgJ53fpnPN316+a mCEoOAKUHNyCsIHIp2GzPhVBZ1ThFaA8Vv6O/JoAdRoJ3KVm0+8a04GZh2Yg+RV/zAFq tJP04pwKmtDQdGliCmLJcb38q5M54fwlLcdixZst2szv5s4LAGPhYLN3JjPpq3NtNN6f KTaAaA+guw8L4qSyLgTOj2yHySz0/vNEI/MaBWVnrySJnj4osY5JxdB1Def3joS8Ays9 b/jA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:subject:from:to :content-language:user-agent:mime-version:date:message-id :ironport-sdr:dmarc-filter:delivered-to; bh=muZTMzRbgn38WVO+EKa1PZIV3jw1jPPWwkQp2xcWYgA=; b=oPq8OIXQdVyY13vGFn45fgTLf29p3vrSca8nzu25sSFFwyo0J+XEUDeihNhNKF3/ob Eq7d39s+55bgZscqitAPSvfjociGahiYu2BJS41XfTD2OcSptgL+6TJsCHXP0si81itD XOOUOnLNEbniM56IeHxUXZpo+20fYupmg4r9wBErtkzlAapU0M19LiPNBCzuSjlDjvNq su3ONo99TdoVzweOo8NJeLGdX7CCopePAlkYjc/MYBSxhKXFTgMfmMUPwt/uNxlXkD54 xxAdqxYFZqjeNZVfJIqfE7UdBlPWvgeDc03IGjKRwJKBeY2QoY25swl4Ms9pwRwvvZzd /DcQ== ARC-Authentication-Results: i=1; mx.google.com; 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" Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id wl5-20020a170907310500b0088e824d1cacsi3264745ejb.154.2023.02.02.06.14.37 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 Feb 2023 06:14:37 -0800 (PST) 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; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 700C2385B50D for ; Thu, 2 Feb 2023 14:14:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from esa4.mentor.iphmx.com (esa4.mentor.iphmx.com [68.232.137.252]) by sourceware.org (Postfix) with ESMTPS id 06CDC3858C74 for ; Thu, 2 Feb 2023 14:14:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 06CDC3858C74 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=codesourcery.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=mentor.com X-IronPort-AV: E=Sophos;i="5.97,267,1669104000"; d="diff'?scan'208";a="95580623" Received: from orw-gwy-02-in.mentorg.com ([192.94.38.167]) by esa4.mentor.iphmx.com with ESMTP; 02 Feb 2023 06:14:04 -0800 IronPort-SDR: Dci+U6pdeKXycBqVpSLyyP5vFIlcw9D/lYkVYk3WyJbsFkjwONSqdzQiO/o4LLDbRCsZKeGZVh 6HJXZaytKHlR+riyFHK8ahJSrh8HsF5Bjk42wfpN1saI+JXurROJEhXu9kORcIY1/8KmbHMrk1 WfRmaml2pV1geQ6IsbobJ0YbMjNbIdYHxZND90YcPKNs8Ds9K3gdHbIfNpnmGI4fgZv5xnLyNn CKCVOiA6ZaOtpRXbrOEI02+znqwlj55qDgTl4AT1cWwkQ9e2/zlYyDa0Rnz5YjB2zzjOVZecHV G3A= Message-ID: <4c058f14-c719-b66a-f556-9bda3a4c4556@codesourcery.com> Date: Thu, 2 Feb 2023 15:13:58 +0100 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.6.1 Content-Language: en-US To: gcc-patches , Jakub Jelinek From: Tobias Burnus Subject: [Patch] libgomp: Fix reverse offload issues X-Originating-IP: [137.202.0.90] X-ClientProxiedBy: svr-ies-mbx-14.mgc.mentorg.com (139.181.222.14) To svr-ies-mbx-12.mgc.mentorg.com (139.181.222.12) X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, 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: , 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?1756728947036219663?= X-GMAIL-MSGID: =?utf-8?q?1756728947036219663?= Found when testing AMD GCN offloading, the second issue came up with libgomp.fortran/reverse-offload-5.f90. (But oddly not with nvptx.) While the first one (new test: libgomp.fortran/reverse-offload-6.f90) came up when debugging the issue. Tobias ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955 libgomp: Fix reverse offload issues If there is nothing to map, skip the mapping and attempting to copy addrs, sizes and kinds which may have issues for size = 0. Additionally, it could happen that a non-allocated address was deallocated, e.g. a pointer set - such that there was a double free for the actual data or in multiple other ways. libgomp/ * target.c (gomp_target_rev): Handle mapnum == 0 and avoid freeing not allocated memory. * testsuite/libgomp.fortran/reverse-offload-6.f90: New test. libgomp/target.c | 8 +++--- .../libgomp.fortran/reverse-offload-6.f90 | 32 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/libgomp/target.c b/libgomp/target.c index b16ee761a95..c1682caea13 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -3324,7 +3324,7 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr, gomp_fatal ("Cannot find reverse-offload function"); void (*host_fn)() = (void (*)()) n->k->host_start; - if (devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM) + if ((devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM) || mapnum == 0) { devaddrs = (uint64_t *) (uintptr_t) devaddrs_ptr; sizes = (uint64_t *) (uintptr_t) sizes_ptr; @@ -3402,7 +3402,7 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr, } } - if (!(devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)) + if (!(devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM) && mapnum > 0) { size_t j, struct_cpy = 0; splay_tree_key n2; @@ -3638,7 +3638,7 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr, host_fn (devaddrs); - if (!(devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM)) + if (!(devicep->capabilities & GOMP_OFFLOAD_CAP_SHARED_MEM) && mapnum > 0) { uint64_t struct_cpy = 0; bool clean_struct = false; @@ -3680,7 +3680,7 @@ gomp_target_rev (uint64_t fn_ptr, uint64_t mapnum, uint64_t devaddrs_ptr, clean_struct = true; struct_cpy = sizes[i]; } - else if (cdata[i].aligned) + else if (!cdata[i].present && cdata[i].aligned) gomp_aligned_free ((void *) (uintptr_t) devaddrs[i]); else if (!cdata[i].present) free ((void *) (uintptr_t) devaddrs[i]); diff --git a/libgomp/testsuite/libgomp.fortran/reverse-offload-6.f90 b/libgomp/testsuite/libgomp.fortran/reverse-offload-6.f90 new file mode 100644 index 00000000000..04866edbba7 --- /dev/null +++ b/libgomp/testsuite/libgomp.fortran/reverse-offload-6.f90 @@ -0,0 +1,32 @@ +! +! Ensure that a mapping with no argument works +! + +module m + implicit none (type, external) + integer :: x = 32 + integer :: dev_num2 = -1 +contains +subroutine foo() + use omp_lib, only: omp_get_device_num + x = x + 10 + dev_num2 = omp_get_device_num() +end +end module m + +use m +use omp_lib +!$omp requires reverse_offload +implicit none (type, external) +integer :: dev_num = -1 +!$omp target map(from:dev_num) + dev_num = omp_get_device_num() + ! This calls GOMP_target_ext with number of maps = 0 + !$omp target device(ancestor:1) + call foo + !$omp end target +!$omp end target + +if (omp_get_num_devices() > 0 .and. dev_num2 == dev_num) stop 1 +if (x /= 42) stop 2 +end