[llvm] r277972 - [SimplifyLibCalls] Emit sqrt intrinsic instead of a libcall.

Hal Finkel via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 8 21:06:30 PDT 2016


Hi Davide,

I don't think this is correct. The problem is that, unlike most of our math intrinsics, our sqrt intrinsic has different semantics from the libc function. Specifically, our LangRef says:

The ‘llvm.sqrt‘ intrinsics return the sqrt of the specified operand, returning the same value as the libm ‘sqrt‘ functions would. Unlike sqrt in libm, however, llvm.sqrt has undefined behavior for negative numbers other than -0.0 (which allows for better optimization, because there is no need to worry about errno being set). llvm.sqrt(-0.0) is defined to return -0.0 like IEEE sqrt.

Now, under fast-math, we don't need to worry about -0.0, but we can't transform pow(x, 0.5) -> sqrt(x) using llvm.sqrt unless we know that x is non-negative. Otherwise, we've potentially added UB (where, before, we just had a NaN return value). The fast/nnan flags are not allowed to induce UB for NaNs, even if we don't define the resulting value. The LangRef says for nnan:

No NaNs - Allow optimizations to assume the arguments and result are not NaN. Such optimizations are required to retain defined behavior over NaNs, but the value of the result is undefined.

Thanks again,
Hal

----- Original Message -----
> From: "Davide Italiano via llvm-commits" <llvm-commits at lists.llvm.org>
> To: llvm-commits at lists.llvm.org
> Sent: Sunday, August 7, 2016 10:23:02 PM
> Subject: [llvm] r277972 - [SimplifyLibCalls] Emit sqrt intrinsic instead of a libcall.
> 
> Author: davide
> Date: Sun Aug  7 22:23:01 2016
> New Revision: 277972
> 
> URL: http://llvm.org/viewvc/llvm-project?rev=277972&view=rev
> Log:
> [SimplifyLibCalls] Emit sqrt intrinsic instead of a libcall.
> 
> Modified:
>     llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
>     llvm/trunk/test/Transforms/InstCombine/pow-sqrt.ll
> 
> Modified: llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp?rev=277972&r1=277971&r2=277972&view=diff
> ==============================================================================
> --- llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp (original)
> +++ llvm/trunk/lib/Transforms/Utils/SimplifyLibCalls.cpp Sun Aug  7
> 22:23:01 2016
> @@ -1052,8 +1052,9 @@ Value *LibCallSimplifier::optimizePow(Ca
>      if (CI->hasUnsafeAlgebra()) {
>        IRBuilder<>::FastMathFlagGuard Guard(B);
>        B.setFastMathFlags(CI->getFastMathFlags());
> -      return emitUnaryFloatFnCall(Op1, TLI->getName(LibFunc::sqrt),
> B,
> -                                  Callee->getAttributes());
> +      Value *Sqrt = Intrinsic::getDeclaration(CI->getModule(),
> Intrinsic::sqrt,
> +                                              Op1->getType());
> +      return B.CreateCall(Sqrt, Op1, "sqrt");
>      }
>  
>      // Expand pow(x, 0.5) to (x == -infinity ? +infinity :
>      fabs(sqrt(x))).
> 
> Modified: llvm/trunk/test/Transforms/InstCombine/pow-sqrt.ll
> URL:
> http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/pow-sqrt.ll?rev=277972&r1=277971&r2=277972&view=diff
> ==============================================================================
> --- llvm/trunk/test/Transforms/InstCombine/pow-sqrt.ll (original)
> +++ llvm/trunk/test/Transforms/InstCombine/pow-sqrt.ll Sun Aug  7
> 22:23:01 2016
> @@ -6,7 +6,7 @@ define double @pow_half(double %x) {
>  }
>  
>  ; CHECK-LABEL: define double @pow_half(
> -; CHECK-NEXT:  %sqrt = call fast double @sqrt(double %x)
> +; CHECK-NEXT:  %sqrt = call fast double @llvm.sqrt.f64(double %x)
>  ; CHECK-NEXT:  ret double %sqrt
>  
>  declare double @llvm.pow.f64(double, double)
> 
> 
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at lists.llvm.org
> http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-commits
> 

-- 
Hal Finkel
Assistant Computational Scientist
Leadership Computing Facility
Argonne National Laboratory


More information about the llvm-commits mailing list