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

via flang-commits flang-commits at lists.llvm.org
Mon Dec 15 08:52:23 PST 2025


Author: Susan Tan (ス-ザン タン)
Date: 2025-12-15T11:52:18-05:00
New Revision: 01c3e2558603142d519b7b2989bf590213295962

URL: https://github.com/llvm/llvm-project/commit/01c3e2558603142d519b7b2989bf590213295962
DIFF: https://github.com/llvm/llvm-project/commit/01c3e2558603142d519b7b2989bf590213295962.diff

LOG: [flang] restrict fir.convert lowering (#172117)

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

Added: 
    flang/test/Fir/convert-memref-codegen.mlir

Modified: 
    flang/lib/Optimizer/CodeGen/CodeGen.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index f96d45d3f6b66..55e885d46d351 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -816,6 +816,15 @@ 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];

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