Skip to content

Commit a10a073

Browse files
committed
[flang] Fix lowering of function calls that return arrays of derived types
When processing the return value of a function call, the lowering code was incorrectly calling the template function "genarr()" for "ProcedureRef" arguments rather than "FunctionRef" arguments. This caused the return type of the function to be specified as "llvm::None" which caused downstream problems. The problem was that the version of "genarr" for "FunctionRef" arguments was declared in such a way that it only was used for arrays whose elements were intrinsic types. I fixed this by implementing a more general version of "genarr" for "FunctionRef" arguments that works for both intrinsic types and derived types.
1 parent 6eb7893 commit a10a073

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5606,11 +5606,12 @@ class ArrayExprLowering {
56065606
return std::visit([&](const auto &v) { return genarr(v, &x); }, x.parent());
56075607
}
56085608

5609-
template <Fortran::common::TypeCategory TC, int KIND>
5610-
CC genarr(
5611-
const Fortran::evaluate::FunctionRef<Fortran::evaluate::Type<TC, KIND>>
5612-
&x) {
5613-
return genProcRef(x, {converter.genType(TC, KIND)});
5609+
template <typename T>
5610+
CC genarr(const Fortran::evaluate::FunctionRef<T> &funRef) {
5611+
// Note that it's possible that the function being called returns either an
5612+
// array or a scalar. In the first case, use the element type of the array.
5613+
return genProcRef(
5614+
funRef, fir::unwrapSequenceType(converter.genType(toEvExpr(funRef))));
56145615
}
56155616

56165617
//===--------------------------------------------------------------------===//

flang/test/Lower/intrinsic-procedures/eoshift.f90

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ subroutine eoshift_test3(arr, shift, dim)
5252
character(4), dimension(3,3) :: arr, res
5353
integer :: shift, dim
5454

55-
! CHECK: %[[resBox:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?x!fir.char<1,?>>>>
55+
! CHECK: %[[resBox:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?x!fir.char<1,4>>>>
5656
! CHECK: %[[arr:.*]]:2 = fir.unboxchar %arg0 : (!fir.boxchar<1>) -> (!fir.ref<!fir.char<1,?>>, index)
5757
! CHECK: %[[array:.*]] = fir.convert %[[arr]]#0 : (!fir.ref<!fir.char<1,?>>) -> !fir.ref<!fir.array<3x3x!fir.char<1,4>>>
5858
! CHECK: %[[res:.*]] = fir.alloca !fir.array<3x3x!fir.char<1,4>> {bindc_name = "res", uniq_name = "_QFeoshift_test3Eres"}
@@ -64,7 +64,7 @@ subroutine eoshift_test3(arr, shift, dim)
6464

6565
! CHECK: %[[boundBox:.*]] = fir.absent !fir.box<none>
6666
! CHECK: %[[shiftBox:.*]] = fir.embox %arg1 : (!fir.ref<i32>) -> !fir.box<i32>
67-
! CHECK: %[[resIRBox:.*]] = fir.convert %[[resBox]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?x!fir.char<1,?>>>>>) -> !fir.ref<!fir.box<none>>
67+
! CHECK: %[[resIRBox:.*]] = fir.convert %[[resBox]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?x!fir.char<1,4>>>>>) -> !fir.ref<!fir.box<none>>
6868
! CHECK: %[[arrayBoxNone:.*]] = fir.convert %[[arrayBox]] : (!fir.box<!fir.array<3x3x!fir.char<1,4>>>) -> !fir.box<none>
6969
! CHECK: %[[shiftBoxNone:.*]] = fir.convert %[[shiftBox]] : (!fir.box<i32>) -> !fir.box<none>
7070
! CHECK: %[[tmp:.*]] = fir.call @_FortranAEoshift(%[[resIRBox]], %[[arrayBoxNone]], %[[shiftBoxNone]], %[[boundBox]], %[[dim]], {{.*}}, {{.*}}) : (!fir.ref<!fir.box<none>>, !fir.box<none>, !fir.box<none>, !fir.box<none>, i32, !fir.ref<i8>, i32) -> none

0 commit comments

Comments
 (0)