Skip to content

Commit

Permalink
[flang][hlfir] Preserve polymorphism for the result of hlfir.transpose.
Browse files Browse the repository at this point in the history
Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D155912
  • Loading branch information
vzakhari committed Jul 21, 2023
1 parent ea7d6a1 commit a262081
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
5 changes: 3 additions & 2 deletions flang/lib/Lower/HlfirIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,8 +278,9 @@ mlir::Value HlfirTransposeLowering::lowerImpl(
mlir::Type elementType = array.getEleTy();
resultShape.push_back(arrayShape[0]);
resultShape.push_back(arrayShape[1]);
mlir::Type resultTy = hlfir::ExprType::get(
builder.getContext(), resultShape, elementType, /*polymorphic=*/false);
mlir::Type resultTy =
hlfir::ExprType::get(builder.getContext(), resultShape, elementType,
fir::isPolymorphicType(stmtResultType));
return createOp<hlfir::TransposeOp>(resultTy, operands[0]);
}

Expand Down
19 changes: 18 additions & 1 deletion flang/test/Lower/HLFIR/transpose.f90
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
! Test lowering of TRANSPOSE intrinsic to HLFIR
! RUN: bbc -emit-hlfir -o - %s 2>&1 | FileCheck %s
! RUN: bbc -emit-hlfir --polymorphic-type -o - %s 2>&1 | FileCheck %s

subroutine transpose1(m, res)
integer :: m(1,2), res(2, 1)
Expand Down Expand Up @@ -45,3 +45,20 @@ subroutine transpose3(m, res)
! CHECK-NEXT: hlfir.destroy %[[EXPR]]
! CHECK-NEXT: return
! CHECK-NEXT: }

! Test that the result type is polymorphic.
subroutine test_polymorphic_result(m, res)
class(*), allocatable, dimension(:, :) :: m, res
res = transpose(m)
end subroutine test_polymorphic_result
! CHECK-LABEL: func.func @_QPtest_polymorphic_result(
! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>> {fir.bindc_name = "m"},
! CHECK-SAME: %[[VAL_1:.*]]: !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>> {fir.bindc_name = "res"}) {
! CHECK: %[[VAL_2:.*]]:2 = hlfir.declare %[[VAL_0]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest_polymorphic_resultEm"} : (!fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>) -> (!fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>, !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>)
! CHECK: %[[VAL_3:.*]]:2 = hlfir.declare %[[VAL_1]] {fortran_attrs = #fir.var_attrs<allocatable>, uniq_name = "_QFtest_polymorphic_resultEres"} : (!fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>) -> (!fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>, !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>)
! CHECK: %[[VAL_4:.*]] = fir.load %[[VAL_2]]#0 : !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>
! CHECK: %[[VAL_5:.*]] = hlfir.transpose %[[VAL_4]] : (!fir.class<!fir.heap<!fir.array<?x?xnone>>>) -> !hlfir.expr<?x?xnone?>
! CHECK: hlfir.assign %[[VAL_5]] to %[[VAL_3]]#0 realloc : !hlfir.expr<?x?xnone?>, !fir.ref<!fir.class<!fir.heap<!fir.array<?x?xnone>>>>
! CHECK: hlfir.destroy %[[VAL_5]] : !hlfir.expr<?x?xnone?>
! CHECK: return
! CHECK: }

0 comments on commit a262081

Please sign in to comment.