Skip to content

Commit

Permalink
mark fastmath operations as not :consistent (JuliaLang#46143)
Browse files Browse the repository at this point in the history
We also need to mark `muladd` as not IPO-`:consistent, but it requires
to revive JuliaLang#31193 to preserve the currently available optimizations so
I left it as TODO for now.
  • Loading branch information
aviatesk committed Aug 5, 2022
1 parent de375ef commit e2bddbf
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 7 deletions.
32 changes: 25 additions & 7 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1875,6 +1875,30 @@ const _ARGMEM_BUILTINS = Any[
swapfield!,
]

const _INCONSISTENT_INTRINSICS = Any[
Intrinsics.pointerref, # this one is volatile
Intrinsics.arraylen, # this one is volatile
Intrinsics.sqrt_llvm_fast, # this one may differ at runtime (by a few ulps)
Intrinsics.have_fma, # this one depends on the runtime environment
Intrinsics.cglobal, # cglobal lookup answer changes at runtime
# ... and list fastmath intrinsics:
# join(string.("Intrinsics.", sort(filter(endswith("_fast")∘string, names(Core.Intrinsics)))), ",\n")
Intrinsics.add_float_fast,
Intrinsics.div_float_fast,
Intrinsics.eq_float_fast,
Intrinsics.le_float_fast,
Intrinsics.lt_float_fast,
Intrinsics.mul_float_fast,
Intrinsics.ne_float_fast,
Intrinsics.neg_float_fast,
Intrinsics.rem_float_fast,
Intrinsics.sqrt_llvm_fast,
Intrinsics.sub_float_fast
# TODO needs to revive #31193 to mark this as inconsistent to be accurate
# while preserving the currently optimizations for many math operations
# Intrinsics.muladd_float, # this is not interprocedurally consistent
]

const _SPECIAL_BUILTINS = Any[
Core._apply_iterate,
]
Expand Down Expand Up @@ -2142,13 +2166,7 @@ function intrinsic_effects(f::IntrinsicFunction, argtypes::Vector{Any})
return Effects()
end

consistent = !(
f === Intrinsics.pointerref || # this one is volatile
f === Intrinsics.arraylen || # this one is volatile
f === Intrinsics.sqrt_llvm_fast || # this one may differ at runtime (by a few ulps)
f === Intrinsics.have_fma || # this one depends on the runtime environment
f === Intrinsics.cglobal # cglobal lookup answer changes at runtime
) ? ALWAYS_TRUE : ALWAYS_FALSE
consistent = contains_is(_INCONSISTENT_INTRINSICS, f) ? ALWAYS_FALSE : ALWAYS_TRUE
effect_free = !(f === Intrinsics.pointerset) ? ALWAYS_TRUE : ALWAYS_FALSE
nothrow = (!(!isempty(argtypes) && isvarargtype(argtypes[end])) && intrinsic_nothrow(f, argtypes))

Expand Down
3 changes: 3 additions & 0 deletions test/compiler/effects.jl
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,9 @@ end |> !Core.Compiler.is_nothrow
Core.svec(nothing, 1, "foo")
end |> Core.Compiler.is_consistent

# fastmath operations are inconsistent
@test !Core.Compiler.is_consistent(Base.infer_effects((a,b)->@fastmath(a+b), (Float64,Float64)))

# issue 46122: @assume_effects for @ccall
@test Base.infer_effects((Vector{Int},)) do a
Base.@assume_effects :effect_free @ccall jl_array_ptr(a::Any)::Ptr{Int}
Expand Down

0 comments on commit e2bddbf

Please sign in to comment.