<div dir="ltr">Hi,<br><div class="gmail_extra"><br><div class="gmail_quote">On Mon, Nov 7, 2016 at 3:09 AM, Octav Zlatior via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div><br></div><div>Hi,</div><div>Those were all very compelling arguments indeed, but I still think it's something we could fix. While other compilers such as gcc are not necessarily an authority on the issue, I think we should avoid inconsistencies.</div><div><br></div><div>For example, the fact that sqrt(-2) is undefined, but log(-2) seems to nicely turn into NaN eventually. That feels rather broken to me.</div><div><br></div></div></blockquote><div><br></div><div>I agree. This may be a missed optimization - we may want to fold log(-2) to undef as well.</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div>Or the fact that when building with -ffast-math or -Ofast, the behaviour of the code is changes completely (like in the xalanc benchmark, where it jumps into an endless loop because of it and eventually segfaults due to a stack overflow).</div><div>The point being that this will introduce a lot of weird side effects in peoples binaries, and I'd rather fix it now in one place in the compiler than have everybody fix it in their (or in 3rd party) code, which most likely is not going to happen and people will happily blame it on the compiler or the way optimisation passes are implemented.</div><div><br></div></div></blockquote><div><br></div><div>If you pass -ffinite-math-only (which -ffast-math implies) and your code relies on correctly handling NaNs, then, yes, your code will break, in potentially "weird" ways. Fixing this in your (or in 3rd party) code sounds like the right thing to me. This is, I think, different from the standard "how badly should we break undefined behavior" arguments, since we're talking about the user explicitly asking the compiler to make specific assumptions.</div><div><br></div><div>It's true that GCC handles this differently. I'm not a GCC developer, and can't speak for them, but I wouldn't be surprised if the only reason for that is to avoid breaking SPEC. I think our approach to this has been consistently different. </div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div></div><div>I can try to change it into a more consistent fix, by either:</div><div>* removing the transform completely, as suggested by Eli, and leave the rest to the optimiser</div><div>* changing the undef to NaN (not very sure what the implications of that would be of even if it is possible, but it seems to be a sensible solution)</div><div>* trying to extend the current fix/workaround to cover the cases when the argument is not a constant, but this seems to be a little more complicated</div><div><br></div><div>I'll pick one later today, but I am open to suggestions :)</div><div><br></div><div><div>@Eli: Good catch! I've noticed this as well, on the other hand from my observations this would only happen if the value of the argument is known (for example if it is specifically assigned in the same scope). It seems to work fine if the argument is calculated somewhere else and passed on, which is probably the typical usecase.</div><div>I could remove that transform completely, that was actually my first workaround to the issue.</div><div>Thanks!</div></div><div><div class="h5"><br><div><blockquote type="cite"><div>On 07 Nov 2016, at 08:51, Michael Kuperstein via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:</div><br class="m_-1268518224679242024Apple-interchange-newline"><div><div dir="ltr">This has been raised several times (xalanc failing with -ffast-math isn't new), and I believe the last time around we've explicitly decided to keep this as undef.<div><br></div><div>A few relevant threads:<br><div><br></div><div><a href="http://lists.llvm.org/pipermail/llvm-commits/Week-of-Mon-20140609/220450.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/llvm-commits/Week-<wbr>of-Mon-20140609/220450.html</a><br></div></div><div><a href="http://lists.llvm.org/pipermail/llvm-dev/2014-September/077137.html" target="_blank">http://lists.llvm.org/<wbr>pipermail/llvm-dev/2014-<wbr>September/077137.html</a><br></div><div><a href="http://llvm.org/bugs/show_bug.cgi?id=21048" target="_blank">http://llvm.org/bugs/show_bug.<wbr>cgi?id=21048</a><br></div><div><br></div><div>Thanks,</div><div> Michael</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 6 November 2016 at 23:31, Vedant Kumar via llvm-commits <span dir="ltr"><<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">The optimizer doesn't handle this right now, although it probably could since<br>
-instsimplify runs pretty late in the pipeline.<br>
<br>
Do you think extending the LibCallSimplifier is the right approach?<br>
<br>
best,<br>
vedant<br>
<br>
P.S: w.r.t the problem that motivated this patch, I think a better solution<br>
would be to go through the test programs and change their uses of sqrt().<br>
<div class="m_-1268518224679242024HOEnZb"><div class="m_-1268518224679242024h5"><br>
<br>
> On Nov 6, 2016, at 9:31 AM, Friedman, Eli via llvm-commits <<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a>> wrote:<br>
><br>
> On 11/4/2016 11:15 AM, Octav Zlatior via llvm-commits wrote:<br>
>><br>
>> Hi,<br>
>><br>
>> I have noticed the following behaviour while running the SPEC benchmark suite (built with llvm, of course). One of the tests was failing (Xalanc) with a segfault (because of stack overflow) because the terminating condition for a recursion involved NaN.<br>
>><br>
>> I've checked a little further and I have noticed that, with fast-math, the square roots of negative numbers will sometimes be replaced with "return undef". But only sometimes, and only the square roots (for example, log(x) does not manifest this behaviour).<br>
>><br>
>> It turns out a rather common way of getting the value of NaN in code is to assign it the return value of sqrt(<negative number>). Because of this bug, any such assignments will basically result undefined values.<br>
>><br>
>> These two patches should provide a fix for this. The first is a test to illustrate the situation (the negative functions will be replaced with return undef in the IR), while the other is a fix for this bug.<br>
>><br>
><br>
> Your patch specifically says that "sqrt(-1)" should be lowered to NaN, but "float x = -1.f; sqrt(x)" should be lowered to the intrinsic, which has undefined behavior according to LangRef. This seems dubious; changing the semantics of sqrt() based on the syntactic form of the source is likely to cause problems. For example, writing a trivial wrapper for sqrt (like the one in libcxx for sqrtf) breaks your check.<br>
><br>
> Maybe we should just kill off this transform completely? The optimizer knows how to handle a regular sqrt() call anyway.<br>
><br>
> -Eli<br>
><br>
> --<br>
> Employee of Qualcomm Innovation Center, Inc.<br>
> Qualcomm Innovation Center, Inc. is a member of Code Aurora Forum, a Linux Foundation Collaborative Project<br>
><br>
> ______________________________<wbr>_________________<br>
> llvm-commits mailing list<br>
> <a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
> <a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
<br>
______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
</div></div></blockquote></div><br></div>
______________________________<wbr>_________________<br>llvm-commits mailing list<br><a href="mailto:llvm-commits@lists.llvm.org" target="_blank">llvm-commits@lists.llvm.org</a><br><a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br></div></blockquote></div><br></div></div><div>
<div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>— </div><div>Octav Zlatior</div><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div style="color:rgb(0,0,0);letter-spacing:normal;text-align:start;text-indent:0px;text-transform:none;white-space:normal;word-spacing:0px;word-wrap:break-word"><div>Theobroma Systems Design und Consulting GmbH<br>Seestadtstrasse 27, 1220 Wien, Austria<br>Phone: +43 1 2369893-407<br><a href="http://www.theobroma-systems.com" target="_blank">http://www.theobroma-systems.<wbr>com</a></div><div><br></div></div><br class="m_-1268518224679242024Apple-interchange-newline"></div><br class="m_-1268518224679242024Apple-interchange-newline"></div><br class="m_-1268518224679242024Apple-interchange-newline"><br class="m_-1268518224679242024Apple-interchange-newline">
</div>
<br></div><br>______________________________<wbr>_________________<br>
llvm-commits mailing list<br>
<a href="mailto:llvm-commits@lists.llvm.org">llvm-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/<wbr>mailman/listinfo/llvm-commits</a><br>
<br></blockquote></div><br></div></div>