[flang] Recognize unused dummy arguments during lowering with HLFIR.
authorSlava Zakharin <szakharin@nvidia.com>
Wed, 26 Apr 2023 05:03:57 +0000 (22:03 -0700)
committerSlava Zakharin <szakharin@nvidia.com>
Wed, 26 Apr 2023 15:56:34 +0000 (08:56 -0700)
So far we've relied on AllocaOp to represent the dummy arguments
not declared for the current entry. With HLFIR we have to account
for hlfir::DeclareOp.

Differential Revision: https://reviews.llvm.org/D149231

flang/lib/Lower/ConvertVariable.cpp
flang/test/HLFIR/dummy_deallocation.f90 [new file with mode: 0644]

index 4d0375b..451f1e2 100644 (file)
@@ -721,9 +721,12 @@ static void deallocateIntentOut(Fortran::lower::AbstractConverter &converter,
     if (auto mutBox = extVal.getBoxOf<fir::MutableBoxValue>()) {
       // The dummy argument is not passed in the ENTRY so it should not be
       // deallocated.
-      if (mlir::Operation *op = mutBox->getAddr().getDefiningOp())
-        if (mlir::isa<fir::AllocaOp>(op))
+      if (mlir::Operation *op = mutBox->getAddr().getDefiningOp()) {
+        if (auto declOp = mlir::dyn_cast<hlfir::DeclareOp>(op))
+          op = declOp.getMemref().getDefiningOp();
+        if (op && mlir::isa<fir::AllocaOp>(op))
           return;
+      }
       mlir::Location loc = converter.getCurrentLocation();
       fir::FirOpBuilder &builder = converter.getFirOpBuilder();
       auto genDeallocateWithTypeDesc = [&]() {
diff --git a/flang/test/HLFIR/dummy_deallocation.f90 b/flang/test/HLFIR/dummy_deallocation.f90
new file mode 100644 (file)
index 0000000..9d3c51c
--- /dev/null
@@ -0,0 +1,16 @@
+! RUN: bbc -emit-fir -hlfir %s -o - | FileCheck %s
+! RUN: bbc -emit-fir %s -o - | FileCheck %s
+
+! Test that the intent(out) allocatable dummy argument
+! is not deallocated in entry SUB_B.
+
+! CHECK-LABEL: func.func @_QPsub_a
+! CHECK: fir.freemem
+
+! CHECK-LABEL: func.func @_QPsub_b
+! CHECK-NOT: fir.freemem
+SUBROUTINE SUB_A(A)
+  INTEGER, INTENT(out), ALLOCATABLE, DIMENSION (:) :: A
+  RETURN
+  ENTRY SUB_B
+END SUBROUTINE SUB_A