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summary</th>
<td>
[MLIR] Add constant and slice folding to `tensor.extract_slice`.
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
benvanik
</td>
</tr>
</table>
<pre>
`tensor.extract_slice` currently does not fold the following cases that show up frequently:
Slice of constants:
```mlir
%cst = arith.constant dense<[10.0, 11.0]> : tensor<2x1xf32>
%slice = tensor.extract_slice %cst[0, 0] [1, 1] [1, 1] : tensor<2x1xf32> to tensor<1x1xf32>
->
%slice = arith.constant dense<10.0> : tensor<1x1xf32>
```
Slice of constant splats, even with dynamic shapes:
```mlir
%cst = arith.constant dense<123.0> : tensor<2x512xf32>
%splat = tensor.extract_slice %cst[%o0, 0] [%s0, 512] [1, 1] : tensor<2x512xf32> to tensor<?x512xf32>
->
%cst = arith.constant 123.0 : f32
%init = linalg.init_tensor [%s0, 512] : tensor<?x512xf32>
%splat = linalg.fill(%cst, %init) : f32, tensor<?x512xf32> -> tensor<?x512xf32>
// or another way to broadcast a value, a tensor.splat or something
```
Slice of a slice:
```mlir
%0 = tensor.extract_slice %arg0[%o0, 0] [%s0, 512] [1, 1] : tensor<2x512xi64> to tensor<?x512xi64>
%1 = tensor.extract_slice %0[0, %o1] [%s0, %s1] [1, 1] : tensor<?x512xi64> to tensor<?x?xi64>
->
%1 = tensor.extract_slice %arg0[%o0, %o1] [%s0, %s1] [1, 1] : tensor<2x512xi64> to tensor<?x?xi64>
```
Slice that's just a rank reduction:
```mlir
%0 = tensor.extract_slice %arg0[0, 0] [1, 512] [1, 1] : tensor<1x512xi64> to tensor<512xi64>
->
%0 = tensor.collapse_shape %arg0 ... // or whatever
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJytlclu2zAQQL-GuhARRMqS5YMOaVIDBdpL-wEBLdIWE5p0ScrL33dI2XEsb0ERQ9ZCambeLJqZGb6rUZl5oZ2xqdh6yxr_4pRsBCzjprNWaK92mBvhsDYez43i2Lci3CizkXqBG-Zg07fMY9eaDe5WeG7F3y5KovwRZc8o25__BNXYzHFjtPNMe3d8ocz6Y6mk7Zcw_BAtGucxyp8xs9K36UESc6AWKH9CxTeSpRmiT5gQuBbPKP8OAo-49wteoVuynecU1k8UR0ej6ksR2JsG9VF3UIyDrWjo_OGyPezNcZkMMB6uA13xNTo69G6o9j2SNyOP3UoxiD84INZC4w0YxHyn2VI2kEi2El-QG0LzC8B0WxB6MSEB6RMJgRtzkpQgGxdA8b3UHG2fJAfl0zOsswRdcTe6Ge0E6Y8CUsteQknN1CINzy-9zYvcH1EvEZ0Faq93LpVCtNpHCBTubSM6eeeC1Su68UOMxn3DUzgwoDPoBa2weMN2IYgzaxiHPuAxw2umOhFssUMSe1iQcmYpfAs94zN1ynDfh-7XYHazYphdZF9RMrIcXSuZfuuEidxkyg5NJVCRIVC4udNgTi2fQYX_R6izOr6NNwzZ_1LeDNyQ8UY1hNmC6Njh1y6WmGX6DVvBu8ZLo-9UyGcL5LzJ3y0Mcs2_YVGcxH8I1MAkZSsnXmLTPRDhNE0_fnEbiAH0aTtwNeF1zif5hCVeeiVqgP3188fvgPrI-bHZM8377ylO8DC2gff66E-Tzqq69X4Vh0CkWEDL62aAu4QHpdaHy8PKmlfRQIam0rkOxgadFnmeFUlb84oTNp6MMjGitCI8L2jTjGhZkhEfj8dlothMKBewATmRNc0ozQiZ0JyWeZVWDS2qEZtXVZkLXhAEmpZMqjQYTo1dJLaODLNu4WBTSefdcZM5JxdaiIN-1vnW2Hom9Jpp-ZZE3jrC_gNUxI92">