From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1534) id C22AC3858C52; Fri, 3 Feb 2023 12:23:28 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org C22AC3858C52 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1675427008; bh=2ynhK1IzJwEfz1K5M/rCdTce2r0jp5qyoOh6UZwk/pc=; h=From:To:Subject:Date:From; b=xgq7mo9bSgkAGAy9w5BM0CEFYPL2lshM7IIaUtdhpLOMC8MsRzS9iwroBi0r1iYiw SUOAHOqjD5EQ5uWg7ER4f/RyxVoY/xYOLNb75U2/rsEeiP20aqEeVnsErDMZPhRobO rQhS5tTF0xr+lWfJ0RxfyVGNZkUrSQ3/r6jJwfDQ= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Tobias Burnus To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-12] libgomp: Fix reverse offload issues X-Act-Checkin: gcc X-Git-Author: Tobias Burnus X-Git-Refname: refs/heads/devel/omp/gcc-12 X-Git-Oldrev: 62cb8efd2ee7fc412d62a426785679dbd50228e4 X-Git-Newrev: 07cea1a91b417731fd1b63f4a68377aaaecc7cbe Message-Id: <20230203122328.C22AC3858C52@sourceware.org> Date: Fri, 3 Feb 2023 12:23:28 +0000 (GMT) List-Id: https://gcc.gnu.org/g:07cea1a91b417731fd1b63f4a68377aaaecc7cbe commit 07cea1a91b417731fd1b63f4a68377aaaecc7cbe Author: Tobias Burnus Date: Fri Feb 3 11:46:38 2023 +0100 libgomp: Fix reverse offload issues If there is nothing to map, skip the mapping and avoid attempting to copy 0 bytes from addrs, sizes and kinds. Additionally, it could happen that a non-allocated address was deallocated, such as a pointer set, leading to a free for the actual data. libgomp/ * target.c (gomp_target_rev): Handle mapnum == 0 and avoid freeing not allocated memory. * testsuite/libgomp.fortran/reverse-offload-6.f90: New test. (cherry picked from commit 0b1ce70a813b98ef2893779d14ad6c90c5d06a71) Diff: --- libgomp/ChangeLog.omp | 9 ++++++ libgomp/target.c | 8 +++--- .../libgomp.fortran/reverse-offload-6.f90 | 32 ++++++++++++++++++++++ 3 files changed, 45 insertions(+), 4 deletions(-) diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp index 75a47a77ed7..2b716f67501 100644 --- a/libgomp/ChangeLog.omp +++ b/libgomp/ChangeLog.omp @@ -1,3 +1,12 @@ +2023-02-03 Tobias Burnus + + Backported from master: + 2023-02-03 Tobias Burnus + + * target.c (gomp_target_rev): Handle mapnum == 0 and avoid + freeing not allocated memory. + * testsuite/libgomp.fortran/reverse-offload-6.f90: New test. + 2023-02-01 Tobias Burnus Backported from master: diff --git a/libgomp/target.c b/libgomp/target.c index cda53d636d1..1272e8347c8 100644 --- a/libgomp/target.c +++ b/libgomp/target.c @@ -3521,7 +3521,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; @@ -3599,7 +3599,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; @@ -3835,7 +3835,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; @@ -3877,7 +3877,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