<div dir="ltr">Hi Martin,<div><br></div><div>I'm having a little difficulty understanding this: </div><div><br></div><div>> <span style="font-size:12pt;line-height:24px;font-family:"book antiqua",serif">I would expect this behaviour if ‘</span><span style="font-size:12pt;line-height:24px;font-family:"courier new"">-ffast-math -fno-math-errno</span><span style="font-size:12pt;line-height:24px;font-family:"book antiqua",serif">’ was selected, but it isn’t, and I think that this is an invalid optimisation.</span></div><div><span style="font-size:12pt;line-height:24px;font-family:"book antiqua",serif"><br></span></div><div><font face="book antiqua, serif"><span style="font-size:16px;line-height:24px">There are two optimizations going on:</span></font></div><div>  1. Constant folding of exp() results.</div><div>  2. Elision of exp() calls.</div><div><br></div><div>I would expect (1) to be valid even in full IEEE compliance mode. I would expect (2) to be valid with -fno-math-errno - that is, assume that errno doesn't exist. This is implied by -ffast-math.</div><div><br></div><div>(1) is valid in all modes because of the "as-if" rule - the return value is as-if the function were called. The function has well defined behaviour, so we don't actually need to call it to get the result. (2) would also be valid in all modes as far as I understand it, because on successful return exp() does *not* change errno. exp(1.0) is well-defined and succeeds, so errno doesn't change. It seems to me that clang has regressed in performance here in 3.9 unless there's a subtlety that I'm missing (probable).</div><div><br></div><div>It sounds to me like you're attempting to test your math library. If you're doing this, you probably want -fno-builtin which informs the compiler not to assume the library calls are well defined. With this flag, no constant folding (or elision) will be done which I presume is what you want.</div><div><br></div><div>Cheers,</div><div><br></div><div>James</div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, 1 Sep 2016 at 09:08 Martin J. O'Riordan via cfe-dev <<a href="mailto:cfe-dev@lists.llvm.org">cfe-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div lang="EN-IE" link="#0563C1" vlink="#954F72"><div><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">Thanks Hal,<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><b><span lang="EN-US">From:</span></b><span lang="EN-US"> Hal Finkel [mailto:<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>] <br><b>Sent:</b> 01 September 2016 01:48<br><br></span></p><div></div></div></div><div lang="EN-IE" link="#0563C1" vlink="#954F72"><div><div><blockquote style="border:none;border-left:solid #1010ff 1.5pt;padding:0cm 0cm 0cm 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt"><p class="MsoNormal" style="margin-bottom:12.0pt"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:black">I would expect this behaviour if ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">-ffast-math -fno-math-errno</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:black">’ was selected, but it isn’t, and I think that this is an invalid optimisation.  It also means that some of my math functional tests are not reporting honestly (this only happens when the argument(s) are constants).  Also, on our architecture, ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">double</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:black">’ is FP32,</span><span style="color:black"><u></u><u></u></span></p></blockquote><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Does Clang for your target emit C-language "double" types as "double" at the IR level? If so, that's wrong. "double" at the IR level is assumed to be an IEEE double-precision number. All of the constant folding will do the wrong thing on your target if this is what is happening.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p></div></div></div><div lang="EN-IE" link="#0563C1" vlink="#954F72"><div><div><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">No, I have:<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">      DoubleFormat = &llvm::APFloat::IEEEsingle;<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">set in my ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">TargetInfo</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’, and the IR shows ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">f32</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’.  But it is the elision of the tests when I am not using ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">-ffast-math</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ that I think is wrong, the tests are not present; I’m quite happy with this behaviour when ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">-ffast-math</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ is used.  In the ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">foo</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ example the calls to ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">foo</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ are retained and the tests are present; it is only when I rename ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">foo</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ to ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">exp</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ or some other math function that this happens.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">With ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">-fno-math-errno</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ it can be assumed that the math functions have no other side-effects, and in combination with ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">-ffast-math</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ the reduction to the optimal ‘</span><span style="font-size:12.0pt;font-family:"Courier New",serif;color:black">return 0</span><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">’ is perfect.  But neither of these options are selected.<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">I downloaded the official v3.8.0 distribution for X86 from the LLVM website and tried that, and got the same behaviour, it’s not particular to my out-of-tree changes.  With v3.9.0 RC3 the calls to the math functions are no longer elided, but the tests still are.<u></u><u></u></span></p></div></div></div><div lang="EN-IE" link="#0563C1" vlink="#954F72"><div><div><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><blockquote style="border:none;border-left:solid #1010ff 1.5pt;padding:0cm 0cm 0cm 4.0pt;margin-left:3.75pt;margin-top:5.0pt;margin-bottom:5.0pt"><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:black">and it is probable that the compiler is using the host platform’s implementation which is FP64 for evaluating the test expressions,</span><span style="color:black"><u></u><u></u></span></p></blockquote><p class="MsoNormal"><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:black">Yes, that's right. See ConstantFoldScalarCall in LLVM's lib/Analysis/ConstantFolding.cpp. We're obviously aware this can cause issues when cross compiling. If you'd like to discuss this behavior, you should do so on llvm-dev. We might want to make this more configurable than it currently is.<br><br></span><span style="font-size:10.0pt;font-family:"Arial",sans-serif;color:#943634"><u></u><u></u></span></p></div></div></div><div lang="EN-IE" link="#0563C1" vlink="#954F72"><div><div><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">I don’t have any issue with the constant-folding, just the elision of the tests.  I assume that the special handling of the C math functions is happening in CLang rather than LLVM, but I don’t generally look much at the semantic analysis code so I am not as familiar with it.  If the math library semantic issues are LLVM then I should post this on LLVM-Dev, but I think that this is more likely front-end issue - no?<u></u><u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634"><u></u> <u></u></span></p><p class="MsoNormal"><span style="font-size:12.0pt;font-family:"Book Antiqua",serif;color:#943634">            MartinO<u></u><u></u></span></p><p class="MsoNormal"><span style="color:black"><u></u> <u></u></span></p></div></div></div>_______________________________________________<br>
cfe-dev mailing list<br>
<a href="mailto:cfe-dev@lists.llvm.org" target="_blank">cfe-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><br>
</blockquote></div>