<div dir="ltr"><div><div>Yeah, it's a hack. I was just hoping to let everyone walk away a winner. :)<br><br></div>One more question then:<br></div>Why return zero instead of undef?<br><br> if (V >= -0.0)<br> return ConstantFoldFP(sqrt, V, Ty);<br> else // Undefined<br> return Constant::getNullValue(Ty);<br><br><div><div>Surely returning UndefValue::get(Ty) would allow greater havoc to ensue? Maybe even the segfault that you suggested. :)<br><br></div><div>I don't think clang emits any warnings for the bogus C code...that would be the nicer thing to do.<br></div></div></div><div class="gmail_extra"><br><div class="gmail_quote">On Fri, Sep 26, 2014 at 1:20 PM, Tim Northover <span dir="ltr"><<a href="mailto:t.p.northover@gmail.com" target="_blank">t.p.northover@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><span class="">On 26 September 2014 12:03, Sanjay Patel <<a href="mailto:spatel@rotateright.com">spatel@rotateright.com</a>> wrote:<br>
> This isn't purely a fast-math issue...ConstantFolding isn't using<br>
> enable-unsafe-fp-math to decide whether to emit the '0'. It's just looking<br>
> for the llvm intrinsic rather than a call to sqrt:<br>
<br>
</span>Yep. As Hal said, the key option is -ffinite-math, which allows the<br>
front-end to emit that intrinsic in the first place. We document<br>
@llvm.sqrt's behaviour and it has no requirements to check<br>
unsafe-fp-math before folding.<br>
<span class=""><br>
> So how about a front-end fix:<br>
> If the parameter is a negative constant, instead of converting the sqrt call<br>
> into the intrinsic, just leave it as-is regardless of fast-math.<br>
<br>
</span>That sounds like a nasty hack to me. It covers only a very limited set<br>
of situations where this would happen. For example, almost certainly<br>
not:<br>
<br>
double foo() {<br>
const double x = -1.0;<br>
return sqrt(x);<br>
}<br>
<br>
Possibly not even (depending on how you define "constant"):<br>
<br>
double foo() { return sqrt(-1.0f); }<br>
<br>
If we're going to change it I'd prefer the active decision in the mid-end.<br>
<br>
Cheers.<br>
<span class="HOEnZb"><font color="#888888"><br>
Tim.<br>
</font></span></blockquote></div><br></div>