Skip to content

Commit

Permalink
make exp nothrow (JuliaLang#46238)
Browse files Browse the repository at this point in the history
This change allows our compiler to remove dead calls of these math ops.

Co-authored-by: Simeon Schaub <[email protected]>
Co-authored-by: Shuhei Kadowaki <[email protected]>
  • Loading branch information
3 people committed Aug 4, 2022
1 parent 480df09 commit bc4bb1b
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
8 changes: 3 additions & 5 deletions base/special/exp.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,12 +175,10 @@ const J_TABLE = (0x0000000000000000, 0xaac00b1afa5abcbe, 0x9b60163da9fb3335, 0xa
0xa66f0f9c1cb64129, 0x93af252b376bba97, 0xacdf3ac948dd7273, 0x99df50765b6e4540, 0x9faf6632798844f8,
0xa12f7bfdad9cbe13, 0xaeef91d802243c88, 0x874fa7c1819e90d8, 0xacdfbdba3692d513, 0x62efd3c22b8f71f1, 0x74afe9d96b2a23d9)

# XXX we want to mark :consistent-cy here so that this function can be concrete-folded,
# because the effect analysis currently can't prove it in the presence of `@inbounds` or
# `:boundscheck`, but still the access to `J_TABLE` is really safe here
Base.@assume_effects :consistent @inline function table_unpack(ind::Int32)
# :nothrow needed since the compiler can't prove `ind` is inbounds.
Base.@assume_effects :nothrow @inline function table_unpack(ind::Int32)
ind = ind & 255 + 1 # 255 == length(J_TABLE) - 1
j = @inbounds J_TABLE[ind]
j = getfield(J_TABLE, ind) # use getfield so the compiler can prove consistent
jU = reinterpret(Float64, JU_CONST | (j&JU_MASK))
jL = reinterpret(Float64, JL_CONST | (j>>8))
return jU, jL
Expand Down
5 changes: 4 additions & 1 deletion test/math.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,10 @@ for fn in (:sin, :cos, :tan, :log, :log2, :log10, :log1p, :exponent, :sqrt, :cbr
@test Core.Compiler.is_foldable(eff)
end
end
for T in (Float32, Float64)
for T in (Float16, Float32, Float64)
for f in (exp, exp2, exp10)
@test Core.Compiler.is_removable_if_unused(Base.infer_effects(f, (T,)))
end
@test Core.Compiler.is_foldable(Base.infer_effects(^, (T,Int)))
@test Core.Compiler.is_foldable(Base.infer_effects(^, (T,T)))
end

0 comments on commit bc4bb1b

Please sign in to comment.