Skip to content

Commit 22d9726

Browse files
authored
[flang] do not finalize or initialize unused entry dummy (llvm#125482)
Dummy arguments from other entry statement that are not live in the current entry have no backing storage, user code referring to them is not allowed to be reached. The compiler was generating initialization/destruction code for them when INTENT(OUT), causing undefined behaviors.
1 parent 30f3752 commit 22d9726

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

flang/lib/Lower/ConvertVariable.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -956,7 +956,15 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
956956
Fortran::lower::SymMap &symMap) {
957957
assert(!var.isAlias());
958958
Fortran::lower::StatementContext stmtCtx;
959+
// isUnusedEntryDummy must be computed before mapSymbolAttributes.
960+
const bool isUnusedEntryDummy =
961+
var.hasSymbol() && Fortran::semantics::IsDummy(var.getSymbol()) &&
962+
!symMap.lookupSymbol(var.getSymbol()).getAddr();
959963
mapSymbolAttributes(converter, var, symMap, stmtCtx);
964+
// Do not generate code to initialize/finalize/destroy dummy arguments that
965+
// are nor part of the current ENTRY. They do not have backing storage.
966+
if (isUnusedEntryDummy)
967+
return;
960968
deallocateIntentOut(converter, var, symMap);
961969
if (needDummyIntentoutFinalization(var))
962970
finalizeAtRuntime(converter, var, symMap);
@@ -999,7 +1007,6 @@ static void instantiateLocal(Fortran::lower::AbstractConverter &converter,
9991007
"trying to deallocate entity not lowered as allocatable");
10001008
Fortran::lower::genDeallocateIfAllocated(*converterPtr, *mutableBox,
10011009
loc, sym);
1002-
10031010
});
10041011
}
10051012
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
! RUN: bbc -emit-hlfir -o - %s | FileCheck %s
2+
3+
! Test initialization and finalizations of dummy arguments in entry statements.
4+
5+
module m
6+
type t
7+
end type
8+
contains
9+
subroutine test1(x)
10+
class(t), intent(out) :: x
11+
entry test1_entry()
12+
end subroutine
13+
subroutine test2(x)
14+
class(t), intent(out) :: x
15+
entry test2_entry(x)
16+
end subroutine
17+
end module
18+
! CHECK-LABEL: func.func @_QMmPtest1_entry(
19+
! CHECK-NOT: Destroy
20+
! CHECK-NOT: Initialize
21+
! CHECK: return
22+
23+
! CHECK-LABEL: func.func @_QMmPtest2_entry(
24+
! CHECK: Destroy
25+
! CHECK: Initialize
26+
! CHECK: return

0 commit comments

Comments
 (0)