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

    <tr>
        <th>Summary</th>
        <td>
            [MLIR Affine Bug] Error in simplification of an expression with a mod
        </td>
    </tr>

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

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

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

<pre>
    Right now, the following affine expression `(s0 mod 32) * 64` is erroneously simplified to `s0 * 64 + (s0 floordiv 32) * 2048.

Reproducer
```
#map = affine_map<()[s0] -> ((s0 mod 32)*64)> 
func.func @affine_test(%arg0: index) -> index {
  %0 = affine.apply #map()[%arg0]
  return %0: index
}
```

When simplified with the `mlir-opt -canonicalize` pass, we get

```
#map = affine_map<()[s0] -> (s0 * 64 - (s0 floordiv 32) * 2048)>
module {
 func.func @affine_test(%arg0: index) -> index {
    %0 = affine.apply #map()[%arg0]
    return %0 : index
  }
}
```

Expected results is the original expression, aka `affine_map<()[s0] -> ((s0 mod 32)*64)> `
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslEGPqzYQxz_NcBklcgYI5MAh2WykSu1lLz1Wxgzg1tjINm93--krQ16TV1Xv0FcpsmWH-Xvm_7NHhqAHy9xAeYHymskljs43Z8Mf0naeX7Ua2bbsB_ZZ67rP5k0PY0Tr3oFeMI6MvTPGvWs7oOx7bRn5Y_YcgnYW4SiA6iBwch3mBHRCoDMeCzgK1AHZe2fZLcF8YtDTbHSvucPoUmQQ948R6IKbTm-c853-8iRGoqj3IK4gztv4xrN33aLY33eP4v7blpRPckbIr_eEf5vkDPkLUA10gvISBJRX3EH-iuveN_kDnY9FmtO_q16_WLVPA0Ih7oqRQ1xjS-kHAfkZte34I6W86q4rhOqySSACleIppb2cZ_OJW6p_J_ZVrrx-DfMcF2_X6MchW5XV9d-rX8dfR7bPhr_rOK4w4Sgmo_3OzRF3SlpntZJG_8kJ2CxDSNTfGQeOz3o_ZvED9O77mDfftzMm1y2Gnzz8Xzj8ZxLfsMB_wEB84Pgul9ePmVXkDj2HxcSQ3kjC4rwetJXm6W0lEPIPmYj96DU-iqxr8u6Un2TGzaE6iPqQ12WVjY0qRMeVojoXp14dakXUyyrntmBVFsUp0w0JKkR-OAkSxaHaKyU61ZY15dWhPx0EFIInqc3emC_T3vkh0yEs3NRlRcfMyJZNWLsPUbsMQAT0AkTpFkJ-3kpLu-U1803S2LXLEKAQRocYHqpRR7O2sV9-_ukNz1sruixDMuHVe-dRP668kjG1J9ejtM_9an0HMtmULd40Y4xzgPwMdAO6DTqOS7tXbgK6pXPv02727ndWEei2lhaAbmt1fwUAAP__Pf2IVQ">