From 94f1071b6d18f70862bd7deeb456cae187444d67 Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki Date: Fri, 22 Jul 2022 14:33:43 -0400 Subject: [PATCH] mark fastmath operations as not `:consistent` We also need to mark `muladd` as not IPO-`:consistent, but it requires to revive #31193 to preserve the currently available optimizations so I left it as TODO for now. --- base/compiler/tfuncs.jl | 32 +++++++++++++++++++++++++------- test/compiler/effects.jl | 3 +++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/base/compiler/tfuncs.jl b/base/compiler/tfuncs.jl index caaa022a29c04..8e889bc395e1e 100644 --- a/base/compiler/tfuncs.jl +++ b/base/compiler/tfuncs.jl @@ -1795,6 +1795,30 @@ const _CONSISTENT_BUILTINS = Any[ throw ] +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 ] @@ -2017,13 +2041,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)) ? ALWAYS_TRUE : ALWAYS_FALSE diff --git a/test/compiler/effects.jl b/test/compiler/effects.jl index b0486968d473d..1106a00b9c054 100644 --- a/test/compiler/effects.jl +++ b/test/compiler/effects.jl @@ -186,6 +186,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}