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

Ensure methodinstance can be used on abstract types #644

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
63 changes: 34 additions & 29 deletions src/jlgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -71,34 +71,7 @@ function generic_methodinstance(@nospecialize(ft::Type), @nospecialize(tt::Type)
return mi::MethodInstance
end

# on 1.11 (JuliaLang/julia#52572, merged as part of JuliaLang/julia#52233) we can use
# Julia's cached method lookup to simply look up method instances at run time.
if VERSION >= v"1.11.0-DEV.1552"

# XXX: version of Base.method_instance that uses a function type
@inline function methodinstance(@nospecialize(ft::Type), @nospecialize(tt::Type),
world::Integer=tls_world_age())
sig = signature_type_by_tt(ft, tt)
@assert Base.isdispatchtuple(sig) # JuliaLang/julia#52233
Copy link
Contributor

@vtjnash vtjnash Oct 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The underlying function this ccalls may assert or segfault without this. There is probably a significant argument to be made that you should be using the public function which(sig) here, which doesn't have that footgun, but it currently doesn't know that it could optimize the isdispatchtuple case by calling jl_method_lookup_by_tt.


mi = ccall(:jl_method_lookup_by_tt, Any,
(Any, Csize_t, Any),
sig, world, #=method_table=# nothing)
mi === nothing && throw(MethodError(ft, tt, world))
mi = mi::MethodInstance

# `jl_method_lookup_by_tt` and `jl_method_lookup` can return a unspecialized mi
if !Base.isdispatchtuple(mi.specTypes)
mi = CC.specialize_method(mi.def, sig, mi.sparam_vals)::MethodInstance
end

return mi
end

# on older versions of Julia, we always need to use the generic lookup
else

const methodinstance = generic_methodinstance
const methodinstance_internal = generic_methodinstance

function methodinstance_generator(world::UInt, source, self, ft::Type, tt::Type)
@nospecialize
Expand Down Expand Up @@ -150,11 +123,43 @@ function methodinstance_generator(world::UInt, source, self, ft::Type, tt::Type)
return new_ci
end

@eval function methodinstance(ft, tt)
@eval function methodinstance_internal(ft, tt)
$(Expr(:meta, :generated_only))
$(Expr(:meta, :generated, methodinstance_generator))
end

# on 1.11 (JuliaLang/julia#52572, merged as part of JuliaLang/julia#52233) we can use
# Julia's cached method lookup to simply look up method instances at run time.
if VERSION >= v"1.11.0-DEV.1552"

# XXX: version of Base.method_instance that uses a function type
@inline function methodinstance(@nospecialize(ft::Type), @nospecialize(tt::Type),
world::Integer=tls_world_age())
sig = signature_type_by_tt(ft, tt)
if Base.isdispatchtuple(sig)
mi = ccall(:jl_method_lookup_by_tt, Any,
(Any, Csize_t, Any),
sig, world, #=method_table=# nothing)
mi === nothing && throw(MethodError(ft, tt, world))
mi = mi::MethodInstance

# `jl_method_lookup_by_tt` and `jl_method_lookup` can return a unspecialized mi
if !Base.isdispatchtuple(mi.specTypes)
mi = CC.specialize_method(mi.def, sig, mi.sparam_vals)::MethodInstance
end
else
return methodinstance_internal(ft, tt, world)
end

return mi
end

# on older versions of Julia, we always need to use the generic lookup
else

const methodinstance = methodinstance_internal
@inline function methodinstance(@nospecialize(ft::Type), @nospecialize(tt::Type),
world::Integer=tls_world_age())
end


Expand Down
Loading