Skip to content

Commit 50172d3

Browse files
vtjnashDilumAluthgeBot
authored andcommitted
make just one MethodTable (JuliaLang#58131)
Instead of hiding the fragments of the method table in each TypeName, make one variable `Core.GlobalMethods` with access to all methods. The need to split them early for performance apparently had been long past since kwargs and constructors were already using a single table and cache anyways. Some new concepts introduced here: - A single Method can now be added to multiple functions. So instead of using eval in a for loop, we could define it just once (see example below). - Several fields (`max_args`, `name`, and `backedges`) were moved from MethodTable to their TypeName. - TypeName currently has a (user-modifiable) field called `singletonname`. If set to something other than `name`, it may be used for pretty printing of a singleton object using its "canonical" (unmangled) name, particularly for `function`. - `Core.Builtin` method table entries are even more normal now, with valid `sig` fields, and special logic to specifically prevent adding methods which would become ambiguous with them (as that would violate the tfuncs we have for them). - `Core.GlobalMethods` is a `Base.Experimental.@MethodTable GlobalMethods`. - Each `MethodTable` contains a separate `MethodCache` object for managing fast dispatch lookups. We may want to use this for the `Method` field containing the `invokes` list so that lookups there get more of the same optimizations as global calls. - Methods could be put into any number of different MethodTables (or none). The `Method.primary_world` field is intended to reflect whether it is currently put into the GlobalMethods table, and what world to use in the GlobalMethods table for running its generator, and otherwise is meaningless. - The lock for TypeName backedges is a single global lock now, in `Core.GlobalMethods.mc`. - The `backedges` in TypeName are stored on the "top-most" typename in the hierarchy, to enable efficient lookup (although we might want to consider replacing this entirely with a TypeMap). The "top-most" typename is the typename of the type closest to Any, after union-splitting, which doesn't have an intersection with Builtin (so Function and Any by implication continue to not require scanning for missing backedges since it is not permitted to add a Method applicable to all functions). - Support for having backedges from experimental method tables was removed since it was unsound and had been already replaced with staticdata.jl several months ago. - Documentation lookup for `IncludeInto` is fixed (previously attached only to `Main.include` instead of all `include` functions). Example: given this existing code in base/operators: for op in (:+, :*, :&, :|, :xor, :min, :max, :kron) @eval begin ($op)(a, b, c, xs...) = (@inline; afoldl($op, ($op)(($op)(a,b),c), xs...)) end end It could now instead be equivalently written as: let ops = Union{typeof(+), typeof(*), typeof(&), typeof(|), typeof(xor), typeof(min), typeof(max), typeof(kron)} (op::ops)(a, b, c, xs...) = (@inline; afoldl(op, (op)((op)(a,b),c), xs...)) end Fixes JuliaLang#57560
1 parent f89ea9c commit 50172d3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+982
-1010
lines changed

Compiler/src/Compiler.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ else
3838
using Core.Intrinsics, Core.IR
3939

4040
using Core: ABIOverride, Builtin, CodeInstance, IntrinsicFunction, MethodInstance, MethodMatch,
41-
MethodTable, PartialOpaque, SimpleVector, TypeofVararg,
41+
MethodTable, MethodCache, PartialOpaque, SimpleVector, TypeofVararg,
4242
_apply_iterate, apply_type, compilerbarrier, donotdelete, memoryref_isassigned,
4343
memoryrefget, memoryrefnew, memoryrefoffset, memoryrefset!, print, println, show, svec,
4444
typename, unsafe_write, write

Compiler/src/abstractinterpretation.jl

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -363,15 +363,13 @@ function find_union_split_method_matches(interp::AbstractInterpreter, argtypes::
363363
arg_n = split_argtypes[i]::Vector{Any}
364364
sig_n = argtypes_to_type(arg_n)
365365
sig_n === Bottom && continue
366-
mt = ccall(:jl_method_table_for, Any, (Any,), sig_n)
367-
mt === nothing && return FailedMethodMatch("Could not identify method table for call")
368-
mt = mt::MethodTable
369366
thismatches = findall(sig_n, method_table(interp); limit = max_methods)
370367
if thismatches === nothing
371368
return FailedMethodMatch("For one of the union split cases, too many methods matched")
372369
end
373370
valid_worlds = intersect(valid_worlds, thismatches.valid_worlds)
374371
thisfullmatch = any(match::MethodMatch->match.fully_covers, thismatches)
372+
mt = Core.GlobalMethods
375373
thisinfo = MethodMatchInfo(thismatches, mt, sig_n, thisfullmatch)
376374
push!(infos, thisinfo)
377375
for idx = 1:length(thismatches)
@@ -385,18 +383,14 @@ function find_union_split_method_matches(interp::AbstractInterpreter, argtypes::
385383
end
386384

387385
function find_simple_method_matches(interp::AbstractInterpreter, @nospecialize(atype), max_methods::Int)
388-
mt = ccall(:jl_method_table_for, Any, (Any,), atype)
389-
if mt === nothing
390-
return FailedMethodMatch("Could not identify method table for call")
391-
end
392-
mt = mt::MethodTable
393386
matches = findall(atype, method_table(interp); limit = max_methods)
394387
if matches === nothing
395388
# this means too many methods matched
396389
# (assume this will always be true, so we don't compute / update valid age in this case)
397390
return FailedMethodMatch("Too many methods matched")
398391
end
399392
fullmatch = any(match::MethodMatch->match.fully_covers, matches)
393+
mt = Core.GlobalMethods
400394
info = MethodMatchInfo(matches, mt, atype, fullmatch)
401395
applicable = MethodMatchTarget[MethodMatchTarget(matches[idx], info.edges, idx) for idx = 1:length(matches)]
402396
return MethodMatches(applicable, info, matches.valid_worlds)

Compiler/src/stmtinfo.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,17 +47,16 @@ end
4747
add_edges_impl(edges::Vector{Any}, info::MethodMatchInfo) = _add_edges_impl(edges, info)
4848
function _add_edges_impl(edges::Vector{Any}, info::MethodMatchInfo, mi_edge::Bool=false)
4949
if !fully_covering(info)
50-
# add legacy-style missing backedge info also
5150
exists = false
5251
for i in 2:length(edges)
53-
if edges[i] === info.mt && edges[i-1] == info.atype
52+
if edges[i] === Core.GlobalMethods && edges[i-1] == info.atype
5453
exists = true
5554
break
5655
end
5756
end
5857
if !exists
5958
push!(edges, info.atype)
60-
push!(edges, info.mt)
59+
push!(edges, Core.GlobalMethods)
6160
end
6261
end
6362
nmatches = length(info.results)

Compiler/src/tfuncs.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3199,15 +3199,12 @@ function _hasmethod_tfunc(interp::AbstractInterpreter, argtypes::Vector{Any}, sv
31993199
isdispatchelem(ft) || return CallMeta(Bool, Any, Effects(), NoCallInfo()) # check that we might not have a subtype of `ft` at runtime, before doing supertype lookup below
32003200
types = rewrap_unionall(Tuple{ft, unwrapped.parameters...}, types)::Type
32013201
end
3202-
mt = ccall(:jl_method_table_for, Any, (Any,), types)
3203-
if !isa(mt, MethodTable)
3204-
return CallMeta(Bool, Any, EFFECTS_THROWS, NoCallInfo())
3205-
end
32063202
match, valid_worlds = findsup(types, method_table(interp))
32073203
update_valid_age!(sv, valid_worlds)
32083204
if match === nothing
32093205
rt = Const(false)
32103206
vresults = MethodLookupResult(Any[], valid_worlds, true)
3207+
mt = Core.GlobalMethods
32113208
vinfo = MethodMatchInfo(vresults, mt, types, false) # XXX: this should actually be an info with invoke-type edge
32123209
else
32133210
rt = Const(true)

Compiler/src/typeinfer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ function store_backedges(caller::CodeInstance, edges::SimpleVector)
767767
if item isa Core.Binding
768768
maybe_add_binding_backedge!(item, caller)
769769
elseif item isa MethodTable
770-
ccall(:jl_method_table_add_backedge, Cvoid, (Any, Any, Any), item, invokesig, caller)
770+
ccall(:jl_method_table_add_backedge, Cvoid, (Any, Any), invokesig, caller)
771771
else
772772
item::MethodInstance
773773
ccall(:jl_method_instance_add_backedge, Cvoid, (Any, Any, Any), item, invokesig, caller)

Compiler/src/utilities.jl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,8 @@ end
158158

159159
function get_compileable_sig(method::Method, @nospecialize(atype), sparams::SimpleVector)
160160
isa(atype, DataType) || return nothing
161-
mt = ccall(:jl_method_get_table, Any, (Any,), method)
162-
mt === nothing && return nothing
163-
return ccall(:jl_normalize_to_compilable_sig, Any, (Any, Any, Any, Any, Cint),
164-
mt, atype, sparams, method, #=int return_if_compileable=#1)
161+
return ccall(:jl_normalize_to_compilable_sig, Any, (Any, Any, Any, Cint),
162+
atype, sparams, method, #=int return_if_compileable=#1)
165163
end
166164

167165

base/Base_compiler.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ function Core.kwcall(kwargs::NamedTuple, ::typeof(invoke), f, T, args...)
218218
return invoke(Core.kwcall, T, kwargs, f, args...)
219219
end
220220
# invoke does not have its own call cache, but kwcall for invoke does
221-
setfield!(typeof(invoke).name.mt, :max_args, 3, :monotonic) # invoke, f, T, args...
221+
setfield!(typeof(invoke).name, :max_args, Int32(3), :monotonic) # invoke, f, T, args...
222222

223223
# define applicable(f, T, args...; kwargs...), without kwargs wrapping
224224
# to forward to applicable
@@ -252,7 +252,7 @@ function Core.kwcall(kwargs::NamedTuple, ::typeof(invokelatest), f, args...)
252252
@inline
253253
return Core.invokelatest(Core.kwcall, kwargs, f, args...)
254254
end
255-
setfield!(typeof(invokelatest).name.mt, :max_args, 2, :monotonic) # invokelatest, f, args...
255+
setfield!(typeof(invokelatest).name, :max_args, Int32(2), :monotonic) # invokelatest, f, args...
256256

257257
"""
258258
invoke_in_world(world, f, args...; kwargs...)
@@ -286,7 +286,7 @@ function Core.kwcall(kwargs::NamedTuple, ::typeof(invoke_in_world), world::UInt,
286286
@inline
287287
return Core.invoke_in_world(world, Core.kwcall, kwargs, f, args...)
288288
end
289-
setfield!(typeof(invoke_in_world).name.mt, :max_args, 3, :monotonic) # invoke_in_world, world, f, args...
289+
setfield!(typeof(invoke_in_world).name, :max_args, Int32(3), :monotonic) # invoke_in_world, world, f, args...
290290

291291
# core operations & types
292292
include("promotion.jl")

base/deprecated.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ macro deprecate(old, new, export_old=true)
211211
maybe_export,
212212
:($(esc(old)) = begin
213213
$meta
214-
depwarn($"`$oldcall` is deprecated, use `$newcall` instead.", Core.Typeof($(esc(fnexpr))).name.mt.name)
214+
depwarn($"`$oldcall` is deprecated, use `$newcall` instead.", Core.Typeof($(esc(fnexpr))).name.singletonname)
215215
$(esc(new))
216216
end))
217217
else
@@ -222,7 +222,7 @@ macro deprecate(old, new, export_old=true)
222222
export_old ? Expr(:export, esc(old)) : nothing,
223223
:(function $(esc(old))(args...; kwargs...)
224224
$meta
225-
depwarn($"`$old` is deprecated, use `$new` instead.", Core.Typeof($(esc(old))).name.mt.name)
225+
depwarn($"`$old` is deprecated, use `$new` instead.", Core.Typeof($(esc(old))).name.singletonname)
226226
$(esc(new))(args...; kwargs...)
227227
end))
228228
end

base/docs/bindings.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,6 @@ end
4242

4343
aliasof(b::Binding) = defined(b) ? (a = aliasof(resolve(b), b); defined(a) ? a : b) : b
4444
aliasof(d::DataType, b) = Binding(d.name.module, d.name.name)
45-
aliasof::Function, b) = (m = typeof(λ).name.mt; Binding(m.module, m.name))
45+
aliasof::Function, b) = (m = typeof(λ).name; Binding(m.module, m.singletonname))
4646
aliasof(m::Module, b) = Binding(m, nameof(m))
4747
aliasof(other, b) = b

base/errorshow.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function showerror(io::IO, ex::MethodError)
328328
print(io, "\nIn case you're trying to index into the array, use square brackets [] instead of parentheses ().")
329329
end
330330
# Check for local functions that shadow methods in Base
331-
let name = ft.name.mt.name
331+
let name = ft.name.singletonname
332332
if f_is_function && isdefined(Base, name)
333333
basef = getfield(Base, name)
334334
if basef !== f && hasmethod(basef, arg_types)

0 commit comments

Comments
 (0)