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

    <tr>
        <th>Summary</th>
        <td>
            [mlir][memref] Collapse on strided memref is conservative
        </td>
    </tr>

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

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

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

<pre>
    Take this example
```
func.func @strided_collapse (%in : memref<1x1x16x4x18xf32>) -> memref<1x16x64xf32, strided<[1152, 72, 1]>> {
 %subview_7 = memref.subview %in[0, 0, 0, 0, 0] [1, 1, 16, 4, 16] [1, 1, 1, 1, 1] : memref<1x1x16x4x18xf32> to memref<1x1x16x4x16xf32, strided<[1152, 1152, 72, 18, 1]>>
  %collapse_shape = memref.collapse_shape %subview_7 [[0], [1, 2], [3, 4]] : memref<1x1x16x4x16xf32, strided<[1152, 1152, 72, 18, 1]>> into memref<1x16x64xf32, strided<[1152, 72, 1]>>
  return %collapse_shape : memref<1x16x64xf32, strided<[1152, 72, 1]>>
}
```
This is a correct op however we will get
```
mlir-opt test.mlir 
test.mlir:3:21: error: 'memref.collapse_shape' op invalid source layout map or collapsing non-contiguous dims
  %collapse_shape = memref.collapse_shape %subview_7 [[0], [1, 2], [3, 4]] : memref<1x1x16x4x16xf32, strided<[1152, 1152, 72, 18, 1]>> into memref<1x16x64xf32, strided<[1152, 72, 1]>>
 ```
 
 The reason is the verifier uses this function 
 https://github.com/llvm/llvm-project/blob/main/mlir/lib/Dialect/MemRef/IR/MemRefOps.cpp#L2416
 which checks if the src and result strides match as a proof of contiguous collapse but that doesnt seem necessary to me?
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzsVV1r6zgT_jXjm6HBkvxRX_giaRp44V0WDr0vsj2OtUeWjCSn6b9f5Dj9OqFw2L1cCOOMxhrN88zjkfReHQ1RDfkO8n0i5zBYVxvlTtQNI_nByTFpbPdaP8mfhGFQHuksx0kTpHtIt1Ck629x-9m0m2gQstQHpzrqnlurtZw8IfB74LkyCGKLI42OehAP7MzOrDhnZ3Z_7gUH8Qi8wjsQj5_eKc5FtsT5A66ZQTxAvmMsXxbLxTLI9zGFeEQod5eqEHju5-ak6OW5RBD7NfFmXcSlKsh3aczwi8n3GI-5ZI-miDZb__4S_WBi7HuoGOytcPEt0i-I7z_jXjFHUFfmn_0gJ_qI_GvkE0FRDLuIOya-guPvvrjgz_ffAPwnCFCZL7T8dvOvJDgKszM3udj-GydAub_5ITzFL0V5lNha56gNaCcc7AudyOEL4YvSGo8Ubm4etXJ3dgoYyIdN9PASePNBbAWILWcRBjln4woCL2-2F3gZj1fmJLXq0NvZtYRavto54CgntA7XHcoc0Vhz11oT1HG2s8dOjf4_Tb1r6kuz1tbg00DoSHprYt_DQHgip3pFDmdP_jI742gMyprrpiGEyYPYAj8APxxVGOZm09oR-EHr0_VxNzn7F7UB-KHRtgF-GKUy8RGlwA9axbW9kvry0h80_qAe-OF_P968Pye_aacJuPg_z1ixnv8yqHbAdqD2p0fVL2V716I0HTrysw4rNR5HGdoBZZT05Kzt0fb4QSVvQ76ZA4ZBBuwseRPQE41oqCXvpXu9zDsQh6SrRVeJSiZUs5JXvGI5K5KhZi0XTc-Ke9b0VFVp1xQdpXlflR0VsiwTVfOUZyxl95xnOSs2ouF5zoqi5V0ueS8hS2mUSm8idxvrjonyfqaaMV5VWaJlQ9ovVx7nFwajDhNXL1w389FDlmrlg3_PEFTQyzW5bIgq3a1ayvf4cMVuzVVJq9KiFFprPLmTDOpEyex0_dtNX8r3wA8rglPN_w4AAP__jUVD3Q">