Skip to content

Precompile what the worker calls along with cooperative_wait - #49

Merged
maleadt merged 1 commit into
mainfrom
tb/precompile-perform
Dec 2, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/precompile-perform

Conversation

@maleadt

@maleadt maleadt commented Dec 2, 2026

Copy link
Copy Markdown
Member

Worker threads call perform! dynamically, so a package image that contains a caller's cooperative_wait doesn't contain the matching perform! specialization. It's compiled on the first wait handed to a worker: 8.5–9.7 ms in CUDA.jl, which works around it by precompiling GPUToolbox.perform! for WaitRequest{typeof(worker_synchronize), Tuple{CuStream, CuContext}, CUresult} by name (JuliaGPU/CUDA.jl#3659). That statement names internals and copies the result type GPUToolbox infers, so it breaks or silently goes stale when either changes.

While generating output, cooperative_wait now performs a request on the calling thread instead of calling wait directly. That gives it a static call to the same perform! specialization, which is then cached along with it. Nothing changes at run time outside precompilation.

A new test precompiles a package that calls cooperative_wait, and checks with --trace-compile that the first wait on a worker compiles nothing from GPUToolbox. On CUDA.jl main with its perform! statements removed, the perform! entry disappears from the trace of the first synchronizing launch, and nothing else changes. After this is released, CUDA.jl can require 3.7.1 and drop those statements.

A worker thread performs a wait by calling `perform!` dynamically, so a
package image that contains a caller's `cooperative_wait` doesn't contain
the `perform!` specialization it needs, which is then compiled the first
time a wait is handed to a worker (about 10 ms in CUDA.jl). To avoid that,
packages had to precompile `perform!` for GPUToolbox's internal request
types themselves.

While generating output, perform the request on the calling thread
instead of calling `wait` directly. That gives `cooperative_wait` a static
call to the same `perform!` specialization, which is compiled and cached
along with it.
@maleadt
maleadt force-pushed the tb/precompile-perform branch from 2c0c9ac to 7aef0d5 Compare December 2, 2026 14:43
@maleadt
maleadt merged commit d80d51f into main Dec 2, 2026
10 checks passed
@maleadt
maleadt deleted the tb/precompile-perform branch December 2, 2026 15:14
maleadt added a commit to JuliaGPU/CUDA.jl that referenced this pull request Dec 3, 2026
GPUToolbox 3.8 caches what a wait worker calls along with
`cooperative_wait` (JuliaGPU/GPUToolbox.jl#49), so CUDACore no longer has
to precompile `perform!` for GPUToolbox's internal request types, which
depended on their names, parameters and the inferred result type.
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