Repository navigation
Register deferred codegen requests when generating code - #1127
Merged
Merged
Conversation
maleadt
force-pushed
the
tb/deferred-codegen
branch
from
November 26, 2026 12:55
e949f4f to
0bb1671
Compare
`deferred_codegen(::Val, ::Val)` was a generated function that registered the requested function in a global dictionary, and emitted the index into it. Generators run while inferring the caller, which happens concurrently on Julia 1.12 and later, so requests got each other's index: compiling 64 distinct kernels from 16 threads at once, 22 to 52 of them linked another kernel's child. A caller cached in a package image also carries an index that only meant something in the process that precompiled it (`KeyError` when compiling it in another session). Make it a call to a method specialized on the request instead, and register the request when Julia has generated code for it, under a lock and once per request. Its id is the address of the registered request, which isn't the id of any job that back-ends or Enzyme.jl register in `deferred_codegen_jobs` themselves. The marker stays the same, so code that handles it, e.g. after a sibling compiler, keeps working.
maleadt
force-pushed
the
tb/deferred-codegen
branch
from
November 26, 2026 14:46
0bb1671 to
b359420
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.
Builds on #1126 (stacked; the change itself is the last commit).
deferred_codegen(::Val{ft}, ::Val{tt}), which CUDA.jl uses for dynamic parallelism, was a generated function. It registered the requested function indeferred_codegen_jobsunderlength + 1and emitted that index into the caller's code. Two things go wrong with that:KeyError: key 1 not found. In a session that had registered other requests, it would have linked whatever is registered under that index.With this PR,
deferred_codegencalls a@noinlinemethod specialized on the request. Its code holds the existing marker with a dummy id. Right after Julia generates a module, before anything is inlined or linked, GPUCompiler finds those specializations, registers their request, and sets the marker's id. The id is the address of the registered request, which none of the jobs that back-ends (CUDA, Metal and OpenCL RNG initialization) and Enzyme.jl register indeferred_codegen_jobsuse. So nothing is written during inference anymore, and inferred code no longer contains process-specific ids. Requests are registered once each, under a lock that doesn't yield, since code generation can happen in generated functions (Enzyme). The marker itself is unchanged, so code compiled by a sibling compiler, and Enzyme's handling of it, keep working.I considered and left out:
finish_module!and could stop writingdeferred_codegen_jobsthemselves. That would come with the downstream changes.Tests cover two children of one parent, nested children sharing a grandchild, a request that's inlined at the call site,
entry_abi=:func, a request in code compiled by the mock Enzyme sibling, and a legacy id with the sign bit set (as an address can have on 32-bit). They also cover concurrent compilation in a separate process, and a parent restored from a package image. The last two fail on main. The full test suite passes on Julia 1.13, and the concurrent check passes on 1.10, 1.11 and 1.12.