<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:#1F497D;
        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:#1F497D;mso-fareast-language:EN-US'>I was compiling without ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>-std</span><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'>’ (default ANSI C89 I presume), but this still happens when I provide ‘</span><span style='font-family:"Courier New";color:black;mso-fareast-language:EN-US'>-std=c11</span><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'>’, so it does seem like something has broken.<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'>Guess it’s back to sleuthing through the change logs </span><span style='font-family:Wingdings;color:#1F497D;mso-fareast-language:EN-US'>J</span><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'><o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'>Thanks for your help,<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'><o:p> </o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;mso-fareast-language:EN-US'>            MartinO<o:p></o:p></span></p><p class=MsoNormal><span style='font-family:"Book Antiqua",serif;color:#1F497D;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'> Hubert Tong [mailto:hubert.reinterpretcast@gmail.com] <br><b>Sent:</b> 20 September 2016 14:54<br><b>To:</b> Martin J. O'Riordan <martin.oriordan@movidius.com><br><b>Cc:</b> mehdi.amini@apple.com; LLVM Developers <llvm-dev@lists.llvm.org><br><b>Subject:</b> Re: [llvm-dev] LLVM v3.9.0 and math built-ins<o:p></o:p></span></p><p class=MsoNormal><o:p> </o:p></p><div><div><div><p class=MsoNormal style='margin-bottom:12.0pt'>Hi Martin,<o:p></o:p></p></div><p class=MsoNormal>I believe Mehdi was referring to C11 subclause 7.12.1 paragraph 7:<o:p></o:p></p></div><div><p class=MsoNormal>If no such error occurs, <span style='font-family:"Courier New"'>errno</span> shall be left unmodified regardless of the setting of <span style='font-family:"Courier New"'>math_errhandling</span>.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><div><p class=MsoNormal>Thus we are left with a question as to why a call with the constant is needs to be preserved when <span style='font-family:"Courier New"'>errno</span> is specified not to change by C11.<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p></div><p class=MsoNormal>-- HT<o:p></o:p></p></div><div><p class=MsoNormal><o:p> </o:p></p><div><p class=MsoNormal>On Tue, Sep 20, 2016 at 7:12 AM, Martin J. O'Riordan via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p><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:#1F497D'>Hi Mehdi,</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:#1F497D'> </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:#1F497D'>The ISO C specification does permit the math functions to modify ‘</span><span style='font-family:"Courier New";color:black'>errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’, but I thought that the ‘</span><span style='font-family:"Courier New";color:black'>-fno-math-errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’  option was to tell the optimiser to assume that ‘</span><span style='font-family:"Courier New";color:black'>errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ is not modified by the math functions.  Explicitly providing ‘</span><span style='font-family:"Courier New";color:black'>-fno-math-errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ is not restoring the elision optimisation that was performed by LLVM v3.8, and this is really only a driver option, with ‘</span><span style='font-family:"Courier New";color:black'>-fmath-errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ passed onto CC1 if ‘</span><span style='font-family:"Courier New";color:black'>MathErrno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ is true, and omitted if false.</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:#1F497D'> </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:#1F497D'>I certainly agree that if ‘</span><span style='font-family:"Courier New";color:black'>MathErrno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ is true, then the compiler should not elide the call as it has to assume that there are memory write side-effects.  It already models the math functions as built-ins and determine the expected answer when the arguments are constants, and elides the tests in the simple example below - all this is good, and when I use ‘</span><span style='font-family:"Courier New";color:black'>-fno-builtin</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ it stops correctly disabled this optimisation.</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:#1F497D'> </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:#1F497D'>I modified my sources to force ‘</span><span style='font-family:"Courier New";color:black'>readnone</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ and even tried ‘</span><span style='font-family:"Courier New";color:black'>setDoesNotAccessMemory()</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ on the math functions, but this still results in one call occurring and the code generated is effectively:</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:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-family:"Courier New";color:black'>(void)exp(1.0);</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-family:"Courier New";color:black'>return 0;</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:#1F497D'> </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:#1F497D'>This is better, but still not as optimal as it was in v3.8.  With this change, compiling with ‘</span><span style='font-family:"Courier New";color:black'>-fmath-errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ reverts to inserting two calls.</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:#1F497D'> </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:#1F497D'>While calling these functions with constants is not terribly common in C code (mostly contrived test cases), it is a lot more common in real C++ code via template expansions and inlining.  With ‘</span><span style='font-family:"Courier New";color:black'>-O3 -S -emit-llvm -fno-math-errno</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ plus my change to call ‘</span><span style='font-family:"Courier New";color:black'>setDoesNotAccessMemory()</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’, the IR generated is as follows:</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:#1F497D'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; ModuleID = 'src/mathElisions.c'</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>source_filename = "src/mathElisions.c"</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>target datalayout = "e-m:e-p:32:32-f64:64-i64:64-v128:64-v64:64-v32:32-v16:16-n8:16:32-S64"</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>target triple = "shave"</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; Function Attrs: nounwind readnone</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>define i32 @useMathName() local_unnamed_addr #0 {</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %1 = tail call float @exp(float 1.000000e+00)</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  ret i32 0</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>}</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; Function Attrs: readnone</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>declare float @exp(float) local_unnamed_addr #1</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; Function Attrs: nounwind</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>define i32 @useOtherName() local_unnamed_addr #2 {</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %1 = tail call float @foo(float 1.000000e+00) #4</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %2 = fcmp olt float %1, 0x4005AE1480000000</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  br i1 %2, label %7, label %3</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; <label>:3:                                      ; preds = %0</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %4 = tail call float @foo(float 1.000000e+00) #4</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %5 = fcmp ogt float %4, 0x4005C28F60000000</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  br i1 %5, label %7, label %6</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; <label>:6:                                      ; preds = %3</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  br label %7</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>; <label>:7:                                      ; preds = %0, %3, %6</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  %8 = phi i32 [ 0, %6 ], [ -1, %3 ], [ -1, %0 ]</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>  ret i32 %8</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>}</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>declare float @foo(float) local_unnamed_addr #3</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'> </span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>attributes #0 = { nounwind readnone "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="myriad2.2" "unsafe-fp-math"="false" "use-soft-float"="false" }</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>attributes #1 = { readnone "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="myriad2.2" "unsafe-fp-math"="false" "use-soft-float"="false" }</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>attributes #2 = { nounwind "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="myriad2.2" "unsafe-fp-math"="false" "use-soft-float"="false" }</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>attributes #3 = { "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="true" "no-frame-pointer-elim-non-leaf" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="myriad2.2" "unsafe-fp-math"="false" "use-soft-float"="false" }</span><o:p></o:p></p><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto;margin-left:36.0pt'><span style='font-size:10.0pt;font-family:"Courier New";color:black'>attributes #4 = { nounwind }</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:#1F497D'> </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:#1F497D'>Our implementation has C ‘</span><span style='font-family:"Courier New";color:black'>float</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ and ‘</span><span style='font-family:"Courier New";color:black'>double</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ as 32-bit IEEE Single-Precision and ‘</span><span style='font-family:"Courier New";color:black'>long double</span><span style='font-family:"Book Antiqua",serif;color:#1F497D'>’ as 64-bit IEEE Double-Precision.</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:#1F497D'> </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:#1F497D'>Thanks,</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:#1F497D'> </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:#1F497D'>            MartinO</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:#1F497D'> </span><o:p></o:p></p><div><div style='border:none;border-top:solid #E1E1E1 1.0pt;padding:3.0pt 0cm 0cm 0cm'><p class=MsoNormal style='mso-margin-top-alt:auto;margin-bottom:12.0pt'><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'> <a href="mailto:mehdi.amini@apple.com" target="_blank">mehdi.amini@apple.com</a> [<a href="mailto:mehdi.amini@apple.com" target="_blank">mailto:mehdi.amini@apple.com</a>] <br><b>Sent:</b> 16 September 2016 21:16</span><o:p></o:p></p></div></div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>On Sep 16, 2016, at 1:30 AM, Martin J. O'Riordan via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<o:p></o:p></p></div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p><div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'>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:</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>#include <math.h></span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>extern double foo(double);</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>int useMathName() {</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>  if ((exp(1.0) < 2.71) || (exp(1.0) > 2.72))</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>    return -1;</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>  return 0;</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>}</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>int useOtherName() {</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>  if ((foo(1.0) < 2.71) || (foo(1.0) > 2.72))</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>    return -1;</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>  return 0;</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span style='font-family:"Courier New"'>}</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'>With v3.8.0 the compiler elided both the calls to ‘</span><span lang=EN-GB style='font-family:"Courier New"'>exp</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ and the tests, so the function ‘</span><span lang=EN-GB style='font-family:"Courier New"'>useMathName</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ reduced to simply:</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Courier New"'>return 0;</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'>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-family:"Courier New"'>errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’, 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-family:"Courier New"'>useMathName</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ becomes:</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Courier New"'>(void)exp(1.0);</span><o:p></o:p></p></div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Courier New"'>(void)exp(1.0);</span><o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>o 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?<o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Also, because we don’t really model errno, we don’t constant fold these call anymore :(<o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><blockquote style='margin-top:5.0pt;margin-bottom:5.0pt'><div><div style='margin-left:36.0pt'><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Courier New"'>return 0;</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'>So I changed our implementation so that ‘</span><span lang=EN-GB style='font-family:"Courier New"'>-fno-math-errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ is the default for the tool-chain, and the ‘</span><span lang=EN-GB style='font-family:"Courier New"'>-fmath-errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ 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-family:"Courier New"'>-ffast-math</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ as this has lots of FP issues, and I don’t want to make it the default.</span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'> </span><o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'><span lang=EN-GB style='font-family:"Book Antiqua",serif'>Any idea why the math functions are not elided?  I am using ‘</span><span lang=EN-GB style='font-family:"Courier New"'>-O3</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ and implying ‘</span><span lang=EN-GB style='font-family:"Courier New"'>-fno-math-errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’.  I have verified that our Toolchain implements ‘</span><span lang=EN-GB style='font-family:"Courier New"'>IsMathErrnoDefault</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ and returns ‘</span><span lang=EN-GB style='font-family:"Courier New"'>false</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’, and that the option ‘</span><span lang=EN-GB style='font-family:"Courier New"'>-fmath-errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’ 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-family:"Courier New"'>errno</span><span lang=EN-GB style='font-family:"Book Antiqua",serif'>’, it is very desirable that this elision occurs in an FP safe way.</span><o:p></o:p></p></div></div></blockquote><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>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.<o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>As a starting point, have you tried to pass -fno-math-errno?<o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>— <o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'>Mehdi<o:p></o:p></p></div><div><p class=MsoNormal style='mso-margin-top-alt:auto;mso-margin-bottom-alt:auto'> <o:p></o:p></p></div></div></div></div><p class=MsoNormal style='margin-bottom:12.0pt'><br>_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><o:p></o:p></p></blockquote></div><p class=MsoNormal><o:p> </o:p></p></div></div></body></html>