Repository navigation
Add shared definitions of device functions, and a check for host leaks - #38
Merged
Merged
Conversation
Back-ends each have their own `@device_override` and `@device_function` macros, which have drifted apart: SPIRVIntrinsics' `@device_function` makes the device implementation a consistent overlay of a host method that throws, and only CUDA.jl's handles definitions in `@doc` blocks on Julia 1.13. A device function that doesn't end up as an overlay leaves device code in the global method table, where sysimage builds compile it for the host; such an image fails to load, as does a package image on Windows. Add `Overlays.device_override` and `Overlays.device_function`, which implement these macros given the back-end's method table, and `Overlays.host_leaks`, which finds the methods that sysimage builds would compile and whose code reaches device-only symbols, for back-ends to check in their tests.
maleadt
force-pushed
the
tb/device-functions
branch
from
October 27, 2026 17:45
c1633bf to
524190f
Compare
|
Documentation preview: https://juliagpu.github.io/GPUToolbox.jl/previews/PR38/ |
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 that ends up in the global method table gets compiled for the host by sysimage builds: Julia 1.12+ compiles every method with a compileable signature, and on 1.11 native code instances leak in. The result refers to symbols that only exist on the device (
julia.gpu.state_getter,julia.opencl.random_keys,__spirv_*) or to intrinsics of a GPU target. Such a sysimage fails to load, and on Windows a package image fails to link. For example, a sysimage with KernelAbstractions 0.10:Back-ends avoid this by defining device-only functions as a host method that throws plus an overlay with the real body (CUDA.jl#3249). Each back-end has its own copy of the
@device_overrideand@device_functionmacros, and the copies have drifted apart:@device_function, which KernelAbstractions, OpenCL.jl and oneAPI.jl use, makes the device body a@consistent_overlay. Its host method throws, so inference can constant-fold calls into that error.@docblocks on Julia 1.13.This PR adds the shared pieces to
GPUToolbox.Overlays:device_override(mod, table, ex)anddevice_function(mod, table, ex)implement these macros, given the back-end's method table. Each back-end keeps a one-line macro:device_overridereplaces an existing host method and uses@consistent_overlayon 1.11+.device_functionuses a plain@overlay, and handles single definitions, blocks, docstrings and@generateddefinitions.host_leaks(mod)finds the methods that sysimage builds compile for the host, following Julia's own enumeration, and whose code reaches device-only symbols. Back-ends can check it in their tests:llvmcallthat declares a non-intrinsic function counts. It doesn't follow calls into other packages, which should run their own check.On current releases (Julia 1.13),
host_leaksreports:signal_exception,claim_output,random_keys,random_counters)llvm.amdgcn.mbcnt.loThese match the functions found by reading the code, and in KernelAbstractions they are exactly what keeps its sysimage from loading.
Tested on Julia 1.10 to 1.13.