[flang-commits] [flang] 24322fc - [flang] lower hlfir.transpose into fir runtime call
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Tue Feb 28 07:22:33 PST 2023
Author: Tom Eccles
Date: 2023-02-28T15:21:25Z
New Revision: 24322fca3bb433d926940b0b084aae252e81d4ac
URL: https://github.com/llvm/llvm-project/commit/24322fca3bb433d926940b0b084aae252e81d4ac
DIFF: https://github.com/llvm/llvm-project/commit/24322fca3bb433d926940b0b084aae252e81d4ac.diff
LOG: [flang] lower hlfir.transpose into fir runtime call
Depends on D144881
Reviewed By: jeanPerier
Differential Revision: https://reviews.llvm.org/D144882
Added:
flang/test/HLFIR/transpose-bufferization.fir
Modified:
flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
Removed:
################################################################################
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index ac60fb24964ab..349a356f52b4a 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -691,6 +691,38 @@ struct MatmulOpConversion : public HlfirIntrinsicConversion<hlfir::MatmulOp> {
}
};
+class TransposeOpConversion
+ : public HlfirIntrinsicConversion<hlfir::TransposeOp> {
+ using HlfirIntrinsicConversion<hlfir::TransposeOp>::HlfirIntrinsicConversion;
+
+ mlir::LogicalResult
+ matchAndRewrite(hlfir::TransposeOp transpose, OpAdaptor adaptor,
+ mlir::ConversionPatternRewriter &rewriter) const override {
+ fir::KindMapping kindMapping{rewriter.getContext()};
+ fir::FirOpBuilder builder{rewriter, kindMapping};
+ const mlir::Location &loc = transpose->getLoc();
+ HLFIRListener listener{builder, rewriter};
+ builder.setListener(&listener);
+
+ mlir::Value arg = transpose.getArray();
+ llvm::SmallVector<IntrinsicArgument, 1> inArgs;
+ inArgs.push_back({arg, arg.getType()});
+
+ auto *argLowering = fir::getIntrinsicArgumentLowering("transpose");
+ llvm::SmallVector<fir::ExtendedValue, 1> args =
+ lowerArguments(transpose, inArgs, rewriter, argLowering);
+
+ mlir::Type scalarResultType =
+ hlfir::getFortranElementType(transpose.getType());
+
+ auto [resultExv, mustBeFreed] = fir::genIntrinsicCall(
+ builder, loc, "transpose", scalarResultType, args);
+
+ processReturnValue(transpose, resultExv, mustBeFreed, builder, rewriter);
+ return mlir::success();
+ }
+};
+
class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
public:
void runOnOperation() override {
@@ -704,11 +736,13 @@ class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
auto module = this->getOperation();
auto *context = &getContext();
mlir::RewritePatternSet patterns(context);
- patterns.insert<
- ApplyOpConversion, AsExprOpConversion, AssignOpConversion,
- AssociateOpConversion, ConcatOpConversion, DestroyOpConversion,
- ElementalOpConversion, EndAssociateOpConversion, MatmulOpConversion,
- NoReassocOpConversion, SetLengthOpConversion, SumOpConversion>(context);
+ patterns
+ .insert<ApplyOpConversion, AsExprOpConversion, AssignOpConversion,
+ AssociateOpConversion, ConcatOpConversion, DestroyOpConversion,
+ ElementalOpConversion, EndAssociateOpConversion,
+ MatmulOpConversion, NoReassocOpConversion,
+ SetLengthOpConversion, SumOpConversion, TransposeOpConversion>(
+ context);
mlir::ConversionTarget target(*context);
target.addIllegalOp<hlfir::ApplyOp, hlfir::AssociateOp, hlfir::ElementalOp,
hlfir::EndAssociateOp, hlfir::SetLengthOp,
diff --git a/flang/test/HLFIR/transpose-bufferization.fir b/flang/test/HLFIR/transpose-bufferization.fir
new file mode 100644
index 0000000000000..0db359adebd32
--- /dev/null
+++ b/flang/test/HLFIR/transpose-bufferization.fir
@@ -0,0 +1,49 @@
+// Test hlfir.transpose operation lowering to fir runtime call
+// RUN: fir-opt %s -bufferize-hlfir | FileCheck %s
+
+func.func @_QPtranspose1(%arg0: !fir.ref<!fir.array<1x2xi32>> {fir.bindc_name = "m"}, %arg1: !fir.ref<!fir.array<2x1xi32>> {fir.bindc_name = "res"}) {
+ %c1 = arith.constant 1 : index
+ %c2 = arith.constant 2 : index
+ %0 = fir.shape %c1, %c2 : (index, index) -> !fir.shape<2>
+ %1:2 = hlfir.declare %arg0(%0) {uniq_name = "_QFtranspose1Em"} : (!fir.ref<!fir.array<1x2xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<1x2xi32>>, !fir.ref<!fir.array<1x2xi32>>)
+ %c2_0 = arith.constant 2 : index
+ %c1_1 = arith.constant 1 : index
+ %2 = fir.shape %c2_0, %c1_1 : (index, index) -> !fir.shape<2>
+ %3:2 = hlfir.declare %arg1(%2) {uniq_name = "_QFtranspose1Eres"} : (!fir.ref<!fir.array<2x1xi32>>, !fir.shape<2>) -> (!fir.ref<!fir.array<2x1xi32>>, !fir.ref<!fir.array<2x1xi32>>)
+ %4 = hlfir.transpose %1#0 : (!fir.ref<!fir.array<1x2xi32>>) -> !hlfir.expr<2x1xi32>
+ hlfir.assign %4 to %3#0 : !hlfir.expr<2x1xi32>, !fir.ref<!fir.array<2x1xi32>>
+ hlfir.destroy %4 : !hlfir.expr<2x1xi32>
+ return
+}
+// CHECK-LABEL: func.func @_QPtranspose1(
+// CHECK: %[[ARG0:.*]]: !fir.ref<!fir.array<1x2xi32>> {fir.bindc_name = "m"}
+// CHECK: %[[ARG1:.*]]: !fir.ref<!fir.array<2x1xi32>> {fir.bindc_name = "res"}
+// CHECK-DAG: %[[M_VAR:.*]]:2 = hlfir.declare %[[ARG0]]
+// CHECK-DAG: %[[RES_VAR:.*]]:2 = hlfir.declare %[[ARG1]]
+
+// CHECK-DAG: %[[RET_BOX:.*]] = fir.alloca !fir.box<!fir.heap<!fir.array<?x?xi32>>>
+// CHECK-DAG: %[[RET_ADDR:.*]] = fir.zero_bits !fir.heap<!fir.array<?x?xi32>>
+// CHECK-DAG: %[[C0:.*]] = arith.constant 0 : index
+// CHECK-DAG: %[[RET_SHAPE:.*]] = fir.shape %[[C0]], %[[C0]] : (index, index) -> !fir.shape<2>
+// CHECK-DAG: %[[RET_EMBOX:.*]] = fir.embox %[[RET_ADDR]](%[[RET_SHAPE]])
+// CHECK-DAG: fir.store %[[RET_EMBOX]] to %[[RET_BOX]]
+
+// CHECK-DAG: %[[M_BOX:.*]] = fir.embox %[[M_VAR]]#1(%[[M_SHAPE:.*]])
+
+// CHECK: %[[RET_ARG:.*]] = fir.convert %[[RET_BOX]] : (!fir.ref<!fir.box<!fir.heap<!fir.array<?x?xi32>>>>)
+// CHECK: %[[M_ARG:.*]] = fir.convert %[[M_BOX]] : (!fir.box<!fir.array<1x2xi32>>) -> !fir.box<none>
+// CHECK: %[[NONE:.*]] = fir.call @_FortranATranspose(%[[RET_ARG]], %[[M_ARG]], %[[LOC_STR:.*]], %[[LOC_N:.*]])
+
+// CHECK: %[[RET:.*]] = fir.load %[[RET_BOX]]
+// CHECK-DAG: %[[BOX_DIMS:.*]]:3 = fir.box_dims %[[RET]]
+// CHECK-DAG: %[[ADDR:.*]] = fir.box_addr %[[RET]]
+// CHECK-NEXT: %[[SHIFT:.*]] = fir.shape_shift %[[BOX_DIMS]]#0, %[[BOX_DIMS]]#1
+// TODO: fix alias analysis in hlfir.assign bufferization
+// CHECK-NEXT: %[[TMP:.*]]:2 = hlfir.declare %[[ADDR]](%[[SHIFT]]) {uniq_name = ".tmp.intrinsic_result"}
+// CHECK: %[[TUPLE0:.*]] = fir.undefined tuple<!fir.box<!fir.array<?x?xi32>>, i1>
+// CHECK: %[[TUPLE1:.*]] = fir.insert_value %[[TUPLE0]], %[[TRUE:.*]], [1 : index]
+// CHECK: %[[TUPLE2:.*]] = fir.insert_value %[[TUPLE1]], %[[TMP]]#0, [0 : index]
+// CHECK: hlfir.assign %[[TMP]]#0 to %[[RES_VAR]]#0
+// CHECK: fir.freemem %[[TMP]]#1
+// CHECK-NEXT: return
+// CHECK-NEXT: }
More information about the flang-commits
mailing list