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 @@ -721,9 +721,12 @@ 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)) + if (mlir::Operation *op = mutBox->getAddr().getDefiningOp()) { + if (auto declOp = mlir::dyn_cast(op)) + op = declOp.getMemref().getDefiningOp(); + if (op && mlir::isa(op)) return; + } mlir::Location loc = converter.getCurrentLocation(); fir::FirOpBuilder &builder = converter.getFirOpBuilder(); auto genDeallocateWithTypeDesc = [&]() { diff --git a/flang/test/HLFIR/dummy_deallocation.f90 b/flang/test/HLFIR/dummy_deallocation.f90 new file mode 100644 --- /dev/null +++ b/flang/test/HLFIR/dummy_deallocation.f90 @@ -0,0 +1,16 @@ +! RUN: bbc -emit-fir -hlfir %s -o - | FileCheck %s +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! Test that the intent(out) allocatable dummy argument +! is not deallocated in entry SUB_B. + +! CHECK-LABEL: func.func @_QPsub_a +! CHECK: fir.freemem + +! CHECK-LABEL: func.func @_QPsub_b +! CHECK-NOT: fir.freemem +SUBROUTINE SUB_A(A) + INTEGER, INTENT(out), ALLOCATABLE, DIMENSION (:) :: A + RETURN + ENTRY SUB_B +END SUBROUTINE SUB_A