Repository navigation
Conversation
This was referenced Nov 24, 2026
maleadt
force-pushed
the
tb/llvmgpu-toolchain
branch
from
November 24, 2026 15:18
728258d to
30f38ce
Compare
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.
This was referenced Nov 25, 2026
maleadt
force-pushed
the
tb/llvmgpu-toolchain
branch
from
November 25, 2026 15:26
30f38ce to
04045e8
Compare
maleadt
force-pushed
the
tb/llvmgpu-toolchain
branch
from
November 26, 2026 13:14
04045e8 to
b1813eb
Compare
maleadt
force-pushed
the
tb/llvmgpu-toolchain
branch
from
November 27, 2026 09:48
b1813eb to
3be29ad
Compare
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
force-pushed
the
tb/llvmgpu-toolchain
branch
from
December 2, 2026 19:54
3be29ad to
efb5e81
Compare
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.
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
toolchainpreference that switches between that default ("julia") and"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
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.GPUCompiler.llvm_library(target). It defaults to Julia's LLVM, so targets defined elsewhere (Enzyme'sEnzymeTarget, CPU targets like StaticCompiler's) and the host target keep their behavior; GPUCompiler's GPU targets (PTX, GCN, SPIR-V, Metal, BPF) returnGPUCompiler.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.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.backend=:external(the default under the preference); PTX:nvptxand SPIR-V:llvmlikewise 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.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
compilereturns 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 ofcompilefrom #1124, which runs its block in the IR's context and works the same with either toolchain:JuliaContext() do ... endcreates 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 callscompile(target, job)inside such a block has to work with the IR insideLLVM.context!(LLVM.context(ir)) do ... end. Hooks that run during or after optimization (optimize_module!,finish_ir!, andlink_libraries!when optimization calls it again) already run in that context, andconvert(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:
RemoveNIpass 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_machinereturnsnothingwhen 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), ornothingfor libNVVM, the Khronos translator and Metal. With llvmgpu, it is libLLVMGPU's version, also when called before the module moved (as AMDGPU.jl does fromfinish_module!). AMDGPU.jl'sdenormal_metadata_nameand CUDA.jl's PTX ISA support check can use it instead ofpkgversion.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'scoretests 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
isprocessorand its throwingclopts. GPUCompiler still only accepts an LLVM 23 build of libLLVMGPU, because it relies on the command-line options of that version.+NAME,NAMEorall): LLVM keeps an extension that--spirv-ext=-NAMEdisables disabled for the rest of the process.allcurrently fails SPIR-V validation (anFPFastMathDefaultexecution mode without the FloatControls2 capability).