<html>
    <head>
      <base href="https://bugs.llvm.org/">
    </head>
    <body><table border="1" cellspacing="0" cellpadding="8">
        <tr>
          <th>Bug ID</th>
          <td><a class="bz_bug_link 
          bz_status_NEW "
   title="NEW - __builtin_isnan/__builtin_isfinite should not produce poison with -ffinite-math-only"
   href="https://bugs.llvm.org/show_bug.cgi?id=46077">46077</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>__builtin_isnan/__builtin_isfinite should not produce poison with -ffinite-math-only
          </td>
        </tr>

        <tr>
          <th>Product</th>
          <td>clang
          </td>
        </tr>

        <tr>
          <th>Version</th>
          <td>trunk
          </td>
        </tr>

        <tr>
          <th>Hardware</th>
          <td>PC
          </td>
        </tr>

        <tr>
          <th>OS</th>
          <td>All
          </td>
        </tr>

        <tr>
          <th>Status</th>
          <td>NEW
          </td>
        </tr>

        <tr>
          <th>Severity</th>
          <td>enhancement
          </td>
        </tr>

        <tr>
          <th>Priority</th>
          <td>P
          </td>
        </tr>

        <tr>
          <th>Component</th>
          <td>LLVM Codegen
          </td>
        </tr>

        <tr>
          <th>Assignee</th>
          <td>unassignedclangbugs@nondot.org
          </td>
        </tr>

        <tr>
          <th>Reporter</th>
          <td>Matthew.Arsenault@amd.com
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>llvm-bugs@lists.llvm.org, neeilans@live.com, richard-llvm@metafoo.co.uk
          </td>
        </tr></table>
      <p>
        <div>
        <pre>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.</pre>
        </div>
      </p>


      <hr>
      <span>You are receiving this mail because:</span>

      <ul>
          <li>You are on the CC list for the bug.</li>
      </ul>
    </body>
</html>