Skip to content

Commit

Permalink
change the variable name linfo::MethodInstance to `mi::MethodInstan…
Browse files Browse the repository at this point in the history
…ce` (#53952)
  • Loading branch information
aviatesk authored Apr 5, 2024
1 parent 57bbff6 commit 6ea67a9
Show file tree
Hide file tree
Showing 9 changed files with 85 additions and 83 deletions.
22 changes: 11 additions & 11 deletions base/compiler/abstractinterpretation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1292,20 +1292,20 @@ struct ConditionalArgtypes <: ForwardableArgtypes
end

"""
matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance,
matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance,
conditional_argtypes::ConditionalArgtypes)
The implementation is able to forward `Conditional` of `conditional_argtypes`,
as well as the other general extended lattice information.
"""
function matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance,
function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance,
conditional_argtypes::ConditionalArgtypes)
(; arginfo, sv) = conditional_argtypes
(; fargs, argtypes) = arginfo
given_argtypes = Vector{Any}(undef, length(argtypes))
def = linfo.def::Method
def = mi.def::Method
nargs = Int(def.nargs)
cache_argtypes, overridden_by_const = matching_cache_argtypes(𝕃, linfo)
cache_argtypes, overridden_by_const = matching_cache_argtypes(𝕃, mi)
local condargs = nothing
for i in 1:length(argtypes)
argtype = argtypes[i]
Expand Down Expand Up @@ -1336,7 +1336,7 @@ function matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance,
end
if condargs !== nothing
given_argtypes = let condargs=condargs
va_process_argtypes(𝕃, given_argtypes, linfo) do isva_given_argtypes::Vector{Any}, last::Int
va_process_argtypes(𝕃, given_argtypes, mi) do isva_given_argtypes::Vector{Any}, last::Int
# invalidate `Conditional` imposed on varargs
for (slotid, i) in condargs
if slotid last && (1 i length(isva_given_argtypes)) # `Conditional` is already widened to vararg-tuple otherwise
Expand All @@ -1346,7 +1346,7 @@ function matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance,
end
end
else
given_argtypes = va_process_argtypes(𝕃, given_argtypes, linfo)
given_argtypes = va_process_argtypes(𝕃, given_argtypes, mi)
end
return pick_const_args!(𝕃, cache_argtypes, overridden_by_const, given_argtypes)
end
Expand Down Expand Up @@ -2278,7 +2278,7 @@ function abstract_call(interp::AbstractInterpreter, arginfo::ArgInfo, si::StmtIn
return abstract_call_known(interp, f, arginfo, si, sv, max_methods)
end

function sp_type_rewrap(@nospecialize(T), linfo::MethodInstance, isreturn::Bool)
function sp_type_rewrap(@nospecialize(T), mi::MethodInstance, isreturn::Bool)
isref = false
if unwrapva(T) === Bottom
return Bottom
Expand All @@ -2293,12 +2293,12 @@ function sp_type_rewrap(@nospecialize(T), linfo::MethodInstance, isreturn::Bool)
else
return Any
end
if isa(linfo.def, Method)
spsig = linfo.def.sig
if isa(mi.def, Method)
spsig = mi.def.sig
if isa(spsig, UnionAll)
if !isempty(linfo.sparam_vals)
if !isempty(mi.sparam_vals)
sparam_vals = Any[isvarargtype(v) ? TypeVar(:N, Union{}, Any) :
v for v in linfo.sparam_vals]
v for v in mi.sparam_vals]
T = ccall(:jl_instantiate_type_in_env, Any, (Any, Any, Ptr{Any}), T, spsig, sparam_vals)
isref && isreturn && T === Any && return Bottom # catch invalid return Ref{T} where T = Any
for v in sparam_vals
Expand Down
68 changes: 35 additions & 33 deletions base/compiler/inferenceresult.jl
Original file line number Diff line number Diff line change
@@ -1,57 +1,58 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

"""
matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance) ->
matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance) ->
(cache_argtypes::Vector{Any}, overridden_by_const::BitVector)
Returns argument types `cache_argtypes::Vector{Any}` for `linfo` that are in the native
Returns argument types `cache_argtypes::Vector{Any}` for `mi` that are in the native
Julia type domain. `overridden_by_const::BitVector` is all `false` meaning that
there is no additional extended lattice information there.
matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance, argtypes::ForwardableArgtypes) ->
matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance, argtypes::ForwardableArgtypes) ->
(cache_argtypes::Vector{Any}, overridden_by_const::BitVector)
Returns cache-correct extended lattice argument types `cache_argtypes::Vector{Any}`
for `linfo` given some `argtypes` accompanied by `overridden_by_const::BitVector`
for `mi` given some `argtypes` accompanied by `overridden_by_const::BitVector`
that marks which argument contains additional extended lattice information.
In theory, there could be a `cache` containing a matching `InferenceResult`
for the provided `linfo` and `given_argtypes`. The purpose of this function is
to return a valid value for `cache_lookup(𝕃, linfo, argtypes, cache).argtypes`,
for the provided `mi` and `given_argtypes`. The purpose of this function is
to return a valid value for `cache_lookup(𝕃, mi, argtypes, cache).argtypes`,
so that we can construct cache-correct `InferenceResult`s in the first place.
"""
function matching_cache_argtypes end

function matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance)
mthd = isa(linfo.def, Method) ? linfo.def::Method : nothing
cache_argtypes = most_general_argtypes(mthd, linfo.specTypes)
return cache_argtypes, falses(length(cache_argtypes))
function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance)
method = isa(mi.def, Method) ? mi.def::Method : nothing
cache_argtypes = most_general_argtypes(method, mi.specTypes)
overridden_by_const = falses(length(cache_argtypes))
return cache_argtypes, overridden_by_const
end

struct SimpleArgtypes <: ForwardableArgtypes
argtypes::Vector{Any}
end

"""
matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance, argtypes::SimpleArgtypes)
matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance, argtypes::SimpleArgtypes)
The implementation for `argtypes` with general extended lattice information.
This is supposed to be used for debugging and testing or external `AbstractInterpreter`
usages and in general `matching_cache_argtypes(::MethodInstance, ::ConditionalArgtypes)`
is more preferred it can forward `Conditional` information.
"""
function matching_cache_argtypes(𝕃::AbstractLattice, linfo::MethodInstance, simple_argtypes::SimpleArgtypes)
function matching_cache_argtypes(𝕃::AbstractLattice, mi::MethodInstance, simple_argtypes::SimpleArgtypes)
(; argtypes) = simple_argtypes
given_argtypes = Vector{Any}(undef, length(argtypes))
for i = 1:length(argtypes)
given_argtypes[i] = widenslotwrapper(argtypes[i])
end
given_argtypes = va_process_argtypes(𝕃, given_argtypes, linfo)
return pick_const_args(𝕃, linfo, given_argtypes)
given_argtypes = va_process_argtypes(𝕃, given_argtypes, mi)
return pick_const_args(𝕃, mi, given_argtypes)
end

function pick_const_args(𝕃::AbstractLattice, linfo::MethodInstance, given_argtypes::Vector{Any})
cache_argtypes, overridden_by_const = matching_cache_argtypes(𝕃, linfo)
function pick_const_args(𝕃::AbstractLattice, mi::MethodInstance, given_argtypes::Vector{Any})
cache_argtypes, overridden_by_const = matching_cache_argtypes(𝕃, mi)
return pick_const_args!(𝕃, cache_argtypes, overridden_by_const, given_argtypes)
end

Expand All @@ -60,7 +61,7 @@ function pick_const_args!(𝕃::AbstractLattice, cache_argtypes::Vector{Any}, ov
given_argtype = given_argtypes[i]
cache_argtype = cache_argtypes[i]
if !is_argtype_match(𝕃, given_argtype, cache_argtype, false)
# prefer the argtype we were given over the one computed from `linfo`
# prefer the argtype we were given over the one computed from `mi`
if (isa(given_argtype, PartialStruct) && isa(cache_argtype, Type) &&
!(𝕃, given_argtype, cache_argtype))
# if the type information of this `PartialStruct` is less strict than
Expand All @@ -80,8 +81,9 @@ function is_argtype_match(𝕃::AbstractLattice,
overridden_by_const::Bool)
if is_forwardable_argtype(𝕃, given_argtype)
return is_lattice_equal(𝕃, given_argtype, cache_argtype)
else
return !overridden_by_const
end
return !overridden_by_const
end

va_process_argtypes(𝕃::AbstractLattice, given_argtypes::Vector{Any}, mi::MethodInstance) =
Expand Down Expand Up @@ -114,24 +116,24 @@ function most_general_argtypes(method::Union{Method, Nothing}, @nospecialize(spe
withfirst::Bool = true)
toplevel = method === nothing
isva = !toplevel && method.isva
linfo_argtypes = Any[(unwrap_unionall(specTypes)::DataType).parameters...]
mi_argtypes = Any[(unwrap_unionall(specTypes)::DataType).parameters...]
nargs::Int = toplevel ? 0 : method.nargs
# For opaque closure, the closure environment is processed elsewhere
withfirst || (nargs -= 1)
cache_argtypes = Vector{Any}(undef, nargs)
# First, if we're dealing with a varargs method, then we set the last element of `args`
# to the appropriate `Tuple` type or `PartialStruct` instance.
mi_argtypes_length = length(mi_argtypes)
if !toplevel && isva
if specTypes::Type == Tuple
linfo_argtypes = Any[Any for i = 1:nargs]
mi_argtypes = Any[Any for i = 1:nargs]
if nargs > 1
linfo_argtypes[end] = Tuple
mi_argtypes[end] = Tuple
end
vargtype = Tuple
else
linfo_argtypes_length = length(linfo_argtypes)
if nargs > linfo_argtypes_length
va = linfo_argtypes[linfo_argtypes_length]
if nargs > mi_argtypes_length
va = mi_argtypes[mi_argtypes_length]
if isvarargtype(va)
new_va = rewrap_unionall(unconstrain_vararg_length(va), specTypes)
vargtype = Tuple{new_va}
Expand All @@ -140,8 +142,8 @@ function most_general_argtypes(method::Union{Method, Nothing}, @nospecialize(spe
end
else
vargtype_elements = Any[]
for i in nargs:linfo_argtypes_length
p = linfo_argtypes[i]
for i in nargs:mi_argtypes_length
p = mi_argtypes[i]
p = unwraptv(isvarargtype(p) ? unconstrain_vararg_length(p) : p)
push!(vargtype_elements, elim_free_typevars(rewrap_unionall(p, specTypes)))
end
Expand All @@ -164,13 +166,12 @@ function most_general_argtypes(method::Union{Method, Nothing}, @nospecialize(spe
# type info as we go (where possible). Note that if we're dealing with a varargs method,
# we already handled the last element of `cache_argtypes` (and decremented `nargs` so that
# we don't overwrite the result of that work here).
linfo_argtypes_length = length(linfo_argtypes)
if linfo_argtypes_length > 0
n = linfo_argtypes_length > nargs ? nargs : linfo_argtypes_length
if mi_argtypes_length > 0
n = mi_argtypes_length > nargs ? nargs : mi_argtypes_length
tail_index = n
local lastatype
for i = 1:n
atyp = linfo_argtypes[i]
atyp = mi_argtypes[i]
if i == n && isvarargtype(atyp)
atyp = unwrapva(atyp)
tail_index -= 1
Expand Down Expand Up @@ -209,13 +210,14 @@ function elim_free_typevars(@nospecialize t)
end
end

function cache_lookup(𝕃::AbstractLattice, linfo::MethodInstance, given_argtypes::Vector{Any}, cache::Vector{InferenceResult})
method = linfo.def::Method
function cache_lookup(𝕃::AbstractLattice, mi::MethodInstance, given_argtypes::Vector{Any},
cache::Vector{InferenceResult})
method = mi.def::Method
nargs = Int(method.nargs)
method.isva && (nargs -= 1)
length(given_argtypes) nargs || return nothing
for cached_result in cache
cached_result.linfo === linfo || continue
cached_result.linfo === mi || continue
cache_argtypes = cached_result.argtypes
cache_overridden_by_const = cached_result.overridden_by_const
for i in 1:nargs
Expand Down
28 changes: 14 additions & 14 deletions base/compiler/inferencestate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -276,14 +276,14 @@ mutable struct InferenceState
# src is assumed to be a newly-allocated CodeInfo, that can be modified in-place to contain intermediate results
function InferenceState(result::InferenceResult, src::CodeInfo, cache_mode::UInt8,
interp::AbstractInterpreter)
linfo = result.linfo
mi = result.linfo
world = get_inference_world(interp)
if world == typemax(UInt)
error("Entering inference from a generated function with an invalid world")
end
def = linfo.def
def = mi.def
mod = isa(def, Method) ? def.module : def
sptypes = sptypes_from_meth_instance(linfo)
sptypes = sptypes_from_meth_instance(mi)
code = src.code::Vector{Any}
cfg = compute_basic_blocks(code)
method_info = MethodInfo(src)
Expand Down Expand Up @@ -339,7 +339,7 @@ mutable struct InferenceState
!iszero(cache_mode & CACHE_MODE_LOCAL) && push!(get_inference_cache(interp), result)

this = new(
linfo, world, mod, sptypes, slottypes, src, cfg, method_info,
mi, world, mod, sptypes, slottypes, src, cfg, method_info,
currbb, currpc, ip, handlers, handler_at, ssavalue_uses, bb_vartables, ssavaluetypes, stmt_edges, stmt_info,
pclimitations, limitations, cycle_backedges, callers_in_cycle, dont_work_on_me, parent,
result, unreachable, valid_worlds, bestguess, exc_bestguess, ipo_effects,
Expand Down Expand Up @@ -582,21 +582,21 @@ end

const EMPTY_SPTYPES = VarState[]

function sptypes_from_meth_instance(linfo::MethodInstance)
def = linfo.def
function sptypes_from_meth_instance(mi::MethodInstance)
def = mi.def
isa(def, Method) || return EMPTY_SPTYPES # toplevel
sig = def.sig
if isempty(linfo.sparam_vals)
if isempty(mi.sparam_vals)
isa(sig, UnionAll) || return EMPTY_SPTYPES
# linfo is unspecialized
# mi is unspecialized
spvals = Any[]
sig′ = sig
while isa(sig′, UnionAll)
push!(spvals, sig′.var)
sig′ = sig′.body
end
else
spvals = linfo.sparam_vals
spvals = mi.sparam_vals
end
nvals = length(spvals)
sptypes = Vector{VarState}(undef, nvals)
Expand All @@ -614,7 +614,7 @@ function sptypes_from_meth_instance(linfo::MethodInstance)
if isType(sⱼ) && sⱼ.parameters[1] === vᵢ
# if this parameter came from `arg::Type{T}`,
# then `arg` is more precise than `Type{T} where lb<:T<:ub`
ty = fieldtype(linfo.specTypes, j)
ty = fieldtype(mi.specTypes, j)
@goto ty_computed
elseif (va = va_from_vatuple(sⱼ)) !== nothing
# if this parameter came from `::Tuple{.., Vararg{T,vᵢ}}`,
Expand Down Expand Up @@ -645,8 +645,8 @@ function sptypes_from_meth_instance(linfo::MethodInstance)
# type variables, we can use it for a more accurate analysis of whether `v`
# is constrained or not, otherwise we should use `def.sig` which always
# doesn't contain any free type variables
if !has_free_typevars(linfo.specTypes)
sig = linfo.specTypes
if !has_free_typevars(mi.specTypes)
sig = mi.specTypes
end
@assert !has_free_typevars(sig)
constrains_param(v, sig, #=covariant=#true)
Expand Down Expand Up @@ -858,8 +858,8 @@ function is_effect_overridden(sv::AbsIntState, effect::Symbol)
end
return false
end
function is_effect_overridden(linfo::MethodInstance, effect::Symbol)
def = linfo.def
function is_effect_overridden(mi::MethodInstance, effect::Symbol)
def = mi.def
return isa(def, Method) && is_effect_overridden(def, effect)
end
is_effect_overridden(method::Method, effect::Symbol) = is_effect_overridden(decode_effects_override(method.purity), effect)
Expand Down
14 changes: 7 additions & 7 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -156,23 +156,23 @@ function OptimizationState(sv::InferenceState, interp::AbstractInterpreter)
sv.sptypes, sv.slottypes, inlining, sv.cfg,
sv.unreachable, sv.bb_vartables, sv.insert_coverage)
end
function OptimizationState(linfo::MethodInstance, src::CodeInfo, interp::AbstractInterpreter)
function OptimizationState(mi::MethodInstance, src::CodeInfo, interp::AbstractInterpreter)
# prepare src for running optimization passes if it isn't already
nssavalues = src.ssavaluetypes
if nssavalues isa Int
src.ssavaluetypes = Any[ Any for i = 1:nssavalues ]
else
nssavalues = length(src.ssavaluetypes::Vector{Any})
end
sptypes = sptypes_from_meth_instance(linfo)
sptypes = sptypes_from_meth_instance(mi)
nslots = length(src.slotflags)
slottypes = src.slottypes
if slottypes === nothing
slottypes = Any[ Any for i = 1:nslots ]
end
stmt_info = CallInfo[ NoCallInfo() for i = 1:nssavalues ]
# cache some useful state computations
def = linfo.def
def = mi.def
mod = isa(def, Method) ? def.module : def
# Allow using the global MI cache, but don't track edges.
# This method is mostly used for unit testing the optimizer
Expand All @@ -186,13 +186,13 @@ function OptimizationState(linfo::MethodInstance, src::CodeInfo, interp::Abstrac
for slot = 1:nslots
])
end
return OptimizationState(linfo, src, nothing, stmt_info, mod, sptypes, slottypes, inlining, cfg, unreachable, bb_vartables, false)
return OptimizationState(mi, src, nothing, stmt_info, mod, sptypes, slottypes, inlining, cfg, unreachable, bb_vartables, false)
end
function OptimizationState(linfo::MethodInstance, interp::AbstractInterpreter)
function OptimizationState(mi::MethodInstance, interp::AbstractInterpreter)
world = get_inference_world(interp)
src = retrieve_code_info(linfo, world)
src = retrieve_code_info(mi, world)
src === nothing && return nothing
return OptimizationState(linfo, src, interp)
return OptimizationState(mi, src, interp)
end

function argextype end # imported by EscapeAnalysis
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/ssair/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ function print_stmt(io::IO, idx::Int, @nospecialize(stmt), used::BitSet, maxleng
stmt = stmt::Expr
# TODO: why is this here, and not in Base.show_unquoted
print(io, "invoke ")
linfo = stmt.args[1]::Core.MethodInstance
mi = stmt.args[1]::Core.MethodInstance
show_unquoted(io, stmt.args[2], indent)
print(io, "(")
# XXX: this is wrong if `sig` is not a concretetype method
# more correct would be to use `fieldtype(sig, i)`, but that would obscure / discard Varargs information in show
sig = linfo.specTypes == Tuple ? Core.svec() : Base.unwrap_unionall(linfo.specTypes).parameters::Core.SimpleVector
sig = mi.specTypes == Tuple ? Core.svec() : Base.unwrap_unionall(mi.specTypes).parameters::Core.SimpleVector
print_arg(i) = sprint(; context=io) do io
show_unquoted(io, stmt.args[i], indent)
if (i - 1) <= length(sig)
Expand Down
Loading

0 comments on commit 6ea67a9

Please sign in to comment.