<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/54249>54249</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [mlir][bufferization] Failing to bufferize expand_shape/collapse_shape of extract_slice
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            mlir:linalg
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            matthias-springer
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          d0k
      </td>
    </tr>
</table>

<pre>
    This only happens when the bufferization pass is set to top down traversal (see https://reviews.llvm.org/D120893)

```
  func @collapse(%arg0: tensor<2xi32>) -> tensor<i32> {
    %0 = tensor.extract_slice %arg0[1] [1] [1] : tensor<2xi32> to tensor<1xi32>
    %1 = tensor.collapse_shape %0 [] : tensor<1xi32> into tensor<i32>
    return %1 : tensor<i32>
  }

  func @expand(%arg0: tensor<2xi32>) -> tensor<1x1xi32> {
    %0 = tensor.extract_slice %arg0[1] [1] [1] : tensor<2xi32> to tensor<1xi32>
    %1 = tensor.expand_shape %0 [[0, 1]] : tensor<1xi32> into tensor<1x1xi32>
    return %1 : tensor<1x1xi32>
  }
```

```
$ mlir-opt -tensor-bufferize t.mlir
t.mlir:3:10: error: 'memref.collapse_shape' op expected collapsed type to be 'memref<i32, affine_map<() -> (1)>>', but got 'memref<i32>'
    %1 = tensor.collapse_shape %0 [] : tensor<1xi32> into tensor<i32>
         ^
t.mlir:3:10: note: see current operation: %2 = "memref.collapse_shape"(%1) {reassociation = []} : (memref<1xi32, affine_map<(d0) -> (d0 + 1)>>) -> memref<i32>
t.mlir:9:10: error: 'memref.expand_shape' op expected collapsed type to be 'memref<1xi32>', but got 'memref<1xi32, affine_map<(d0) -> (d0 + 1)>>'
    %1 = tensor.expand_shape %0 [[0, 1]] : tensor<1xi32> into tensor<1x1xi32>
         ^
t.mlir:9:10: note: see current operation: %2 = "memref.expand_shape"(%1) {reassociation = [[0, 1]]} : (memref<1xi32, affine_map<(d0) -> (d0 + 1)>>) -> memref<1x1xi32>
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVttymzAQ_RrxsmMPCGPMAw923HxB3zMCFlCLgZFE4vTru-JiGzvOJO2kU4-MQFp29xydFUqa7DX-XkoNTV29QinaFmsNLyXWYEqEpMtzVPKXMLKpoRVaA9lqNGAaai1kzQtZKvGMSosKGN9oRCiNaTXzt4w_UlP4LPFFL6vq-bBsVEFDe4-7m8hnPGLunrnb8bp2x9Y_AuRdnQJbuWlTVaLVSO4ZD4QqXHIOhlJtFPMf-FH6nPnfyB0sqD_PDOPAwt3kEijHwAXm70erJR4p_9Q86UqmCJP_YOexYA83_VtxezKmQW9K5jKgdxlwQvOkiW8c8wl2N-4nTyDrywDX7hWaTtVTlO09QxbuL7k-k4vHVtTZp6n1jqf8_i96Bzw35O5cxh_ARvko0WeEHyD71vhM-FzWbw_yFRwqqRZNa2AxeF1M1YdglnZyMB3v_a1Pf69fLlTKJrGlvMIDHhTmVyKjcaBqJWowNZjBNJuBeSWWCHWC55dH7RBdIs9ljU8H0dJYL5FRBnTv2eoluFYcoTVOOgNFY278DAb_qhz6Hwu-3SWrbgza3m5UaacU1oa4QdXvcQOJAe8TZJzfYZMP5WIpsOpXSDtjk8phm-xf7QGQBGDwuDkxMsB4g9vMvWQ3IxL4Di5JHievuZ3jjN4TxWVpfFoSJ33fX-0_x_aOPr60nu-oJfortcxp_phWZnC-VjfXPJy2IieL_SzyI-EYaSqMKaueD5vTbnYUsJQ_ClnJuuiVctqo5tAfr2q7yWH2OXA6VcXzw0IhTdklVHAHerAnhrFbtKr5QUqlR6l1h5pughVfRU4Zr9Db5EG-StNojTxMNxFP3TDF3E_SJAtCpxIJVtrisYs0LDHlLqrCLk-wd2TMXc5d3w09f-X6fJllm2DtrjkmGXUitd_JAwE-HWIcFfdpJV2habKS2pxPOA4tsSxqxCmkMKaUQi90q4gyVGNY0ZmyUXHm_nR6UHGP6DehVsHf">