Skip to content

Read binding overlays as constants in device code - #1147

Open
maleadt wants to merge 2 commits into
tb/overlay-edgesfrom
tb/binding-overlays
Open

maleadt wants to merge 2 commits into
tb/overlay-edgesfrom
tb/binding-overlays

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

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...) reads Base.stdout, so that PR overlays the IO-less print/println/show and has to restrict their first argument to keep them from capturing calls that do pass an IO. That hides user methods such as println(::MyNumber), and println(stdout, x) or f(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:

GPUToolbox.Overlays.@binding_overlay method_table Base.stdout = CuDeviceIO(STDOUT_HANDLE)
GPUToolbox.Overlays.@binding_overlay method_table Base.stderr = CuDeviceIO(STDERR_HANDLE)

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-defined print(::IO, ::Point), println(stdout, x), println(Base.stderr, x) and a non-inlined sink(stdout, x). The same mechanism makes ndigits compile (Base.powers_of_ten, a Vector, as a tuple) and lets back-ends drop their copies of Random's Ziggurat samplers, which exist only because the tables are Vectors; 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:

  • Only reads that name the binding in the code: 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 and isdefined refer to the host binding.
  • An overlaid global is found through any 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 in cache_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 an invoke edge 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:

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's randexp and Random's Ziggurat randn sample 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.

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

1 participant