<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/101708>101708</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][tensor] `InsertSliceOfTransferWriteOpFolder` does not check if write fully-overwrites destination
</td>
</tr>
<tr>
<th>Labels</th>
<td>
good first issue,
mlir,
mlir:tensor
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
MacDue
</td>
</tr>
</table>
<pre>
Take this example:
```mlir
func.func @insert_slice_of_transfer_write(%arg0: tensor<1000x1000xf32>, %arg1: vector<5x6xf32>, %arg2: index, %arg3: tensor<100x100xf32>) -> tensor<1000x1000xf32> {
%c0 = arith.constant 0 : index
%0 = vector.transfer_write %arg1, %arg3[%c0, %c0] {in_bounds = [true, true]} : vector<5x6xf32>, tensor<100x100xf32>
%inserted_slice = tensor.insert_slice %0 into %arg0[3, %arg2] [100, 100] [1, 1] : tensor<100x100xf32> into tensor<1000x1000xf32>
return %inserted_slice : tensor<1000x1000xf32>
}
```
It becomes:
```mlir
func.func @insert_slice_of_transfer_write(%arg0: tensor<1000x1000xf32>, %arg1: vector<5x6xf32>, %arg2: index, %arg3: tensor<100x100xf32>) -> tensor<1000x1000xf32> {
%c3 = arith.constant 3 : index
%0 = vector.transfer_write %arg1, %arg0[%c3, %arg2] {in_bounds = [true, true]} : vector<5x6xf32>, tensor<1000x1000xf32>
return %0 : tensor<1000x1000xf32>
}
```
The `transfer_write` only writes to `5x6` of the `100x100` elements in `%arg3`, but the `insert_slice` slice is eliminated (which may lead to incorrect results). The rewrite should be updated to check if the `transfer_write` overwrites all elements of the `insert_slice` (and only apply in those cases). This is easy for fixed-size tensors but may need something more complex (like value bounds analysis) for dynamic cases.
See: https://godbolt.org/z/sP9Ybz78a
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzkVs9v4ygU_mvI5akWxr-Sgw9tM5HmsJqVptJqTx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