Skip to content

Commit cc96240

Browse files
authored
Remove sqrt from the volatile list (#43786)
The LLVM IR spec now explicitly says that this intrinsic is required to be rounded correctly. That means that without fasthmath flag (which we do not set here), this intrinsic must have the bitwise correctly rounded answer and should thus not differ between compile and runtime. If there is still a case where it does differ, that is likely some other underlying bug that we should fix instead. Before: ``` julia> f() = sqrt(2) f (generic function with 1 method) julia> @code_typed f() CodeInfo( 1 ─ %1 = Base.Math.sqrt_llvm(2.0)::Float64 └── return %1 ) => Float64 ``` After: ``` julia> @code_typed f() CodeInfo( 1 ─ return 1.4142135623730951 ) => Float64 ```
1 parent 23b4743 commit cc96240

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

base/compiler/optimize.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -618,7 +618,6 @@ function is_pure_intrinsic_infer(f::IntrinsicFunction)
618618
f === Intrinsics.pointerset || # this one is never effect-free
619619
f === Intrinsics.llvmcall || # this one is never effect-free
620620
f === Intrinsics.arraylen || # this one is volatile
621-
f === Intrinsics.sqrt_llvm || # this one may differ at runtime (by a few ulps)
622621
f === Intrinsics.sqrt_llvm_fast || # this one may differ at runtime (by a few ulps)
623622
f === Intrinsics.have_fma || # this one depends on the runtime environment
624623
f === Intrinsics.cglobal) # cglobal lookup answer changes at runtime

test/compiler/inline.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -891,3 +891,7 @@ let
891891
ftest(a) = (fcond(a, nothing); a)
892892
@test fully_eliminated(ftest, Tuple{Bool})
893893
end
894+
895+
# sqrt not considered volatile
896+
f_sqrt() = sqrt(2)
897+
@test fully_eliminated(f_sqrt, Tuple{})

test/math.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,8 +1386,10 @@ end
13861386
# Runtime version
13871387
@test sqrt(x) === y
13881388
# Interpreter compile-time version
1389+
@test Base.invokelatest((@eval ()->sqrt(Base.inferencebarrier($x)))) == y
1390+
# Inference const-prop version
13891391
@test Base.invokelatest((@eval ()->sqrt($x))) == y
13901392
# LLVM constant folding version
1391-
@test Base.invokelatest((@eval ()->(@force_compile; sqrt($x)))) == y
1393+
@test Base.invokelatest((@eval ()->(@force_compile; sqrt(Base.inferencebarrier($x))))) == y
13921394
end
13931395
end

0 commit comments

Comments
 (0)