← Back to Pull requests

Support programs compiled by `juliac --trim` #3888

Open
unknown opened this pull request with 570 lines diff across 7 files
View on GitHub
unknown unknown commented on Oct 31, 2026 11:25 UTC
Author

Makes Enzyme work in programs compiled ahead of time by juliac, including with --trim, with plain Reverse/Forward (no set_abi(…, InlineABI) needed). Motivated by this Discourse thread, where trimming failed with Enzyme.

The example from the thread now builds to a 3.8 MB trimmed executable on 1.12.6 (reverse over an array, reverse of a scalar Active, forward mode), and the binary does not load libEnzyme or libLLVMExtra at runtime.

A trimmed program runs without Julia's compiler, so every derivative has to be generated while juliac builds the program and linked into the code that calls it, and nothing of Enzyme's compiler may reach the image. Five independent problems stood in the way:

1. Compiler.__init__ rooted the whole compiler into the image. It registered the ~115 @cfunction rule trampolines, the handlers and the ORC JIT; __init__ methods are roots of a trimmed image, so the trim verifier saw the entire compiler and LLVM.jl (~1200 errors). The compiler now initializes on first use (ensure_compiler_init!, called at the compile entry points), through Base.OncePerProcess on 1.12+ (it resets itself when serialized) and a pid-keyed flag + __init__ before. API, JIT, FFI and BLASSupport lose their __init__ the same way.
2. FFIABI bakes a JIT address of the build process into the generated code, dead in the binary. aot_abi substitutes InlineABI when trimming() (generating_output() && JLOptions().trim != 0).
3. juliac's build process defers every package __init__ (it generates a system image: --output-o --output-incremental=no), so when the thunk generator ran, Enzyme_jll/LLVMExtra_jll had never located their libraries (libEnzyme == ""), the generator threw, and the verifier reported thunk(...)::Any. run_deferred_inits! runs those JLL __init__s (then unsets their lib_path so the image resolves the libraries where it runs) and LLVM's error handlers. LLVM.jl's and GPUCompiler's full __init__ cannot be run there: they call atexit, and the build process is already exiting. The build script also empties DEPOT_PATH before compiling, so artifact"…" fails; with_depot_path restores it around the lookup.
4. Link error undefined reference to julia.safepoint. The AOT compiler partitions the module and compiles our alwaysinline InlineABI wrapper as a standalone function in its own partition. use_gcstack_arg! had replaced its julia.get_pgcstack call with the argument, so FinalLowerGC (which on ≤1.12 only finds a pgcstack through that call in the entry block) skipped it. The wrapper's argument now carries the "gcstack" attribute (what 1.13's getPGCstack looks for), and on <1.13, when generating output, a julia.get_pgcstack call is kept in the entry block instead of using the argument.
5. The JIT disposed its target machine in an atexit hook; a process that generates output runs those
before* it compiles the output.

Also adds test/juliac.jl (builds and runs a trimmed program; ~20 s; skipped on <1.12, Windows, or without a C compiler) and an FAQ section.

Verification


  • juliac --trim=safe on 1.12.6: zero verifier errors from Enzyme; the executable runs and gives correct derivatives.

  • callconv (the InlineABI tests), precompile, sugar, juliac test sets pass on 1.12; smoke tests on 1.10 (flag path) and 1.13-rc1.

  • On 1.13.0-rc1 Enzyme's part links (the "gcstack" path), but rc1 itself fails trim on JLLWrappers' unique! → issorted dispatch and on Downloads.Curl.__init__; nothing Enzyme-side.

  • git runic --diff main clean.
  • Remaining limitations, all upstream (documented in the FAQ with workarounds)


  • The __init__ of Pkg's REPLExt (Pkg comes via LazyArtifacts, REPL via GPUCompiler) and of GPUCompiler (get(stderr, :color, false)) do not pass the trim verifier; users need a two-line shim in their program until those are fixed, the way juliac-trim-stdlib.jl shims Pkg.__init__.

  • juliac's link line lacks -lm on Linux; a derivative calling cos/exp needs JULIA_CC="cc -Wl,--no-as-needed -lm".

  • autodiff_deferred/deferred_codegen paths need a JIT and are unavailable in a trimmed program.
  • A possible follow-up: switching to InlineABI under any generating_output() (not only --trim) would likely also fix the stale-pointer @test_broken cases in test/precompile.jl; this PR keeps the change scoped to trimming.

    Diff: 570 lines (+427 / -143) across 7 files Reviewer: vchuravy Review duration: 43669m
    All Vouch review confidence checks passed
    Residual risk 0.27 = Change Risk (0.50) × (1 − Review Confidence (0.47)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.50 · 570 lines diff across 7 files
    0.50
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.02 · 1 substantive comment(s)
    0.02
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    46%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.380
    Shallow Review Depth shallow_review_depth
    0.344
    Real PR Conversation Timeline
    1 comments
    "Review active on #3888"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 46.7% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 49.8% (MEDIUM) · Higher = larger blast radius
    49.8%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 46.7% · Lower = rushed or shallow review
    46.7%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 26.5% · Safe to merge
    26.5%