Skip to content

__builtin_isnan/__builtin_isfinite should not produce poison with -ffinite-math-only #45422

@arsenm

Description

@arsenm
Bugzilla Link 46077
Version trunk
OS All
CC @efriedma-quic,@aqjune,@LebedevRI,@zhengyang92,@RKSimon,@nunoplopes,@regehr,@zygoloid,@rotateright
Fixed by commit(s) b89ae10

Extended Description

With -ffinite-math-only, I would intuitively expect __builtin_isnan to always return false, and __builtin_isfinite to always return true. This would allow compiling a fast and slow version of the same math library sources, where the special case paths are naturally optimized out in the fast variant of the library.

However, if I look at the IR produced and reading the LangRef, I believe these will currently return poison.

For these basic test functions:

bool test_isnan(float x) {
    return __builtin_isnan(x);
}

bool test_isfinite(float x) {
    return __builtin_isfinite(x);
}

I currently get this IR:

define hidden zeroext i1 @test_isnan(float %0) #0 {
  %2 = alloca float, align 4, addrspace(5)
  store float %0, float addrspace(5)* %2, align 4, !tbaa !5
  %3 = load float, float addrspace(5)* %2, align 4, !tbaa !5
  %4 = fcmp nnan ninf uno float %3, %3
  ret i1 %4
}

define hidden zeroext i1 @test_isfinite(float %0) #0 {
  %2 = alloca float, align 4, addrspace(5)
  store float %0, float addrspace(5)* %2, align 4, !tbaa !5
  %3 = load float, float addrspace(5)* %2, align 4, !tbaa !5
  %4 = call nnan ninf float @llvm.fabs.f32(float %3) #2
  %5 = fcmp nnan ninf one float %4, 0x7FF0000000000000
  ret i1 %5
}

The description for fast math flags states that the return value is poison if an input is a nan/inf with the corresponding flag. This is trivially violated in the isfinite case since a compare to a constant infinity.

I think the correct solution is to not emit the nnan/ninf flags when emitting these builtins.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugzillaIssues migrated from bugzillaclang:codegenIR generation bugs: mangling, exceptions, etc.floating-pointFloating-point math

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions