Skip to content

Add an llvmgpu toolchain, optimizing with a newer LLVM - #1122

Open
maleadt wants to merge 6 commits into
mainfrom
tb/llvmgpu-toolchain
Open

maleadt wants to merge 6 commits into
mainfrom
tb/llvmgpu-toolchain

Conversation

@maleadt

@maleadt maleadt commented Nov 24, 2026 •

Copy link
Copy Markdown
Member

GPUCompiler optimizes code with Julia's LLVM, which is old and fixed for each Julia release, so improvements to LLVM's optimizer reach GPU code a year or more late. GPU code generation also depends on Julia's LLVM keeping back-ends (NVPTX, AMDGPU, BPF) that Julia itself doesn't need.

This adds a toolchain preference that switches between that default ("julia") and "llvmgpu":

using Preferences, GPUCompiler
set_preferences!(GPUCompiler, "toolchain" => "llvmgpu")

The preference is read when GPUCompiler is compiled, so changing it recompiles GPUCompiler and its dependents, and caches never mix code of both toolchains.

What the llvmgpu toolchain does

  • Julia's code generator still emits IR into Julia's LLVM. Once the module of a top-level job is linked and post-processed (finish_linked_module!, relocation baking), it moves as bitcode to libLLVMGPU, a standalone LLVM 23 from LLVMGPU_jll with the NVPTX, AMDGPU, SPIR-V and BPF back-ends. Optimization, target finalization (finish_ir!), validation and code generation all happen there. Nested jobs are linked into their parent before it moves.
  • Which library optimizes a target's code is a new public method, GPUCompiler.llvm_library(target). It defaults to Julia's LLVM, so targets defined elsewhere (Enzyme's EnzymeTarget, CPU targets like StaticCompiler's) and the host target keep their behavior; GPUCompiler's GPU targets (PTX, GCN, SPIR-V, Metal, BPF) return GPUCompiler.toolchain_library(), which is libLLVMGPU under the preference, and other targets can opt in the same way. It is not derived from the back-ends libLLVMGPU happens to have.
  • Passes follow the module's library: the pipeline uses the pass constructors of JuliaIR.jl 0.2 (JuliaIR.AllocOptPass() etc.), which return Julia's C++ passes when constructed in Julia's LLVM, and JuliaIR's ports of them in libLLVMGPU. Contexts of Julia's LLVM therefore keep using typed pointers on Julia 1.10 and 1.11; the bitcode reader upgrades them when a module moves.
  • libLLVMGPU's back-ends generate the code for PTX, GCN, SPIR-V and BPF, replacing the per-target back-end JLLs. GCN targets only support backend=:external (the default under the preference); PTX :nvptx and SPIR-V :llvm likewise mean libLLVMGPU. Like the back-end libraries did, unknown processors, features and too-old PTX ISA versions are rejected up front; the minimum PTX version per processor is asked from the back-end itself.
  • The alternative code generators get code optimized by libLLVMGPU too: the Khronos SPIR-V translator (backend=:khronos, built against LLVM 23) reads its bitcode as is, libNVVM (backend=:nvvm), which reads LLVM 22 bitcode at most, gets it downgraded with LLVMDowngrader_jll to the newest format that is at most 22 (22 with LLVMDowngrader_jll 0.13, 20 with older ones), and Metal code is downgraded to AIR as before.

Working with the returned IR

With llvmgpu, the IR that compile returns for jobs that moved (meta.ir, meta.entry, or the module for :llvm) belongs to libLLVMGPU, and LLVM.jl only works with it in that library's context. The recommended way to use it is the do-block form of compile from #1124, which runs its block in the IR's context and works the same with either toolchain:

entry = GPUCompiler.compile(:obj, job) do obj, meta
    meta.entry.name
end

JuliaContext() do ... end creates a context of libLLVMGPU next to the one of Julia's LLVM, which owns the IR of the jobs that move, and frees both at the end of the block. Code that calls compile(target, job) inside such a block has to work with the IR inside LLVM.context!(LLVM.context(ir)) do ... end. Hooks that run during or after optimization (optimize_module!, finish_ir!, and link_libraries! when optimization calls it again) already run in that context, and convert(LLVMType, T) lowers Julia types there (since LLVM.jl 10.8, it works in a context of any LLVM library). The downstream packages moved to the do-block form in JuliaGPU/CUDA.jl#3627, JuliaGPU/AMDGPU.jl#1250, JuliaGPU/oneAPI.jl#780, JuliaGPU/OpenCL.jl#631, JuliaGPU/Metal.jl#1184 and JuliaGPU/KernelAbstractions.jl#995.

Other changes

These also affect the default toolchain:

  • Julia's RemoveNI pass removes the first non-integral address space component of the datalayout, which for GCN was AMDGPU's own rather than Julia's. Julia's now come first.
  • llvm_machine returns nothing when the LLVM library lacks the target, as documented, instead of throwing; BPF compilation failed during IR generation with a Julia whose LLVM lacks that back-end.
  • GPUCompiler.backend_llvm_version(target) is now generic and public: the version of the LLVM that generates the target's machine code, asked from the back-end libraries (<prefix>GetLLVMVersion) rather than derived from package versions, which will stop matching LLVM's (Pin AMDGPU_LLVM_Backend_jll, ask the back-end for its LLVM version AMDGPU.jl#1247), or nothing for libNVVM, the Khronos translator and Metal. With llvmgpu, it is libLLVMGPU's version, also when called before the module moved (as AMDGPU.jl does from finish_module!). AMDGPU.jl's denormal_metadata_name and CUDA.jl's PTX ISA support check can use it instead of pkgversion.

CI

GitHub Actions runs the suite with llvmgpu on x86_64 Linux for every Julia version, on aarch64 Linux for 1.11 (Julia 1.12 and 1.13 crash on JuliaIR's code there, JuliaLang/julia#62899), and on macOS and Windows for 1.12. Buildkite runs every downstream package with it too, in an "(llvmgpu)" step of its own ([only llvmgpu] and [skip llvmgpu] select those).

Validation

On x86_64 Linux (LLVM.jl 10.8.1, LLVMGPU_jll 1.5.0, JuliaIR.jl 0.2.0), GPUCompiler's test suite passes with both toolchains on Julia 1.10, 1.12 and nightly, and with llvmgpu on 1.11 and 1.13 (with llvmgpu: 12,905 tests on 1.10, 56,412 on 1.11, 56,302 on 1.12, 56,577 on 1.13, 56,628 on nightly). With llvmgpu, that includes GCN, BPF, Metal, SPIR-V with :khronos, and the NVVM tests through the downgrade. Before this revision's changes to the move decision and contexts, CUDA.jl's core tests on an RTX 5080, OpenCL.jl's suite on PoCL and KernelAbstractions.jl's POCL suite passed with llvmgpu as well.

Known differences and gaps

  • LLVM 23 enables runtime loop unrolling for AMDGPU ([AMDGPU] Enable runtime loop unrolling llvm/llvm-project#194924) while its cost model underestimates integer division, so GCN loops with a division can grow considerably. Clang has the same behavior, and so will the default toolchain once Julia moves to LLVM 23.
  • Not reproduced from the per-target back-end JLLs: recovery from fatal LLVM errors (these end the process, so GPUCompiler validates targets up front), and the Windows locale workaround for SPIR-V.
  • This PR requires LLVM.jl 10.8.1, LLVMGPU_jll 1.5 and JuliaIR.jl 0.2. Processors and SPIR-V extensions are validated by the back-ends themselves, through LLVM.jl's isprocessor and its throwing clopts. GPUCompiler still only accepts an LLVM 23 build of libLLVMGPU, because it relies on the command-line options of that version.
  • With libLLVMGPU, SPIR-V extensions can only be enabled (+NAME, NAME or all): LLVM keeps an extension that --spirv-ext=-NAME disables disabled for the rest of the process. all currently fails SPIR-V validation (an FPFastMathDefault execution mode without the FloatControls2 capability).
  • Compile time is somewhat higher than with the default toolchain, mostly because JuliaIR's passes run in Julia. Warm compiles to LLVM IR (Julia 1.12, median of 60, ms): PTX 1.66 vs 1.33 for a small kernel and 4.92 vs 4.17 for a larger one (6.93 vs 6.42 to PTX assembly), SPIR-V 1.39 vs 0.88 and 3.73 vs 2.95. The first kernel takes about 0.5 s longer.

@maleadt
maleadt force-pushed the tb/llvmgpu-toolchain branch from 728258d to 30f38ce Compare November 24, 2026 15:18
maleadt added a commit that referenced this pull request Nov 25, 2026
compile(f, target, job) compiles in a JuliaContext of its own, runs f
with the result, and frees the IR and its context afterwards. Most
callers create a context only to read a few values from the IR, e.g. the
entry point's name, and then dispose of it; this does that for them.
The llvmgpu toolchain (#1122) needs it: there, the IR can belong to
another LLVM library than the context the caller created, and reading it
from the wrong one crashes.
@maleadt
maleadt force-pushed the tb/llvmgpu-toolchain branch from 30f38ce to 04045e8 Compare November 25, 2026 15:26
@maleadt
maleadt force-pushed the tb/llvmgpu-toolchain branch from 04045e8 to b1813eb Compare November 26, 2026 13:14
@maleadt
maleadt force-pushed the tb/llvmgpu-toolchain branch from b1813eb to 3be29ad Compare November 27, 2026 09:48
GPUCompiler adds Julia's non-integral address spaces to the target's datalayout, and
removes them again after optimization with Julia's RemoveNI pass. That pass removes the
first `ni` component of the layout, which for targets with non-integral address spaces of
their own, like AMDGPU, was the target's: those were lost before code generation, and
Julia's kept. Julia's now come first.
`emit_llvm` generates the IR, links what it needs, and then optimizes, finalizes and
validates it. Moving the second half into `finish_llvm!` gives that part a single entry
point, so that it can run in another LLVM context than the first.
`llvm_machine` is documented to return `nothing` for targets the LLVM library doesn't
support, but the default method and BPF's threw instead, so compiling for BPF failed with
a Julia whose LLVM lacks that back-end, already during IR generation. Look the target up
without throwing, like the PTX and GCN methods check for their back-ends.
`backend_llvm_version` derived the version of the external GCN back-end's LLVM from the
version of AMDGPU_LLVM_Backend_jll, which will stop matching LLVM's once that package is
versioned independently (JuliaGPU/AMDGPU.jl#1247). The back-end libraries report it
through `<prefix>GetLLVMVersion`, every build of them since they became libraries.

Make the query generic and public, so that downstream packages can ask it instead of
looking at package versions (e.g. CUDA.jl for the PTX ISA versions the back-end supports):
it returns the version of the LLVM that generates the target's machine code, from
NVPTX_LLVM_Backend_jll, SPIRV_LLVM_Backend_jll or AMDGPU_LLVM_Backend_jll, or `nothing`
when that is not LLVM (libNVVM, the Khronos translator, Apple's Metal compiler).
GPUCompiler optimizes code with Julia's LLVM, which is old and fixed for each Julia
release, so improvements to LLVM's optimizer reach GPU code a year or more late, and GPU
code depends on Julia's LLVM keeping back-ends that Julia itself doesn't need.

The `toolchain` preference now selects between that default (`"julia"`) and `"llvmgpu"`,
which moves the linked and post-processed module of every top-level job for a GPU target
as bitcode to libLLVMGPU, a standalone LLVM 23 from LLVMGPU_jll. Optimization,
target finalization, validation and code generation then happen there: libLLVMGPU's
back-ends generate PTX, GCN, SPIR-V and BPF code, libNVVM gets the optimized code
downgraded to the newest bitcode it reads, the Khronos translator gets it as is, and
Metal code is downgraded to AIR as before. Julia's code generator keeps emitting into
Julia's LLVM.

The library that optimizes a target's code is `llvm_library(target)`. It defaults to
Julia's LLVM, so the host target and targets defined elsewhere (Enzyme's, CPU targets)
keep their behavior; GPUCompiler's PTX, GCN, SPIR-V, Metal and BPF targets return
`toolchain_library()`, libLLVMGPU under the preference, which other targets can opt into.
Passes follow the module's library: Julia's C++ passes in Julia's LLVM, JuliaIR.jl's
ports of them in libLLVMGPU, so contexts of Julia's LLVM keep using typed pointers on
Julia 1.10 and 1.11. `backend_llvm_version` defaults to the version of that library, and
GCN targets only support `backend=:external` under the preference.

The preference is read when GPUCompiler is compiled, so changing it recompiles GPUCompiler
and the packages that depend on it, and caches never mix code of both toolchains.

With llvmgpu, `JuliaContext() do ... end` also creates a context of libLLVMGPU, which
owns the IR of the jobs that move. The do-block form of `compile` runs its block in the
context of the IR; other callers have to work with it inside `context!(context(ir))`, as
LLVM.jl passes values to the current library. Hooks that run during or after
optimization run in that context too, including `link_libraries!` when optimization calls
it again, and `convert(LLVMType, T)` lowers Julia types in it (since LLVM.jl 10.8).

libNVVM reads bitcode of LLVM 22 at most, so newer bitcode is downgraded to the newest
format LLVMDowngrader_jll emits up to that (LLVM 22 with 0.13, 20 before), which also
covers a future Julia on LLVM 23 with the default toolchain.
GitHub Actions runs the test suite with the llvmgpu toolchain on x86_64 Linux for every
Julia version, on aarch64 Linux for Julia 1.11, and on macOS and Windows for Julia 1.12.
Julia 1.12 and 1.13 on aarch64 Linux are left out, as Julia's GC lowering crashes on
JuliaIR.jl's code there (JuliaLang/julia#62899).

Buildkite tests every downstream package with it as well, in a step of its own. The steps
of CUDA.jl, AMDGPU.jl, oneAPI.jl and OpenCL.jl may fail until those packages work with
the IR in libLLVMGPU's context, using the do-block form of `compile`.
@maleadt
maleadt force-pushed the tb/llvmgpu-toolchain branch from 3be29ad to efb5e81 Compare December 2, 2026 19:54
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