[LLVMdev] Optimization of sqrt() with invalid argument

Tim Northover t.p.northover at gmail.com
Fri Sep 26 12:20:22 PDT 2014


On 26 September 2014 12:03, Sanjay Patel <spatel at rotateright.com> wrote:
> This isn't purely a fast-math issue...ConstantFolding isn't using
> enable-unsafe-fp-math to decide whether to emit the '0'. It's just looking
> for the llvm intrinsic rather than a call to sqrt:

Yep. As Hal said, the key option is -ffinite-math, which allows the
front-end to emit that intrinsic in the first place. We document
@llvm.sqrt's behaviour and it has no requirements to check
unsafe-fp-math before folding.

> So how about a front-end fix:
> If the parameter is a negative constant, instead of converting the sqrt call
> into the intrinsic, just leave it as-is regardless of fast-math.

That sounds like a nasty hack to me. It covers only a very limited set
of situations where this would happen. For example, almost certainly
not:

double foo() {
  const double x = -1.0;
  return sqrt(x);
}

Possibly not even (depending on how you define "constant"):

    double foo() { return sqrt(-1.0f); }

If we're going to change it I'd prefer the active decision in the mid-end.

Cheers.

Tim.



More information about the llvm-dev mailing list