Skip to content

[Draft, for CI] Hand off optimized code to a standalone LLVM, as an opt-in - #1114

Closed
maleadt wants to merge 3 commits into
mainfrom
tb/late-handoff
Closed

maleadt wants to merge 3 commits into
mainfrom
tb/late-handoff

Conversation

@maleadt

@maleadt maleadt commented Nov 18, 2026 •

Copy link
Copy Markdown
Member

GPUCompiler optimizes code with Julia's LLVM, which is old and fixed for each Julia release. This PR adds an opt-in mode, CompilerConfig(...; handoff=:late) or the handoff = "late" preference, that works as follows:

  1. Optimize with Julia's LLVM, as before.
  2. Transfer the module as bitcode to libLLVMGPU, a standalone LLVM 23 with the NVPTX, AMDGPU and SPIR-V back-ends (LLVMGPU_jll), loaded next to Julia's LLVM through LLVM.jl's library API.
  3. Optimize again with that library's standard pipeline.
  4. Run finish_ir!, validation, relocation collection, prepare_execution! and code generation with that library too, through LLVMTargetMachineEmitToMemoryBuffer instead of the per-target back-end JLLs.

Optimizer and code generator are then the same, current LLVM. This is "G1" of the plan to move the middle-end to a newer LLVM. It is experimental, and the default path is unchanged.

The library is whichever one is registered with LLVM.jl as :gpu, or else LLVMGPU_jll's if that package is loaded. The IR that compile returns belongs to that library, in a context owned by the enclosing JuliaContext block. Callers have to work with it inside context!(context(ir)): LLVM.jl routes calls on values (unlike modules) to the current library, so for example meta.entry.name outside that block calls Julia's LLVM on a libLLVMGPU value. Downstream packages need small changes for that. Only CUDA.jl's emit_ptx and OpenCL.jl's compile_to_obj needed one for the tests below.

Targets

  • PTX: supported.
  • GCN: supported with the external back-end, whose datalayout the library shares.
  • SPIR-V: supported with both back-ends. It is not optimized again, because default<O2> cannot honor SPIR-V's switch_to_lookup=false.
  • Metal and the native target: stay on Julia's LLVM.
  • Nested jobs: stay on Julia's LLVM; they are linked into their parent before the hand-off.

What the back-end JLLs did beyond llc

  • Reproduced:
    • Processors, PTX ISA versions (including each processor's minimum), AMDGPU features and SPIR-V extension names are validated before a target machine is created.
    • -nvptx-fma-level=1.
    • The AMDGPU i128:64 ABI for Julia < 1.12.
    • Each SPIR-V target machine gets exactly its job's extensions. Every --spirv-ext replaces the set, while -NAME disables an extension for good, so only +NAME lists are passed, under a lock.
    • The module is verified before code generation.
    • target-cpu/target-features are set on functions.
    • Code generation is serialized.
  • Not reproduced: recovery from fatal LLVM errors (these now end the process), catching C++ exceptions, the Windows locale workaround for SPIR-V, and the target library info.

Results of handed-off jobs aren't persisted in package images, which don't track the library.

Tests

  • New test/handoff.jl covers:
    • configuration rules;
    • ownership of the IR;
    • emit_asm after compile(:llvm);
    • code generation and validation errors for PTX, GCN and SPIR-V;
    • SPIR-V extension sequences;
    • relocations surviving the transfer.
  • Some IR checks of the existing tests are conditional on the mode, each with the reason next to it:
    • the second optimization inlines the test runtimes' empty functions;
    • LLVM 23 upgrades denormal-fp-math-f32 when reading bitcode;
    • Julia's IR printer hides its address spaces.
  • A CI job per Julia version (1.12, 1.13) runs the whole suite with the preference set. It installs the registered LLVMGPU_jll.

Validation (pegasus, Linux x86_64, with a local build of the library)

  • GPUCompiler test suite:

  • CUDA.jl on an RTX 5080: passes with the one-line emit_ptx change. The test covers vector add, saxpy, an atomic reduction, device exceptions, broadcast, mapreduce and accumulate.

  • OpenCL.jl with PoCL: the same kernels and atomics pass.

  • Compile time (median of 15, warm):

    default hand-off
    GPUCompiler test kernels, PTX 2.4–5.6 ms +13–42%
    GPUCompiler test kernels, GCN 5.5–9.9 ms +23–59%
    GPUCompiler test kernels, SPIR-V (no second optimization) 2.2–4.4 ms +7–18%
    CUDA.jl code_ptx (vadd, atomic reduce, math, shared memory) 4.9–10.0 ms +22–41%
    first broadcast and mapreduce launch 337 / 328 ms +6–9%

    Registering the library takes 30–130 ms, and initializing it 8 ms.

Stacked on #1113.

@codecov

codecov Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 26.44628% with 178 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.33%. Comparing base (8cea94b) to head (5405b6c).

Files with missing lines Patch % Lines
src/handoff.jl 17.24% 120 Missing ⚠️
src/gcn.jl 16.12% 26 Missing ⚠️
src/ptx.jl 19.04% 17 Missing ⚠️
src/spirv.jl 43.75% 9 Missing ⚠️
src/driver.jl 77.27% 5 Missing ⚠️
src/reflection.jl 50.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1114      +/-   ##
==========================================
- Coverage   92.23%   89.33%   -2.90%     
==========================================
  Files          32       33       +1     
  Lines        8292     8513     +221     
==========================================
- Hits         7648     7605      -43     
- Misses        644      908     +264     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@maleadt
maleadt force-pushed the tb/late-handoff branch 3 times, most recently from 9458498 to 3b021c6 Compare November 19, 2026 07:38
Base automatically changed from tb/terminal-ordering to main November 19, 2026 19:22
Everything that happens to the optimized IR, from `finish_ir!` to collecting relocations, moves into its own function, so that it can run with another LLVM library than optimization did.
GPUCompiler optimizes with Julia's LLVM, which is old and fixed per Julia
release. With `handoff=:late` (or the `handoff` preference), the optimized
module is transferred as bitcode to a standalone LLVM with GPU back-ends,
libLLVMGPU (LLVM 23, registered with LLVM.jl as `:gpu`, or provided by
LLVMGPU_jll), optimized again with its standard pipeline, and finalized,
validated and compiled there. Code generation then goes through the C API of
that library instead of the per-target back-end JLLs, so optimizer and code
generator are the same, current LLVM. The IR that `compile` returns belongs to
that library, in a context owned by the enclosing `JuliaContext` block.

This is supported for PTX, GCN (with the external back-end, whose datalayout
the library shares) and SPIR-V (both back-ends; not optimized again, as the
standard pipeline cannot honor its restrictions). Metal and the native target
keep using Julia's LLVM, as do nested jobs, which are linked into their parent
before the hand-off.

What the back-end JLLs did beyond `llc` is reproduced: processors, PTX
versions, AMDGPU features and SPIR-V extensions are validated up front,
`-nvptx-fma-level=1`, the AMDGPU `i128:64` ABI for Julia < 1.12, per-target
SPIR-V extensions, verification before code generation, function CPU and
feature attributes, and serialized code generation. Not reproduced: recovery
from fatal LLVM errors (which now end the process), catching C++ exceptions,
the Windows locale workaround for SPIR-V, and the target library info.

Results of handed-off jobs are not persisted in package images, which don't
track the library.
A job per Julia version runs the test suite with the `handoff` preference set,
so that PTX, GCN and SPIR-V go through libLLVMGPU from LLVMGPU_jll.
@maleadt

maleadt commented Nov 24, 2026

Copy link
Copy Markdown
Member Author

Superseded by #1122, which combines JuliaIR's passes and the hand-off to libLLVMGPU behind a single toolchain preference, and hands off before optimization.

@maleadt maleadt closed this Nov 24, 2026
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