llvm.org GIT mirror llvm / 055a715
[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). Raw diff Collapse all Expand all
925925 if (V == nullptr)
926926 return nullptr;
927927
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
928946 // Propagate fast-math flags from the existing call to the new call.
929947 IRBuilder<>::FastMathFlagGuard Guard(B);
930948 B.setFastMathFlags(CI->getFastMathFlags());
0 ; RUN: opt -S -instcombine -o - %s | FileCheck %s
1
2 ; Test that fast math lib call simplification of double math function to float
3 ; equivalent doesn't occur when the calling function matches the float
4 ; equivalent math function. Otherwise this can cause the generation of infinite
5 ; loops when compiled with -O2/3 and fast math.
6
7 ; Test case C source:
8 ;
9 ; extern double exp(double x);
10 ; inline float expf(float x) { return (float) exp((double) x); }
11 ; float fn(float f) { return expf(f); }
12 ;
13 ; IR generated with command:
14 ;
15 ; clang -cc1 -O2 -ffast-math -emit-llvm -disable-llvm-passes -triple x86_64-unknown-unknown -o -
16
17 target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
18 target triple = "x86_64-unknown-unknown"
19
20 ; Function Attrs: nounwind
21 define float @fn(float %f) #0 {
22 ; CHECK: define float @fn(
23 ; CHECK: call fast float @expf(
24 %f.addr = alloca float, align 4
25 store float %f, float* %f.addr, align 4, !tbaa !1
26 %1 = load float, float* %f.addr, align 4, !tbaa !1
27 %call = call fast float @expf(float %1) #3
28 ret float %call
29 }
30
31 ; Function Attrs: inlinehint nounwind readnone
32 define available_externally float @expf(float %x) #1 {
33 ; CHECK: define available_externally float @expf(
34 ; CHECK: fpext float
35 ; CHECK: call fast double @exp(
36 ; CHECK: fptrunc double
37 %x.addr = alloca float, align 4
38 store float %x, float* %x.addr, align 4, !tbaa !1
39 %1 = load float, float* %x.addr, align 4, !tbaa !1
40 %conv = fpext float %1 to double
41 %call = call fast double @exp(double %conv) #3
42 %conv1 = fptrunc double %call to float
43 ret float %conv1
44 }
45
46 ; Function Attrs: nounwind readnone
47 declare double @exp(double) #2
48
49 attributes #0 = { nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
50 attributes #1 = { inlinehint nounwind readnone "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="false" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-features"="+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
51 attributes #2 = { nounwind readnone }
52
53 !llvm.ident = !{!0}
54
55 !0 = !{!"clang version 5.0.0"}
56 !1 = !{!2, !2, i64 0}
57 !2 = !{!"float", !3, i64 0}
58 !3 = !{!"omnipotent char", !4, i64 0}
59 !4 = !{!"Simple C/C++ TBAA"}