diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -612,6 +612,9 @@ static void deallocateIntentOut(Fortran::lower::AbstractConverter &converter, const Fortran::lower::pft::Variable &var, Fortran::lower::SymMap &symMap) { + if (!var.hasSymbol()) + return; + const Fortran::semantics::Symbol &sym = var.getSymbol(); if (Fortran::semantics::IsDummy(sym) && Fortran::semantics::IsIntentOut(sym) && @@ -619,6 +622,11 @@ if (auto symbox = symMap.lookupSymbol(sym)) { fir::ExtendedValue extVal = symbox.toExtendedValue(); if (auto mutBox = extVal.getBoxOf()) { + // The dummy argument is not passed in the ENTRY so it should not be + // deallocated. + if (mlir::Operation* op = mutBox->getAddr().getDefiningOp()) + if (mlir::isa(op)) + return; mlir::Location loc = converter.getCurrentLocation(); if (Fortran::semantics::IsOptional(sym)) { fir::FirOpBuilder &builder = converter.getFirOpBuilder(); diff --git a/flang/test/Lower/intentout-deallocate.f90 b/flang/test/Lower/intentout-deallocate.f90 --- a/flang/test/Lower/intentout-deallocate.f90 +++ b/flang/test/Lower/intentout-deallocate.f90 @@ -141,5 +141,53 @@ ! CHECK: fir.store %[[EMBOX]] to %[[ARG0]] : !fir.ref>>> ! CHECK: } + subroutine sub10(a) + integer, intent(out), allocatable :: a(:) + + entry sub11 + end subroutine + +! CHECK-LABEL: func.func @_QMmod1Psub10( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref>>> {fir.bindc_name = "a"}) { +! CHECK: %[[LOAD:.*]] = fir.load %[[ARG0]] : !fir.ref>>> +! CHECK: %[[BOX_ADDR:.*]] = fir.box_addr %[[LOAD]] : (!fir.box>>) -> !fir.heap> +! CHECK: fir.freemem %[[BOX_ADDR]] : !fir.heap> +! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.heap> +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1> +! CHECK: %[[EMBOX:.*]] = fir.embox %[[ZERO]](%[[SHAPE]]) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> +! CHECK: fir.store %[[EMBOX]] to %[[ARG0]] : !fir.ref>>> + +! CHECK-LABEL: func.func @_QMmod1Psub11() { +! CHECK-NOT: fir.freemem + + subroutine sub12(a) + integer, intent(out), allocatable :: a(:) + entry sub13(a) + end subroutine + +! CHECK-LABEL: func.func @_QMmod1Psub12( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref>>> {fir.bindc_name = "a"}) { +! CHECK: %[[LOAD:.*]] = fir.load %[[ARG0]] : !fir.ref>>> +! CHECK: %[[BOX_ADDR:.*]] = fir.box_addr %[[LOAD]] : (!fir.box>>) -> !fir.heap> +! CHECK: fir.freemem %[[BOX_ADDR]] : !fir.heap> +! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.heap> +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1> +! CHECK: %[[EMBOX:.*]] = fir.embox %[[ZERO]](%[[SHAPE]]) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> +! CHECK: fir.store %[[EMBOX]] to %[[ARG0]] : !fir.ref>>> + +! CHECK-LABEL: func.func @_QMmod1Psub13( +! CHECK-SAME: %[[ARG0:.*]]: !fir.ref>>> {fir.bindc_name = "a"}) { +! CHECK: %[[LOAD:.*]] = fir.load %[[ARG0]] : !fir.ref>>> +! CHECK: %[[BOX_ADDR:.*]] = fir.box_addr %[[LOAD]] : (!fir.box>>) -> !fir.heap> +! CHECK: fir.freemem %[[BOX_ADDR]] : !fir.heap> +! CHECK: %[[ZERO:.*]] = fir.zero_bits !fir.heap> +! CHECK: %[[C0:.*]] = arith.constant 0 : index +! CHECK: %[[SHAPE:.*]] = fir.shape %[[C0]] : (index) -> !fir.shape<1> +! CHECK: %[[EMBOX:.*]] = fir.embox %[[ZERO]](%[[SHAPE]]) : (!fir.heap>, !fir.shape<1>) -> !fir.box>> +! CHECK: fir.store %[[EMBOX]] to %[[ARG0]] : !fir.ref>>> + + end module