[LLVMbugs] [Bug 21635] New: C++ math functions and errno dependencies not captured correctly in LLVM builtins

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Fri Nov 21 12:25:48 PST 2014


http://llvm.org/bugs/show_bug.cgi?id=21635

            Bug ID: 21635
           Summary: C++ math functions and errno dependencies not captured
                    correctly in LLVM builtins
           Product: clang
           Version: trunk
          Hardware: PC
                OS: Windows NT
            Status: NEW
          Severity: normal
          Priority: P
         Component: C++
          Assignee: unassignedclangbugs at nondot.org
          Reporter: lawrence at codeaurora.org
                CC: dgregor at apple.com, llvmbugs at cs.uiuc.edu
    Classification: Unclassified

Created attachment 13383
  --> http://llvm.org/bugs/attachment.cgi?id=13383&action=edit
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 bug #11858 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.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20141121/ed65efce/attachment.html>


More information about the llvm-bugs mailing list