[PATCH] Make the sqrt intrinsic return undef for a negative input.

Sanjay Patel spatel at rotateright.com
Mon Sep 29 10:11:21 PDT 2014

Hi Jiangning, t.p.northover, hfinkel, jmolloy, resistor, wschmidt, willschm, joerg,

As discussed here:

And again here:

The sqrt of a negative number when using the llvm intrinsic is undefined. We should return undef rather than 0.0 to match the definition in the LLVM IR lang ref.

This change should not affect any code that isn't using "no-nans-fp-math"; ie, no-nans is a requirement for generating the llvm intrinsic in place of a sqrt function call.

Unfortunately, the behavior introduced by this patch will not match current gcc, xlc, icc, and possibly other compilers. The current clang/llvm behavior of returning 0.0 doesn't either. We knowingly approve of this difference with the other compilers in an attempt to flag code that is invoking undefined behavior. 

A front-end warning should also try to convince the user that the program will fail:


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D5527.14174.patch
Type: text/x-patch
Size: 1670 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20140929/80f18ad0/attachment.bin>

More information about the llvm-commits mailing list