[flang-commits] [flang] [flang] do not finalize or initialize unused entry dummy (PR #125482)

via flang-commits flang-commits at lists.llvm.org
Mon Feb 3 03:30:16 PST 2025


https://github.com/jeanPerier created https://github.com/llvm/llvm-project/pull/125482

None

>From 90742f966cb95be8295e99f6bb5724203b5a744e Mon Sep 17 00:00:00 2001
From: Jean Perier <jperier at nvidia.com>
Date: Mon, 3 Feb 2025 03:26:22 -0800
Subject: [PATCH] [flang] do not finalize or initialize unused entry dummy

---
 flang/lib/Lower/ConvertVariable.cpp       |  9 ++++++++
 flang/test/Lower/entry-statement-init.f90 | 26 +++++++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 flang/test/Lower/entry-statement-init.f90

diff --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 87236dc293ebbcf..d7ef1e9478a3ab2 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 000000000000000..731ccebef6873c9
--- /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