diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp index 87236dc293ebb..81d14fbb1d777 100644 --- a/flang/lib/Lower/ConvertVariable.cpp +++ b/flang/lib/Lower/ConvertVariable.cpp @@ -956,7 +956,15 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter, Fortran::lower::SymMap &symMap) { assert(!var.isAlias()); Fortran::lower::StatementContext stmtCtx; + // isUnusedEntryDummy must be computed before mapSymbolAttributes. + const bool isUnusedEntryDummy = + var.hasSymbol() && Fortran::semantics::IsDummy(var.getSymbol()) && + !symMap.lookupSymbol(var.getSymbol()).getAddr(); mapSymbolAttributes(converter, var, symMap, stmtCtx); + // Do not generate code to initialize/finalize/destroy dummy arguments that + // are nor part of the current ENTRY. They do not have backing storage. + if (isUnusedEntryDummy) + return; deallocateIntentOut(converter, var, symMap); if (needDummyIntentoutFinalization(var)) finalizeAtRuntime(converter, var, symMap); @@ -999,7 +1007,6 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter, "trying to deallocate entity not lowered as allocatable"); Fortran::lower::genDeallocateIfAllocated(*converterPtr, *mutableBox, loc, sym); - }); } } diff --git a/flang/test/Lower/entry-statement-init.f90 b/flang/test/Lower/entry-statement-init.f90 new file mode 100644 index 0000000000000..731ccebef6873 --- /dev/null +++ b/flang/test/Lower/entry-statement-init.f90 @@ -0,0 +1,26 @@ +! RUN: bbc -emit-hlfir -o - %s | FileCheck %s + +! Test initialization and finalizations of dummy arguments in entry statements. + +module m + type t + end type +contains + subroutine test1(x) + class(t), intent(out) :: x + entry test1_entry() + end subroutine + subroutine test2(x) + class(t), intent(out) :: x + entry test2_entry(x) + end subroutine +end module +! CHECK-LABEL: func.func @_QMmPtest1_entry( +! CHECK-NOT: Destroy +! CHECK-NOT: Initialize +! CHECK: return + +! CHECK-LABEL: func.func @_QMmPtest2_entry( +! CHECK: Destroy +! CHECK: Initialize +! CHECK: return