Skip to content

Register deferred codegen requests when generating code - #1127

Merged
maleadt merged 1 commit into
tb/lockingfrom
tb/deferred-codegen
Nov 27, 2026
Merged

maleadt merged 1 commit into
tb/lockingfrom
tb/deferred-codegen

Conversation

@maleadt

@maleadt maleadt commented Nov 26, 2026 •

Copy link
Copy Markdown
Member

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 in deferred_codegen_jobs under length + 1 and emitted that index into the caller's code. Two things go wrong with that:

  • Generators run while the caller is inferred, which happens concurrently on Julia 1.12 and later. Requests then end up sharing an index, and a kernel silently links another kernel's child. Compiling 64 distinct parent kernels from 16 threads at once, 22 to 52 of them linked the wrong child. Just expanding the generator for 2000 functions from 16 threads left 1833 to 1995 of the indices pointing at the wrong function.
  • A caller whose code is cached in a package image carries an index that only meant something in the process that precompiled it. Compiling it in a fresh session failed with 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_codegen calls a @noinline method 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 in deferred_codegen_jobs use. 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:

  • An inference edge from the caller to the child, so that redefining the child invalidates the caller (the existing TODO). New deferred_codegen implementation #582 does this with an abstract-interpretation hook. Doing it right needs care about overlay method tables and package-image verification, and a reproducer for reusing a stale compiled caller. That's for a separate PR.
  • An API for the back-ends' RNG hooks, which register jobs from finish_module! and could stop writing deferred_codegen_jobs themselves. 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.

`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
maleadt force-pushed the tb/deferred-codegen branch from 0bb1671 to b359420 Compare November 26, 2026 14:46
@maleadt
maleadt merged commit b5f6e47 into main Nov 27, 2026
61 of 66 checks passed
@maleadt
maleadt deleted the tb/deferred-codegen branch November 27, 2026 13:21
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