-
Notifications
You must be signed in to change notification settings - Fork 53
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
methodinstance(foo, types)
returns MethodError permanently if method is not defined
#530
Comments
Ah, interesting, I hadn't considered that. @vtjnash how should invalidation work here? |
methodinstance_generator seems like it really should not be |
We index the GPUCompiler compilation cache with a GPUCompiler.jl/src/execution.jl Lines 91 to 99 in e5cd575
|
Looking up a methodinstance is usually faster than computing most hash functions |
Can you explain how to implement / where to find such a fast lookup? The fallback one I've implemented in GPUCompiler (for Julia <1.10, without a generator) is relatively slow: julia> tls_world_age() = ccall(:jl_get_tls_world_age, UInt, ())
tls_world_age (generic function with 1 method)
julia> @inline function typed_signature(ft::Type, tt::Type)
u = Base.unwrap_unionall(tt)
return Base.rewrap_unionall(Tuple{ft, u.parameters...}, tt)
end
typed_signature (generic function with 1 method)
julia> function methodinstance(ft::Type, tt::Type, world::Integer=tls_world_age())
sig = typed_signature(ft, tt)
match, _ = Core.Compiler._findsup(sig, nothing, world)
match === nothing && throw(MethodError(ft, tt, world))
mi = Core.Compiler.specialize_method(match)
return mi::Core.MethodInstance
end
methodinstance (generic function with 2 methods)
julia> methodinstance(typeof(sin), Tuple{Float32})
MethodInstance for sin(::Float32)
julia> using BenchmarkTools
julia> @benchmark methodinstance(typeof(sin), Tuple{Float32})
BenchmarkTools.Trial: 10000 samples with 214 evaluations.
Range (min … max): 348.313 ns … 8.419 μs ┊ GC (min … max): 0.00% … 94.22%
Time (median): 365.136 ns ┊ GC (median): 0.00%
Time (mean ± σ): 374.696 ns ± 251.231 ns ┊ GC (mean ± σ): 2.38% ± 3.38%
▁▃▆█▆▄▂▁
▂▁▂▂▂▂▂▂▂▂▂▂▂▃▃▃▃▄▅▆▆▇████████▇▆▅▃▃▃▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂▂ ▃
348 ns Histogram: frequency by time 388 ns <
Memory estimate: 288 bytes, allocs estimate: 6.
julia> @benchmark GPUCompiler.methodinstance(typeof(sin), Tuple{Float32})
BenchmarkTools.Trial: 10000 samples with 1000 evaluations.
Range (min … max): 2.119 ns … 10.350 ns ┊ GC (min … max): 0.00% … 0.00%
Time (median): 2.150 ns ┊ GC (median): 0.00%
Time (mean ± σ): 2.151 ns ± 0.120 ns ┊ GC (mean ± σ): 0.00% ± 0.00%
▄ ▆ ▁█ ▆ ▁
▃█▁▁▁▁▁▁▁▁▁▁▁▁▆█▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁██▁▁▁▁▁▁▁▁▁▁▁▁▇█ █
2.12 ns Histogram: log(frequency) by time 2.16 ns <
Memory estimate: 0 bytes, allocs estimate: 0. |
It used to be exported as |
I see. We have slightly different inputs here, e.g., function and argument types instead of values. Would it be OK to change |
I was looking at this earlier. I think the worry is that we would need to construct the tuple type unecessarily. In particular the hot-path here https://github.com/JuliaLang/julia/blob/e746ba4085b7de0036e596e19c0979071ee0a920/src/gf.c#L3007 |
Would it be feasible to make |
I looked at this some more. The fast lookup heavily uses |
What's the cost of a slow TT vs fast arg lookup? EDIT: answering my own question, JuliaLang/julia#52176 (comment) |
If I had made no mistake and defined the method before calling
methodinstance
:The text was updated successfully, but these errors were encountered: