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

    <tr>
        <th>Summary</th>
        <td>
            [MathDialect] Legalize-to-f32 crashes when processing scf.if
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            crash
      </td>
    </tr>

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

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

<pre>
    The below case crashes when calling `scf.if` foldHook. The foldHook will be called twice, and in the second run, `numRegions` becomes `1`, which is supposed to be `2`.
```
mlir-opt %s --math-legalize-to-f32
```
```
module {
  func.func @example_function(%arg0: tensor<1x32xf32>, %arg1: tensor<1x32xbf16>, %arg2: i1) -> tensor<1x32xbf16> {
    %10 = scf.if %arg2 -> (tensor<1x32xbf16>) {
      %11 = linalg.generic {
        indexing_maps = [
 affine_map<(d0, d1) -> (d0, d1)>,
          affine_map<(d0, d1) -> (d0, d1)>
        ],
        iterator_types = ["parallel", "parallel"]
      } ins(%arg0 : tensor<1x32xf32>) outs(%arg1 : tensor<1x32xbf16>) {
        ^bb0(%in: f32, %out: bf16):
          %12 = arith.truncf %in : f32 to bf16
          linalg.yield %12 : bf16
 } -> tensor<1x32xbf16>
      scf.yield %11 : tensor<1x32xbf16>
    } else {
      scf.yield %arg1 : tensor<1x32xbf16>
    }
    return %10 : tensor<1x32xbf16>
  }
}

```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyclU1vpDYYxz-NuTwCGfMycOCQXTLqob1UvUfGPIBbYyPb7CT76StDNoTZpCtVGs2M7f__Z54XbO6cHDViQ4ovpGgjvvrJ2EZY_v2lx9lEnelfmr8mhA6VuYHgDkFY7iZ0cJtQg-BKST0CKakTQyIHUlIYjOp_M-afBIL1xwhuUinocPNgD_4mBRL2FbjuQWrwE4JDYXQPdtVhgZRUr_OfOEqjXQB3KMyMLiykpKRBc5ukmEA6cOuyGBe4JmxCSspISRNCW0Ifgnj_bMNZSRubxQNhhYM4nrmfYoUjV_I7xt7EQ8Y-NN5zTL8qBHL5so8BhlWLJHwBySk-83lR-BTGXhpNWEVYwe1ISfYAHrUzlmRf0-eMPYcds8ct6k2S_izphrQ8aVjQyJSwGmKSPX4sf_90EIwpBZK1sJfrB2kHEFZ9smV9xuygdAMpqbkakxE1WinudQBS9_gs9fg088VtjtBtu4QPg9QYVkj2lbCq32raHyGd5_boz3j4P5ATgRTtT1Tp0XJv7JN_WfB4asYWbkP7KsLYXojTTNGecnRpQWp3lB0-r3sNZvWHNP1A-nkxQgyPXUd3u9TBG8B7p5jVh4nNHsJ_uE9gqCXbYuRW-inxdtViaw2p4ZW1vVaBcOd9rf6LRNW_gR7eS0MWPm_P97zQkgfov1Jw9POlBVQO7zNyQv0in-9hx8CiX61-e2F-YX6zHn_OB0bUN1lfZzWPsEkvLLswllc0mpos7QRmnLOB96zuaC66nlVYiKwoRT9gJBtGWU6rtEjTvKI0yaoM07qqq7wvK1aX4aSZuVSJUt_mxNgxks6t2KQ0zzMWKd6hcs3evt067p1LGNsO8r1rI9sEc9ytoyM5VdJ5d-C89Gq7I_7gfmolVyg8KVr4_Xxinm-GxRqBzoXLYT9qotWqZvJ-caEF2ZWw6yj9tHaJMDNh17Dd60-8WPN32IRdt1AcYdfXaL417N8AAAD__7XM2-M">