<html><head><meta http-equiv="Content-Type" content="text/html charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; -webkit-line-break: after-white-space;" class=""><br class=""><div><blockquote type="cite" class=""><div class="">On Sep 16, 2016, at 1:30 AM, Martin J. O'Riordan via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" class="">llvm-dev@lists.llvm.org</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">A little while ago I asked a question on CFE-Dev about a change in the behaviour of programs using the ISO C math functions, although that question should have been put to LLVM-Dev.  But I got excellent clarification of the problem anyway.  However, since then I have been trying to adapt our out-of-tree implementation to get the previous behaviour.  The problem is that something like:<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">#include <math.h><o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">extern double foo(double);<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">int useMathName() {<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">  if ((exp(1.0) < 2.71) || (exp(1.0) > 2.72))<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">    return -1;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">  return 0;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">}<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">int useOtherName() {<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">  if ((foo(1.0) < 2.71) || (foo(1.0) > 2.72))<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">    return -1;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">  return 0;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span style="font-size: 12pt; font-family: 'Courier New';" class="">}<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">With v3.8.0 the compiler elided both the calls to ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">exp</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ and the tests, so the function ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">useMathName</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ reduced to simply:<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">return 0;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">But this was not correct as it ignored the possibility that the math functions could have the side-effect of changing ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’, and this was fixed in v3.9.0, and the calls are no longer elided, though using the “as-if” rule, the tests are still eliminated and ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">useMathName</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ becomes:<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">(void)exp(1.0);<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">(void)exp(1.0);</span></div></div></div></blockquote><div><br class=""></div><div><br class=""></div><div>It is not clear to me: isn’t the spec saying that errno shall be set on underflow and overflow. The constant folding should be able to detect this and fold if it does not happen?</div><div><br class=""></div><div>Also, because we don’t really model errno, we don’t constant fold these call anymore :(</div><div><br class=""></div><br class=""><blockquote type="cite" class=""><div class=""><div class="WordSection1" style="page: WordSection1; font-family: Helvetica; font-size: 12px; font-style: normal; font-variant-caps: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: start; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class=""><o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt 36pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">return 0;<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">So I changed our implementation so that ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-fno-math-errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ is the default for the tool-chain, and the ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-fmath-errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ option is not passed on to the CC1 phase.  I expected that this would allow the compiler to elide the calls, but it is not doing so.  I don’t want to use ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-ffast-math</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ as this has lots of FP issues, and I don’t want to make it the default.<o:p class=""></o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class=""><o:p class=""> </o:p></span></div><div style="margin: 0cm 0cm 0.0001pt; font-size: 11pt; font-family: Calibri, sans-serif;" class=""><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">Any idea why the math functions are not elided?  I am using ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-O3</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ and implying ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-fno-math-errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’.  I have verified that our Toolchain implements ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">IsMathErrnoDefault</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ and returns ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">false</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’, and that the option ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">-fmath-errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’ is not being passed to the CC1 stage.  Since our math function implementation does not in fact change ‘</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Courier New';" class="">errno</span><span lang="EN-GB" style="font-size: 12pt; font-family: 'Book Antiqua', serif;" class="">’, it is very desirable that this elision occurs in an FP safe way.</span></div></div></div></blockquote><div><br class=""></div><div>You can check that clang when emitting the IR is adding the attribute "readnone” on the declaration of exp(). If it does not then it assumes errno.</div><div><br class=""></div><div>As a starting point, have you tried to pass -fno-math-errno?</div><div><br class=""></div><div>— </div><div>Mehdi</div><div><br class=""></div></div></body></html>