Skip to content

Incorrect optimization around __builtin_set_flt_rounds #140239

Open
@zhangjunphy

Description

@zhangjunphy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    clang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzerfloating-pointFloating-point math

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions