[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
Sun Jun 8 23:31:29 PDT 2025


================
@@ -87,13 +87,59 @@ 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();
+    mlir::scf::IfOp scfIfOp = rewriter.create<scf::IfOp>(
+        loc, resultTypes, condition, !ifOp.getElseRegion().empty());
+    // then region
+    scfIfOp.getThenRegion().takeBody(ifOp.getThenRegion());
+    Block &scfthenBlock = scfIfOp.getThenRegion().front();
+    Operation *scfthenTerminator = scfthenBlock.getTerminator();
+    // fir.result->scf.yield
+    rewriter.setInsertionPointToEnd(&scfthenBlock);
+    if (hasResult) {
+      rewriter.create<scf::YieldOp>(scfthenTerminator->getLoc(),
+                                    scfthenTerminator->getOperands());
----------------
NexMing wrote:

If `scfThenTerminator->getOperands()` is empty, it's still valid, so the `hasResult` check is redundant.

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


More information about the flang-commits mailing list