[flang-commits] [flang] 22d9726 - [flang] do not finalize or initialize unused entry dummy (#125482)
via flang-commits
flang-commits at lists.llvm.org
Mon Feb 3 09:09:04 PST 2025
Author: jeanPerier
Date: 2025-02-03T18:09:01+01:00
New Revision: 22d9726593ef3e1f137a4f88d284747d20ec6cd9
URL: https://github.com/llvm/llvm-project/commit/22d9726593ef3e1f137a4f88d284747d20ec6cd9
DIFF: https://github.com/llvm/llvm-project/commit/22d9726593ef3e1f137a4f88d284747d20ec6cd9.diff
LOG: [flang] do not finalize or initialize unused entry dummy (#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.
Added:
flang/test/Lower/entry-statement-init.f90
Modified:
flang/lib/Lower/ConvertVariable.cpp
Removed:
################################################################################
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 87236dc293ebbc..81d14fbb1d7773 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 00000000000000..731ccebef6873c
--- /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
More information about the flang-commits
mailing list