<div dir="ltr">Hi, <div><br></div><div>I don't mean to hijack this thread but after reading through some discussion here and poking around in the constant folder myself just now, is my understanding correct in that LLVM only uses host routines for constant folding? Has there been discussion in the past about using MPFR or something like it instead? (I'm aware that doing so wouldn't solve the original problem in the general case -- I'm asking out of curiosity).</div><div><br></div><div>What happens currently when cross compiling and endianness does not match between host/target? What about folding types that are unsupported on the host (such as __float128)? Does LLVM reject the fold in those cases? </div><div><br></div><div>Regards,</div><div><br></div><div>Scott</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 17, 2019 at 10:31 AM Finkel, Hal J. via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">



<div bgcolor="#FFFFFF">
<p><br>
</p>
<div class="gmail-m_-3839374226708215863moz-cite-prefix">On 4/16/19 4:18 PM, Amara Emerson wrote:<br>
</div>
<blockquote type="cite">
<br>
<div><br>
<blockquote type="cite">
<div>On Apr 16, 2019, at 1:47 PM, Kaylor, Andrew via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div>
<br class="gmail-m_-3839374226708215863Apple-interchange-newline">
<div>
<div class="gmail-m_-3839374226708215863WordSection1" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Thanks, Hal.<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
I hear what you are saying about the accuracy. The problem, from my perspective, is trying to explain to users what they are going to get. The constant folding may be as accurate as the lib call would have been, but it isn’t necessarily value safe. I’ve been
 operating on the assumption that LLVM’s FP optimizations are value safe unless fast math flags are used. For the most part that appears to be true. This case breaks my assumption.<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
I realize that any call to a library function puts claims of value safety on shaky ground, but the standard I’m going for is that you’ll get the same bitwise results compiling at -O0 as you will at -O2 (for instance).<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
That said, I agree that the difference between constant folding a library call and substituting an approximate calculation is significant. Most users would probably prefer to have this optimization enabled by default. It just leads to a kind of murky answer
 to the question of whether or not we’re value safe by default for the users who do care about that.<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
I guess what I’m saying is that I do like the idea of a separate flag for this, though as I recall we’re running out of bits for fast math flags. I’m also not sure whether it should be on by default. If we want to permit this transformation by default, then
 it shouldn’t be a fast math flag. Probably an attribute on the call site is better? And in that case it feels like we’d be circling back toward “nobuiltin” but can the front end identify which call sites would need that?</div>
</div>
</div>
</blockquote>
Could this not be a function attribute if it’s intended to be consistent across entire functions/programs?</div>
<div><br>
</div>
<div>I agree with Hal that afn doesn’t sound like the right approach, and in terms of how the compiler actually treats these calls (I’m thinking about more than constant folding here) then it seems to be that this is the same as -fno-builtin. For example, can
 the optimizer assume some properties about the result value of a call if it knowns some (partial) information about the argument (e.g. sign)? If we prevent constant folding then that also precludes this kind of optimization. Perhaps an umbrella flag like -fno-builtin-math-lib
 that would turn on -fno-builtin for all of the libm functions?</div>
</blockquote>
<p><br>
</p>
<p>I certainly think that the umbrella flag is a good approach. The tricky part is implementing it so that Clang does not need to have a list of the relevant functions that LLVM's optimizer knows about (thus, I don't think that we can implement this just by
 having Clang add metadata to a predefined list of math functions).</p>
<p> -Hal<br>
</p>
<p><br>
</p>
<blockquote type="cite">
<div><br>
</div>
<div>Amara<br>
<blockquote type="cite">
<div>
<div class="gmail-m_-3839374226708215863WordSection1" style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
-Andy<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div>
<div style="border-style:solid none none;border-top-width:1pt;border-top-color:rgb(225,225,225);padding:3pt 0in 0in">
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<a name="m_-3839374226708215863______replyseparator"></a><b>From:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>Finkel, Hal J. <<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span><br>
<b>Sent:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>Tuesday, April 16, 2019 1:01 PM<br>
<b>To:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>>; Kaylor, Andrew <<a href="mailto:andrew.kaylor@intel.com" target="_blank">andrew.kaylor@intel.com</a>><br>
<b>Subject:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>Re: [FP] Constant folding math library functions<u></u><u></u></div>
</div>
</div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<u></u> <u></u></div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt">Hi, Andy,<u></u><u></u></span></div>
</div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt"><u></u> <u></u></span></div>
</div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt">This is somewhat tricky. 'afn' is for approximate functions, to "allow substitution of approximate calculations for functions", but in this case, the answers aren't any more approximate than the original function calls.
 Different, but likely no less accurate. This has long caused these kinds of subtle differences when cross compiling, etc. but it's not clear what the best thing to do actually is. Users often want the constant folding, and I've certainly seen code where the
 performance depends critically on it, and yet, the compiler will likely never be able to exactly replicate the behavior of whatever libm implementation is used at runtime. Maybe having a dedicated flag to disable just this behavior, aside from suggesting that
 users use -fno-builtin=..., would be useful for users who depend on the compiler not folding these kinds of expressions in ways that might differ from their runtime libm behavior?<u></u><u></u></span></div>
</div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt"><u></u> <u></u></span></div>
</div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt"> -Hal<u></u><u></u></span></div>
</div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt"><u></u> <u></u></span></div>
</div>
<div id="gmail-m_-3839374226708215863Signature">
<div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:10pt">Hal Finkel<br>
Lead, Compiler Technology and Programming Languages<br>
Leadership Computing Facility<br>
Argonne National Laboratory<u></u><u></u></span></div>
</div>
</div>
<div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<span style="font-size:12pt"><u></u> <u></u></span></div>
</div>
<div class="MsoNormal" style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif;text-align:center" align="center">
<hr width="98%" size="3" align="center">
</div>
<div id="gmail-m_-3839374226708215863divRplyFwdMsg">
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
<b><span>From:</span></b><span><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>llvm-dev <<a href="mailto:llvm-dev-bounces@lists.llvm.org" style="color:rgb(149,79,114);text-decoration:underline" target="_blank">llvm-dev-bounces@lists.llvm.org</a>>
 on behalf of Kaylor, Andrew via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" style="color:rgb(149,79,114);text-decoration:underline" target="_blank">llvm-dev@lists.llvm.org</a>><br>
<b>Sent:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>Tuesday, April 16, 2019 2:23 PM<br>
<b>To:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>llvm-dev<br>
<b>Subject:</b><span class="gmail-m_-3839374226708215863Apple-converted-space"> </span>[llvm-dev] [FP] Constant folding math library functions</span><u></u><u></u></div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
</div>
</div>
<div>
<div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Hi everyone,<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
I noticed today that LLVM’s constant folding of math library functions can lead to minor differences in results. A colleague sent me the following test case which demonstrates the issue:<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
#include <stdio.h><u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
#include <math.h><u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
typedef union {<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  double d;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  unsigned long long i;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
} my_dbl;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
int main(void) {<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  my_dbl res, x;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  x.i = 0x3feeb39556255de2ull;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  res.d = tanh(x.d);<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  printf("tanh(%f) = %f = %016LX\n", x.d, res.d, res.i);<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  return 0;<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
}<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Compiling with “clang -O2 -g0 -emit-llvm” I get this:<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
define dso_local i32 @main() local_unnamed_addr #0 {<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  %1 = tail call double @tanh(double 0x3FEEB39556255DE2) #2<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  %2 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0),<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
                                                             double 0x3FEEB39556255DE2, double 0x3FE7CF009CE7F169,<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
                                                             i64 4604876745549017449)<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
  ret i32 0<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
}<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
We’re still calling ‘tanh’ but all the values passed to printf are constant folded. The constant folding is based on a call to tanh made by the compiler. The problem with this is that if I am linking my program against a different version of the math library
 than was used by the compiler I may get a different result.<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
I can prevent this constant folding with either the ‘nobuiltin’ or ‘strictfp’ attribute. However, it seems to me like this optimization should really be checking the ‘afn’ fast math flag.<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Opinions?<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Thanks,<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
Andy<u></u><u></u></div>
<div style="margin:0in 0in 0.0001pt;font-size:11pt;font-family:Calibri,sans-serif">
 <u></u><u></u></div>
</div>
</div>
</div>
</div>
</div>
<span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">_______________________________________________</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline">LLVM
 Developers mailing list</span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline"><a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a></span><br style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none">
<span style="font-family:Helvetica;font-size:12px;font-style:normal;font-variant-caps:normal;font-weight:normal;letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;text-decoration:none;float:none;display:inline"><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a></span></div>
</blockquote>
</div>
<br>
</blockquote>
<pre class="gmail-m_-3839374226708215863moz-signature" cols="72">-- 
Hal Finkel
Lead, Compiler Technology and Programming Languages
Leadership Computing Facility
Argonne National Laboratory</pre>
</div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>