Skip to content

Commit

Permalink
remove inferred field from InferenceState (#48915)
Browse files Browse the repository at this point in the history
  • Loading branch information
aviatesk committed Mar 7, 2023
1 parent 0f5f62c commit 608e26d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 26 deletions.
6 changes: 3 additions & 3 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1018,9 +1018,9 @@ function abstract_call_method_with_const_args(interp::AbstractInterpreter,
add_remark!(interp, sv, "[constprop] Fresh constant inference hit a cycle")
return nothing
end
@assert !isa(inf_result.result, InferenceState)
@assert inf_result.result !== nothing
else
if isa(inf_result.result, InferenceState)
if inf_result.result === nothing
add_remark!(interp, sv, "[constprop] Found cached constant inference in a cycle")
return nothing
end
Expand Down Expand Up @@ -2820,7 +2820,7 @@ end

# make as much progress on `frame` as possible (without handling cycles)
function typeinf_local(interp::AbstractInterpreter, frame::InferenceState)
@assert !frame.inferred
@assert !is_inferred(frame)
frame.dont_work_on_me = true # mark that this function is currently on the stack
W = frame.ip
nargs = narguments(frame, #=include_va=#false)
Expand Down
20 changes: 9 additions & 11 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ mutable struct InferenceState
callers_in_cycle::Vector{InferenceState}
dont_work_on_me::Bool
parent::Union{Nothing, InferenceState}
inferred::Bool # TODO move this to InferenceResult?

#= results =#
result::InferenceResult # remember where to put the result
Expand Down Expand Up @@ -165,7 +164,6 @@ mutable struct InferenceState
callers_in_cycle = Vector{InferenceState}()
dont_work_on_me = false
parent = nothing
inferred = false

valid_worlds = WorldRange(src.min_world, src.max_world == typemax(UInt) ? get_world_counter() : src.max_world)
bestguess = Bottom
Expand All @@ -180,23 +178,23 @@ mutable struct InferenceState
@assert cache === :no || cache === :local || cache === :global
cached = cache === :global

frame = new(
# some more setups
InferenceParams(interp).unoptimize_throw_blocks && mark_throw_blocks!(src, handler_at)
cache !== :no && push!(get_inference_cache(interp), result)

return new(
linfo, world, mod, sptypes, slottypes, src, cfg,
currbb, currpc, ip, handler_at, ssavalue_uses, bb_vartables, ssavaluetypes, stmt_edges, stmt_info,
pclimitations, limitations, cycle_backedges, callers_in_cycle, dont_work_on_me, parent, inferred,
pclimitations, limitations, cycle_backedges, callers_in_cycle, dont_work_on_me, parent,
result, valid_worlds, bestguess, ipo_effects,
restrict_abstract_call_sites, cached, insert_coverage,
interp)

# some more setups
InferenceParams(interp).unoptimize_throw_blocks && mark_throw_blocks!(src, handler_at)
result.result = frame
cache !== :no && push!(get_inference_cache(interp), result)

return frame
end
end

is_inferred(sv::InferenceState) = is_inferred(sv.result)
is_inferred(result::InferenceResult) = result.result !== nothing

Effects(state::InferenceState) = state.ipo_effects

function merge_effects!(::AbstractInterpreter, caller::InferenceState, effects::Effects)
Expand Down
12 changes: 5 additions & 7 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,6 @@ function _typeinf(interp::AbstractInterpreter, frame::InferenceState)
for caller in frames
caller.valid_worlds = valid_worlds
finish(caller, interp)
# finalize and record the linfo result
caller.inferred = true
end
# collect results for the new expanded frame
results = Tuple{InferenceResult, Vector{Any}, Bool}[
Expand Down Expand Up @@ -291,7 +289,7 @@ function CodeInstance(interp::AbstractInterpreter, result::InferenceResult,
@nospecialize(inferred_result), valid_worlds::WorldRange)
local const_flags::Int32
result_type = result.result
@assert !(result_type isa LimitedAccuracy)
@assert !(result_type === nothing || result_type isa LimitedAccuracy)

if isa(result_type, Const) && is_foldable_nothrow(result.ipo_effects) && is_inlineable_constant(result_type.val)
# use constant calling convention
Expand Down Expand Up @@ -919,7 +917,7 @@ function typeinf_edge(interp::AbstractInterpreter, method::Method, @nospecialize
end
typeinf(interp, frame)
update_valid_age!(frame, caller)
edge = frame.inferred ? mi : nothing
edge = is_inferred(frame) ? mi : nothing
return EdgeCallResult(frame.bestguess, edge, Effects(frame)) # effects are adjusted already within `finish`
elseif frame === true
# unresolvable cycle
Expand All @@ -937,7 +935,7 @@ end
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
is_inferred(frame) || return nothing, Any
code = frame.src
rt = widenconst(ignorelimited(frame.result.result))
return code, rt
Expand Down Expand Up @@ -1065,7 +1063,7 @@ function typeinf_type(interp::AbstractInterpreter, method::Method, @nospecialize
result = InferenceResult(mi, typeinf_lattice(interp))
typeinf(interp, result, :global)
ccall(:jl_typeinf_timing_end, Cvoid, ())
result.result isa InferenceState && return nothing
is_inferred(result) || return nothing
return widenconst(ignorelimited(result.result))
end

Expand All @@ -1084,7 +1082,7 @@ function typeinf_ext_toplevel(interp::AbstractInterpreter, linfo::MethodInstance
result = InferenceResult(linfo, typeinf_lattice(interp))
frame = InferenceState(result, src, #=cache=#:global, interp)
typeinf(interp, frame)
@assert frame.inferred # TODO: deal with this better
@assert is_inferred(frame) # TODO: deal with this better
src = frame.src
end
ccall(:jl_typeinf_timing_end, Cvoid, ())
Expand Down
10 changes: 5 additions & 5 deletions base/compiler/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,18 +58,18 @@ A type that represents the result of running type inference on a chunk of code.
See also [`matching_cache_argtypes`](@ref).
"""
mutable struct InferenceResult
linfo::MethodInstance
argtypes::Vector{Any}
overridden_by_const::BitVector
result # ::Type, or InferenceState if WIP
const linfo::MethodInstance
const argtypes::Vector{Any}
const overridden_by_const::BitVector
result # extended lattice element if inferred, nothing otherwise
src # ::Union{CodeInfo, IRCode, OptimizationState} if inferred copy is available, nothing otherwise
valid_worlds::WorldRange # if inference and optimization is finished
ipo_effects::Effects # if inference is finished
effects::Effects # if optimization is finished
argescapes # ::ArgEscapeCache if optimized, nothing otherwise
must_be_codeinf::Bool # if this must come out as CodeInfo or leaving it as IRCode is ok
function InferenceResult(linfo::MethodInstance, cache_argtypes::Vector{Any}, overridden_by_const::BitVector)
return new(linfo, cache_argtypes, overridden_by_const, Any, nothing,
return new(linfo, cache_argtypes, overridden_by_const, nothing, nothing,
WorldRange(), Effects(), Effects(), nothing, true)
end
end
Expand Down

0 comments on commit 608e26d

Please sign in to comment.