240 lines · plain
1! Test automatic deallocation of local allocatables as described in2! Fortran 2018 standard 9.7.3.2 point 2. and 3.3 4! RUN: bbc -emit-hlfir -o - %s | FileCheck %s5module dtypedef6 type must_finalize7 integer :: i8 contains9 final :: finalize10 end type11 type contain_must_finalize12 type(must_finalize) :: a13 end type14 interface15 subroutine finalize(a)16 import :: must_finalize17 type(must_finalize), intent(inout) :: a18 end subroutine19 end interface20 real, allocatable :: x21end module22 23subroutine simple()24 real, allocatable :: x25 allocate(x)26 call bar()27end subroutine28! CHECK-LABEL: func.func @_QPsimple() {29! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare {{.*}}"_QFsimpleEx"30! CHECK: fir.call @_QPbar31! CHECK: %[[VAL_6:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<f32>>>32! CHECK: %[[VAL_7:.*]] = fir.box_addr %[[VAL_6]] : (!fir.box<!fir.heap<f32>>) -> !fir.heap<f32>33! CHECK: %[[VAL_8:.*]] = fir.convert %[[VAL_7]] : (!fir.heap<f32>) -> i6434! CHECK: %[[VAL_9:.*]] = arith.constant 0 : i6435! CHECK: %[[VAL_10:.*]] = arith.cmpi ne, %[[VAL_8]], %[[VAL_9]] : i6436! CHECK: fir.if %[[VAL_10]] {37! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<f32>>>38! CHECK: %[[VAL_12:.*]] = fir.box_addr %[[VAL_11]] : (!fir.box<!fir.heap<f32>>) -> !fir.heap<f32>39! CHECK: fir.freemem %[[VAL_12]] : !fir.heap<f32>40! CHECK: %[[VAL_13:.*]] = fir.zero_bits !fir.heap<f32>41! CHECK: %[[VAL_14:.*]] = fir.embox %[[VAL_13]] : (!fir.heap<f32>) -> !fir.box<!fir.heap<f32>>42! CHECK: fir.store %[[VAL_14]] to %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<f32>>>43! CHECK: }44 45subroutine multiple_return(cdt)46 real, allocatable :: x47 logical :: cdt48 allocate(x)49 if (cdt) return50 call bar()51end subroutine52! CHECK-LABEL: func.func @_QPmultiple_return(53! CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb254! CHECK: ^bb1:55! CHECK-NOT: fir.freemem56! CHECK: cf.br ^bb357! CHECK: ^bb2:58! CHECK: fir.call @_QPbar59! CHECK: cf.br ^bb360! CHECK: ^bb3:61! CHECK: fir.if {{.*}} {62! CHECK: fir.freemem63! CHECK: }64! CHECK: return65 66subroutine derived()67 use dtypedef, only : must_finalize68 type(must_finalize), allocatable :: x69 allocate(x)70 call bar()71end subroutine72! CHECK-LABEL: func.func @_QPderived() {73! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare {{.*}}"_QFderivedEx"74! CHECK: fir.call @_QPbar75! CHECK: %[[VAL_11:.*]] = fir.load %[[VAL_3]]#0 : !fir.ref<!fir.box<!fir.heap<!fir.type<_QMdtypedefTmust_finalize{i:i32}>>>>76! CHECK: %[[VAL_12:.*]] = fir.box_addr %[[VAL_11]] : (!fir.box<!fir.heap<!fir.type<_QMdtypedefTmust_finalize{i:i32}>>>) -> !fir.heap<!fir.type<_QMdtypedefTmust_finalize{i:i32}>>77! CHECK: %[[VAL_13:.*]] = fir.convert %[[VAL_12]] : (!fir.heap<!fir.type<_QMdtypedefTmust_finalize{i:i32}>>) -> i6478! CHECK: %[[VAL_14:.*]] = arith.constant 0 : i6479! CHECK: %[[VAL_15:.*]] = arith.cmpi ne, %[[VAL_13]], %[[VAL_14]] : i6480! CHECK: fir.if %[[VAL_15]] {81! CHECK: %[[VAL_16:.*]] = arith.constant false82! CHECK: %[[VAL_17:.*]] = fir.absent !fir.box<none>83! CHECK: %[[VAL_20:.*]] = fir.convert %[[VAL_3]]#0 : (!fir.ref<!fir.box<!fir.heap<!fir.type<_QMdtypedefTmust_finalize{i:i32}>>>>) -> !fir.ref<!fir.box<none>>84! CHECK: %[[VAL_22:.*]] = fir.call @_FortranAAllocatableDeallocate(%[[VAL_20]], %[[VAL_16]], %[[VAL_17]], %{{.*}}, %{{.*}})85! CHECK: }86 87subroutine derived2()88 use dtypedef, only : contain_must_finalize89 type(contain_must_finalize), allocatable :: x90 allocate(x)91end subroutine92! CHECK-LABEL: func.func @_QPderived2(93! CHECK: fir.if {{.*}} {94! CHECK: fir.call @_FortranAAllocatableDeallocate95! CHECK: }96 97subroutine simple_block()98 block99 real, allocatable :: x100 allocate(x)101 call bar()102 end block103 call bar_after_block()104end subroutine105! CHECK-LABEL: func.func @_QPsimple_block(106! CHECK: fir.call @_QPbar107! CHECK: fir.if {{.*}} {108! CHECK: fir.freemem109! CHECK: }110! CHECK: fir.call @_QPbar_after_block111 112subroutine mutiple_return_block(cdt)113 logical :: cdt114 block115 real, allocatable :: x116 allocate(x)117 if (cdt) return118 call bar()119 end block120 call bar_after_block()121end subroutine122! CHECK-LABEL: func.func @_QPmutiple_return_block(123! CHECK: cf.cond_br %{{.*}}, ^bb1, ^bb2124! CHECK: ^bb1:125! CHECK: fir.if {{.*}} {126! CHECK: fir.freemem127! CHECK: }128! CHECK: cf.br ^bb3129! CHECK: ^bb2:130! CHECK: fir.call @_QPbar131! CHECK: fir.if {{.*}} {132! CHECK: fir.freemem133! CHECK: }134! CHECK: fir.call @_QPbar_after_block135! CHECK: cf.br ^bb3136! CHECK: ^bb3:137! CHECK: return138 139 140subroutine derived_block()141 use dtypedef, only : must_finalize142 block143 type(must_finalize), allocatable :: x144 allocate(x)145 call bar()146 end block147 call bar_after_block()148end subroutine149! CHECK-LABEL: func.func @_QPderived_block(150! CHECK: fir.call @_QPbar151! CHECK: fir.if {{.*}} {152! CHECK: fir.call @_FortranAAllocatableDeallocate153! CHECK: }154! CHECK: fir.call @_QPbar_after_block155 156subroutine derived_block2()157 use dtypedef, only : contain_must_finalize158 call bar()159 block160 type(contain_must_finalize), allocatable :: x161 allocate(x)162 end block163 call bar_after_block()164end subroutine165! CHECK-LABEL: func.func @_QPderived_block2(166! CHECK: fir.call @_QPbar167! CHECK: fir.if {{.*}} {168! CHECK: fir.call @_FortranAAllocatableDeallocate169! CHECK: }170! CHECK: fir.call @_QPbar_after_block171 172subroutine no_dealloc_saved()173 real, allocatable, save :: x174 allocate(x)175end subroutine176! CHECK-LABEL: func.func @_QPno_dealloc_save177! CHECK-NOT: freemem178! CHECK-NOT: Deallocate179! CHECK: return180 181subroutine no_dealloc_block_saved()182 block183 real, allocatable, save :: x184 allocate(x)185 end block186end subroutine187! CHECK-LABEL: func.func @_QPno_dealloc_block_saved188! CHECK-NOT: freemem189! CHECK-NOT: Deallocate190! CHECK: return191 192function no_dealloc_result() result(x)193 real, allocatable :: x194 allocate(x)195end function196! CHECK-LABEL: func.func @_QPno_dealloc_result197! CHECK-NOT: freemem198! CHECK-NOT: Deallocate199! CHECK: return200 201subroutine no_dealloc_dummy(x)202 real, allocatable :: x203 allocate(x)204end subroutine205! CHECK-LABEL: func.func @_QPno_dealloc_dummy206! CHECK-NOT: freemem207! CHECK-NOT: Deallocate208! CHECK: return209 210subroutine no_dealloc_module_var()211 use dtypedef, only : x212 allocate(x)213end subroutine214! CHECK-LABEL: func.func @_QPno_dealloc_module_var215! CHECK-NOT: freemem216! CHECK-NOT: Deallocate217! CHECK: return218 219subroutine no_dealloc_host_assoc()220 real, allocatable :: x221 call internal()222contains223 subroutine internal()224 allocate(x)225 end subroutine226end subroutine227! CHECK-LABEL: func.func private @_QFno_dealloc_host_assocPinternal228! CHECK-NOT: freemem229! CHECK-NOT: Deallocate230! CHECK: return231 232subroutine no_dealloc_pointer(x)233 real, pointer :: x234 allocate(x)235end subroutine236! CHECK-LABEL: func.func @_QPno_dealloc_pointer237! CHECK-NOT: freemem238! CHECK-NOT: Deallocate239! CHECK: return240