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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect optimization around __builtin_set_flt_rounds
        </td>
    </tr>

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

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

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

<pre>
    Clang seems to believe these two additions produce the same result and will optimize away the later one. However this is incorrect due to the `__builtin_set_flt_rounds` call, potentially changing the rounding mode.

Input:
```
void f(float a, float b, float *d, float *e) {
  *d = a + b;
  __builtin_set_flt_rounds(2);
  *e = a + b;
}
```
 
ir:
```
define dso_local void @f(float, float, float*, float*)(float noundef %a, float noundef %b, ptr noundef writeonly captures(none) initializes((0, 4)) %d, ptr noundef writeonly captures(none) initializes((0, 4)) %e) local_unnamed_addr {
entry:
  %add = fadd float %a, %b
  store float %add, ptr %d, align 4
  tail call void @llvm.set.rounding(i32 2)
 store float %add, ptr %e, align 4
  ret void
}
```

godbolt: https://godbolt.org/z/aaKnhvqMj


</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJysVNuO4zYM_Rr5hdhAkW_xgx88Owi6KPoNgWzRtgay5Ep0gszXF1IuOzvb6VMBA6HEQ4rnkKEMQU8WsWXlCytfM7nR7Hz7Pks7vW12na9Z79S1_W6knSAgLgHIQY9G4xmBZgwIdHEgldKknQ2weqe2IfkgyAXBY9gMgbQKLtoYcCvpRb8jyIu8JpiRhB6cxR384S54Rg806wDxs4PzHgcCtWF8OuJZxU-nftOGtD0FpNNo6OTdZlVgFYdBGsPEd1gdoSUtjbnCEBlpO6X4BI2HxSncMd4x3v2w60Ysjyar-P3j3dlpBSMTh9E4SSBj3pvZ_zSZ6NQvJ2SiAVa_MN5B8gLLX0ECEy_Qs_x2_yUFcRBMNA9YTPd7OKtfP1UKjHfa_85A4agtggruZNwgDSRKrOBPVs_SPxjdr3bzVMDGGnEEJsoPYny4Tbqs5J93F68JnY1NkCttHiND62wSSVsdO6Tf0y0TBx7Di_RiE9Op_zddQiUdTpu1ckF1kkr5e7PQkr_eJITEUN06N0bj0d0b78Q0wgI5jx-c6lnxo3pp9GShSGiS2qQBfbbBmPOyC0i7x1QycdC5gDQDvPvv_Pg5v0dKmf91RBjvJqd6Z-Kgw0y0hkhWHJk43h075ycmju9MHKX8087nv_96u0Vmqs1Vkzcyw3ZfF3XeFHUusrnFaqjFWCgpx_qwL_eyL0RdHEqBxV40hybTreCi5OW-2u9FmYtddRjLpuJqqCrZiHpkBcdFarNLWjg_ZTqEDdt9wUXeZEb2aMJjRfk2or712xSiejpQ-BlHmgy2P55L475rZFxNIJPCX_7vss2b9pMomuat3w1uYeIYH7n_fFu9e8OBmDimSgMTx3ux51b8EwAA__-b4arA">