[PATCH] D113295: [flang] Transform `fir.end` to an empty statement

Andrzej Warzynski via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 5 09:54:21 PDT 2021


awarzynski created this revision.
awarzynski added reviewers: clementval, schweitz, kiranchandramohan.
Herald added a subscriber: mehdi_amini.
Herald added a project: Flang.
awarzynski requested review of this revision.
Herald added subscribers: llvm-commits, jdoerfert.
Herald added a project: LLVM.

This patch extends the `FIRToLLVMLowering` pass in Flang by adding a
hook to transform `fir.end` to `` (empty statement).

This is part of the upstreaming effort from the `fir-dev` branch in [1].

[1] https://github.com/flang-compiler/f18-llvm-project

Patch originally written by:
Co-authored-by: Eric Schweitz <eschweitz at nvidia.com>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D113295

Files:
  flang/include/flang/Optimizer/Dialect/FIROps.td
  flang/lib/Optimizer/CodeGen/CodeGen.cpp
  flang/test/Fir/convert-to-llvm.fir


Index: flang/test/Fir/convert-to-llvm.fir
===================================================================
--- flang/test/Fir/convert-to-llvm.fir
+++ flang/test/Fir/convert-to-llvm.fir
@@ -323,3 +323,12 @@
 // CHECK-SAME:                     %[[TUPLE:.*]]: !llvm.struct<(i32, f32)>
 // CHECK:         %{{.*}} = llvm.insertvalue %{{.*}}, %[[TUPLE]][1 : index] : !llvm.struct<(i32, f32)>
 // CHECK:         llvm.return
+
+// -----
+
+// Test fir.end
+
+fir.end
+
+// CHECK-LABEL: module  {
+// CHECK-NEXT: }
Index: flang/lib/Optimizer/CodeGen/CodeGen.cpp
===================================================================
--- flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -79,6 +79,18 @@
   }
 };
 
+/// `fir.end` -> ``
+struct FirEndOpConversion : public FIROpConversion<fir::FirEndOp> {
+  using FIROpConversion::FIROpConversion;
+
+  mlir::LogicalResult
+  matchAndRewrite(fir::FirEndOp op, OpAdaptor,
+                  mlir::ConversionPatternRewriter &rewriter) const override {
+    rewriter.replaceOp(op, {});
+    return success();
+  }
+};
+
 /// Lower `fir.has_value` operation to `llvm.return` operation.
 struct HasValueOpConversion : public FIROpConversion<fir::HasValueOp> {
   using FIROpConversion::FIROpConversion;
@@ -488,11 +500,12 @@
     auto *context = getModule().getContext();
     fir::LLVMTypeConverter typeConverter{getModule()};
     mlir::OwningRewritePatternList pattern(context);
-    pattern.insert<
-        AddrOfOpConversion, ExtractValueOpConversion, HasValueOpConversion,
-        GlobalOpConversion, InsertOnRangeOpConversion, InsertValueOpConversion,
-        SelectOpConversion, SelectRankOpConversion, UndefOpConversion,
-        UnreachableOpConversion, ZeroOpConversion>(typeConverter);
+    pattern
+        .insert<AddrOfOpConversion, ExtractValueOpConversion,
+                FirEndOpConversion, HasValueOpConversion, GlobalOpConversion,
+                InsertOnRangeOpConversion, InsertValueOpConversion,
+                SelectOpConversion, SelectRankOpConversion, UndefOpConversion,
+                UnreachableOpConversion, ZeroOpConversion>(typeConverter);
     mlir::populateStdToLLVMConversionPatterns(typeConverter, pattern);
     mlir::arith::populateArithmeticToLLVMConversionPatterns(typeConverter,
                                                             pattern);
Index: flang/include/flang/Optimizer/Dialect/FIROps.td
===================================================================
--- flang/include/flang/Optimizer/Dialect/FIROps.td
+++ flang/include/flang/Optimizer/Dialect/FIROps.td
@@ -808,6 +808,8 @@
     for these operations.  It is implicit and need not appear in the textual
     representation.
   }];
+
+  let parser = "return mlir::success();";
 }
 
 def fir_HasValueOp : fir_Op<"has_value", [Terminator, HasParent<"GlobalOp">]> {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D113295.385108.patch
Type: text/x-patch
Size: 2856 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20211105/f1da7be3/attachment.bin>


More information about the llvm-commits mailing list