Skip to content

Invalidate code when an overlay method is added for a call it made - #1035

Closed
maleadt wants to merge 1 commit into
mainfrom
tb/overlay-invalidation
Closed

maleadt wants to merge 1 commit into
mainfrom
tb/overlay-invalidation

Conversation

@maleadt

@maleadt maleadt commented Oct 19, 2026

Copy link
Copy Markdown
Member

Fixes #207: adding an overlay method didn't invalidate code that called the method it overrides, so even a fresh compile kept returning stale code.

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

Julia only records that a call depends on the absence of a method (a "missing-method" edge) when the methods it found don't fully cover the call. child() misses in the overlay table and is fully covered by Base's method, so the caller only gets an edge to that method, and inserting the overlay invalidates nothing. Redefining an overlay that already exists did work, through the edge to it. This affects Julia's own OverlayMethodTable the same way; I filed JuliaLang/julia#63959.

This PR records a missing-method edge for every lookup when GPUInterpreter finishes a frame:

  • on 1.12+, one (sig, Core.methodtable) pair per signature. These backedges are stored globally and checked on insertion into any table, including overlay tables, so one edge covers the whole stack;
  • on 1.10 and 1.11, one (mt, sig) pair per overlay table, because those backedges are stored per table.

The check on insertion only invalidates when the new method is more specific than what that table already has for the signature, so defining unrelated methods in Base's table doesn't cause extra invalidations.

The cost is roughly three times as many edges, and correspondingly more time in Julia's store_backedges. Inferring a set of Base functions (sort!, sum, string, repr, ...; about 3600 CodeInstances) on 1.12 went from 1.74 s to 1.82–2.0 s.

Not covered: code in a package image that was precompiled while a lookup missed in an overlay table is still not invalidated by an overlay defined before the image is loaded. Edge verification at load time skips these edges and only looks at Base's method table (see the Julia issue). On 1.12+, an overlay defined after loading is handled, since the edges are re-registered when the image is loaded.

Julia only records that a call depends on the absence of a method when the
methods it found don't fully cover the call. A call that misses in the
overlay tables and resolves to a method in a later table, or in Base's, has
no dependency on the overlay tables, so adding an overlay later left stale
code in the cache (#207).

Record a missing-method edge for every lookup when GPUInterpreter finishes a
frame: one global edge per signature on 1.12+, where these are checked on
insertion into any table, and one per overlay table before. This roughly
triples the number of edges and costs 5-15% inference time; it should move
into Julia (JuliaLang/julia#63959).
@maleadt
maleadt force-pushed the tb/overlay-invalidation branch from b2d3f97 to 043a91a Compare October 22, 2026 10:07
@codecov

codecov Bot commented Oct 22, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.36842% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 92.55%. Comparing base (4939c9d) to head (043a91a).
⚠️ Report is 28 commits behind head on main.

Files with missing lines Patch % Lines
src/jlgen.jl 97.36% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1035      +/-   ##
==========================================
+ Coverage   92.41%   92.55%   +0.14%     
==========================================
  Files          31       31              
  Lines        7261     7337      +76     
==========================================
+ Hits         6710     6791      +81     
+ Misses        551      546       -5     

☔ 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.

@maleadt

maleadt commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Superseded by #1146 (rebased onto main).

@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 do not re-trigger compilation

1 participant