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

    <tr>
        <th>Summary</th>
        <td>
            SCF ForLoopRangeFoldingPass has multiple correctness issues
        </td>
    </tr>

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

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

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

<pre>
    I spent some time trying to use the SCF ForLoopRangeFoldingPass, and I found that it mis-compiles on at least two cases, neither of which have unit tests:

1. When folding a mul into an induction variable where the loop has a non-zero lower bound.
2. Folding a mul by a negative number.

The first is relatively easily dealt with by also folding the lower bound, but the second is not so easy to do within any generality -- and I think it calls into question whether we should have this pass at all. Perhaps there is some trick to getting this case right but it seems to me that the optimization either needs to be a lot smarter or a lot more restrictive (at which point, it may not be worth it).

As written, this is a bit of a footgun and I think we should either fix it or remove it.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx9VE2PmzAQ_TVwGQUlEDbNgcN2V5FW6mHVVurZwASma2xqm02zv77PJv3YS6UIB4_nzZv3xrS2vzZP5Gc2gbydmILEh7uKGShYWjzeRqYvDyc6WffJ2vmzMgOfrO5x5Fl5n5UPpExPT3S2C9YwqkASaBK_6ew0i2ZP1hB2NSsfKFwsdcpzyjQswHdkz3QZpRtpVK9MiwFAYB98Vt1n28dse3vuCvo2skGpVJ8UTYsmMaCqDNZ-6YKg2KtyolrNAGW3tqDBHegeOcaazRs7i70LareRd7EWKAs6vcNurzGBBxUExMwyteyKfyl9BfZZHBoTT451OqivhF4FS89KB7qgywSlvf1DfmX1h0GUo11C2vbcWWgJRGOjMxHtGg3pbcIS6GmuNLBhp7SEK202Nxdi8CUa0Cmt_arNjwVaRl0gR5L7ghKjXXS_6o0cTzPMjC4hraBndqOafSQD_RBdp8NJ9xJpDBzC2gJC0UxyMowh8Udpzzz5eC7mxHmIPdkZwyVvKhG52W6Y-3SwZcisY6-TciEOhLttTBYEHPijdvIgKz8AcZ2W2aK_KFwcOHVNagHqYh30FkSO77y693RxEgKbmJPISxyIFumYQAVrbBgW807Kv1rdSJ_lZ6wHho4nC0YSirxvqv5YHVUeJGhu_nNh0hBitILMGNDOOsddMOwjFw-j8sXpZgxhTsNfnvAbUHhpC1wnvGj9-nvZzM5-RzZe11z8qe_Kqs7H5nwuqzPX7WF_2HZ113_ou31VH_rqeDzsduUu16pl7Zus_piV5aTFYcnqx1yacluW27vyriyrw64u2mPF-_2hVttaqb7us_2WJyW6iBQK64bcNYlNuwweQS24uH-DaFkGGJ0qAV8tYbSu8YG1Vk51nfT4_OSpgSax_wW3P5bX">