llvm.org GIT mirror
[SimplifyLibCalls] Fix infinite loop with fast-math optimization. One of the fast-math optimizations is to replace calls to standard double functions with their float equivalents, e.g. exp -> expf. However, this can cause infinite loops for the following: float expf(float val) { return (float) exp((double) val); } A similar inline declaration exists in the MinGW-w64 math.h header file which when compiled with -O2/3 and fast-math generates infinite loops. So this fix checks that the calling function to the standard double function that is being replaced does not match the float equivalent. Differential Revision: https://reviews.llvm.org/D31806 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@301304 91177308-0d34-0410-b5e6-96231b3b80d8 Andrew Ng 2 years ago
2 changed file(s) with 78 addition(s) and 0 deletion(s).
 925 925 if (V == nullptr) 926 926 return nullptr; 927 927 928 // If call isn't an intrinsic, check that it isn't within a function with the 929 // same name as the float version of this call. 930 // 931 // e.g. inline float expf(float val) { return (float) exp((double) val); } 932 // 933 // A similar such definition exists in the MinGW-w64 math.h header file which 934 // when compiled with -O2 -ffast-math causes the generation of infinite loops 935 // where expf is called. 936 if (!Callee->isIntrinsic()) { 937 const Function *F = CI->getFunction(); 938 StringRef FName = F->getName(); 939 StringRef CalleeName = Callee->getName(); 940 if ((FName.size() == (CalleeName.size() + 1)) && 941 (FName.back() == 'f') && 942 FName.startswith(CalleeName)) 943 return nullptr; 944 } 945 928 946 // Propagate fast-math flags from the existing call to the new call. 929 947 IRBuilder<>::FastMathFlagGuard Guard(B); 930 948 B.setFastMathFlags(CI->getFastMathFlags());