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

    <tr>
        <th>Summary</th>
        <td>
            Loop Fusion crashes because control flow equivalences are broken after fusion
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    We are trying to fuse loops in 481.wrf benchmark from SPEC, but we are getting errors after fusing some loops.

The CFG before doing any fusion is provided [here](https://github.com/llvm/llvm-project/assets/148104143/7db2824e-f10b-48ea-8ea7-49f5bd4a8946).


Initially loops 704, 710, 751 and few other ones are identified as fusion candidates. Branch conditions of the blocks 702 and 749 are the same so loop 704 dominates both 710 and 751 while loop 751 post-dominates 710 and 704. This makes them control flow equivalent and thus candidates to be fused together. 

 However, right after loops 704 and 710 get fused together we get [this](https://github.com/llvm/llvm-project/assets/148104143/f6f12c32-bbbd-44e9-8e48-95c7cb08015e).


The problem here is that the resulting fused loop (704) is guarded by block 702 (before fusion loops 704 and 710 are not guarded). This means that now loop 751 should post-dominate block 702 instead of 704 in order to be control flow equivalent to the fused loop (which clearly doesn't). This makes the fusion candidate set invalid and results in error.

We would appreciate your thoughts and any possible solution!
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyslEFv4zYThn8NfRnYIGnJkg46bJLPXwv0sGgX2DMpjkw2NMclqRjury9IK5ukuz0U6EUCpOFw5n2fGZWSOwXEkbUPrH3aqCVbiuOvtPyp0X5Wz9kFlTeazG38iqAiQo43F06QCeYlIXiiSwIXoOnF7hpn0Bgme1bxGeZIZ_jt8_8emXwEvWS43jOcMOeSAmOkmEDNGWNJVr4lOq85d4w_Mf7p_vxiER6P_weNM0UEQyVWhVs9RgFcgkukF2fQAGsfLEZk7ROTvc35ktj-E5NHJo8nl-2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