https://bugs.llvm.org/show_bug.cgi?id=46077

            Bug ID: 46077
           Summary: __builtin_isnan/__builtin_isfinite should not produce
                    poison with -ffinite-math-only
           Product: clang
           Version: trunk
          Hardware: PC
                OS: All
            Status: NEW
          Severity: enhancement
          Priority: P
         Component: LLVM Codegen
          Assignee: unassignedclangb...@nondot.org
          Reporter: matthew.arsena...@amd.com
                CC: llvm-bugs@lists.llvm.org, neeil...@live.com,
                    richard-l...@metafoo.co.uk

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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to