<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/135389>135389</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[mlir][tosa] tosa-to-tensor pass for tosa.slice fails with known output shape but dynamic size attribute
</td>
</tr>
<tr>
<th>Labels</th>
<td>
mlir
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
sahas3
</td>
</tr>
</table>
<pre>
Consider the input IR:
```
func.func @slice(%arg0 : tensor<2x60x59x5xf32>) -> tensor<?x60x58x5xf32> {
%0 = "tosa.cast"(%arg0) : (tensor<2x60x59x5xf32>) -> tensor<?x60x59x5xf32>
%1 = tosa.const_shape {values = dense<0> : tensor<4xindex>} : () -> !tosa.shape<4>
%2 = tosa.const_shape {values = dense<[-1, 60, 58, 5]> : tensor<4xindex>} : () -> !tosa.shape<4>
%3 = tosa.slice %0, %1, %2 : (tensor<?x60x59x5xf32>, !tosa.shape<4>, !tosa.shape<4>) -> tensor<?x60x58x5xf32>
return %3 : tensor<?x60x58x5xf32>
}
```
running `mlir-opt --tosa-to-tensor` succeeds:
```
func.func @slice(%arg0: tensor<2x60x59x5xf32>) -> tensor<?x60x58x5xf32> {
%0 = tosa.cast %arg0 : (tensor<2x60x59x5xf32>) -> tensor<?x60x59x5xf32>
%c0 = arith.constant 0 : index
%dim = tensor.dim %0, %c0 : tensor<?x60x59x5xf32>
%c0_0 = arith.constant 0 : index
%1 = arith.subi %dim, %c0_0 : index
%extracted_slice = tensor.extract_slice %0[0, 0, 0, 0] [%1, 60, 58, 5] [1, 1, 1, 1] : tensor<?x60x59x5xf32> to tensor<?x60x58x5xf32>
return %extracted_slice : tensor<?x60x58x5xf32>
}
```
but running `mlir-opt --tosa-infer-shapes --tosa-to-tensor` fails:
```
// -----// IR Dump After TosaInferShapesPass (tosa-infer-shapes) //----- //
func.func @slice(%arg0: tensor<2x60x59x5xf32>) -> tensor<?x60x58x5xf32> {
%0 = tosa.cast %arg0 : (tensor<2x60x59x5xf32>) -> tensor<2x60x59x5xf32>
%1 = tosa.const_shape {values = dense<0> : tensor<4xindex>} : () -> !tosa.shape<4>
%2 = tosa.const_shape {values = dense<[-1, 60, 58, 5]> : tensor<4xindex>} : () -> !tosa.shape<4>
%3 = tosa.slice %0, %1, %2 : (tensor<2x60x59x5xf32>, !tosa.shape<4>, !tosa.shape<4>) -> tensor<2x60x58x5xf32>
%cast = tensor.cast %3 : tensor<2x60x58x5xf32> to tensor<?x60x58x5xf32>
return %cast : tensor<?x60x58x5xf32>
}
error: expected type to be 'tensor<?x60x58x5xf32>' or a rank-reduced version. (size mismatch)
%3 = tosa.slice %0, %1, %2 : (tensor<?x60x59x5xf32>, !tosa.shape<4>, !tosa.shape<4>) -> tensor<?x60x58x5xf32>
^
note: see current operation: %4 = "tensor.extract_slice"(%arg0, %3) <{operandSegmentSizes = array<i32: 1, 0, 1, 0>, static_offsets = array<i64: 0, 0, 0, 0>, static_sizes = array<i64: -9223372036854775808, 60, 58, 5>, static_strides = array<i64: 1, 1, 1, 1>}> : (tensor<2x60x59x5xf32>, index) -> tensor<2x60x58x5xf32>
// -----// IR Dump After TosaToTensorPass Failed (tosa-to-tensor) //----- //
"builtin.module"() ({
"func.func"() <{function_type = (tensor<2x60x59x5xf32>) -> tensor<?x60x58x5xf32>, sym_name = "slice"}> ({
^bb0(%arg0: tensor<2x60x59x5xf32>):
%0 = "arith.constant"() <{value = 0 : index}> : () -> index
%1 = "tensor.dim"(%arg0, %0) : (tensor<2x60x59x5xf32>, index) -> index
%2 = "arith.constant"() <{value = 0 : index}> : () -> index
%3 = "arith.subi"(%1, %2) <{overflowFlags = #arith.overflow<none>}> : (index, index) -> index
%4 = "tensor.extract_slice"(%arg0, %3) <{operandSegmentSizes = array<i32: 1, 0, 1, 0>, static_offsets = array<i64: 0, 0, 0, 0>, static_sizes = array<i64: -9223372036854775808, 60, 58, 5>, static_strides = array<i64: 1, 1, 1, 1>}> : (tensor<2x60x59x5xf32>, index) -> tensor<2x60x58x5xf32>
%5 = "tensor.cast"(%4) : (tensor<2x60x58x5xf32>) -> tensor<?x60x58x5xf32>
"func.return"(%5) : (tensor<?x60x58x5xf32>) -> ()
}) : () -> ()
}) : () -> ()
```
I think `--tosa-infer-shapes` does the correct thing by updating the dynamic dim value in the input and output to `2` but the `size` attribute of `tosa.slice` op isn't updated which is correct too as `tosa-infer-shapes` only updates IO shapes as per my understanding.
I think there are a couple of fixes:
1. Enhance `tosa-to-tensor` to not consider the dynamic size attribute if output shape is known to be static when creating the `tensor.extract_slice` op.
2. Alternatively, as part of SliceOp canonicalization, update the size attribute to not have `-1` when output shape is known.
Any suggestions @sjarus , @eric-k256, @Tai78641 ?
Thanks!
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWFtv4roT_zTmZQQKDoHwwAOFIvVp_9r2HTnJQLwNNrKdFvbT_zVOSLik23bP7jl7pFO1aRKP5-b5zSXCWrlViDMW3bFo2ROly7WZWZELG_YSnR1nC62szNCAyxGk2pcOHr6ycM4C_zsO6t9gvilVOqALsFFgC5ki4zHjkTDbAFg4B4fKasPCBT-Mg0M0PUSHTchZeM_4FPosvG8pWLjyNHFDA2xyx4I5AADjETFcAuPcaSsGqbCOcd6KI4YkkfH4J4Se0TQCh15gJU0r69Y2F3skpV5EUaL1yxkqiyxcBF7fc4tHB6kyPBDLyfKkWqMB40PP2fMk6gvRvFv0G7JZdNcfMr6AcUDXKPZXFi1_qU5hq5M_aX8mJIlcVf_nN0fQ4WBP2iHpzffvR0qtpUFXGnVSdv7ODjZZXoUzC-amVEqqLbBxsCuk6eu9g36flOo73a8ZjgOwZZoiZvbTsPgtqGggAefg-3VQSCsxwkiXVxEplINKShVRDWkmd5VKnu_AP7aBkgbdB9MtdP1xscMzSlsmslalEbvu3IUHZ0TqMFvXId0qXi-tz2I9uvNmnF-iJVAerRFwjT9a8wvnl2j5ngvA6U8F-60V74U-ZZKO4E9KBz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