Repository navigation
Conversation
`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
force-pushed
the
tb/method-table-views
branch
from
December 3, 2026 17:16
3b289d0 to
a664b12
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
An overlay method for the function that is compiled is ignored (#208):
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:
method_table_viewmethod_table_view(config::CompilerConfig, world::UInt), next tomethod_table_view(job)methodinstancemethodinstance(ft, tt, view::Core.Compiler.MethodTableView), next tomethodinstance(ft, tt, [world])CompilerJobCompilerJob(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_ownerrelies on), so they are now defined for a config:method_tables(config), withmethod_tables(job)remaining what the compiler calls. Back-ends opt in by defining the config-level method and forwarding the job-level one:and change where they look up the function, e.g. for CUDA.jl, from
to
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):cufunction(kernel, tt), cached (every launch)cached_lookupmatches 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, alsointeractive=true) looked the signature ofjob.sourceup 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; withcached_lookup(#1061) that cost is off the launch path.Fixes #208