Hide deferred_codegen ccall body behind overlay table#3112
Open
gbaraldi wants to merge 1 commit into
Open
Conversation
The `Compiler.deferred_codegen` body contains `ccall("extern
deferred_codegen", llvmcall, ...)`, which is not valid for normal Julia
codegen. When `jl_compile_all_defs` enqueues the despecialized form
during a sysimage build (or via an explicit `precompile(...)`), it
fails with `UndefVarError: deferred_codegen not defined in Enzyme`,
preventing Enzyme from being included in a sysimage (#3091).
Move the ccall body into a `Base.Experimental.@overlay
EnzymeMethodTable` definition, leaving the public function empty.
Overlay methods have `external_mt` set, which `jl_compile_all_defs`
skips (precompile_utils.c:147), so the invalid body is no longer
visible to the sysimage build.
Enzyme jobs pick up the overlay through `GPUCompiler.method_table`,
which now returns `EnzymeMethodTable` for `CompilerJob{<:EnzymeTarget}`.
High-order CPU `autodiff_deferred` callers continue to work because
they are compiled by the EnzymeInterpreter, which consults the overlay.
Direct CPU `autodiff_deferred` test calls (which compiled under regular
Julia and bypassed the overlay) are removed; the equivalent coverage
remains in the GPU test files and the high-order tests.
Fixes #3091.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Contributor
|
Your PR requires formatting changes to meet the project's style guidelines. Click here to view the suggested changes.diff --git a/test/core/abi.jl b/test/core/abi.jl
index d6cddcf9..ea2eaad6 100644
--- a/test/core/abi.jl
+++ b/test/core/abi.jl
@@ -113,7 +113,7 @@ end
@test res[] ≈ 6.0
@test dres[] ≈ 2.0
@test orig == Float64
-
+
function inplace2(x)
x[] *= 2
return nothing |
Contributor
Benchmark Results
Benchmark PlotsA plot of the benchmark results has been uploaded as an artifact at https://github.com/EnzymeAD/Enzyme.jl/actions/runs/26050655422/artifacts/7065439274. |
vchuravy
reviewed
May 19, 2026
Comment on lines
+176
to
+178
|
|
||
| # Enzyme jobs see `deferred_codegen`'s body via the EnzymeMethodTable overlay. | ||
| GPUCompiler.method_table(@nospecialize(job::CompilerJob{<:EnzymeTarget})) = EnzymeMethodTable |
Member
There was a problem hiding this comment.
This will break things w.r.t CUDA iirc
vchuravy
reviewed
May 19, 2026
Comment on lines
+98
to
+101
| # Method table used to hide compiler-internal definitions (e.g. `deferred_codegen`) | ||
| # whose bodies are not valid for normal Julia codegen — putting them behind this | ||
| # table prevents `jl_compile_all_defs` from despecializing them into the sysimage. | ||
| Base.Experimental.@MethodTable EnzymeMethodTable |
Member
There was a problem hiding this comment.
I don't think we currently need an EnzymeMethodTable,
what we need is an entry in CUDA overlay table making Enzyme.autodiff use deferred_codegen.
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.
This is probably wrong but does some of the plumbing that we will eventually need
Summary
Fixes #3091.
Compiler.deferred_codegen's body containsccall(\"extern deferred_codegen\", llvmcall, ...), which is not valid for normal Julia codegen. Whenjl_compile_all_defsenqueues the despecialized form during a sysimage build, it fails withUndefVarError: deferred_codegen not defined in Enzyme.Base.Experimental.@overlay EnzymeMethodTabledefinition; the publicdeferred_codegenis nowfunction deferred_codegen end(empty).external_mtset, whichjl_compile_all_defsskips (precompile_utils.c:147), so the invalid body is no longer despecialized into the sysimage.GPUCompiler.method_table(::CompilerJob{<:EnzymeTarget}) = EnzymeMethodTableso the EnzymeInterpreter consults the overlay during nested codegen.Test plan
autodiff_deferredcallers intest/core/abi.jl,test/core/deferred.jl("Deferred and deferred thunk" testset), andtest/tests.jlremoved — those direct CPU calls were compiled by regular Julia and bypassed the overlay, so they now would throwMethodError. The remaining coverage:test/basic.jl:665(nested_df!),test/advanced.jl:1513(f_gradient_deferred!), andtest/core/deferred.jl("Deferred upgrade" testset) — theseautodiff_deferredcalls live inside anotherautodiff-compiled function, so the EnzymeInterpreter sees the overlay and inlines the ccall version.test/cuda.jl,test/amdgpu.jl,test/metal.jl).Known follow-up
The GPU test paths currently won't see the overlay, because each GPU framework uses its own
@MethodTable(e.g.CUDA.method_table). To restore GPU coverage ofdeferred_codegen, add weakdep extensions (ext/EnzymeCUDAExt.jl, etc.) that re-emit the overlay into each framework's method table:```julia
Base.Experimental.@overlay CUDA.method_table function Enzyme.Compiler.deferred_codegen(...)
# same ccall body
end
```
🤖 Generated with Claude Code