Skip to content

llvm.fma.bf16 intrinsic is expanded incorrectly #131531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
beetrees opened this issue Mar 16, 2025 · 0 comments
Open

llvm.fma.bf16 intrinsic is expanded incorrectly #131531

beetrees opened this issue Mar 16, 2025 · 0 comments
Labels

Comments

@beetrees
Copy link
Contributor

Consider the following LLVM IR:

define bfloat @do_fma(bfloat %a, bfloat %b, bfloat %c) {
    %res = call bfloat @llvm.fma.bf16(bfloat %a, bfloat %b, bfloat %c)
    ret bfloat %res
}

LLVM turns this into the equivalent of:

define bfloat @do_fma(bfloat %a, bfloat %b, bfloat %c) {
    %a_f32 = fpext bfloat %a to float
    %b_f32 = fpext bfloat %b to float
    %c_f32 = fpext bfloat %c to float
    %res_f32 = call float @llvm.fma.f32(float %a_f32, float %b_f32, float %c_f32)
    %res = fptrunc float %res_f32 to bfloat
    ret bfloat %res
}

This is a miscompilation, however, as float does not have enough precision to do a fused-multiply-add for bfloat without double rounding becoming an issue. For instance: do_fma(0x1.40p+127, 0x1.04p+0, 0x1.00p-133) = 0x1.46p+127, but LLVM's lowering to float FMA gives an incorrect result of 0x1.44p+127.

Just using double instead of float would also not be a correct lowering: it would give the same incorrect result as the example above (using the reasoning from #128450 (comment), a 126 + 127 + 8 = 261-bit significand would be required for double rounding not to be a problem with this lowering). I suspect the best option here is to lower to a libcall instead.

Closely related to #98389/#128450

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants