[Mlir-commits] [mlir] [mlir][Bufferization]Support to_tensor / to_buffer in One-Shot Bufferize analysis (PR #170261)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Tue Dec 2 18:42:29 PST 2025


kimm240 wrote:

@matthias-springer 
I fix the "copy omission leading to correctness violation" issue.

Root Cause:
ToBufferOp::bufferizesToMemoryWrite always returned false, so OSB did not treat it as a memory write, and the required memref.copy was not inserted.

Solution:
-  Changed ToBufferOp::bufferizesToMemoryWrite to return !getReadOnly(). When read_only is not set, it returns true, so OSB treats it as a memory write and inserts a copy when needed.
- Overrode ToBufferOp::resultBufferizesToMemoryWrite to handle memref results correctly (prevents the default implementation from calling getAliasingOpOperands on memref types, which would fail).
- Modified ToBufferOp::getAliasingValues to return the input tensor, enabling RaW conflict detection when the result memref is written to and the input tensor is read from.

Test Added:
- Added a test case based on your IR example (linalg.fill + to_buffer + memref.store + tensor.extract). The test confirms that memref.copy is correctly inserted, preserving the original tensor's immutability. All tests pass.

Changes:
- mlir/include/mlir/Dialect/Bufferization/IR/BufferizationOps.td: Fixed ToBufferOp memory write handling
- mlir/test/Dialect/Bufferization/Transforms/one-shot-module-bufferize.mlir: Added test case based on your IR example
bufferization.to_buffer now correctly inserts the required memref.copy, ensuring tensor SSA immutability.


If there are additional edge cases or scenarios to verify, please let me know and I'll address them.

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


More information about the Mlir-commits mailing list