From c852174a4935debc6d724dd057c4955678e12117 Mon Sep 17 00:00:00 2001 From: Jean Perier Date: Thu, 20 Oct 2022 10:15:14 +0200 Subject: [PATCH] [flang] add fir.declare codegen support For now, nothing is done about debug info and the fir.declare is simply replaced by the memref argument. This is done in the PreCGRewrite in order to avoid requiring adding support for fir.shape codegen, which would still be useless and undesired at that point. Differential Revision: https://reviews.llvm.org/D136254 --- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp | 17 +++++++++++++++-- flang/test/Fir/declare-codegen.fir | 20 ++++++++++++++++++++ 2 files changed, 35 insertions(+), 2 deletions(-) create mode 100644 flang/test/Fir/declare-codegen.fir diff --git a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp index c134ff9..8862a32 100644 --- a/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp +++ b/flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp @@ -266,6 +266,18 @@ public: } }; +class DeclareOpConversion : public mlir::OpRewritePattern { +public: + using OpRewritePattern::OpRewritePattern; + + mlir::LogicalResult + matchAndRewrite(fir::DeclareOp declareOp, + mlir::PatternRewriter &rewriter) const override { + rewriter.replaceOp(declareOp, declareOp.getMemref()); + return mlir::success(); + } +}; + class CodeGenRewrite : public fir::impl::CodeGenRewriteBase { public: void runOn(mlir::Operation *op, mlir::Region ®ion) { @@ -276,6 +288,7 @@ public: fir::FIRCodeGenDialect, mlir::func::FuncDialect>(); target.addIllegalOp(); target.addIllegalOp(); + target.addIllegalOp(); target.addDynamicallyLegalOp([](fir::EmboxOp embox) { return !(embox.getShape() || embox.getType() .cast() @@ -283,8 +296,8 @@ public: .isa()); }); mlir::RewritePatternSet patterns(&context); - patterns.insert( - &context); + patterns.insert(&context); if (mlir::failed( mlir::applyPartialConversion(op, target, std::move(patterns)))) { mlir::emitError(mlir::UnknownLoc::get(&context), diff --git a/flang/test/Fir/declare-codegen.fir b/flang/test/Fir/declare-codegen.fir new file mode 100644 index 0000000..1b0bea8 --- /dev/null +++ b/flang/test/Fir/declare-codegen.fir @@ -0,0 +1,20 @@ +// Test rewrite of fir.declare. The result is replaced by the memref operand. +// RUN: fir-opt --cg-rewrite %s -o - | FileCheck %s + + +func.func @test(%arg0: !fir.ref>) { + %c-1 = arith.constant -1 : index + %c12 = arith.constant 12 : index + %c-2 = arith.constant -2 : index + %c23 = arith.constant 23 : index + %0 = fir.shape_shift %c12, %c-1, %c23, %c-2 : (index, index, index, index) -> !fir.shapeshift<2> + %1 = fir.declare %arg0(%0) {uniq_name = "_QFarray_numeric_lboundsEx"} : (!fir.ref>, !fir.shapeshift<2>) -> !fir.ref> + fir.call @bar(%1) : (!fir.ref>) -> () + return +} +func.func private @bar(%arg0: !fir.ref>) + + +// CHECK-LABEL: func.func @test( +// CHECK-SAME: %[[arg0:.*]]: !fir.ref>) { +// CHECK-NEXT: fir.call @bar(%[[arg0]]) : (!fir.ref>) -> () -- 2.7.4