[flang-commits] [flang] d311cb6 - [flang] Recognize unused dummy arguments during lowering with HLFIR.

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Apr 26 08:56:44 PDT 2023


Author: Slava Zakharin
Date: 2023-04-26T08:56:34-07:00
New Revision: d311cb64a7d339c8d2be5351edaebd9bc21ebfe6

URL: https://github.com/llvm/llvm-project/commit/d311cb64a7d339c8d2be5351edaebd9bc21ebfe6
DIFF: https://github.com/llvm/llvm-project/commit/d311cb64a7d339c8d2be5351edaebd9bc21ebfe6.diff

LOG: [flang] Recognize unused dummy arguments during lowering with HLFIR.

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

Added: 
    flang/test/HLFIR/dummy_deallocation.f90

Modified: 
    flang/lib/Lower/ConvertVariable.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertVariable.cpp b/flang/lib/Lower/ConvertVariable.cpp
index 4d0375bfad4eb..451f1e2fbc134 100644
--- a/flang/lib/Lower/ConvertVariable.cpp
+++ b/flang/lib/Lower/ConvertVariable.cpp
@@ -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
index 0000000000000..9d3c51c843bcc
--- /dev/null
+++ b/flang/test/HLFIR/dummy_deallocation.f90
@@ -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


        


More information about the flang-commits mailing list