Skip to content

Add methodinstance(job) to look up kernels in the job's method tables - #1037

Closed
maleadt wants to merge 3 commits into
tb/overlay-invalidationfrom
tb/overlay-kernel
Closed

maleadt wants to merge 3 commits into
tb/overlay-invalidationfrom
tb/overlay-kernel

Conversation

@maleadt

@maleadt maleadt commented Oct 19, 2026

Copy link
Copy Markdown
Member

Fixes #208: an overlay method for the kernel function itself was ignored.

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 up job.source with methodinstance(ft, tt), which only considers Base's method table, so the job compiles the global method. The rest of the pipeline already handles an overlay method instance fine. This PR adds methodinstance(job), which looks the job's source up again in the job's method tables (method_table_view(job)). It takes a job because the table stack may depend on the configuration. Back-ends opt in after constructing the job:

job = CompilerJob(methodinstance(ft, tt), config)
job = CompilerJob(job; source=methodinstance(job))

GPUCompiler uses it for deferred compilation and in the native test helper. CompilerJob(mi, config) keeps meaning "compile this method instance". Because the lookup happens in the job's world, an overlay added after the kernel was first compiled is picked up as well.

methodinstance(job) is meant for the launch path. With Julia 1.11.9+, 1.12.5+ or 1.13.0-beta2+ (JuliaLang/julia#60718) it uses Julia's dispatch cache for every table; otherwise it falls back to a generic lookup. Julia doesn't cache failed lookups, which cost ~70 ns per table against ~10 ns for a hit, so the misses are remembered for the world they happened in. For a back-end with its own overlay table plus SHARED_METHOD_TABLE, calling methodinstance(job) for a kernel without an overlay takes ~30 ns (without remembering the misses, ~140 ns); methodinstance(ft, tt) itself takes 10–18 ns. These were measured on a loaded machine.

Reflection (code_lowered, code_typed, code_warntype) looked the method up again by the signature of job.source, so it showed the global method even for a job compiling the overlay. It now uses job.source directly.

Stacked on #1035, which fixes the related invalidation problem (#207) and introduces the overlay_tables helper used here.

Not covered: a kernel that only has an overlay method and no method in Base's table, since methodinstance(ft, tt) throws before there is a job.

@maleadt
maleadt added this pull request to stack #1038 October 19, 2026 18:01
@codecov

codecov Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 85.88235% with 12 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.39%. Comparing base (043a91a) to head (e6f56b2).

Files with missing lines Patch % Lines
src/jlgen.jl 85.71% 8 Missing ⚠️
src/reflection_compat.jl 78.94% 4 Missing ⚠️
Additional details and impacted files
@@                     Coverage Diff                     @@
##           tb/overlay-invalidation    #1037      +/-   ##
===========================================================
- Coverage                    92.55%   92.39%   -0.16%     
===========================================================
  Files                           31       31              
  Lines                         7337     7394      +57     
===========================================================
+ Hits                          6791     6832      +41     
- Misses                         546      562      +16     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

`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.
Back-ends look up the method instance for a kernel with `methodinstance(ft,
tt)`, which only considers Base's method table, so an overlay method for the
kernel function itself was never compiled (#208). Looking it up in the job's
method tables requires the config, as the tables may depend on it, so add
`methodinstance(job)` that resolves `job.source` again through
`method_table_view(job)`, for back-ends to call after constructing the job.

Use it for deferred compilation and in the native test helper. On Julia
versions with JuliaLang/julia#60718 this uses Julia's dispatch cache for every
table; otherwise it falls back to a slower generic lookup.
Julia doesn't cache lookups that fail, and those take about 70 ns per overlay
table, against 10 ns for a cached lookup. Kernels rarely have an overlay
method, so `methodinstance(job)` would add 140 ns or more to every launch for
a back-end with a couple of tables. Remember the misses for the world they
happened in, which brings that back to around 30 ns in total.
@maleadt
maleadt marked this pull request as draft October 25, 2026 06:54
@maleadt

maleadt commented Oct 25, 2026

Copy link
Copy Markdown
Member Author

Marked draft because there's a significant cost here, and Jameson is convinced we need to handle this differently ourselves.

@maleadt

maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Superseded by #1149, which resolves kernels through the job's method-table view instead of a separate lookup with a miss cache.

@maleadt maleadt closed this Dec 3, 2026
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