<html>
    <head>
      <base href="http://llvm.org/bugs/" />
    </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 --- - C++ math functions and errno dependencies not captured correctly in LLVM builtins"
   href="http://llvm.org/bugs/show_bug.cgi?id=21635">21635</a>
          </td>
        </tr>

        <tr>
          <th>Summary</th>
          <td>C++ math functions and errno dependencies not captured correctly in LLVM builtins
          </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>Windows NT
          </td>
        </tr>

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

        <tr>
          <th>Severity</th>
          <td>normal
          </td>
        </tr>

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

        <tr>
          <th>Component</th>
          <td>C++
          </td>
        </tr>

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

        <tr>
          <th>Reporter</th>
          <td>lawrence@codeaurora.org
          </td>
        </tr>

        <tr>
          <th>CC</th>
          <td>dgregor@apple.com, llvmbugs@cs.uiuc.edu
          </td>
        </tr>

        <tr>
          <th>Classification</th>
          <td>Unclassified
          </td>
        </tr></table>
      <p>
        <div>
        <pre>Created <span class=""><a href="attachment.cgi?id=13383" name="attach_13383" title="IR Dump for the testcase">attachment 13383</a> <a href="attachment.cgi?id=13383&action=edit" title="IR Dump for the testcase">[details]</a></span>
IR Dump for the testcase

The problem is exposed by a very small testcase which do a log(-4) then check
errno, the program doesn’t run correctly if I compile it with llvm for arm 32
bit at O1 and above, the reason is the call to the log function is moved down
after errno checking; If I replace std::log with log,  program can run
correctly, see my command line and testcase below and IR dump in attached t.ll:

clang -mcpu=cortex-a9 -O1   -static -Wno-return-type -fmath-errno -static
-trigraphs -fexceptions t.C  -o t
./t

//t.C
#ifdef NO_CPP_C_HEADERS
#include <math.h>
#else
#include <cmath>
#endif

#include <errno.h>
#include <stdio.h>

float          x3 = -4.0f;
int            locflg=0;

int
main( void )
{
  float          val;

  val = std::log(x3);    // clang generated a call to _ZSt3logf(),which
contains a call to logf(), because std::log is just a wrapper of
__builtin_logf()
  //val = log(x3);       // clang generated a call to log() directly
  if (errno != EDOM) {
    locflg = 1;
    printf( "std::log: GOT %f, EXPECTED EDOM\n", val);
  }

return( locflg );
}

After some debugging, I think the root cause is because of the attribute “c” of
“Fnc” in the following line of tools/clang/include/clang/Basic/Builtins.def:
BUILTIN(__builtin_logf , "dd"  , "Fnc")

“c” means const, then later clang will set the function attribute to readnone
based on that (CodeGenModule::ConstructAttributeList of
tools/clang/lib/CodeGen/CGCall.cpp), that will enable optimizer to do a lot of
unsafe optimization such as move logf pass errno checking, that should not
happen since I have –fmath-errno option.

For C version log function, the attribute is “Fne” according to the following
line in the same Builtins.def
LIBBUILTIN(log, "dd", "fne", "math.h", ALL_LANGUAGES)

Note that “e” mean “const, but only when -fmath-errno=0”, that’s the correct
attribute, and that’s why the optimizer doesn’t do the unsafe optimization.

During filing this bug, I noticed there were related <a class="bz_bug_link 
          bz_status_RESOLVED  bz_closed"
   title="RESOLVED INVALID - clang should not default to -fno-math-errno"
   href="show_bug.cgi?id=11858">bug #11858</a> and #5971,
however I think mine is a little bit different since I have -fmath-errno:

so my questions are:

1. why does __builtin_logf and its brothers has different attributes than log
and its brothers?

2. If __builtin_logfs and logs should all have "Fnc", then there should be a
later mechanism in clang to remove readnone if -fmath-errno is present, but I
found none.</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>