[flang-commits] [flang] a262081 - [flang][hlfir] Preserve polymorphism for the result of hlfir.transpose.

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Jul 21 12:57:08 PDT 2023


Author: Slava Zakharin
Date: 2023-07-21T12:56:51-07:00
New Revision: a262081a22001b59073a7bd1ce8bd1dd33e288c4

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

LOG: [flang][hlfir] Preserve polymorphism for the result of hlfir.transpose.

Reviewed By: kiranchandramohan

Differential Revision: https://reviews.llvm.org/D155912

Added: 
    

Modified: 
    flang/lib/Lower/HlfirIntrinsics.cpp
    flang/test/Lower/HLFIR/transpose.f90

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/HlfirIntrinsics.cpp b/flang/lib/Lower/HlfirIntrinsics.cpp
index e55cb0f82934a8..1b8631e0adeb3b 100644
--- a/flang/lib/Lower/HlfirIntrinsics.cpp
+++ b/flang/lib/Lower/HlfirIntrinsics.cpp
@@ -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]);
 }
 

diff  --git a/flang/test/Lower/HLFIR/transpose.f90 b/flang/test/Lower/HLFIR/transpose.f90
index f15028c4742694..70ae3724d1ccb2 100644
--- a/flang/test/Lower/HLFIR/transpose.f90
+++ b/flang/test/Lower/HLFIR/transpose.f90
@@ -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)
@@ -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:         }


        


More information about the flang-commits mailing list