<html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40"><head><meta http-equiv=Content-Type content="text/html; charset=utf-8"><meta name=Generator content="Microsoft Word 15 (filtered medium)"><style><!--
/* Font Definitions */
@font-face
        {font-family:Wingdings;
        panose-1:5 0 0 0 0 0 0 0 0 0;}
@font-face
        {font-family:"Cambria Math";
        panose-1:2 4 5 3 5 4 6 3 2 4;}
@font-face
        {font-family:Calibri;
        panose-1:2 15 5 2 2 2 4 3 2 4;}
@font-face
        {font-family:"Book Antiqua";
        panose-1:2 4 6 2 5 3 5 3 3 4;}
/* Style Definitions */
p.MsoNormal, li.MsoNormal, div.MsoNormal
        {margin:0cm;
        margin-bottom:.0001pt;
        font-size:12.0pt;
        font-family:"Times New Roman",serif;}
a:link, span.MsoHyperlink
        {mso-style-priority:99;
        color:blue;
        text-decoration:underline;}
a:visited, span.MsoHyperlinkFollowed
        {mso-style-priority:99;
        color:purple;
        text-decoration:underline;}
span.EmailStyle17
        {mso-style-type:personal-reply;
        font-family:"Book Antiqua",serif;
        color:#943634;
        font-weight:normal;
        font-style:normal;
        text-decoration:none none;}
.MsoChpDefault
        {mso-style-type:export-only;
        font-family:"Calibri",sans-serif;
        mso-fareast-language:EN-US;}
@page WordSection1
        {size:612.0pt 792.0pt;
        margin:72.0pt 72.0pt 72.0pt 72.0pt;}
div.WordSection1
        {page:WordSection1;}
--></style><!--[if gte mso 9]><xml>
<o:shapedefaults v:ext="edit" spidmax="1026" />
</xml><![endif]--><!--[if gte mso 9]><xml>
<o:shapelayout v:ext="edit">
<o:idmap v:ext="edit" data="1" />
</o:shapelayout></xml><![endif]--></head><body lang=EN-IE link=blue vlink=purple><div class=WordSection1><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>Thanks James and Hal,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>I hadn’t thought of ‘</span><span style='font-family:"Courier New",serif;color:black;mso-fareast-language:EN-US'>-fno-builtin</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ (blush), and as you say the “as if” rule is fine.  With ‘</span><span style='font-family:"Courier New",serif;color:black;mso-fareast-language:EN-US'>-fno-builtin</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ the behaviour is as I had expected.  And you are right, for testing the math functions in the library I need to use this.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><br>Regarding James’ points below:<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal style='margin-left:36.0pt'><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>Bullet 2, in v3.8.0 the call was being elided when ‘</span><span style='font-family:"Courier New",serif;color:black;mso-fareast-language:EN-US'>-fno-math-errno</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>’ was not specified, and this appears to have been changed in v3.9.0 (which I see just got tagged </span><span style='font-family:Wingdings;color:#943634;mso-fareast-language:EN-US'>J</span><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'> ) which does not elide the call.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>It was this change that drew my attention to the problem.  Sorry for putting this on CFE-Dev, I incorrectly thought that this was a Front-End optimisation issue.  If I have further thoughts on this I will start a new thread on LLVM-Dev.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>All the best,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'>            MartinO<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#943634;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><b><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri",sans-serif'>From:</span></b><span lang=EN-US style='font-size:11.0pt;font-family:"Calibri",sans-serif'> James Molloy [mailto:james@jamesmolloy.co.uk] <br><b>Sent:</b> 01 September 2016 09:48<br><b>To:</b> Martin.ORiordan@movidius.com; Hal Finkel <hfinkel@anl.gov><br><b>Cc:</b> Clang Dev <cfe-dev@lists.llvm.org><br><b>Subject:</b> Re: [cfe-dev] CLang and ISO C math functions<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>Hi Martin,<o:p></o:p></p><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>I'm having a little difficulty understanding this: <o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>> <span style='font-family:"Book Antiqua",serif'>I would expect this behaviour if ‘</span><span style='font-family:"Courier New",serif'>-ffast-math -fno-math-errno</span><span style='font-family:"Book Antiqua",serif'>’ was selected, but it isn’t, and I think that this is an invalid optimisation.</span><o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal><span style='font-family:"Book Antiqua",serif'>There are two optimizations going on:</span><o:p></o:p></p></div><div><p class=MsoNormal>  1. Constant folding of exp() results.<o:p></o:p></p></div><div><p class=MsoNormal>  2. Elision of exp() calls.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>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.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>(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).<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>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.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Cheers,<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>James<o:p></o:p></p></div></div><p class=MsoNormal><o:p> </o:p></p><div><div><p class=MsoNormal>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:<o:p></o:p></p></div><blockquote style='border:none;border-left:solid #CCCCCC 1.0pt;padding:0cm 0cm 0cm 6.0pt;margin-left:4.8pt;margin-right:0cm'><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'>Thanks Hal,</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;margin-bottom:12.0pt'><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</span><o:p></o:p></p></div></div><div><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='mso-margin-top-alt:auto;margin-bottom:12.0pt'><span style='font-family:"Book Antiqua",serif;color:black'>I would expect this behaviour if ‘</span><span style='font-family:"Courier New",serif;color:black'>-ffast-math -fno-math-errno</span><span style='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-family:"Courier New",serif;color:black'>double</span><span style='font-family:"Book Antiqua",serif;color:black'>’ is FP32,</span><o:p></o:p></p></blockquote><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><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.</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p></div></div></div><div><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'>No, I have:</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New",serif;color:black'>      DoubleFormat = &llvm::APFloat::IEEEsingle;</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'>set in my ‘</span><span style='font-family:"Courier New",serif;color:black'>TargetInfo</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’, and the IR shows ‘</span><span style='font-family:"Courier New",serif;color:black'>f32</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’.  But it is the elision of the tests when I am not using ‘</span><span style='font-family:"Courier New",serif;color:black'>-ffast-math</span><span style='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-family:"Courier New",serif;color:black'>-ffast-math</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ is used.  In the ‘</span><span style='font-family:"Courier New",serif;color:black'>foo</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ example the calls to ‘</span><span style='font-family:"Courier New",serif;color:black'>foo</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ are retained and the tests are present; it is only when I rename ‘</span><span style='font-family:"Courier New",serif;color:black'>foo</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ to ‘</span><span style='font-family:"Courier New",serif;color:black'>exp</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ or some other math function that this happens.</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'>With ‘</span><span style='font-family:"Courier New",serif;color:black'>-fno-math-errno</span><span style='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-family:"Courier New",serif;color:black'>-ffast-math</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ the reduction to the optimal ‘</span><span style='font-family:"Courier New",serif;color:black'>return 0</span><span style='font-family:"Book Antiqua",serif;color:#943634'>’ is perfect.  But neither of these options are selected.</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='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.</span><o:p></o:p></p></div></div></div><div><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></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 style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='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><o:p></o:p></p></blockquote><p class=MsoNormal style='mso-margin-top-alt:auto;margin-bottom:12.0pt'><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.</span><o:p></o:p></p></div></div></div><div><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='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?</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Book Antiqua",serif;color:#943634'>            MartinO</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='color:black'> </span><o:p></o:p></p></div></div></div><p class=MsoNormal>_______________________________________________<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" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-dev</a><o:p></o:p></p></blockquote></div></div></body></html>