[flang-commits] [PATCH] D136254: [flang] add fir.declare codegen support

Jean Perier via Phabricator via flang-commits flang-commits at lists.llvm.org
Wed Oct 19 06:01:22 PDT 2022


jeanPerier created this revision.
jeanPerier added reviewers: clementval, vdonaldson.
jeanPerier added a project: Flang.
Herald added subscribers: mehdi_amini, jdoerfert.
Herald added a project: All.
jeanPerier requested review of this revision.

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.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D136254

Files:
  flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
  flang/test/Fir/declare-codegen.fir


Index: flang/test/Fir/declare-codegen.fir
===================================================================
--- /dev/null
+++ 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<!fir.array<12x23xi32>>) {
+  %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.array<12x23xi32>>, !fir.shapeshift<2>) -> !fir.ref<!fir.array<12x23xi32>>
+  fir.call @bar(%1) : (!fir.ref<!fir.array<12x23xi32>>) -> ()
+  return
+}
+func.func private @bar(%arg0: !fir.ref<!fir.array<12x23xi32>>)
+
+
+// CHECK-LABEL: func.func @test(
+// CHECK-SAME: %[[arg0:.*]]: !fir.ref<!fir.array<12x23xi32>>) {
+// CHECK-NEXT: fir.call @bar(%[[arg0]]) : (!fir.ref<!fir.array<12x23xi32>>) -> ()
Index: flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
+++ flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
@@ -266,6 +266,18 @@
   }
 };
 
+class DeclareOpConversion : public mlir::OpRewritePattern<fir::DeclareOp> {
+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<CodeGenRewrite> {
 public:
   void runOn(mlir::Operation *op, mlir::Region &region) {
@@ -276,6 +288,7 @@
                            fir::FIRCodeGenDialect, mlir::func::FuncDialect>();
     target.addIllegalOp<fir::ArrayCoorOp>();
     target.addIllegalOp<fir::ReboxOp>();
+    target.addIllegalOp<fir::DeclareOp>();
     target.addDynamicallyLegalOp<fir::EmboxOp>([](fir::EmboxOp embox) {
       return !(embox.getShape() || embox.getType()
                                        .cast<fir::BaseBoxType>()
@@ -283,8 +296,8 @@
                                        .isa<fir::SequenceType>());
     });
     mlir::RewritePatternSet patterns(&context);
-    patterns.insert<EmboxConversion, ArrayCoorConversion, ReboxConversion>(
-        &context);
+    patterns.insert<EmboxConversion, ArrayCoorConversion, ReboxConversion,
+                    DeclareOpConversion>(&context);
     if (mlir::failed(
             mlir::applyPartialConversion(op, target, std::move(patterns)))) {
       mlir::emitError(mlir::UnknownLoc::get(&context),


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D136254.468888.patch
Type: text/x-patch
Size: 2828 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221019/30f9ede1/attachment-0001.bin>


More information about the flang-commits mailing list