diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -496,6 +496,8 @@ void genSystemClock(llvm::ArrayRef); fir::ExtendedValue genTransfer(mlir::Type, llvm::ArrayRef); + fir::ExtendedValue genTranspose(mlir::Type, + llvm::ArrayRef); fir::ExtendedValue genUbound(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genUnpack(mlir::Type, llvm::ArrayRef); fir::ExtendedValue genVerify(mlir::Type, llvm::ArrayRef); @@ -770,6 +772,10 @@ &I::genTransfer, {{{"source", asAddr}, {"mold", asAddr}, {"size", asValue}}}, /*isElemental=*/false}, + {"transpose", + &I::genTranspose, + {{{"matrix", asAddr}}}, + /*isElemental=*/false}, {"ubound", &I::genUbound, {{{"array", asBox}, {"dim", asValue}, {"kind", asValue}}}, @@ -2864,6 +2870,29 @@ return mlir::Value(); } +// TRANSPOSE +fir::ExtendedValue +IntrinsicLibrary::genTranspose(mlir::Type resultType, + llvm::ArrayRef args) { + + assert(args.size() == 1); + + // Handle source argument + mlir::Value source = builder.createBox(loc, args[0]); + + // Create mutable fir.box to be passed to the runtime for the result. + mlir::Type resultArrayType = builder.getVarLenSeqTy(resultType, 2); + fir::MutableBoxValue resultMutableBox = + fir::factory::createTempMutableBox(builder, loc, resultArrayType); + mlir::Value resultIrBox = + fir::factory::getMutableIRBox(builder, loc, resultMutableBox); + // Call runtime. The runtime is allocating the result. + fir::runtime::genTranspose(builder, loc, resultIrBox, source); + // Read result from mutable fir.box and add it to the list of temps to be + // finalized by the StatementContext. + return readAndAddCleanUp(resultMutableBox, resultType, + "unexpected result for TRANSPOSE"); +} // UNPACK fir::ExtendedValue IntrinsicLibrary::genUnpack(mlir::Type resultType, diff --git a/flang/test/Lower/Intrinsics/transpose.f90 b/flang/test/Lower/Intrinsics/transpose.f90 new file mode 100644 --- /dev/null +++ b/flang/test/Lower/Intrinsics/transpose.f90 @@ -0,0 +1,24 @@ +! RUN: bbc -emit-fir %s -o - | FileCheck %s + +! CHECK-LABEL: func @_QPtranspose_test( +! CHECK-SAME: %[[source:.*]]: !fir.ref>{{.*}}) { +subroutine transpose_test(mat) +! CHECK: %[[resultDescr:.*]] = fir.alloca !fir.box>> + real :: mat(2,3) + call bar_transpose_test(transpose(mat)) +! CHECK: %[[sourceBox:.*]] = fir.embox %[[source]]({{.*}}) : (!fir.ref>, !fir.shape<2>) -> !fir.box> +! CHECK: %[[zeroArray:.*]] = fir.zero_bits !fir.heap +! CHECK: %[[c0:.*]] = arith.constant 0 : index +! CHECK: %[[shapeResult:.*]] = fir.shape %[[c0]], %[[c0]] : (index, index) -> !fir.shape<2> +! CHECK: %[[resultBox:.*]] = fir.embox %[[zeroArray]](%[[shapeResult]]) : (!fir.heap>, !fir.shape<2>) -> !fir.box>> +! CHECK: fir.store %[[resultBox]] to %[[resultDescr]] : !fir.ref>>> +! CHECK: %[[resultOpaque:.*]] = fir.convert %[[resultDescr]] : (!fir.ref>>>) -> !fir.ref> +! CHECK: %[[sourceOpaque:.*]] = fir.convert %[[sourceBox]] : (!fir.box>) -> !fir.box +! CHECK: %{{.*}} = fir.call @_FortranATranspose(%[[resultOpaque]], %[[sourceOpaque]], %{{.*}}, %{{.*}}) : (!fir.ref>, !fir.box, !fir.ref, i32) -> none +! CHECK: %[[tmp1:.*]] = fir.load %[[resultDescr]] : !fir.ref>>> +! CHECK: %[[tmp2:.*]] = fir.box_addr %[[tmp1]] : (!fir.box>>) -> !fir.heap> +! CHECK: %[[tmp3:.*]] = fir.convert %[[tmp2]] : (!fir.heap>) -> !fir.ref> +! CHECK: fir.call @_QPbar_transpose_test(%[[tmp3]]) : (!fir.ref>) -> () +! CHECK: fir.freemem %[[tmp2]] : +end subroutine +