Repository navigation
Conversation
maleadt
force-pushed
the
tb/binding-overlays
branch
3 times, most recently
from
December 3, 2026 15:24
658c367 to
8e6fde9
Compare
maleadt
force-pushed
the
tb/binding-overlays
branch
from
December 3, 2026 15:37
8e6fde9 to
ed08c98
Compare
maleadt
force-pushed
the
tb/binding-overlays
branch
4 times, most recently
from
December 3, 2026 20:06
ccda5ad to
892febf
Compare
Back-ends can now declare, with GPUToolbox's `@binding_overlay`, that a host global reads as one of their constants in device code: `Base.stdout` as a handle to the device's output, so that Base's `print(xs...)` works without replacing it, or a lookup table that Base keeps in a `Vector` as a tuple. Julia only asks the interpreter about a global during inference; the optimizer and codegen look at the binding again, and on 1.14 a pass resolves every remaining read to the host's binding partition. So the reads are redirected before inference, in each frame's copy of the lowered code, to the constant that holds the overlay's value. Every later stage then sees an ordinary read of a constant. Only reads that name the binding in the code are redirected: a name, or `M.name` and `getglobal(M, :name)` with modules that are named too, and without a memory order. Treating a module that inference merely knows to be constant the same way would make a specialized copy of a function read the overlay where the generic one reads the host binding. A global is matched by the binding a read loads from, through imports, and a constant by the object it holds. That follows what Julia tracks for compiled code, which is the binding for a global (from 1.14 on) but only the value for a constant, so that code is invalidated whenever a name starts or stops referring to an overlaid binding. On 1.12 and 1.13 one case is missed, for our code as for Julia's own: a name that is found through `using` and is later imported from another module, with the same type. Declarations are overlay methods, which the interpreter finds with GPUToolbox's `binding_overlay_signature` in each of its method tables, at its world. Code that reads a global or an object that could be overlaid depends on no declaration being added to those tables, so declaring one recompiles it, like adding a method overlay does; that is conservative, as it also covers reads of other bindings. Code that reads an overlay depends on its declaration with an `invoke` edge, which Julia invalidates when the method is deleted or replaced, and validates in package images by the method's own table. On 1.10, GPUCompiler's own cache needs a callback for that. As for method overlays, a package image doesn't learn of a declaration that was loaded before it (JuliaLang/julia#63959). Code also depends on the bindings involved, and is marked as not to be evaluated by running host code. Cthulhu infers with an interpreter of its own that wraps ours; the extension redirects the reads of its frames too.
Julia 1.11+ installs it through `[sources]`. Julia 1.10 ignores those, so CI adds it by URL and runs the tests in the test environment there. To be dropped once JuliaGPU/GPUToolbox.jl#50 is merged and registered.
maleadt
force-pushed
the
tb/binding-overlays
branch
from
December 3, 2026 20:21
892febf to
b5a3615
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.
Device code can't read host globals, so Base methods that do have to be replaced as a whole. The motivating case is device printing in CUDA.jl (JuliaGPU/CUDA.jl#3665): Base's
print(xs...) = print(stdout, xs...)readsBase.stdout, so that PR overlays the IO-lessprint/println/showand has to restrict their first argument to keep them from capturing calls that do pass an IO. That hides user methods such asprintln(::MyNumber), andprintln(stdout, x)orf(stdout)in a kernel still don't work.With this PR a back-end declares what the global is in device code, in the method table it already has:
and Base's own methods work: with those two lines and #3665's five IO-less overlays deleted, a kernel prints through
println(xs...), a user-definedprint(::IO, ::Point),println(stdout, x),println(Base.stderr, x)and a non-inlinedsink(stdout, x). The same mechanism makesndigitscompile (Base.powers_of_ten, aVector, as a tuple) and lets back-ends drop their copies of Random's Ziggurat samplers, which exist only because the tables areVectors; GPUToolbox ships a table with those overlays. The macro lives in GPUToolbox (JuliaGPU/GPUToolbox.jl#50), so packages can declare overlays without depending on GPUCompiler.Julia only asks the interpreter about a global during inference: the optimizer and codegen look at the binding again, and on 1.14 a pass resolves every remaining read to the host's binding partition. So the reads are redirected before inference, in each frame's copy of the lowered code, to the constant that holds the overlay's value. From then on every stage sees an ordinary read of a constant. It is one pass, and the same code on 1.10 through nightly.
Which reads are redirected:
name,M.name,getglobal(M, :name), with modules that are named too. A module that inference merely knows to be constant doesn't count, or a specialized copy of a function would read the overlay where the generic one reads the host binding.getfield, reads with a memory order, stores andisdefinedrefer to the host binding.using/import. An overlaid constant is matched by the object it holds, so any name bound to that object reads the overlay. This follows what Julia tracks for compiled code, the binding for a global and only the value for a constant, so that code is invalidated whenever a name starts or stops referring to an overlaid binding. It is also why only constants holding a mutable object can be overlaid.Declarations are overlay methods, which the interpreter looks up per table of its stack, at its world, through GPUToolbox's
binding_overlay_signature; nothing changes incache_owner. Code that reads a global or such a constant depends on no declaration being added to those tables, in the way #1146 does for method lookups, which this builds on: declaring an overlay after a kernel was compiled recompiles it, also when the declaration comes from a package image loaded later. Code that reads an overlay also depends on its declaration through aninvokeedge on that method, so deleting or replacing it (Base.delete_method, Revise) recompiles the code too, and package images validate that edge by the method's own table. Such code also depends on the bindings involved (1.12+), and is not constant-evaluated by running host code. The Cthulhu extension redirects the reads of Cthulhu's frames too.Limits:
usingthat is later imported from another module, with the same declared type, keeps reading the overlay. Julia has the same bug for its own code there (fixed in 1.14 by IR: reformulate global reads via inference to binding partition JuliaLang/julia#62452); the test is marked broken on those versions. On 1.10 and 1.11 nothing depends on bindings, for us as for Julia.Tested on the native target on 1.10, 1.11, 1.12, 1.13 and nightly: the kinds of reads, effects, stacked tables, reflection, late declarations, redeclaration under another spelling, deletion (warm cache, a lower-priority table taking over, a job for an older world), declarations in another table leaving the cache alone, rebinding on 1.12+,
ndigits's table through GPUToolbox's overlays, and a round trip through package images, including a second package adding an overlay and deleting a declaration that restored code read. With CUDA.jl on an RTX 5080, the kernel described above prints all five lines, and Base'srandexpand Random's Zigguratrandnsample correctly with GPUToolbox's lookup tables (2^22 samples: mean, variance, kurtosis and tail probabilities as expected).The last commit takes GPUToolbox from the branch of JuliaGPU/GPUToolbox.jl#50 until 3.9 is registered, and is to be dropped then.