<div dir="ltr">On Thu, Sep 12, 2013 at 3:55 PM, Hal Finkel <span dir="ltr"><<a href="mailto:hfinkel@anl.gov" target="_blank">hfinkel@anl.gov</a>></span> wrote:<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="HOEnZb"><div class="h5">----- Original Message -----<br>
><br>
> On Thu, Sep 12, 2013 at 2:44 PM, Hal Finkel < <a href="mailto:hfinkel@anl.gov">hfinkel@anl.gov</a> ><br>
> wrote:<br>
><br>
><br>
><br>
><br>
><br>
> ----- Original Message -----<br>
> ><br>
> > On Thu, Sep 12, 2013 at 2:03 PM, Hal Finkel < <a href="mailto:hfinkel@anl.gov">hfinkel@anl.gov</a> ><br>
> > wrote:<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > Hello,<br>
> ><br>
> > Please review the attached patch which restores the libm sqrt* -><br>
> > @llvm.sqrt* mapping, but only in fast-math mode (specifically, when<br>
> > the UnsafeFPMath or NoNaNsFPMath CodeGen options are enabled). The<br>
> > @llvm.sqrt* intrinsics have slightly different semantics from the<br>
> > libm call, specifically, they are undefined when given a non-zero<br>
> > negative number (the libm calls will always return NaN for any<br>
> > negative number).<br>
> ><br>
> > This mapping was removed in r100613, and replaced with a TODO, but<br>
> > at<br>
> > that time the fast-math flags were not yet implemented. Now that we<br>
> > have these, restoring this mapping is important because it will<br>
> > enable autovectorization of sqrt calls in loops (at least in<br>
> > fast-math mode).<br>
> ><br>
> ><br>
> ><br>
> ><br>
> > This is dangerous, if LangRef is actually correct. People don't<br>
> > associate -ffast-math with "my program will crash at random". :) Of<br>
> > course, LangRef is probably overstating the issue.<br>
><br>
> I agree, and the LangRef does indeed say "undefined behavior", but I<br>
> assume that should really mean, "returns an undefined value." Do you<br>
> agree?<br>
><br>
><br>
><br>
> Well, if we map llvm.sqrt to sqrt and sqrt sets errno, we really do<br>
> mean "undefined behavior"... or at least something more that<br>
> "returns an undefined value".<br>
<br>
</div></div>Agreed, but this possibility-of-setting-errno problem exists for all LLVM libm-style intrinsics, and so also exists for pow() [for which we currently do this exact kind of replacement whenever -fmath-errno=0]. So, FWIW, this is not without precedent.<br>
<div class="im"><br>
><br>
><br>
><br>
><br>
> ><br>
> ><br>
> > That said, there's actually a general issue here: if we map the<br>
> > LLVM<br>
> > intrinsics to libc functions, and the libc functions set errno, we<br>
> > could break code that depends on errno for non-math calls (e.g.<br>
> > fopen().)<br>
><br>
> Perhaps, but I'm not changing that here. For one thing, if the<br>
> mapping does, in effect, sqrt -> llvm.sqrt -> sqrt, and only when<br>
> -fmath-errno=0. Are you worried about cases where the libm functions<br>
> actually do set errno (even though we have -fmath-errno=0)?<br>
><br>
><br>
><br>
><br>
> I'm not sure our implementation of -fno-math-errno is safe: according<br>
> to the gcc manual, it isn't equivalent to marking the math functions<br>
> with attribute((const)). (For example, the gcc manual's definition<br>
> allows transforming a call to sqrt() into the SSE sqrt instruction,<br>
> but it doesn't allow hoisting a call to sqrt out of arbitrary loops<br>
> on a machine where the sqrt() call could set errno.)<br>
<br>
</div>I'm sure it is not safe, for the very reason that you highlight. Nevertheless, this is a long-standing problem, affecting the implementation of -fmath-errno=0 on all systems for which libm math functions actually do set errno, and will require a general solution (fairly orthogonal to this patch).<br>
<br>
I recommend that we:<br>
<br>
1. Commit this change (so that we can autovectorize calls to sqrt()).<br>
2. Have a discussion about how to actually solve this problem: I think that it involves making a specific function attribute for setting errno, and teaching the alias analysis infrastructure to do something sensible with it.<br>
<div class="HOEnZb"><div class="h5"></div></div></blockquote></div><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Okay.</div><div class="gmail_extra"><br></div><div class="gmail_extra">-Eli</div></div>