| Issue |
233726
|
| Summary |
[mlir][bufferization][vector] transfer_write claims it does not read its destination for partial writes; One-Shot Bufferize drops the copy (miscompile)
|
| Labels |
mlir
|
| Assignees |
|
| Reporter |
wildoranges
|
`TransferWriteOpInterface::bufferizesToMemoryRead` returns `false` (meaning "the destination is completely overwritten") for some `vector.transfer_write` ops that write only part of the destination. When such a write bufferizes out-of-place, `AnalysisState::canOmitTensorCopy` then allocates the new buffer **without copying** the old contents, so the elements that were not written are uninitialized.
**Reproducer 1**: a lower-rank write. The default minor-identity map writes only row 0.
`repro.mlir`:
```mlir
func.func @main() {
%c0 = arith.constant 0 : index
%t = arith.constant dense<[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0],
[9.0, 10.0, 11.0, 12.0], [13.0, 14.0, 15.0, 16.0]]> : tensor<4x4xf32>
%v = arith.constant dense<[-1.0, -2.0, -3.0, -4.0]> : vector<4xf32>
%w = vector.transfer_write %v, %t[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, tensor<4x4xf32>
%pad = arith.constant 0.0 : f32
%r = vector.transfer_read %w[%c0, %c0], %pad {in_bounds = [true, true]} : tensor<4x4xf32>, vector<4x4xf32>
vector.print %r : vector<4x4xf32>
return
}
```
(`$LLVM_BUILD` is the LLVM/MLIR build directory.)
```console
$ LOWER="-one-shot-bufferize -buffer-deallocation-pipeline -convert-bufferization-to-memref -test-lower-to-llvm"
$ RUN="mlir-runner -e main -entry-point-result=void -shared-libs=$LLVM_BUILD/lib/libmlir_runner_utils.so,$LLVM_BUILD/lib/libmlir_c_runner_utils.so"
$ mlir-opt repro.mlir $LOWER | $RUN
( ( -1, -2, -3, -4 ), ( 9.83576e-14, 3.44579e-41, 0, 3.44579e-41 ), ( 9.8356e-14, 3.44579e-41, 0, 0 ), ( 9.8358e-14, 3.44579e-41, 0, 3.44579e-41 ) )
$ mlir-opt repro.mlir -one-shot-bufferize=copy-before-write -buffer-deallocation-pipeline -convert-bufferization-to-memref -test-lower-to-llvm | $RUN
( ( -1, -2, -3, -4 ), ( 1.36302e+09, 3.45868e-41, 0, 3.45868e-41 ), ... )
```
(Rows 1–3 are uninitialized memory; the exact values change from run to run.)
Expected: `( ( -1, -2, -3, -4 ), ( 5, 6, 7, 8 ), ( 9, 10, 11, 12 ), ( 13, 14, 15, 16 ) )`. Writing the same row as a `vector<1x4xf32>` (with `in_bounds = [true, true]`) prints exactly that.
After `-one-shot-bufferize`, the IR has a fresh allocation and no `memref.copy` of the constant:
```mlir
%alloc = memref.alloc() alignment = 64 : memref<4x4xf32>
vector.transfer_write %cst, %alloc[%c0, %c0] {in_bounds = [true]} : vector<4xf32>, memref<4x4xf32>
```
`-one-shot-bufferize=copy-before-write` produces the same wrong result, because it goes through the same `canOmitTensorCopy`. If the destination is a function argument that is read after the write (`bufferize-function-boundaries`), the write is again out-of-place, and the returned tensor again has uninitialized rows 1–3.
**Reproducer 2**: a write with its own mask operand.
`repro_mask.mlir`:
```mlir
func.func @main() {
%c0 = arith.constant 0 : index
%t = arith.constant dense<[1.0, 2.0, 3.0, 4.0]> : tensor<4xf32>
%v = arith.constant dense<[-1.0, -2.0, -3.0, -4.0]> : vector<4xf32>
%m = arith.constant dense<[true, false, true, false]> : vector<4xi1>
%w = vector.transfer_write %v, %t[%c0], %m {in_bounds = [true]} : vector<4xf32>, tensor<4xf32>
%pad = arith.constant 0.0 : f32
%r = vector.transfer_read %w[%c0], %pad {in_bounds = [true]} : tensor<4xf32>, vector<4xf32>
vector.print %r : vector<4xf32>
return
}
```
```console
$ mlir-opt repro_mask.mlir $LOWER | $RUN
( -1, 0, -3, 4.59135e-41 )
```
Lanes 1 and 3 are uninitialized; expected `( -1, 2, -3, 4 )`.
**Analysis**
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Vector/Transforms/BufferizableOpInterfaceImpl.cpp#L77-L107
1. The "full overwrite" check zips the destination shape with the vector shape **positionally** and ignores the permutation map. A `vector<4xf32>` written into `tensor<4x4xf32>` covers only one row, but dim 0 compares as 4 vs 4, so the write is treated as covering everything.
2. `writeOp.isMasked()` is `MaskableOpInterface::isMasked()`, which checks for an enclosing `vector.mask`. It does not look at the op's own `mask` operand.
The interface documentation says it is always safe to answer "read". `canOmitTensorCopy` relies on a `false` answer being exact:
https://github.com/llvm/llvm-project/blob/018b722b2d48cf42dccded46e1226f1d9c29f339/mlir/lib/Dialect/Bufferization/IR/BufferizableOpInterface.cpp#L703-L712
A possible fix:
- return `true` unless the permutation map is a (minor) identity whose vector dims cover every destination dim; and
- return `true` whenever the op has a mask operand.
**Version**: llvm-project main @ 018b722b2d48cf42dccded46e1226f1d9c29f339 (2026-10-17), Release + assertions, x86_64 Linux. The check dates back to D152823 (2023). Also reproduced on main @ 4c407e843c975298b6892d50afd9e72cfb0cbe46 (2026-10-20).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs