[flang-commits] [flang] [flang] Enable speculation of fir.convert with memref<> type. (PR #190413)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Fri Apr 3 15:10:15 PDT 2026


https://github.com/vzakhari created https://github.com/llvm/llvm-project/pull/190413

Such `fir.convert`s may appear after FIRToMemref conversion and it would be good to be able to speculate them.

>From 65753674a1ac2afdf52375af376aa1ba4d793006 Mon Sep 17 00:00:00 2001
From: Slava Zakharin <szakharin at nvidia.com>
Date: Fri, 3 Apr 2026 15:07:15 -0700
Subject: [PATCH] [flang] Enable speculation of fir.convert with memref<> type.

Such `fir.convert`s may appear after FIRToMemref conversion
and it would be good to be able to speculate them.
---
 flang/lib/Optimizer/Dialect/FIROps.cpp |  3 ++-
 flang/test/Transforms/licm.fir         | 27 ++++++++++++++++++++++++++
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index 93a141da111a2..b5e767ef3030b 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -1802,7 +1802,8 @@ mlir::Speculation::Speculatability fir::ConvertOp::getSpeculatability() {
   // Also disallow speculation for converts to/from non-FIR types, except
   // for some builtin types.
   auto canSpeculateType = [](mlir::Type ty) {
-    if (fir::isa_fir_type(ty) || fir::isa_integer(ty))
+    if (fir::isa_fir_type(ty) || fir::isa_integer(ty) ||
+        mlir::isa<mlir::MemRefType>(ty))
       return true;
     return false;
   };
diff --git a/flang/test/Transforms/licm.fir b/flang/test/Transforms/licm.fir
index b8a7ef64845f9..049d86b6171fe 100644
--- a/flang/test/Transforms/licm.fir
+++ b/flang/test/Transforms/licm.fir
@@ -2060,3 +2060,30 @@ func.func @_QMmPtest(%arg0: !fir.ref<i32> {fir.bindc_name = "n"}) {
   fir.store %9 to %4 : !fir.ref<i32>
   return
 }
+
+// -----
+// Test hoisting of fir.convert for memref<> type.
+// CHECK-LABEL:   func.func @test_() {
+// CHECK:           %[[ALLOCA_0:.*]] = memref.alloca() {bindc_name = "x", uniq_name = "_QFtestEx"} : memref<10xf32>
+// CHECK:           %[[CONVERT_0:.*]] = fir.convert %[[ALLOCA_0]] : (memref<10xf32>) -> !fir.ref<!fir.array<10xf32>>
+// CHECK:           %[[CONVERT_1:.*]] = fir.convert %[[CONVERT_0]] : (!fir.ref<!fir.array<10xf32>>) -> memref<10xf32>
+// CHECK:           scf.for %[[VAL_0:.*]] = {{.*}} {
+// CHECK:             memref.store %{{.*}}, %[[CONVERT_1]]{{\[}}%[[VAL_0]]] : memref<10xf32>
+// CHECK:           }
+// CHECK:           return
+// CHECK:         }
+
+func.func @test_() {
+  %c0 = arith.constant 0 : index
+  %cst = arith.constant 0.000000e+00 : f32
+  %c1 = arith.constant 1 : index
+  %c10 = arith.constant 10 : index
+  %alloca_0 = memref.alloca() {bindc_name = "x", uniq_name = "_QFtestEx"} : memref<10xf32>
+  scf.for %arg0 = %c0 to %c10 step %c1 {
+    %3 = fir.convert %alloca_0 : (memref<10xf32>) -> !fir.ref<!fir.array<10xf32>>
+    %10 = fir.convert %3 : (!fir.ref<!fir.array<10xf32>>) -> memref<10xf32>
+    memref.store %cst, %10[%arg0] : memref<10xf32>
+    scf.yield
+  }
+  return
+}



More information about the flang-commits mailing list