From fa08e97d03afd215caeb297a822895c4d0d93b7b Mon Sep 17 00:00:00 2001 From: jeanPerier Date: Thu, 20 Jun 2024 09:01:07 +0200 Subject: [PATCH] [flang] lower assumed-rank TARGET to intent(in) POINTER (#96082) The only special thing to do is to use fir.rebox_assumed_rank when reboxing the target to properly set the POINTER attribute inside the descriptor. --- flang/lib/Lower/ConvertCall.cpp | 3 --- flang/lib/Optimizer/Builder/MutableBox.cpp | 9 ++++++++- flang/test/Lower/HLFIR/assumed-rank-calls.f90 | 18 ++++++++++++++++++ 3 files changed, 26 insertions(+), 4 deletions(-) diff --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp index 382419a1c326..5e20f9eee4fc 100644 --- a/flang/lib/Lower/ConvertCall.cpp +++ b/flang/lib/Lower/ConvertCall.cpp @@ -1597,9 +1597,6 @@ void prepareUserCallArguments( if (dataTy.isAssumedRank()) { dataTy = dataTy.getBoxTypeWithNewShape(fir::getBase(actualExv).getType()); - if (dataTy.isAssumedRank()) - TODO(loc, "associating assumed-rank target to pointer assumed-rank " - "argument"); } mlir::Value irBox = builder.createTemporary(loc, dataTy); fir::MutableBoxValue ptrBox(irBox, diff --git a/flang/lib/Optimizer/Builder/MutableBox.cpp b/flang/lib/Optimizer/Builder/MutableBox.cpp index 16e543fe86a7..52d149fa7b9a 100644 --- a/flang/lib/Optimizer/Builder/MutableBox.cpp +++ b/flang/lib/Optimizer/Builder/MutableBox.cpp @@ -527,7 +527,14 @@ void fir::factory::associateMutableBox(fir::FirOpBuilder &builder, mlir::ValueRange newLbounds = lbounds.empty() ? mlir::ValueRange{arr.getLBounds()} : mlir::ValueRange{lbounds}; - if (box.isDescribedByVariables()) { + if (box.hasAssumedRank()) { + assert(arr.hasAssumedRank() && + "expect both arr and box to be assumed-rank"); + mlir::Value reboxed = builder.create( + loc, box.getBoxTy(), arr.getAddr(), + fir::LowerBoundModifierAttribute::Preserve); + writer.updateWithIrBox(reboxed); + } else if (box.isDescribedByVariables()) { // LHS is a contiguous pointer described by local variables. Open RHS // fir.box to update the LHS. auto rawAddr = builder.create(loc, arr.getMemTy(), diff --git a/flang/test/Lower/HLFIR/assumed-rank-calls.f90 b/flang/test/Lower/HLFIR/assumed-rank-calls.f90 index 071589bf9514..9d4503fef6fc 100644 --- a/flang/test/Lower/HLFIR/assumed-rank-calls.f90 +++ b/flang/test/Lower/HLFIR/assumed-rank-calls.f90 @@ -40,6 +40,24 @@ end subroutine ! CHECK: return ! CHECK: } +subroutine test_target_to_pointer(x) + real, target :: x(..) + interface + subroutine takes_target_as_pointer(x) + real, pointer, intent(in) :: x(..) + end subroutine + end interface + call takes_target_as_pointer(x) +end subroutine +! CHECK-LABEL: func.func @_QPtest_target_to_pointer( +! CHECK-SAME: %[[VAL_0:.*]]: !fir.box> {fir.bindc_name = "x", fir.target}) { +! CHECK: %[[VAL_1:.*]] = fir.alloca !fir.box>> +! CHECK: %[[VAL_2:.*]] = fir.dummy_scope : !fir.dscope +! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_0]] dummy_scope %[[VAL_2]] {fortran_attrs = #fir.var_attrs, uniq_name = "_QFtest_target_to_pointerEx"} : (!fir.box>, !fir.dscope) -> (!fir.box>, !fir.box>) +! CHECK: %[[VAL_4:.*]] = fir.rebox_assumed_rank %[[VAL_3]]#0 lbs preserve : (!fir.box>) -> !fir.box>> +! CHECK: fir.store %[[VAL_4]] to %[[VAL_1]] : !fir.ref>>> +! CHECK: fir.call @_QPtakes_target_as_pointer(%[[VAL_1]]) fastmath : (!fir.ref>>>) -> () + subroutine test_poly_to_nonepoly(x) type t integer :: i