[Mlir-commits] [mlir] [mlir] [memref] [transform] Add alloc_to_global op. (PR #211141)
Federico Bruzzone
llvmlistbot at llvm.org
Wed Aug 5 05:34:29 PDT 2026
================
@@ -125,6 +125,64 @@ void transform::ApplyResolveRankedShapedTypeResultDimsPatternsOp::
memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns);
}
+//===----------------------------------------------------------------------===//
+// Alloc and alloca to global utilities
+//===----------------------------------------------------------------------===//
+
+/// Converts an allocation operation (`memref.alloca` or `memref.alloc`) to a
+/// `memref.global` operation in the nearest symbol table, and replaces the
+/// allocation with a `memref.get_global` operation. Any `memref.dealloc`
+/// operations referencing the allocation are erased.
+template <typename AllocLikeOp>
+static DiagnosedSilenceableFailure
+allocLikeToGlobal(transform::TransformRewriter &rewriter,
+ AllocLikeOp allocLikeOp, StringRef globalName,
+ memref::GlobalOp &globalOp,
+ memref::GetGlobalOp &getGlobalOp) {
+ MemRefType memrefType = allocLikeOp.getType();
+ if (!memrefType.hasStaticShape()) {
----------------
FedericoBruzzone wrote:
I think that `hasStaticShape()` is not a sufficient precondition :(
Look at the following the symbol operands are currently dropped:
```mlir
#map = affine_map<(d0, d1)[s0] -> (d0 + s0, d1)>
%0 = memref.alloc()[%s] : memref<8x8xf32, #map>
```
has a static shape, so the check passes, and the result is:
```mlir
memref.global "private" @alloc : memref<8x8xf32, #map>
%0 = memref.get_global @alloc : memref<8x8xf32, #map> // %s is gone
```
I also think the the same goes for static shapes with dynamic strides (?)
We can require `getSymbolOperands().empty()` and a layout with no dynamic components, maybe? 🤔
https://github.com/llvm/llvm-project/pull/211141
More information about the Mlir-commits
mailing list