Skip to content

Conversation

@SusanTan
Copy link
Contributor

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

@SusanTan SusanTan self-assigned this Dec 13, 2025
@llvmbot llvmbot added flang Flang issues not falling into any other category flang:fir-hlfir flang:codegen labels Dec 13, 2025
@llvmbot
Copy link
Member

llvmbot commented Dec 13, 2025

@llvm/pr-subscribers-flang-fir-hlfir

@llvm/pr-subscribers-flang-codegen

Author: Susan Tan (ス-ザン タン) (SusanTan)

Changes

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


Full diff: https://github.com/llvm/llvm-project/pull/172117.diff

2 Files Affected:

  • (modified) flang/lib/Optimizer/CodeGen/CodeGen.cpp (+10)
  • (added) flang/test/Fir/convert-memref-codegen.mlir (+15)
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];
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
+  }
+}
+
+

@rscottmanley
Copy link
Contributor

Would it be better that the current ConvertOp lowering pattern return failure() instead of aborting with an error? Then another pattern that handles MemRef types could "try"?

Copy link
Contributor

@jeanPerier jeanPerier left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

if (mlir::isa<mlir::MemRefType>(fromFirTy) ||
mlir::isa<mlir::MemRefType>(toFirTy)) {
return mlir::failure();
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: no braces for single line if.

@jeanPerier
Copy link
Contributor

jeanPerier commented Dec 15, 2025

Would it be better that the current ConvertOp lowering pattern return failure() instead of aborting with an error? Then another pattern that handles MemRef types could "try"?

Isn't this what this change is doing?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

flang:codegen flang:fir-hlfir flang Flang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants