Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix return_type model when run in toplevel context #44670

Merged
merged 1 commit into from
Mar 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -638,6 +638,7 @@ function abstract_call_method(interp::AbstractInterpreter, method::Method, @nosp
if edge === nothing
edgecycle = edgelimited = true
end

# we look for the termination effect override here as well, since the :terminates effect
# may have been tainted due to recursion at this point even if it's overridden
if is_effect_overridden(sv, :terminates_globally)
Expand Down
6 changes: 5 additions & 1 deletion base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ mutable struct InferenceState
inferred::Bool
dont_work_on_me::Bool

# Whether to restrict inference of abstract call sites to avoid excessive work
# Set by default for toplevel frame.
restrict_abstract_call_sites::Bool

# Inferred purity flags
ipo_effects::Effects

Expand Down Expand Up @@ -193,7 +197,7 @@ mutable struct InferenceState
#=callers_in_cycle=#Vector{InferenceState}(),
#=parent=#nothing,
#=cached=#cache === :global,
#=inferred=#false, #=dont_work_on_me=#false,
#=inferred=#false, #=dont_work_on_me=#false, #=restrict_abstract_call_sites=# isa(linfo.def, Module),
#=ipo_effects=#Effects(consistent, ALWAYS_TRUE, ALWAYS_TRUE, ALWAYS_TRUE, false, inbounds_taints_consistency),
interp)
result.result = frame
Expand Down
6 changes: 6 additions & 0 deletions base/compiler/tfuncs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2030,7 +2030,13 @@ function return_type_tfunc(interp::AbstractInterpreter, argtypes::Vector{Any}, s
if contains_is(argtypes_vec, Union{})
return CallMeta(Const(Union{}), false)
end
# Run the abstract_call without restricting abstract call
# sites. Otherwise, our behavior model of abstract_call
# below will be wrong.
old_restrict = sv.restrict_abstract_call_sites
sv.restrict_abstract_call_sites = false
call = abstract_call(interp, ArgInfo(nothing, argtypes_vec), sv, -1)
sv.restrict_abstract_call_sites = old_restrict
info = verbose_stmt_info(interp) ? ReturnTypeCallInfo(call.info) : false
rt = widenconditional(call.rt)
if isa(rt, Const)
Expand Down
2 changes: 1 addition & 1 deletion base/compiler/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ It also bails out from local statement/frame inference when any lattice element
but `AbstractInterpreter` doesn't provide a specific interface for configuring it.
"""
bail_out_toplevel_call(::AbstractInterpreter, @nospecialize(callsig), sv#=::InferenceState=#) =
return isa(sv.linfo.def, Module) && !isdispatchtuple(callsig)
return sv.restrict_abstract_call_sites && !isdispatchtuple(callsig)
bail_out_call(::AbstractInterpreter, @nospecialize(rt), sv#=::InferenceState=#) =
return rt === Any
bail_out_apply(::AbstractInterpreter, @nospecialize(rt), sv#=::InferenceState=#) =
Expand Down
7 changes: 7 additions & 0 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4066,3 +4066,10 @@ let bsbmp = Core.Compiler.BitSetBoundedMinPrioritySet(5)
@test Core.Compiler.popfirst!(bsbmp) == 1
@test Core.Compiler.isempty(bsbmp)
end

# Make sure return_type_tfunc doesn't accidentally cause bad inference if used
# at top level.
@test let
Base.Experimental.@force_compile
Core.Compiler.return_type(+, NTuple{2, Rational})
end == Rational