diff --git a/base/compiler/typeinfer.jl b/base/compiler/typeinfer.jl index 998a589c6905b..0516d9d912db8 100644 --- a/base/compiler/typeinfer.jl +++ b/base/compiler/typeinfer.jl @@ -904,15 +904,22 @@ end # compute an inferred AST and return type function typeinf_code(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool) + frame = typeinf_frame(interp, method, atype, sparams, run_optimizer) + frame === nothing && return nothing, Any + frame.inferred || return nothing, Any + return frame.src, widenconst(ignorelimited(result.result)) +end + +# compute an inferred frame +function typeinf_frame(interp::AbstractInterpreter, method::Method, @nospecialize(atype), sparams::SimpleVector, run_optimizer::Bool) mi = specialize_method(method, atype, sparams)::MethodInstance ccall(:jl_typeinf_begin, Cvoid, ()) result = InferenceResult(mi) frame = InferenceState(result, run_optimizer ? :global : :no, interp) - frame === nothing && return (nothing, Any) + frame === nothing && return nothing typeinf(interp, frame) ccall(:jl_typeinf_end, Cvoid, ()) - frame.inferred || return (nothing, Any) - return (frame.src, widenconst(ignorelimited(result.result))) + return frame end # compute (and cache) an inferred AST and return type diff --git a/base/compiler/types.jl b/base/compiler/types.jl index 0d8275ac0e7a0..d57477d3976a9 100644 --- a/base/compiler/types.jl +++ b/base/compiler/types.jl @@ -78,14 +78,20 @@ function Effects(e::Effects = EFFECTS_UNKNOWN; inbounds_taints_consistency) end +is_consistent(effects::Effects) = effects.consistent === ALWAYS_TRUE +is_effect_free(effects::Effects) = effects.effect_free === ALWAYS_TRUE +is_nothrow(effects::Effects) = effects.nothrow === ALWAYS_TRUE +is_terminating(effects::Effects) = effects.terminates === ALWAYS_TRUE +is_overlayed(effects::Effects) = effects.overlayed + is_total_or_error(effects::Effects) = - effects.consistent === ALWAYS_TRUE && - effects.effect_free === ALWAYS_TRUE && - effects.terminates === ALWAYS_TRUE + is_consistent(effects) && + is_effect_free(effects) && + is_terminating(effects) is_total(effects::Effects) = is_total_or_error(effects) && - effects.nothrow === ALWAYS_TRUE + is_nothrow(effects) is_removable_if_unused(effects::Effects) = effects.effect_free === ALWAYS_TRUE && diff --git a/base/reflection.jl b/base/reflection.jl index f114949ba6fd3..c80c4d9f258cb 100644 --- a/base/reflection.jl +++ b/base/reflection.jl @@ -1306,6 +1306,28 @@ function return_types(@nospecialize(f), @nospecialize(types=default_tt(f)); return rt end +function infer_effects(@nospecialize(f), @nospecialize(types=default_tt(f)); + world = get_world_counter(), + interp = Core.Compiler.NativeInterpreter(world)) + ccall(:jl_is_in_pure_context, Bool, ()) && error("code reflection cannot be used from generated functions") + types = to_tuple_type(types) + effects = Core.Compiler.EFFECTS_TOTAL + if isa(f, Core.Builtin) + args = Any[types.parameters...] + rt = Core.Compiler.builtin_tfunction(interp, f, args, nothing) + return Core.Compiler.builtin_effects(f, args, rt) + else + for match in _methods(f, types, -1, world)::Vector + match = match::Core.MethodMatch + frame = Core.Compiler.typeinf_frame(interp, + match.method, match.spec_types, match.sparams, #=run_optimizer=#false) + frame === nothing && return Core.Compiler.Effects() + effects = Core.Compiler.tristate_merge(effects, frame.ipo_effects) + end + return effects + end +end + """ print_statement_costs(io::IO, f, types) diff --git a/test/compiler/irpasses.jl b/test/compiler/irpasses.jl index 6c77891bede5a..48682b9af3b95 100644 --- a/test/compiler/irpasses.jl +++ b/test/compiler/irpasses.jl @@ -1036,8 +1036,9 @@ let ci = code_typed(foo_cfg_empty, Tuple{Bool}, optimize=true)[1][1] @test isa(ir.stmts[length(ir.stmts)][:inst], ReturnNode) end -@test Core.Compiler.builtin_effects(getfield, Any[Complex{Int}, Symbol], Any).effect_free.state == 0x01 -@test Core.Compiler.builtin_effects(getglobal, Any[Module, Symbol], Any).effect_free.state == 0x01 +@test Core.Compiler.is_effect_free(Base.infer_effects(getfield, (Complex{Int}, Symbol))) +@test Core.Compiler.is_effect_free(Base.infer_effects(getglobal, (Module, Symbol))) + # Test that UseRefIterator gets SROA'd inside of new_to_regular (#44557) # expression and new_to_regular offset are arbitrary here, we just want to see the UseRefIterator erased let e = Expr(:call, Core.GlobalRef(Base, :arrayset), false, Core.SSAValue(4), Core.SSAValue(9), Core.SSAValue(8)) diff --git a/test/reflection.jl b/test/reflection.jl index b1a5b6eb822a3..31057655fe141 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -964,3 +964,33 @@ end @eval m f4(a) = return @test Base.default_tt(m.f4) == Tuple end + +Base.@assume_effects :terminates_locally function issue41694(x::Int) + res = 1 + 1 < x < 20 || throw("bad") + while x > 1 + res *= x + x -= 1 + end + return res +end +may_be_effectsful(x::Int) = 42 +may_be_effectsful(x::Any) = unknown_operation() + +@testset "infer_effects" begin + @test Base.infer_effects(issue41694, (Int,)) |> Core.Compiler.is_terminating + @test Base.infer_effects((Int,)) do x + issue41694(x) + end |> Core.Compiler.is_terminating + @test Base.infer_effects(issue41694) |> Core.Compiler.is_terminating # use `default_tt` + let effects = Base.infer_effects(may_be_effectsful, (Any,)) # union split + @test !Core.Compiler.is_consistent(effects) + @test !Core.Compiler.is_effect_free(effects) + @test !Core.Compiler.is_nothrow(effects) + @test !Core.Compiler.is_terminating(effects) + @test Core.Compiler.is_overlayed(effects) + end + # builtins + @test Base.infer_effects(typeof, (Any,)) |> Core.Compiler.is_total + @test Base.infer_effects(===, (Any,Any)) |> Core.Compiler.is_total +end