Skip to content

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

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

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

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 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.

This is #1035 rebased onto current main, where lookups now go through MethodTableStack; overlay_tables handles that type too. x-ref #1035

This is a stopgap. Upstream hasn't settled how external interpreters should be invalidated: Jameson argues they shouldn't add native backedges for non-native semantics and should verify their own forward edges instead (JuliaLang/julia#63959 (comment)), while the drafts JuliaLang/julia#64726 and JuliaLang/julia#64727 would have Julia record and re-verify lookups through overlay tables itself. The edges are recorded in one place, so this can follow whichever lands.

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