[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
This commit is contained in:
Slava Zakharin 2023-04-25 22:03:57 -07:00
parent 85a13c716f
commit d311cb64a7
2 changed files with 21 additions and 2 deletions

View 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 = [&]() {

View File

@ -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