[flang-commits] [flang] 71479f5 - [flang] lower transpose intrinsic to hlfir.transpose operation

Tom Eccles via flang-commits flang-commits at lists.llvm.org
Tue Feb 28 07:22:31 PST 2023


Author: Tom Eccles
Date: 2023-02-28T15:21:25Z
New Revision: 71479f5e3a21d02dc0e4b9d8c122c63d734af56e

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

LOG: [flang] lower transpose intrinsic to hlfir.transpose operation

Depends on D144880

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

Added: 
    flang/test/Lower/HLFIR/transpose.f90

Modified: 
    flang/lib/Lower/ConvertCall.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Lower/ConvertCall.cpp b/flang/lib/Lower/ConvertCall.cpp
index 53bd06696558..0fe48585126d 100644
--- a/flang/lib/Lower/ConvertCall.cpp
+++ b/flang/lib/Lower/ConvertCall.cpp
@@ -1317,6 +1317,24 @@ genHLFIRIntrinsicRefCore(PreparedActualArguments &loweredActuals,
 
     return {hlfir::EntityWithAttributes{matmulOp.getResult()}};
   }
+  if (intrinsic.name == "transpose") {
+    llvm::SmallVector<mlir::Value> operands = getOperandVector(loweredActuals);
+    hlfir::ExprType::Shape resultShape;
+    mlir::Type normalisedResult =
+        hlfir::getFortranElementOrSequenceType(*callContext.resultType);
+    auto array = normalisedResult.cast<fir::SequenceType>();
+    llvm::ArrayRef<int64_t> arrayShape = array.getShape();
+    assert(arrayShape.size() == 2 && "arguments to transpose have a rank of 2");
+    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);
+    hlfir::TransposeOp transposeOp =
+        builder.create<hlfir::TransposeOp>(loc, resultTy, operands[0]);
+
+    return {hlfir::EntityWithAttributes{transposeOp.getResult()}};
+  }
 
   // TODO add hlfir operations for other transformational intrinsics here
 

diff  --git a/flang/test/Lower/HLFIR/transpose.f90 b/flang/test/Lower/HLFIR/transpose.f90
new file mode 100644
index 000000000000..7d5005d6d574
--- /dev/null
+++ b/flang/test/Lower/HLFIR/transpose.f90
@@ -0,0 +1,17 @@
+! Test lowering of TRANSPOSE intrinsic to HLFIR
+! RUN: bbc -emit-fir -hlfir -o - %s 2>&1 | FileCheck %s
+
+subroutine transpose1(m, res)
+  integer :: m(1,2), res(2, 1)
+  res = TRANSPOSE(m)
+endsubroutine
+! CHECK-LABEL: func.func @_QPtranspose1
+! CHECK:           %[[M_ARG:.*]]: !fir.ref<!fir.array<1x2xi32>>
+! CHECK:           %[[RES_ARG:.*]]: !fir.ref<!fir.array<2x1xi32>>
+! CHECK-DAG:     %[[ARG:.*]]:2 = hlfir.declare %[[M_ARG]](%[[M_SHAPE:.*]]) {[[NAME:.*]]} : (!fir.ref<!fir.array<1x2xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<1x2xi32>>, !fir.ref<!fir.array<1x2xi32>>)
+! CHECK-DAG:     %[[RES:.*]]:2 = hlfir.declare %[[RES_ARG]](%[[RES_SHAPE:.*]]) {[[NAME2:.*]]} : (!fir.ref<!fir.array<2x1xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<2x1xi32>>, !fir.ref<!fir.array<2x1xi32>>)
+! CHECK:         %[[EXPR:.*]] = hlfir.transpose %[[ARG]]#0 : (!fir.ref<!fir.array<1x2xi32>>) -> !hlfir.expr<2x1xi32>
+! CHECK-NEXT:    hlfir.assign %[[EXPR]] to %[[RES]]#0
+! CHECK-NEXT:    hlfir.destroy %[[EXPR]]
+! CHECK-NEXT:    return
+! CHECK-NEXT:  }


        


More information about the flang-commits mailing list