[flang-commits] [flang] [flang][fir] Add fir.if -> scf.if and add filecheck test file (PR #142965)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Thu Jun 5 17:26:30 PDT 2025


================
@@ -87,13 +87,67 @@ struct DoLoopConversion : public OpRewritePattern<fir::DoLoopOp> {
     return success();
   }
 };
+
+struct IfConversion : public OpRewritePattern<fir::IfOp> {
+  using OpRewritePattern<fir::IfOp>::OpRewritePattern;
+  LogicalResult matchAndRewrite(fir::IfOp ifOp,
+                                PatternRewriter &rewriter) const override {
+    mlir::Location loc = ifOp.getLoc();
+    mlir::detail::TypedValue<mlir::IntegerType> condition = ifOp.getCondition();
+    ValueTypeRange<ResultRange> resultTypes = ifOp.getResultTypes();
+    bool hasResult = !resultTypes.empty();
+    auto scfIfOp = rewriter.create<scf::IfOp>(loc, resultTypes, condition,
+                                              !ifOp.getElseRegion().empty());
+    // then region
+    assert(!ifOp.getThenRegion().empty() && "must have then region");
+    auto &firThenBlock = ifOp.getThenRegion().front();
+    auto &scfThenBlock = scfIfOp.getThenRegion().front();
+    auto &firThenOps = firThenBlock.getOperations();
+    mlir::Operation *firThenTerminator = firThenBlock.getTerminator();
+
+    rewriter.setInsertionPointToStart(&scfThenBlock);
+    // not splice terminator
+    scfThenBlock.getOperations().splice(scfThenBlock.begin(), firThenOps,
----------------
vzakhari wrote:

Please consider using https://mlir.llvm.org/doxygen/classmlir_1_1Region.html#ad27658d6e09cba6338419699eabb9c5b (`Region::takeBody` method) instead of moving the operations from one block to another.

https://github.com/llvm/llvm-project/pull/142965


More information about the flang-commits mailing list