Skip to content
Permalink
Browse files

internals: better representation for code (#31191)

This'll help solve many of the problems with edges getting
mis-represented and broken by adding an extra level of indirection between
MethodInstance (now really just representing a particular specialization of a
method) and the executable object, now called Lambda (representing some
functional operator that converts some input arguments to some output
values, with whatever metadata is convenient to contain there).
This fixes many of the previous representation problems with back-edges,
since a MethodInstance (like Method) no longer tries to also represent a
computation. That task is now relegated strictly to Lambda.
  • Loading branch information...
vtjnash committed Mar 29, 2019
1 parent 4b12302 commit 8c445663547791e59f33f9e8d49b276332107614
@@ -65,6 +65,9 @@
#mutable struct MethodInstance
#end

#mutable struct CodeInstance
#end

#mutable struct CodeInfo
#end

@@ -442,11 +445,11 @@ Symbol(s::Symbol) = s

# module providing the IR object model
module IR
export CodeInfo, MethodInstance, GotoNode,
export CodeInfo, MethodInstance, CodeInstance, GotoNode,
NewvarNode, SSAValue, Slot, SlotNumber, TypedSlot,
PiNode, PhiNode, PhiCNode, UpsilonNode, LineInfoNode

import Core: CodeInfo, MethodInstance, GotoNode,
import Core: CodeInfo, MethodInstance, CodeInstance, GotoNode,
NewvarNode, SSAValue, Slot, SlotNumber, TypedSlot,
PiNode, PhiNode, PhiCNode, UpsilonNode, LineInfoNode

@@ -89,7 +89,8 @@ function abstract_call_gf_by_type(@nospecialize(f), argtypes::Vector{Any}, @nosp
this_rt === Any && break
end
else
this_rt, edgecycle, edge = abstract_call_method(method, sig, match[2]::SimpleVector, sv)
this_rt, edgecycle1, edge = abstract_call_method(method, sig, match[2]::SimpleVector, sv)
edgecycle |= edgecycle1::Bool
if edge !== nothing
push!(edges, edge)
end
@@ -170,53 +171,58 @@ function abstract_call_method_with_const_args(@nospecialize(rettype), @nospecial
method.isva && (nargs -= 1)
length(argtypes) >= nargs || return Any
haveconst = false
allconst = true
# see if any or all of the arguments are constant and propagating constants may be worthwhile
for a in argtypes
a = widenconditional(a)
if has_nontrivial_const_info(a)
haveconst = const_prop_profitable(a)
haveconst && break
if allconst && !isa(a, Const) && !isconstType(a) && !isa(a, PartialStruct)
allconst = false
end
if !haveconst && has_nontrivial_const_info(a) && const_prop_profitable(a)
haveconst = true
end
if haveconst && !allconst
break
end
end
haveconst || improvable_via_constant_propagation(rettype) || return Any
sig = match[1]
sparams = match[2]::SimpleVector
code = code_for_method(method, sig, sparams, sv.params.world)
code === nothing && return Any
code = code::MethodInstance
# decide if it's likely to be worthwhile
declared_inline = isdefined(method, :source) && ccall(:jl_ast_flag_inlineable, Bool, (Any,), method.source)
cache_inlineable = declared_inline
if isdefined(code, :inferred) && !cache_inlineable
cache_inf = code.inferred
if !(cache_inf === nothing)
cache_src_inferred = ccall(:jl_ast_flag_inferred, Bool, (Any,), cache_inf)
cache_src_inlineable = ccall(:jl_ast_flag_inlineable, Bool, (Any,), cache_inf)
cache_inlineable = cache_src_inferred && cache_src_inlineable
end
force_inference = allconst || sv.params.aggressive_constant_propagation
if istopfunction(f, :getproperty) || istopfunction(f, :setproperty!)
force_inference = true
end
if !cache_inlineable && !sv.params.aggressive_constant_propagation
tm = _topmod(sv)
if !istopfunction(f, :getproperty) && !istopfunction(f, :setproperty!)
# in this case, see if all of the arguments are constants
for a in argtypes
a = widenconditional(a)
if !isa(a, Const) && !isconstType(a) && !isa(a, PartialStruct)
return Any
end
mi = specialize_method(method, sig, sparams, !force_inference)
mi === nothing && return Any
mi = mi::MethodInstance
# decide if it's likely to be worthwhile
if !force_inference
code = inf_for_methodinstance(mi, sv.params.world)
declared_inline = isdefined(method, :source) && ccall(:jl_ast_flag_inlineable, Bool, (Any,), method.source)
cache_inlineable = declared_inline
if isdefined(code, :inferred) && !cache_inlineable
cache_inf = code.inferred
if !(cache_inf === nothing)
cache_src_inferred = ccall(:jl_ast_flag_inferred, Bool, (Any,), cache_inf)
cache_src_inlineable = ccall(:jl_ast_flag_inlineable, Bool, (Any,), cache_inf)
cache_inlineable = cache_src_inferred && cache_src_inlineable
end
end
if !cache_inlineable
return Any
end
end
inf_result = cache_lookup(code, argtypes, sv.params.cache)
inf_result = cache_lookup(mi, argtypes, sv.params.cache)
if inf_result === nothing
inf_result = InferenceResult(code, argtypes)
inf_result = InferenceResult(mi, argtypes)
frame = InferenceState(inf_result, #=cache=#false, sv.params)
frame.limited = true
frame.parent = sv
push!(sv.params.cache, inf_result)
typeinf(frame) || return Any
end
result = inf_result.result
isa(result, InferenceState) && return Any # TODO: is this recursive constant inference?
isa(result, InferenceState) && return Any # TODO: unexpected, is this recursive constant inference?
add_backedge!(inf_result.linfo, sv)
return result
end
@@ -237,7 +243,7 @@ function abstract_call_method(method::Method, @nospecialize(sig), sparams::Simpl
# necessary in order to retrieve this field from the generated `CodeInfo`, if it exists.
# The other `CodeInfo`s we inspect will already have this field inflated, so we just
# access it directly instead (to avoid regeneration).
method2 = method_for_inference_heuristics(method, sig, sparams, sv.params.world) # Union{Method, Nothing}
method2 = method_for_inference_heuristics(method, sig, sparams) # Union{Method, Nothing}
sv_method2 = sv.src.method_for_inference_limit_heuristics # limit only if user token match
sv_method2 isa Method || (sv_method2 = nothing) # Union{Method, Nothing}
while !(infstate === nothing)
@@ -6,7 +6,7 @@
# since we won't be able to specialize & infer them at runtime

let fs = Any[typeinf_ext, typeinf, typeinf_edge, pure_eval_call, run_passes],
world = ccall(:jl_get_world_counter, UInt, ())
world = get_world_counter()
for x in T_FFUNC_VAL
push!(fs, x[3])
end
@@ -5,7 +5,7 @@ getfield(getfield(Main, :Core), :eval)(getfield(Main, :Core), :(baremodule Compi
using Core.Intrinsics, Core.IR

import Core: print, println, show, write, unsafe_write, stdout, stderr,
_apply, svec, apply_type, Builtin, IntrinsicFunction, MethodInstance
_apply, svec, apply_type, Builtin, IntrinsicFunction, MethodInstance, CodeInstance

const getproperty = getfield
const setproperty! = setfield!
@@ -9,13 +9,8 @@ mutable struct InferenceResult
result # ::Type, or InferenceState if WIP
src #::Union{CodeInfo, OptimizationState, Nothing} # if inferred copy is available
function InferenceResult(linfo::MethodInstance, given_argtypes = nothing)
if isdefined(linfo, :inferred_const)
result = Const(linfo.inferred_const)
else
result = linfo.rettype
end
argtypes, overridden_by_const = matching_cache_argtypes(linfo, given_argtypes)
return new(linfo, argtypes, overridden_by_const, result, nothing)
return new(linfo, argtypes, overridden_by_const, Any, nothing)
end
end

@@ -5,7 +5,7 @@ const LineNum = Int
mutable struct InferenceState
params::Params # describes how to compute the result
result::InferenceResult # remember where to put the result
linfo::MethodInstance # used here for the tuple (specTypes, env, Method) and world-age validity
linfo::MethodInstance
sptypes::Vector{Any} # types of static parameter
slottypes::Vector{Any}
mod::Module
@@ -87,13 +87,8 @@ mutable struct InferenceState
inmodule = linfo.def::Module
end

if cached && !toplevel
min_valid = min_world(linfo.def)
max_valid = max_world(linfo.def)
else
min_valid = typemax(UInt)
max_valid = typemin(UInt)
end
min_valid = UInt(1)
max_valid = get_world_counter()
frame = new(
params, result, linfo,
sp, slottypes, inmodule, 0,
@@ -194,15 +189,12 @@ _topmod(sv::InferenceState) = _topmod(sv.mod)
function update_valid_age!(min_valid::UInt, max_valid::UInt, sv::InferenceState)
sv.min_valid = max(sv.min_valid, min_valid)
sv.max_valid = min(sv.max_valid, max_valid)
@assert(!isa(sv.linfo.def, Method) ||
!sv.cached ||
sv.min_valid <= sv.params.world <= sv.max_valid,
@assert(sv.min_valid <= sv.params.world <= sv.max_valid,
"invalid age range update")
nothing
end

update_valid_age!(edge::InferenceState, sv::InferenceState) = update_valid_age!(edge.min_valid, edge.max_valid, sv)
update_valid_age!(li::MethodInstance, sv::InferenceState) = update_valid_age!(min_world(li), max_world(li), sv)

function record_ssa_assign(ssa_id::Int, @nospecialize(new), frame::InferenceState)
old = frame.src.ssavaluetypes[ssa_id]
@@ -226,6 +218,7 @@ function add_cycle_backedge!(frame::InferenceState, caller::InferenceState, curr
update_valid_age!(frame, caller)
backedge = (caller, currpc)
contains_is(frame.cycle_backedges, backedge) || push!(frame.cycle_backedges, backedge)
add_backedge!(frame.linfo, caller)
return frame
end

@@ -236,7 +229,6 @@ function add_backedge!(li::MethodInstance, caller::InferenceState)
caller.stmt_edges[caller.currpc] = []
end
push!(caller.stmt_edges[caller.currpc], li)
update_valid_age!(li, caller)
nothing
end

@@ -56,7 +56,7 @@ mutable struct OptimizationState
return new(linfo,
s_edges::Vector{Any},
src, inmodule, nargs,
min_world(linfo), max_world(linfo),
UInt(1), get_world_counter(),
params, sptypes_from_meth_instance(linfo), slottypes, false)
end
end
@@ -104,19 +104,21 @@ _topmod(sv::OptimizationState) = _topmod(sv.mod)
function update_valid_age!(min_valid::UInt, max_valid::UInt, sv::OptimizationState)
sv.min_valid = max(sv.min_valid, min_valid)
sv.max_valid = min(sv.max_valid, max_valid)
@assert(!isa(sv.linfo.def, Method) ||
(sv.min_valid == typemax(UInt) && sv.max_valid == typemin(UInt)) ||
sv.min_valid <= sv.params.world <= sv.max_valid,
@assert(sv.min_valid <= sv.params.world <= sv.max_valid,
"invalid age range update")
nothing
end

update_valid_age!(li::MethodInstance, sv::OptimizationState) = update_valid_age!(min_world(li), max_world(li), sv)

function add_backedge!(li::MethodInstance, caller::OptimizationState)
#TODO: deprecate this?
isa(caller.linfo.def, Method) || return # don't add backedges to toplevel exprs
push!(caller.calledges, li)
update_valid_age!(li, caller)
nothing
end

function add_backedge!(li::CodeInstance, caller::OptimizationState)
update_valid_age!(min_world(li), max_world(li), caller)
add_backedge!(li.def, caller)
nothing
end

@@ -56,6 +56,8 @@ struct Params
tuple_splat)
end
function Params(world::UInt)
world == typemax(UInt) && (world = get_world_counter()) # workaround for bad callers
@assert world <= get_world_counter()
inlining = inlining_enabled()
return new(Vector{InferenceResult}(),
world, true,

0 comments on commit 8c44566

Please sign in to comment.
You can’t perform that action at this time.