Skip to content

Commit

Permalink
[flang] lower transpose intrinsic to hlfir.transpose operation
Browse files Browse the repository at this point in the history
Depends on D144880

Differential Revision: https://reviews.llvm.org/D144881
  • Loading branch information
tblah committed Feb 28, 2023
1 parent 7f7ebff commit 71479f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions flang/lib/Lower/ConvertCall.cpp
Expand Up @@ -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

Expand Down
17 changes: 17 additions & 0 deletions 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: }

0 comments on commit 71479f5

Please sign in to comment.