[Mlir-commits] [mlir] [mlir][bufferize] Add hoist-dynamic-allocs-option to buffer-results-to-out-params (PR #160985)

lonely eagle llvmlistbot at llvm.org
Sat Sep 27 19:02:44 PDT 2025


================
@@ -120,12 +166,22 @@ updateReturnOps(func::FuncOp func, ArrayRef<BlockArgument> appendedEntryArgs,
         keepAsReturnOperands.push_back(operand);
     }
     OpBuilder builder(op);
+    SmallVector<SmallVector<Value>> dynamicSizes;
     for (auto [orig, arg] : llvm::zip(copyIntoOutParams, appendedEntryArgs)) {
-      if (options.hoistStaticAllocs &&
+      bool hoistStaticAllocs =
+          options.hoistStaticAllocs &&
+          cast<MemRefType>(orig.getType()).hasStaticShape();
+      bool hoistDynamicAllocs =
+          options.hoistDynamicAllocs &&
+          !cast<MemRefType>(orig.getType()).hasStaticShape();
+      if ((hoistStaticAllocs || hoistDynamicAllocs) &&
           isa_and_nonnull<bufferization::AllocationOpInterface>(
-              orig.getDefiningOp()) &&
-          mlir::cast<MemRefType>(orig.getType()).hasStaticShape()) {
+              orig.getDefiningOp())) {
         orig.replaceAllUsesWith(arg);
+        if (hoistDynamicAllocs) {
+          SmallVector<Value> dynamicSize = getDynamicSize(orig, func);
----------------
linuxlonelyeagle wrote:

```
func.func  @foo() -> memref<?xf32> {
  %c1 = arith.constant 1 : index
  %c0 = arith.constant 0 : index
  %f1 = arith.constant 1.0 : f32
  %alloc = memref.alloc(%c1) : memref<?xf32>
  memref.store %f1, %alloc[%c0] : memref<?xf32>
  return %alloc : memref<?xf32>
}

run the pass

module {
  func.func private @foo(%arg0: memref<?xf32>) {
    %c1 = arith.constant 1 : index
    %c0 = arith.constant 0 : index
    %cst = arith.constant 1.000000e+00 : f32
    memref.store %cst, %arg0[%c0] : memref<?xf32>
    return
  }
}
```

can't find dynamic size case.
```
func.func private @foo() -> memref<?xf32> {
  %c1 = arith.constant 1 : index
  %c0 = arith.constant 0 : index
  %f1 = arith.constant 1.0 : f32
  %alloc = memref.alloc(%c1) : memref<?xf32>
  memref.store %f1, %alloc[%c0] : memref<?xf32>
  return %alloc : memref<?xf32>
}

func.func @main() {
  func.call @foo() : () -> memref<?xf32>
  return
}

run the pass.
a.mlir:11:3: error: cannot create out param for dynamically shaped result
  func.call @foo() : () -> memref<?xf32>
  ^
a.mlir:11:3: note: see current operation: %0 = "func.call"() <{callee = @foo}> : () -> memref<?xf32>

```



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


More information about the Mlir-commits mailing list