[LLVMdev] Optimize away sqrt in simple cases?

Christoph Grenz christophg+llvm at grenz-bonn.de
Tue Apr 23 15:33:46 PDT 2013


Hello,

Am Dienstag, 23. April 2013, 13:26:19 schrieb Owen Anderson:
> That's a pretty seriously unsafe floating point optimization.  It could be
> done in fast-math mode, but I doubt we currently do it.

I just saw this thread and wonder why it's "seriously" unsafe? I see only one 
problematic corner case.

For x >= 0.0 the result cannot lose precision.
For x = NaN the result is the same. sqrt(NaN) = NaN; pow(NaN,2) = NaN
For x = +inf the result is the same. sqrt(inf) = inf; pow(inf,2) = inf
For negative numbers llvm.sqrt(x) by definition has undefined behavior and so 
returning x from llvm.pow(llvm.sqrt(x),2) is acceptable.
The same should be true for -inf.

Only the result for -0.0 changes from 0.0 to -0.0.

Is there something I overlooked?

> --Owen

Christoph
 
> On Apr 23, 2013, at 1:12 PM, Erkki Lindpere <villane at gmail.com> wrote:
> > hi!
> > 
> > I'm using LLVM 3.2. Does it optimize away llvm.pow(llvm.sqrt(x), 2) to `x`
> > with any settings? I tried with llc -O3, but that didn't do it.
> > 
> > Would be nice to write |v|² in my language ('v' being a 2D vector say and
> > |...| and ...² being two separate infix "operators") -- when I could
> > compare squares of lengths as well as lengths, and know that the sqrt is
> > optimized away.
> > 
> > Erkki
> > _______________________________________________
> > LLVM Developers mailing list
> > LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
> 
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev




More information about the llvm-dev mailing list