Skip to content

Look up the function to compile through the method table view - #1149

Open
maleadt wants to merge 2 commits into
mainfrom
tb/method-table-views
Open

maleadt wants to merge 2 commits into
mainfrom
tb/method-table-views

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

An overlay method for the function that is compiled is ignored (#208):

Base.Experimental.@MethodTable(method_table)
kernel() = 0
Base.Experimental.@overlay method_table kernel() = 1
Native.code_llvm(kernel, Tuple{}; method_table)    # ret i64 0

Back-ends look the function up with methodinstance(ft, tt), which only knows Base's method table, while inference looks up the calls in that function through the job's overlay tables. A function that only has an overlay method can't be compiled at all.

The fix is to look the function up the way inference looks up everything else. That takes two general operations, which are small extensions of names GPUCompiler already has:

Concept Name Signature
the view a compilation looks methods up through method_table_view new: method_table_view(config::CompilerConfig, world::UInt), next to method_table_view(job)
look a signature up, to the method instance to compile methodinstance new: methodinstance(ft, tt, view::Core.Compiler.MethodTableView), next to methodinstance(ft, tt, [world])
a job for what was found CompilerJob unchanged: CompilerJob(source, config, world)

The view was only available from a job, and a job needs the method instance first. But the tables may only depend on the target and parameters anyway (that is what cache_owner relies on), so they are now defined for a config: method_tables(config), with method_tables(job) remaining what the compiler calls. Back-ends opt in by defining the config-level method and forwarding the job-level one:

GPUCompiler.method_tables(config::MyCompilerConfig) = (method_table, other_table)
GPUCompiler.method_tables(job::MyCompilerJob) = GPUCompiler.method_tables(job.config)

and change where they look up the function, e.g. for CUDA.jl, from

(l::KernelLinker)() = cufunction_link(methodinstance(l.ft, l.tt); l.kw...)
function cufunction_link(source::Core.MethodInstance; kwargs...)
    config = compiler_config(cuda.device; kwargs...)
    job = CompilerJob(source, config)

to

(l::KernelLinker)() = cufunction_link(l.ft, l.tt; l.kw...)
function cufunction_link(ft, tt; kwargs...)
    config = compiler_config(cuda.device; kwargs...)
    world = GPUCompiler.tls_world_age()
    source = methodinstance(ft, tt, GPUCompiler.method_table_view(config, world))
    job = CompilerJob(source, config, world)

A back-end that only defines the job-level method keeps compiling what it did. Deferred compilation looks up what it compiles through the view of the job that asks, and the runtime library already was looked up this way, so the same two verbs now cover the function, its callees, deferred functions and the runtime.

Looking up through a view is slower than the global lookup, but it doesn't happen on the launch path: back-ends cache launches with cached_lookup, and the lookup is in the function that runs on a miss. With the CUDA.jl change above applied locally (Julia 1.12, four overlay tables):

before after
cufunction(kernel, tt), cached (every launch) 8.7–8.9 ns 8.7–9.1 ns
the miss function with everything compiled (once per signature, world and context) 265–285 ns 538–558 ns

cached_lookup matches the world exactly, so an overlay that is added after a kernel was launched is found by the next launch, without a dependency having to be recorded. With that patch, a CUDA kernel with an overlay runs the overlay, also when the overlay is declared after a first launch, or is the kernel's only method.

Reflection on a job (code_lowered, code_typed, code_warntype, also interactive=true) looked the signature of job.source up again in Base's table, and so showed other code than what is compiled; it now uses the method instance (first commit).

Not covered here: code compiled for a parent is not invalidated when an overlay is added later for a function it compiles deferred. That needs the parent to depend on the lookup, which is a follow-up on top of #1146.

This supersedes #1037, which added a separate methodinstance(job) with a cache of failed lookups to keep it affordable on every launch; with cached_lookup (#1061) that cost is off the launch path.

Fixes #208

`code_lowered`, `code_typed` and `code_warntype` looked up the method for the
signature of `job.source` again, in Base's method table (and on 1.12+ through
the interpreter for `code_typed`). When the job compiles an overlay method,
that showed different code from what is compiled. Use `job.source` directly.

Generated methods are expanded in the world of the job.
Back-ends look up the function they compile with `methodinstance(ft, tt)`,
which only knows Base's method table, while inference looks up the calls
in that function through the job's overlay tables. So an overlay method
for the function itself was never compiled (#208), and a function with
only an overlay method couldn't be compiled at all.

Looking the function up like inference does needs the view before there
is a job, since a job is constructed for a method instance. The tables
already may only depend on the target and parameters, so define them for
a config: `method_tables(config)` and `method_table_view(config, world)`,
next to the methods for jobs that the compiler uses. Back-ends opt in by
defining the config-level method and forwarding the job-level one to it;
one that only defines the job-level method keeps compiling what it did,
as the view of its configs doesn't know its tables.

`methodinstance(ft, tt, view)` looks a signature up through a view, in
the view's world. With those two, the function, the calls it makes, the
functions that deferred compilation asks for, and the runtime library
are all looked up the same way:

    source = methodinstance(ft, tt, method_table_view(config, world))
    job = CompilerJob(source, config, world)

Looking up through a view takes some 200 ns with four tables, against
6 ns for the global lookup. Back-ends do it where a launch isn't cached
(`cached_lookup`), in the world that the launch is cached for, so launches
don't get slower. As that cache matches the world exactly, an overlay
that is added later is found too, without a dependency having to be
recorded.

Deferred compilation and the native test helper use it. Code that was
compiled for a parent is not invalidated yet when an overlay is added for
a function it compiles deferred.
@maleadt
maleadt force-pushed the tb/method-table-views branch from 3b289d0 to a664b12 Compare December 3, 2026 17:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Method overrides cannot override kernels

1 participant