From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1729) id 09E5A384F017; Wed, 29 Jun 2022 14:46:06 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 09E5A384F017 Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Kwok Yeung To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/omp/gcc-12] Fix a crash due to mismatch of free and GOMP_alloc. X-Act-Checkin: gcc X-Git-Author: Hafiz Abid Qadeer X-Git-Refname: refs/heads/devel/omp/gcc-12 X-Git-Oldrev: 0a63a142294e43addbb832fc247da362dd2561ea X-Git-Newrev: 8099ba0e264b1292a785417381947f274616b5b0 Message-Id: <20220629144606.09E5A384F017@sourceware.org> Date: Wed, 29 Jun 2022 14:46:06 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 29 Jun 2022 14:46:06 -0000 https://gcc.gnu.org/g:8099ba0e264b1292a785417381947f274616b5b0 commit 8099ba0e264b1292a785417381947f274616b5b0 Author: Hafiz Abid Qadeer Date: Wed Mar 30 18:52:22 2022 +0100 Fix a crash due to mismatch of free and GOMP_alloc. With allocate directive, we replace the malloc calls to GOMP_alloc if it is associated with the allocate statement. The memory was supposed to be free-d by the implicitely generated free calls which also get replaced. But if user explicitely deallocated the memory using the deallocate statement, it can cause a mismatch. This commit handles that case and also replaces the free call generated for deallocate clause. Also added deallocate in the testcase and tidied it up a bit. gcc/ChangeLog: * omp-low.cc (lower_omp_allocate): Move allocate declaration inside loop. Set it to false at the end of condition. libgomp/ChangeLog: * testsuite/libgomp.fortran/allocate-2.f90: Remove commented lines. Add deallocate. Remove omp_atk_pool_size trait. Diff: --- gcc/ChangeLog.omp | 5 +++++ gcc/omp-low.cc | 6 +++++- libgomp/ChangeLog.omp | 5 +++++ libgomp/testsuite/libgomp.fortran/allocate-2.f90 | 12 ++++-------- 4 files changed, 19 insertions(+), 9 deletions(-) diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp index d32ba90b99d..c419396e16f 100644 --- a/gcc/ChangeLog.omp +++ b/gcc/ChangeLog.omp @@ -1,3 +1,8 @@ +2022-03-31 Abid Qadeer + + * omp-low.cc (lower_omp_allocate): Move allocate declaration + inside loop. Set it to false at the end of condition. + 2022-03-30 Andrew Stubbs * omp-builtins.def (BUILT_IN_GOMP_ENABLE_PINNED_MODE): New. diff --git a/gcc/omp-low.cc b/gcc/omp-low.cc index 850174185cc..8685a0b6abc 100644 --- a/gcc/omp-low.cc +++ b/gcc/omp-low.cc @@ -9310,13 +9310,13 @@ lower_omp_allocate (gimple_stmt_iterator *gsi_p, omp_context *ctx) int kind = gimple_omp_allocate_kind (st); gcc_assert (kind == GF_OMP_ALLOCATE_KIND_ALLOCATE || kind == GF_OMP_ALLOCATE_KIND_FREE); - bool allocate = (kind == GF_OMP_ALLOCATE_KIND_ALLOCATE); for (tree c = clauses; c; c = OMP_CLAUSE_CHAIN (c)) { if (OMP_CLAUSE_CODE (c) != OMP_CLAUSE_ALLOCATOR) continue; + bool allocate = (kind == GF_OMP_ALLOCATE_KIND_ALLOCATE); /* The allocate directives that appear in a target region must specify an allocator clause unless a requires directive with the dynamic_allocators clause is present in the same compilation unit. */ @@ -9377,6 +9377,10 @@ lower_omp_allocate (gimple_stmt_iterator *gsi_p, omp_context *ctx) gimple_call_set_lhs (g, gimple_call_lhs (gs)); gimple_set_location (g, gimple_location (stmt)); gsi_replace (&gsi, g, true); + /* The malloc call has been replaced. Now see if there is + any free call due to deallocate statement and replace + that too. */ + allocate = false; } } else diff --git a/libgomp/ChangeLog.omp b/libgomp/ChangeLog.omp index 3372116bb4b..888fc16bc03 100644 --- a/libgomp/ChangeLog.omp +++ b/libgomp/ChangeLog.omp @@ -1,3 +1,8 @@ +2022-03-31 Abid Qadeer + + * testsuite/libgomp.fortran/allocate-2.f90: Remove commented lines. + Add deallocate. Remove omp_atk_pool_size trait. + 2022-03-29 Andrew Stubbs * plugin/plugin-nvptx.c (GOMP_OFFLOAD_supported_features): Allow diff --git a/libgomp/testsuite/libgomp.fortran/allocate-2.f90 b/libgomp/testsuite/libgomp.fortran/allocate-2.f90 index fdd69d7de18..2219f107fe7 100644 --- a/libgomp/testsuite/libgomp.fortran/allocate-2.f90 +++ b/libgomp/testsuite/libgomp.fortran/allocate-2.f90 @@ -17,36 +17,32 @@ contains subroutine foo (x, y, h) use omp_lib - !use iso_c_binding integer :: x integer :: y integer (kind=omp_allocator_handle_kind) :: h integer, allocatable :: var1 - !integer, allocatable :: var2(:) !$omp allocate (var1) allocator(h) allocate (var1) - !y = 1 if (is_64bit_aligned(var1) == 0) then stop 19 end if + deallocate(var1) end subroutine end module m program main use omp_lib use m - type (omp_alloctrait) :: traits(3) + type (omp_alloctrait) :: traits(2) integer (omp_allocator_handle_kind) :: a traits = [omp_alloctrait (omp_atk_alignment, 64), & - omp_alloctrait (omp_atk_fallback, omp_atv_null_fb), & - omp_alloctrait (omp_atk_pool_size, 8192)] - a = omp_init_allocator (omp_default_mem_space, 3, traits) + omp_alloctrait (omp_atk_fallback, omp_atv_null_fb)] + a = omp_init_allocator (omp_default_mem_space, 2, traits) if (a == omp_null_allocator) stop 1 - !call omp_set_default_allocator (omp_default_mem_alloc); call foo (42, 12, a); call omp_destroy_allocator (a); end