Skip to content

Conversation

jeanPerier
Copy link
Contributor

No description provided.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir labels Feb 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Feb 3, 2025

@llvm/pr-subscribers-flang-fir-hlfir

Author: None (jeanPerier)

Changes

Full diff: https://github.com/llvm/llvm-project/pull/125482.diff

2 Files Affected:

  • (modified) flang/lib/Lower/ConvertVariable.cpp (+9)
  • (added) flang/test/Lower/entry-statement-init.f90 (+26)
diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 87236dc293ebbc..d7ef1e9478a3ab 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -956,7 +956,16 @@ 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()
+                      : false;
   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);
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

@jeanPerier jeanPerier merged commit 22d9726 into llvm:main Feb 3, 2025
8 checks passed
Icohedron pushed a commit to Icohedron/llvm-project that referenced this pull request Feb 11, 2025
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:fir-hlfir flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants