Skip to content

Add @binding_overlay to give host globals a device-side value - #50

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

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

Conversation

@maleadt

@maleadt maleadt commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Base's print(xs...) = print(stdout, xs...) reads the global Base.stdout, which device code can't load. ndigits reads Base.powers_of_ten, and Random's randn/randexp read their Ziggurat tables, all Vectors. Today a back-end has to replace such methods as a whole: CUDA.jl's device printing overlays the IO-less print/println/show with a restricted first-argument type that hides user methods, four back-ends carry copies of Random's samplers with an llvmcall helper for the tables, and ndigits simply doesn't compile in kernels.

This adds a way to replace the binding instead of the methods that read it:

using GPUToolbox.Overlays: @binding_overlay

struct MyDeviceIO <: IO end
@binding_overlay method_table Base.stdout = MyDeviceIO()
@binding_overlay method_table Base.powers_of_ten = Tuple(Base.powers_of_ten)

In code compiled with method_table, a read of the binding reads the given constant. It is to bindings what Base.Experimental.@overlay is to methods, and is declared in the same tables. The compiler side is in GPUCompiler (JuliaGPU/GPUCompiler.jl#1147); GPUToolbox holds the declarations so that packages can declare overlays without depending on GPUCompiler.

A declaration is an overlay method of an internal function in that table, whose signature encodes the table, the declaring module, the source binding and the constant that holds the value. That gives binding overlays the properties of method overlays without machinery of their own: they are stacked with the tables, have a world age, are saved in the declaring package's image, a package can add one to another package's table, and declaring or deleting one is a method insertion or deletion, which compilers invalidate code on. A declaration is identified by (declaring module, table, source binding), so evaluating it again, also spelled differently or by Revise, replaces it.

The binding has to be a global, or a constant that holds a mutable object. Compiled code depends on which global it loads from, but for a constant only on the value, so a constant like const N = 64 can't be told apart from other constants that equal 64. The value has to be of a global's declared type (stdout::IO); for a constant the type is free, since replacing a Vector by a tuple is the point. The table is checked before the value is evaluated: anything but a @MethodTable would make the declaration an ordinary method.

The second commit adds Overlays.lookup_table_overrides, a table with the overlays for Base.powers_of_ten and Random's Ziggurat tables, next to throw_overrides and int128_overrides. GPUToolbox now depends on Random, which is in the system image.

The semantics are documented as experimental. What compilers use, binding_overlay_signature (the lookup signature for a table), binding_overlay_refs (source and target of a declaration) and has_identity, is promised stable across GPUToolbox 3.x, so that GPUCompiler can require GPUToolbox = "3.9".

Tested on Julia 1.10, 1.12, 1.13 and nightly: declarations, redeclarations under other spellings, the checks, and the lookup-table declarations. With GPUCompiler#1147 and CUDA.jl on an RTX 5080, ndigits, Base's randexp and Random's Ziggurat randn work in kernels with this table stacked (2^22 samples: mean, variance and tail probabilities match).

@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

Documentation preview: https://juliagpu.github.io/GPUToolbox.jl/previews/PR50/

maleadt added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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
maleadt force-pushed the tb/binding-overlays branch from 1b2bce9 to d03e499 Compare December 3, 2026 15:11
maleadt added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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.
A Base method can be unsuited for a device because of a global it reads:
`print(xs...)` reads `stdout`, `ndigits` and Random's samplers read
lookup tables that are kept in `Vector`s. Back-ends have to replace such
methods as a whole, with overlays that hide other methods or copy Base's
code.

`Overlays.@binding_overlay table Mod.name = value` declares that code
compiled with a method table reads `value` where it reads the binding. A
declaration is an overlay method of `Overlays.binding_overlay` in that
table, with the value in a constant of the declaring module. That makes
binding overlays behave like method overlays without support of their
own: they are stacked with the tables, have a world age, are part of the
declaring package's image, and declaring or deleting one is a method
insertion or deletion, which compilers invalidate code on.

A declaration is identified by the declaring module, the table and the
source binding, which the method's signature encodes; evaluating it
again, however it is spelled, replaces it. Replacing the value needs
Julia 1.12, where code depends on the constants it reads. The table is
checked before the value is evaluated, as anything but a `@MethodTable`
would make the declaration an ordinary method.

The binding has to be a global, or a constant that holds a mutable
object: compiled code depends on which global it loads from, but only on
the value of a constant, so other constants can't be told apart from
equal ones. The value has to be of a global's declared type.

Compilers find the declarations of a table with
`binding_overlay_signature`, and decode them with `binding_overlay_refs`;
that interface is stable across GPUToolbox 3.x. The feature itself is
experimental. GPUCompiler implements the reads.
`Overlays.lookup_table_overrides` is a method table with binding overlays
that make `Base.powers_of_ten` and Random's Ziggurat tables (`ki`, `wi`,
`fi`, `ke`, `we`, `fe`) constant tuples in device code. With it stacked,
Base's `ndigits` and Random's `randn`/`randexp` samplers compile in
kernels of back-ends that provide a random number generator, instead of
back-ends keeping copies of those methods with tables of their own.

This adds a dependency on Random, which is part of Julia's system image.
@maleadt
maleadt force-pushed the tb/binding-overlays branch from d03e499 to 80f283b Compare December 3, 2026 20:06
maleadt added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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 added a commit to JuliaGPU/GPUCompiler.jl that referenced this pull request Dec 3, 2026
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.

This branch has not been deployed

No deployments
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