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

via flang-commits flang-commits at lists.llvm.org
Thu Jun 5 20:58:25 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,
----------------
StarryCSF wrote:

And takeBody will clear the blocks, so use splice to move all operations except for the last one

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


More information about the flang-commits mailing list