]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fix a crash due to mismatch of free and GOMP_alloc.
authorHafiz Abid Qadeer <abidh@codesourcery.com>
Wed, 30 Mar 2022 17:52:22 +0000 (18:52 +0100)
committerHafiz Abid Qadeer <abidh@codesourcery.com>
Fri, 1 Apr 2022 13:15:53 +0000 (14:15 +0100)
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:

* omp-low.c (lower_omp_allocate): Move allocate declaration
inside loop.  Set it to false at the end of condition.

libgomp/ChangeLog.omp:
* testsuite/libgomp.fortran/allocate-2.f90: Remove commented lines.
Add deallocate.  Remove omp_atk_pool_size trait.

gcc/ChangeLog.omp
gcc/omp-low.c
libgomp/ChangeLog.omp
libgomp/testsuite/libgomp.fortran/allocate-2.f90

index 6dba86714285e91c52168fb17069cdb5607f98b9..ef220fa9796b8beff7b24d71487f0b782423eee0 100644 (file)
@@ -1,3 +1,8 @@
+2022-03-31  Abid Qadeer  <abidh@codesourcery.com>
+
+       * omp-low.c (lower_omp_allocate): Move allocate declaration
+       inside loop.  Set it to false at the end of condition.
+
 2022-03-30  Andrew Stubbs  <ams@codesourcery.com>
 
        * omp-builtins.def (BUILT_IN_GOMP_ENABLE_PINNED_MODE): New.
index 4e8ab9e4ca09240aeca214131f9e354b2c029972..a2fec50192c54abd6fcaffc42804f7e4ff4d486d 100644 (file)
@@ -9254,13 +9254,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.  */
@@ -9321,6 +9321,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
index 2273a2f0fa801a9d431b819c6ddb5c4ee9e505e3..299b8169031278dbc61f6e865d9a562886fa7e8e 100644 (file)
@@ -1,3 +1,8 @@
+2022-03-31  Abid Qadeer  <abidh@codesourcery.com>
+
+       * testsuite/libgomp.fortran/allocate-2.f90: Remove commented lines.
+       Add deallocate.  Remove omp_atk_pool_size trait.
+
 2022-03-29  Andrew Stubbs  <ams@codesourcery.com>
 
        * plugin/plugin-nvptx.c (GOMP_OFFLOAD_supported_features): Allow
index fdd69d7de18efe16f487db8d47d9669e0e8fbb60..2219f107fe737121cf9d13e83e7fb909d9dd060a 100644 (file)
@@ -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