Skip to content

Commit 316bbe4

Browse files
aviateskFrancesco Fucci
authored andcommitted
mark fastmath operations as not :consistent (JuliaLang#46143)
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.
1 parent 6cc23e8 commit 316bbe4

File tree

2 files changed

+28
-7
lines changed

2 files changed

+28
-7
lines changed

base/compiler/tfuncs.jl

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,6 +1875,30 @@ const _ARGMEM_BUILTINS = Any[
18751875
swapfield!,
18761876
]
18771877

1878+
const _INCONSISTENT_INTRINSICS = Any[
1879+
Intrinsics.pointerref, # this one is volatile
1880+
Intrinsics.arraylen, # this one is volatile
1881+
Intrinsics.sqrt_llvm_fast, # this one may differ at runtime (by a few ulps)
1882+
Intrinsics.have_fma, # this one depends on the runtime environment
1883+
Intrinsics.cglobal, # cglobal lookup answer changes at runtime
1884+
# ... and list fastmath intrinsics:
1885+
# join(string.("Intrinsics.", sort(filter(endswith("_fast")∘string, names(Core.Intrinsics)))), ",\n")
1886+
Intrinsics.add_float_fast,
1887+
Intrinsics.div_float_fast,
1888+
Intrinsics.eq_float_fast,
1889+
Intrinsics.le_float_fast,
1890+
Intrinsics.lt_float_fast,
1891+
Intrinsics.mul_float_fast,
1892+
Intrinsics.ne_float_fast,
1893+
Intrinsics.neg_float_fast,
1894+
Intrinsics.rem_float_fast,
1895+
Intrinsics.sqrt_llvm_fast,
1896+
Intrinsics.sub_float_fast
1897+
# TODO needs to revive #31193 to mark this as inconsistent to be accurate
1898+
# while preserving the currently optimizations for many math operations
1899+
# Intrinsics.muladd_float, # this is not interprocedurally consistent
1900+
]
1901+
18781902
const _SPECIAL_BUILTINS = Any[
18791903
Core._apply_iterate,
18801904
]
@@ -2142,13 +2166,7 @@ function intrinsic_effects(f::IntrinsicFunction, argtypes::Vector{Any})
21422166
return Effects()
21432167
end
21442168

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

test/compiler/effects.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,9 @@ end |> !Core.Compiler.is_nothrow
301301
Core.svec(nothing, 1, "foo")
302302
end |> Core.Compiler.is_consistent
303303

304+
# fastmath operations are inconsistent
305+
@test !Core.Compiler.is_consistent(Base.infer_effects((a,b)->@fastmath(a+b), (Float64,Float64)))
306+
304307
# issue 46122: @assume_effects for @ccall
305308
@test Base.infer_effects((Vector{Int},)) do a
306309
Base.@assume_effects :effect_free @ccall jl_array_ptr(a::Any)::Ptr{Int}

0 commit comments

Comments
 (0)