<div dir="ltr"><div class="gmail_extra" style>Hi Sergei,</div><div class="gmail_extra" style><br></div><div class="gmail_extra">The degree to which LLVM actually makes any guarantees about IEEE arithmetic precision is ambiguous. LangRef, for one, doesn't even mention it (it mentions formats, but nothing else). The de-facto way of interpreting holes in LangRef is to consider how the IR is used by clang and follow the path up into the C and/or C++ standards and then work from there. C describes a binding to IEC 60559, but it is optional, and clang doesn't opt in. C++ doesn't even have the option. So from an official perspective, it's not clear that you have any basis to complain ;-).</div>
<div class="gmail_extra"><br></div><div class="gmail_extra" style>I mention all this not to dismiss your concern, but to put it in context. Right or wrong, much of the C/C++ software world is not that keenly concerned in these matters. This includes LLVM in some respects. The folding of floating-point library routines which you point out in LLVM is one example of this.</div>
<div class="gmail_extra" style><br></div><div class="gmail_extra" style>One idea for addressing this would be to teach LLVM's TargetLibraryInfo to carry information about how precise the target's library functions are. Then, you could either implement soft-float functions within LLVM itself for the affected library functions, or you could disable folding for those functions which are not precise enough on the host (in non-fast-math mode)</div>
<div class="gmail_extra" style><br></div><div class="gmail_extra" style>Another idea for addressing this would be to convince the LLVM community that LLVM shouldn't constant-fold floating-point library functions at all (in non-fast-math mode). I think you could make a reasonable argument for this. There are ways to do this without loosing much optimization -- such expressions are still constant after all, so they can be hoisted out of any loop at all. They could even be hoisted out to main if you want. It's also worth noting that this problem predates the implementation of fast-math mode in LLVM's optimizer. Now that fast-math mode is available, it may be easier to convince people to make the non-fast-math mode more conservative. I don't know that everyone will accept this, but it's worth considering.</div>
<div class="gmail_extra" style><br></div><div class="gmail_extra">Dan</div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Apr 26, 2013 at 12:44 PM, Sergei Larin <span dir="ltr"><<a href="mailto:slarin@codeaurora.org" target="_blank">slarin@codeaurora.org</a>></span> wrote:<br>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-US" link="blue" vlink="purple"><div><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Dan, and anyone else interested… <u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  I am not sure if this has been discussed before, but I do have a case when the following logic fails to work:<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">lib/Analysis/ConstantFolding.cpp<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">static Constant *ConstantFoldBinaryFP(double (*NativeFP)(double, double),<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">                                      double V, double W, Type *Ty) {<u></u><u></u></span></p><p class="MsoNormal">

<span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  sys::llvm_fenv_clearexcept();<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  V = NativeFP(V, W);<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  if (sys::llvm_fenv_testexcept()) {<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">    sys::llvm_fenv_clearexcept();<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">    return 0;<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  }<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">….<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">This fragment seems to assumes that host and target behave in exact the same way in regard to FP exception handling. In some way I understand it, but… On some cross compilation platforms this might not be always true. In case of Hexagon for example our FP math handling is apparently more precise then “stock” one on x86 host.  Specific (but not the best) example would be computing sqrtf(1.000001). Result is 1 + FE_INEXACT set. My current linux x86 host fails the inexact part… resulting in wrong code emitted.<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">  Once again, my question is not about this specific example, but rather about the assumption of identical behavior of completely different systems. What if my target’s “objective” is to exceed IEEE precision? …and I happen to have a set of tests to verify that I do </span><span style="font-size:11.0pt;font-family:Wingdings;color:#1f497d">J</span><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Thank you for any comment.<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d">Sergei<u></u><u></u></span></p>

<p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:11.0pt;font-family:"Calibri","sans-serif";color:#1f497d"><u></u> <u></u></span></p>

<p class="MsoNormal"><span style="font-size:10.5pt;font-family:Consolas;color:#1f497d">---<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:10.5pt;font-family:"Calibri","sans-serif";color:#1f497d">Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, hosted by The Linux Foundation</span><span style="font-size:10.5pt;font-family:Consolas;color:#1f497d"><u></u><u></u></span></p>

<div style="border:none;border-left:solid blue 1.5pt;padding:0in 0in 0in 4.0pt"><div><div><div><div><p class="MsoNormal"><u></u> <u></u></p></div></div></div></div></div></div></div></blockquote></div><br></div></div>