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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] normalize-memrefs: Ilegal IR on memref.load/memref.store
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            mlir,
            mlir:memref
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          mgehre-amd
      </td>
    </tr>
</table>

<pre>
    `-normalize-memrefs` on
```
#map = affine_map<(d0, d1) -> (d1, d0)>
func.func @forward(%arg0: index, %arg1: index) {
    %alloc = memref.alloc() : memref<85x40xf32, #map>
    %8 = memref.load %alloc[%arg0, %arg1] : memref<85x40xf32, #map>
    return
}
```
produces
```
 func.func @forward(%arg0: index, %arg1: index) {
    %alloc = memref.alloc() : memref<40x85xf32>
    %0 = memref.load %alloc[%arg0, %arg1] : memref<40x85xf32>
    return
  }
```
where the indices into `%alloc` within the memref.load have not been swapped as they should be.
I would expect the result to be
```
  func.func @forward(%arg0: index, %arg1: index) {
    %alloc = memref.alloc() : memref<40x85xf32>
 %0 = memref.load %alloc[%arg1, %arg0] : memref<40x85xf32>
 return
  }
```

As comparison, applying this to the same IR with `affine.load`:
```
func.func @affine(%arg0: index, %arg1: index) {
 %alloc = memref.alloc() : memref<85x40xf32, #map>
    %8 = affine.load %alloc[%arg0, %arg1] : memref<85x40xf32, #map>
 return
}
```
results in a correct swap of the indices:
```
 func.func @affine(%arg0: index, %arg1: index) {
    %alloc = memref.alloc() : memref<40x85xf32>
    %0 = affine.load %alloc[symbol(%arg1), symbol(%arg0)] : memref<40x85xf32>
    return
 }
```

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzMVlGPozYQ_jXmZZTI2EDggYfcZiNFal_uD1QGBnBlMLLNJumvr2xyd-w1kdLtVq0UEXlsf_PNN-OxhbWyGxFLkn4h6SESs-u1KYcOe4MbMTRRpZtrSTK6GbUZhJJ_4GbAwWBrSUZBj4QeCN2TjN5-y5DxQUxA-AFE28oRfxvERPgLYXlDCXuBJiasgA3hr-BtcbBRwgrCXxeIdh7rrf8ASWirzVmYhrCcsFSYjhK-Bzk2ePEbF1u8shVAdl8WHAAIC5TSdSC0sN8GQwAswO9czIS_5OkloZeWswWaB-av78DyNZDSovnuwct4Y7gilh7-nguDbjbflN0d7ko8Gd3MNdq7k_CfqZfQS56G0H7SjP5Dze4Dr5UCeKTVuUeD4Hr0EcoaLcjRafDz3zhkFM7S9XIMy9Y0e_GGMGoHFeII9iymCRsQ1i-8gu31rBqocLu4OsE5GPAyYe0CmEE7KwdOQ4X3s_W_StdzuYp_cKHP5Oq5RC3fvYVaD5Mw0urR-xHTpK5y7MD10nohvaxWDAinryFtPpVLowl0PR7f3_XwTully4eE_ndayiqGz2wpz_STpUr9yQABtTbGl6-vdtDt-ug8EhY-S9lP7jgPJLXXodLqO0F_HXlqP5nDjfSBTvRI5KgpeVPwQkRYxjuWJ1lcJEnUl1mGoi52acHzRqRF1SZtgzWrsjyjNat4JEtGWRJTWtA8LmiyzXa7PGc8R5bvOItjklAchFRbpd6GrTZdJK2dsYzjOEuySIkKlQ3XPGPV3BHmK4UwNihp3g_4_hYqY_5JYEqPuKnmzpKEKmmd_eHDSafC4-HXX05fvVJ_fSXwPZwUdkL5w6rHdVsh7HgbWacNRrNRZe_cFIqMHQk7dtL1c7Wt9UDY0Xu9_W0mo3_H2hF2DGFawo63SN9K9mcAAAD__7wgfPk">