[flang-commits] [flang] [flang] Lowering FIR memory ops to MemRef dialect (PR #173507)

Slava Zakharin via flang-commits flang-commits at lists.llvm.org
Wed Dec 24 15:01:05 PST 2025


================
@@ -0,0 +1,1171 @@
+//===-- FIRToMemRef.cpp - Convert FIR loads and stores to MemRef ---------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// This pass lowers FIR dialect memory operations to the MemRef dialect.
+// It is adapted from the NVHPC FIRToMemRef implementation so that it can
+// be used as a regular flang/MLIR transform pass.
+//
+// In particular it:
+//
+//  - Rewrites `fir.alloca` to `memref.alloca`.
+//
+//  - Rewrites `fir.load` / `fir.store` to `memref.load` / `memref.store`.
+//
+//  - Marshals FIR reference-like values (boxes, array coordinates,
+//    embox/rebox, and optionals) into MemRef descriptors by introducing
+//    `fir.convert` at use sites. For example:
+//
+//        %fir_ref = ... : !fir.ref<!fir.array<...>>
+//        %memref = fir.convert %fir_ref : !fir.ref<!fir.array<...>> ->
+//        memref<...> %val = memref.load %memref[...] : memref<...> fir.call
+//        @callee(%fir_ref) : (!fir.ref<!fir.array<...>>) -> ()
+//
+//    Here the MemRef-typed value is used for `memref.load`, while the original
+//    FIR-typed value is preserved for `fir.call`.
+//
+//  - Computes shapes, strides, and indices as needed for slices and shifts
+//    and emits `memref.reinterpret_cast` when dynamic layout is required.
----------------
vzakhari wrote:

Yes, we'd better use `memref.cast`, since it preserves the shape, IIUIC. A TODO will be good. Thanks for the heads up, Razvan!

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


More information about the flang-commits mailing list