[flang-commits] [flang] [flang] restrict fir.convert lowering (PR #172117)

Susan Tan ス-ザン タン via flang-commits flang-commits at lists.llvm.org
Fri Dec 12 17:09:24 PST 2025


https://github.com/SusanTan created https://github.com/llvm/llvm-project/pull/172117

Restrict lowering of fir.convert and exclude core memref types from it. This is in preparation for a lowering that accommodates MemRef dialect.

>From d2c5715efce24cbbc490b269cdd17f544956a63e Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Fri, 12 Dec 2025 15:09:32 -0800
Subject: [PATCH 1/2] change upstream conversion

---
 flang/lib/Optimizer/CodeGen/CodeGen.cpp | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index f96d45d3f6b66..012cddf291efd 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -816,6 +816,16 @@ struct ConvertOpConversion : public fir::FIROpConversion<fir::ConvertOp> {
                   mlir::ConversionPatternRewriter &rewriter) const override {
     auto fromFirTy = convert.getValue().getType();
     auto toFirTy = convert.getRes().getType();
+
+    // Let more specialized conversions (e.g. FIR to memref
+    // converters) handle fir.convert when either side is a memref. This
+    // avoids interfering with descriptor-based flows such as fir.box /
+    // fir.box_addr and keeps this pattern focused on value conversions.
+    if (mlir::isa<mlir::MemRefType>(fromFirTy) ||
+        mlir::isa<mlir::MemRefType>(toFirTy)) {
+      return mlir::failure();
+    }
+
     auto fromTy = convertType(fromFirTy);
     auto toTy = convertType(toFirTy);
     mlir::Value op0 = adaptor.getOperands()[0];

>From 6b7513b43103a2a0698ad1a632e52e6e2f39ff22 Mon Sep 17 00:00:00 2001
From: Susan Tan <zujunt at nvidia.com>
Date: Fri, 12 Dec 2025 17:07:46 -0800
Subject: [PATCH 2/2] add test

---
 flang/test/Fir/convert-memref-codegen.mlir | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 flang/test/Fir/convert-memref-codegen.mlir

diff --git a/flang/test/Fir/convert-memref-codegen.mlir b/flang/test/Fir/convert-memref-codegen.mlir
new file mode 100644
index 0000000000000..4496af19e0f67
--- /dev/null
+++ b/flang/test/Fir/convert-memref-codegen.mlir
@@ -0,0 +1,15 @@
+// RUN: not fir-opt --fir-to-llvm-ir="target=x86_64-unknown-linux-gnu" %s -o - 2>&1 | FileCheck %s
+
+// This test ensures that the FIR CodeGen ConvertOpConversion
+// rejects fir.convert when either the source or the destination
+// type is a memref (i.e. it fails to legalize those ops).
+
+module {
+  // CHECK: error: failed to legalize operation 'fir.convert'
+  func.func @memref_to_ref_convert(%arg0: memref<f32>) {
+    %0 = fir.convert %arg0 : (memref<f32>) -> !fir.ref<f32>
+    return
+  }
+}
+
+



More information about the flang-commits mailing list