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

    <tr>
        <th>Summary</th>
        <td>
            LLVM incorrectly calls __truncsfbf2 after a bfloat function call in unoptimized code on x86
        </td>
    </tr>

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

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

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

<pre>
    Here is a LLVM IR snippet that has incorrect codegen on x86: https://godbolt.org/z/4Ezb51We8

Here is the expected code that should have been generated for the above snippet on x86_64 (without the incorrect __truncsfbf2 call):
```
BitCastI16ToBF16Wrapper:                # @BitCastI16ToBF16Wrapper
        push    rax
        call BitCastI16ToBF16
        lea     rdi, [rsp + 6]
        call CreateBF16WrapperFromBF16
        mov     ax, word ptr [rsp + 6]
 pop     rcx
        ret
BitCastI16ToBF16:                       # @BitCastI16ToBF16
        pinsrw  xmm0, word ptr [rdi], 0
 ret
CreateBF16WrapperFromBF16:              # @CreateBF16WrapperFromBF16
 pextrw  eax, xmm0, 0
        mov     word ptr [rdi], ax
 ret
```

In addition, there is an additional bug on x86_32 that assumes that the result of the BitCastI16ToBF16 is returned as a 32-bit floating-point value in `st(0)` instead of as an BF16 value in the `xmm0` register.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VMGOozgQ_RrnUurImECSA4ekZ9G2NHtZrXaOrQIK8MjYyC7Smf76lSHpzmYyE0WyANd7z89VD0PQnSUqRHYU2ZcVTtw7X3x3vR0NModV5ZofxZ_kCXQAhK9f__0LXv6GYPU4EgP3yNBjAG1r5z3VDLVrqCMLzsJ5l4v0AD3zGER6EKoUquxcUznDa-c7ocp3ocrNH-9VlnyjnZAHIQ9XNu4J6DxSzdTMqAtb6N1kGujxRFARWejIkse4qXV-rsLKnehD4yLkNd-AULs3zb2beN72qfn1lf1k69BWrYIajRFqHwXLg8jl5S8PR83PGPglyf9xxzLJv3kcR_LxiHc_oVIQG_mrAvlRME6hj6vH883bqADui2--G8J59Y0W6hlEdvRhBKGOkIvsyz3QsydkuuEvvRvuEAd3mlc8R8A35xsY2T9CHt24cNe3ij3xA4MeOPN7g26d0Tb4N4DzMMifNDU6qlHPEK_lQv7rY96ruJD_1peRzhzpaXHkqkI-8OyxsuVCF2m3TSTk4cUCNo1m7Wzcyf11vD7fo4Fq6q6tm6ql9TGEaaCwPMQO9hQmw-Da-enezYjpiSdvqQGM45uqp0oztMYha9s9jU5bhhOaKU4DiFwGFmonY_vnErQNTNhEfJzlzagf2yOnyOXsTS7BU6cDk1-vmiJt9ukeV1Qk22wjM5Vk6aovknS7bfdbud_vNq2SuJNyv88VyrzdNCrBlS6UVJncySRRG5Wm6wTzbFulKPMqzRpMxEbSgNqsjTkNMUJWOoSJiiRL8r1aGazIhDnNlLL0BvNXoVQMN1_Eoqdq6oLYSKMDh08Y1myomOPtIxbMj3mAwv_jAVsmDwjV7CK0k63jjS2zpi1M1o2sB_1-Ta3lEleTN8VdEmrup2pdu0GoMiq5LE-jd9-pZqHKWX8Qqrwc8FSo_wIAAP__Ysi8yA">