Issue 242611
Summary [mlir][bufferization] buffer-results-to-out-params{hoist-static-allocs} erases a returned memref.realloc / bufferization.clone and drops its copy
Labels mlir
Assignees
Reporter wildoranges
    The documentation of `-buffer-results-to-out-params` says that with `hoist-static-allocs` the optimization applies to a returned memref that "is allocated by memref.alloc in the function", and that "it will use the memref given in function argument to replace the allocated memref":
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/include/mlir/Dialect/Bufferization/Transforms/Passes.td#L341-L345

The implementation applies it to any op implementing `bufferization::AllocationOpInterface`:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Bufferization/Transforms/BufferResultsToOutParams.cpp#L176-L209

That set includes `memref.realloc` and `bufferization.clone`. Both **copy** data into their result. Replacing the result with the out-param and erasing the op drops that copy, so the caller reads uninitialized memory. The broader matching seems to have come in with #120288.

Related: #231608 (merged 2026-10-15) fixed the same confusion, `memref.realloc` treated like a fresh allocation, for `buffer-hoisting` / `buffer-loop-hoisting`, by giving realloc `HoistingKind::None`. `buffer-results-to-out-params` does not consult `HoistingKind` or any other property. It only checks `isa<AllocationOpInterface>`, so it is not covered by that fix.

`clone.mlir`:
```mlir
// @snapshot returns a bufferization.clone (a copy) of a global-backed buffer.
// buffer-results-to-out-params{hoist-static-allocs} replaces the clone with
// the out param and erases it -> the copy never happens.
memref.global "private" constant @g : memref<4xf32> = dense<[1.0, 2.0, 3.0, 4.0]>

func.func private @snapshot() -> memref<4xf32> {
  %g = memref.get_global @g : memref<4xf32>
  %c = bufferization.clone %g : memref<4xf32> to memref<4xf32>
  return %c : memref<4xf32>
}

func.func @main() {
  %r = call @snapshot() : () -> memref<4xf32>
  %c0 = arith.constant 0 : index
  %pad = arith.constant 0.0 : f32
  %v = vector.transfer_read %r[%c0], %pad {in_bounds = [true]} : memref<4xf32>, vector<4xf32>
  // expected: ( 1, 2, 3, 4 )
  vector.print %v : vector<4xf32>
  memref.dealloc %r : memref<4xf32>
 return
}
```

`realloc.mlir`:
```mlir
// @grow returns a buffer produced by memref.realloc. realloc copies the
// first 4 elements of %a into the new buffer. buffer-results-to-out-params
// {hoist-static-allocs} treats every AllocationOpInterface op as a fresh
// allocation and replaces the realloc result with the caller-provided out
// param, erasing the realloc (and its copy).
func.func private @grow() -> memref<8xf32> {
  %_one_ = arith.constant 1.0 : f32
  %two = arith.constant 2.0 : f32
  %c4 = arith.constant 4 : index
  %c8 = arith.constant 8 : index
  %c1 = arith.constant 1 : index
  %a = memref.alloc() : memref<4xf32>
 linalg.fill ins(%one : f32) outs(%a : memref<4xf32>)
  %b = memref.realloc %a : memref<4xf32> to memref<8xf32>
  scf.for %i = %c4 to %c8 step %c1 {
 memref.store %two, %b[%i] : memref<8xf32>
  }
  return %b : memref<8xf32>
}

func.func @main() {
  %r = call @grow() : () -> memref<8xf32>
  %c0 = arith.constant 0 : index
  %pad = arith.constant 0.0 : f32
  %v = vector.transfer_read %r[%c0], %pad {in_bounds = [true]} : memref<8xf32>, vector<8xf32>
  // expected: ( 1, 1, 1, 1, 2, 2, 2, 2 )
 vector.print %v : vector<8xf32>
  memref.dealloc %r : memref<8xf32>
 return
}
```

(`$LLVM_BUILD` is the LLVM/MLIR build directory.)

```console
$ LOWER="-expand-realloc -convert-bufferization-to-memref -test-lower-to-llvm"
$ RUN="mlir-runner -e main -entry-point-result=void -O0 -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt clone.mlir -buffer-results-to-out-params $LOWER | $RUN
( 1, 2, 3, 4 )
$ mlir-opt clone.mlir -buffer-results-to-out-params=hoist-static-allocs $LOWER | $RUN
( -4.20927e-32, 3.24499e-41, 0, 0 )
$ mlir-opt realloc.mlir -buffer-results-to-out-params $LOWER | $RUN
( 1, 1, 1, 1, 2, 2, 2, 2 )
$ mlir-opt realloc.mlir -buffer-results-to-out-params=hoist-static-allocs $LOWER | $RUN
( -3.35697e-35, 3.57485e-41, 0, 0, 2, 2, 2, 2 )
$ mlir-opt realloc.mlir -buffer-results-to-out-params=hoist-static-allocs
module {
 func.func private @grow(%arg0: memref<8xf32>) {
    ...
    %alloc = memref.alloc() : memref<4xf32>
    linalg.fill ins(%cst : f32) outs(%alloc : memref<4xf32>)
    scf.for %arg1 = %c4 to %c8 step %c1 {
      memref.store %cst_0, %arg0[%arg1] : memref<8xf32>
    }
    return
  }
 ...
```

The uninitialized elements print different values on each run. With `hoist-static-allocs`, `@grow` fills a dead local alloc and writes only `[4, 8)` of the out-param; the `memref.realloc` and its copy are gone.

Both inputs verify and are UB-free: `realloc` preserves the first 4 elements, and the clone source is an initialized global.

Possible fix: only hoist `memref.alloc` (and `alloca`), as documented. Alternatively, ask the interface whether the op produces a fresh, uninitialized buffer, analogous to how #231608 used `HoistingKind`.

**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. Also reproduced on main @ a60677dca3a8536a0cf5bec1b846610014a983fd (2026-11-12).

*Disclosure: I wrote this report with the help of Claude Code. I reproduced and reviewed everything in this report myself.*
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to