<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/141622>141622</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
[AMDGPU] InstCombine moving freeze instructions breaks FMA formation
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
jayfoad
</td>
</tr>
</table>
<pre>
This is a code quality issue that has been affecting some graphics workloads recently. The LLPC frontend tends to insert `freeze` instructions between `cmp` and conditional `br` instructions, to avoid undefined behavior if the condition is undef or poison. Then InstCombine moves the `freeze` instructions into places where they interfere with optimizations like FMA formation.
With this [test case](https://github.com/user-attachments/files/20460389/r.txt) I get this ISA including a `v_fma_f32` instruction:
```
$ llc -mtriple=amdgcn -mcpu=gfx1010 r.txt -o -
...
main: ; @main
; %bb.0: ; %bb
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_fma_f32 v0, v0, v1, 1.0
v_cmp_lt_f32_e32 vcc_lo, 0, v0
v_cndmask_b32_e64 v0, 0, 1, vcc_lo
s_setpc_b64 s[30:31]
```
But after running it through InstCombine, I get separate `v_mul_f32` and `v_add_f32` instructions:
```
$ opt -passes=instcombine r.txt -o - | llc -mtriple=amdgcn -mcpu=gfx1010
...
main: ; @main
; %bb.0: ; %bb
s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)
v_mul_f32_e32 v0, v0, v1
v_add_f32_e32 v0, 1.0, v0
v_cmp_lt_f32_e32 vcc_lo, 0, v0
v_cndmask_b32_e64 v0, 0, 1, vcc_lo
s_setpc_b64 s[30:31]
```
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUVU2P4ygT_jXkUkqE8UeSgw_pjvKqpRlppHdWe4wwlG2mMXihnHTPr1_huKc_NLs7p5U2irCAp6qoeh4oGaPpHGLNyjtWHldyot6H-pt8br3Uq8br5_prbyKYCBKU1wh_TN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