Skip to content

Take Julia's LLVM passes from JuliaIR.jl, as an opt-in - #1026

Closed
maleadt wants to merge 24 commits into
mainfrom
tb/juliair-ports
Closed

maleadt wants to merge 24 commits into
mainfrom
tb/juliair-ports

Conversation

@maleadt

@maleadt maleadt commented Oct 19, 2026 •

Copy link
Copy Markdown
Member

GPUCompiler's optimization pipeline runs fifteen of the LLVM passes of Julia's code
generator (AllocOpt, LateLowerGCFrame, PropagateJuliaAddrspaces, ...), which only exist
inside Julia's own libLLVM. This lets a compilation take them from
JuliaIR.jl instead, which ports them to Julia, as
the first step towards running GPUCompiler's middle end on a newer LLVM than Julia's.
Nothing changes by default.

  • CompilerConfig gets a julia_passes field, :cpp (Julia's passes, as today) or :ports.
    Its default comes from the julia_passes preference. It is part of the configuration, so
    cached artifacts are keyed on it; inference caches are not.
  • JuliaIR.jl is a dependency (it depends only on LLVM.jl). The preference only selects the
    provider, also in precompilation workloads, so what a package precompiles is used by a
    session with the same preference.
  • The pipeline in optim.jl gets each Julia pass from julia_pass(job, :Name).
  • The ports need opaque pointers. On Julia 1.10 and 1.11, reflection then compiles in an
    opaque-pointer context, JuliaContext() creates one when the preference selects the
    ports, and compiling a :ports job in a typed context is an error.
  • The cached bitcode of runtime functions records whether it uses opaque pointers, and is
    recompiled when read in the other kind of context (on 1.10 and 1.11, reading opaque
    bitcode in a typed context failed; a pre-existing issue that mixing the modes exposes).
  • classify_arguments checks that the number of parameters matches the signature, which a
    Type{T} argument not keyed as Core.TypeEgal would silently break.
  • On Julia 1.15, native jobs that use Julia's runtime run CancellationLowering before GC
    lowering, as Julia does (cancellation: Wire up compute cancellation JuliaLang/julia#62604). Julia 1.14 reverted the cancellation
    work before its first beta ([1.14] Revert task cancellation (moving to 1.15) JuliaLang/julia#64651), so it is not run there.

Load time and time to first code_native (native target, a small kernel), Julia 1.13.4 on
x86_64 Linux, median of 10 fresh processes on a shared machine:

using GPUCompiler first code_native
cpp, JuliaIR.jl as extension 0.29 s 1.9 s
cpp, JuliaIR.jl as dependency 0.28 s 2.1 s
ports, JuliaIR.jl as extension 0.28 s 12.8 s
ports, JuliaIR.jl as dependency 0.40 s 1.9 s

With the C++ passes, both are within noise. With the ports, GPUCompiler's own workload
now compiles through them, so their code is in the package image: loading takes 0.1 s
longer, and the first compilation no longer spends 11 s compiling the ports.

Tests: test/julia_passes.jl covers the configuration, the providers, the requirements, a
precompilation workload under the preference, that the ports actually run (JuliaIR.jl's
ledger), and a differential: a corpus of kernels compiled for PTX, GCN, SPIR-V, Metal,
native and BPF with both providers must give identical IR. It compares IR without the
names of values and blocks, because LLVM's per-function counter for unique names advances
differently when a port names its temporaries differently, and that shows only in the
suffixes later passes pick. On Julia before 1.14, the C++ FinalLowerGC zeroes GC frames
with a volatile memset (it passes the TBAA tag as the volatility flag; fixed in
JuliaLang/julia#62061). The ports zero them as 1.14 does, so such IR is compared after
making that memset non-volatile and running dead-store elimination on both sides. The
C++ FinalLowerGC also builds the two halves of a frame push as arguments of one call,
whose order depends on the compiler that built Julia (it differs on aarch64 Linux), so
that independent pair is put in one order on both sides. A failing comparison prints a
line diff of the two modules. On
1.10 and 1.11 the test also compares typed with opaque pointers using the C++ passes
(unless Julia itself runs with --opaque-pointers). With the preference set to the ports,
the existing precompilation tests check that what a workload caches is found again.

CI

GitHub Actions tests both pass providers (cpp and ports). Each job sets up the test
environment with the selected preference and runs the suite with the flags and coverage
reporting of julia-runtest; ports jobs also check that the preference was applied and
that JuliaIR's ledger records the core passes.

Buildkite runs the back-end suites with their normal provider and in a separate ports
environment. Each ports step requires ledger evidence that the passes ran. Back-end steps
remain skipped on this draft unless selected by a commit tag: [only ports],
[only cuda] (etc.), or [only cuda, amdgpu, metal, oneapi, opencl] for both variants of
all five back-ends.

JuliaIR.jl version

This requires JuliaIR.jl 0.1.4, which follows changes to Julia's passes since 0.1.0: the GC
stack's thread-local storage on macOS (JuliaLang/julia#63921), RemoveJuliaAddrspaces moving
function bodies (JuliaLang/julia#65016, done like Julia 1.15 since 0.1.3), and modules with
inline assembly in RemoveJuliaAddrspaces (the Julia master failure, fixed in 0.1.2), and
AllocOpt's preserve regions and debug locations after debug intrinsics (0.1.4), among
others.

Known failure not caused by this PR: Julia 1.13 on aarch64 Linux can crash in Julia's own
LateLowerGCFrame while compiling JuliaIR.jl's code (JuliaLang/julia#62899, fixed by
JuliaLang/julia#62933 in 1.14 and backported for the next 1.13 release, not to 1.12).

@maleadt
maleadt force-pushed the tb/juliair-ports branch 2 times, most recently from ca56bc2 to b004fbc Compare October 21, 2026 09:53
@maleadt
maleadt force-pushed the tb/juliair-ports branch 2 times, most recently from 38b51b7 to 36731e6 Compare October 28, 2026 19:46
@maleadt
maleadt force-pushed the tb/juliair-ports branch 5 times, most recently from 4f34206 to 2a60c9e Compare November 18, 2026 22:33
@maleadt maleadt changed the title [Draft, for CI] Take Julia's LLVM passes from JuliaIR.jl, as an opt-in Take Julia's LLVM passes from JuliaIR.jl, as an opt-in Nov 19, 2026
@maleadt
maleadt marked this pull request as ready for review November 19, 2026 05:46
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 96.66667% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.06%. Comparing base (ead724c) to head (864f604).

Files with missing lines Patch % Lines
src/driver.jl 75.00% 1 Missing ⚠️
src/optim.jl 96.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1026      +/-   ##
==========================================
+ Coverage   93.02%   93.06%   +0.03%     
==========================================
  Files          32       33       +1     
  Lines        8302     8332      +30     
==========================================
+ Hits         7723     7754      +31     
+ Misses        579      578       -1     

☔ 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.

`CompilerConfig` gains `julia_passes`: `:cpp` for the passes of Julia's
code generator (wrapped by `LLVM.Interop`), `:ports` for their ports in
JuliaIR.jl. The default comes from the `julia_passes` preference. Being a
config field, it is part of the identity of every cached LLVM artifact,
but not of the inference caches.

JuliaIR.jl is a weak dependency: its extension tells GPUCompiler how to
register the ports and which of Julia's passes they replace. Loading it
alone changes nothing; selecting `:ports` without it is an error before
compilation starts. Until JuliaIR.jl is registered, the test environment takes
it from `[sources]`.
`optimize!` now asks `julia_pass` for each of Julia's passes, which
returns the C++ pass or, with `julia_passes=:ports`, the name of the
port, registered on the pass builder. Passes JuliaIR.jl does not port
yet (FinalLowerGC, JuliaLICM, LowerPTLS, RemoveJuliaAddrspaces) run as
the C++ pass in both modes; the extension lists them explicitly.
…sses

JuliaIR.jl's passes only support opaque pointers, while Julia 1.10 and
1.11 use typed pointers by default. With `julia_passes=:ports`, the
reflection entry points create opaque contexts, and `JuliaContext()`
does so too when the preference selects the ports, so that the many
callers that create their own context follow the preference. Compiling
a `:ports` job in a typed context is an error. Without the preference,
`JuliaContext()` keeps LLVM's default.

The precompilation workload pins the C++ passes, as JuliaIR.jl is not
loaded while GPUCompiler precompiles.
On Julia 1.10 and 1.11, contexts may use typed or opaque pointers, and
compiling with the ports of Julia's passes makes opaque ones common
there. The bitcode of runtime functions is cached per configuration, so a
function compiled in one kind of context was read back in the other,
which fails for opaque bitcode in a typed context. Recompile it instead.
Running the suite with the `julia_passes` preference set to "ports" needs
JuliaIR.jl loaded, also in the processes some tests spawn. The test of
Metal's lowering of atomics on thread-private objects pins the C++
passes: JuliaIR.jl's AllocOpt uses the escape analysis of Julia 1.13 on
every version, which removes the object, so the IR it lowers would not
occur.
Covers the configuration, the providers and the passes left to C++, the
requirements (JuliaIR.jl loaded, opaque pointers), and, through
JuliaIR.jl's ledger, that the ports run. A differential compiles a corpus
of kernels (allocations, `@simd` loops, Float16, exceptions, Int128 and
aggregate arguments, closures) for every target with both providers in
opaque-pointer contexts and requires identical IR, modulo name counters.

On Julia 1.10 and 1.11, a second set judges the switch from typed to
opaque pointers that the ports need, with the C++ passes: PTX code is
identical, other targets compile or fail alike.
`classify_arguments` treats `Type{T}` arguments as ghosts, which matches
codegen only when the signature keys them as `Core.TypeEgal`, as
GPUCompiler's `methodinstance` does. A job built from another
MethodInstance could get a boxed `Type{T}` parameter, misaligning every
later argument without an error. Before optimization the number of
parameters is known, so check it.
With the preference selecting the ports, precompilation workloads of
packages that don't depend on JuliaIR.jl (GPUCompiler's own, or a
back-end's) would fail, as they compile in a process without it. There,
configurations now default to the C++ passes. That is part of the
configuration, so whatever such a workload caches is not used for
configurations selecting the ports.
Explains the `julia_passes` setting and its preference, what the ports require, and how to run the test suite with them.
With `--opaque-pointers` on Julia 1.10 and 1.11, Julia's code generator does not
support typed-pointer contexts, so the code generated in one differs and the
comparison of typed with opaque pointers fails. Julia then never generates typed IR,
so there is nothing to compare.
Packages building on GPUCompiler do not load JuliaIR.jl, so with the julia_passes
preference set to "ports", their compilations failed for want of the ports unless the
user loaded it. When the preference selects the ports, GPUCompiler now loads JuliaIR.jl
at initialization if the environment provides it (not while precompiling), and the
extension registers the ports once GPUCompiler has loaded. This also makes the
preference usable in a package's test suite, by adding JuliaIR.jl to its test
environment.
JuliaIR.jl now ports RemoveJuliaAddrspaces, FinalLowerGC, LowerPTLS and JuliaLICM too, so
with julia_passes=:ports every one of Julia's passes in the pipeline is a port and none
falls back to the C++ pass. The ledger test now also expects JuliaLICM and
RemoveJuliaAddrspaces, and FinalLowerGC and LowerPTLS for a target that uses Julia's
runtime.

[only cuda oneapi amdgpu metal opencl]
Both test/native/precompile.jl and the test of precompilation without JuliaIR.jl
created a package named NativeCompiler. When one ran after the other on the same
worker, the cache of the second test's package depended on a NativeCompiler other
than the loaded one, so it was loaded from source in the test process: the workload
then reported the ports, and native/precompile found results and inferred code that
should only exist in the precompilation process.
On Julia 1.10 and 1.11 on AArch64, where Julia aligns Int128 to 16 bytes, the PTX
of the aggregate kernel taking its argument by reference differs between typed and
opaque pointers: with opaque pointers the argument's padding is copied too. Only the
padding bytes of the stored value differ, so there only require the code to be
generated.
Julia's LLVM is built without the AMDGPU target on macOS, so GCNCompilerTarget has no
datalayout there and Julia emits allocas in addrspace(0). Since LLVM 23 the verifier
rejects those for an amdgcn triple, aborting the julia_passes test on macOS with Julia
nightly. The GCN test suite itself is skipped on macOS already.
On Julia master (1.15), `Base.@cancel_check` emits `julia.cancellation_point()`
calls, which Julia lowers with CancellationLowering right before LateLowerGCFrame
(JuliaLang/julia#62604). GPUCompiler never ran that pass, so native jobs using
Julia's runtime kept the calls unlowered. Run it there, as the port with
`julia_passes=:ports`, or else by its pipeline name since LLVM.jl has no
constructor for it. Other targets have no task to deliver a cancellation to.

Julia 1.14 reverted the cancellation work before its first beta
(JuliaLang/julia#64651), so it has neither the intrinsic nor the pass.
Resolve JuliaIR through General in the test and downstream environments.
Keep both provider variants and their ledger checks in CI. The tests run with the
flags and the coverage reporting of julia-runtest, which the separate set-up step
replaces.

[only cuda, amdgpu, metal, oneapi, opencl]
With JuliaIR.jl as a weak dependency, precompilation workloads ran without it and
compiled with the C++ passes, so a session that selects the ports recompiled what
packages had precompiled (Metal.jl's precompilation test counted the recompilations),
and loading it at initialization when the preference selected the ports deadlocked once
a package image depended on it. As a regular dependency the ports are always available:
the `julia_passes` preference only selects them, also in workloads, so that what they
cache matches the configurations of a session with the same preference.
Julia 1.10 to 1.13 zero a GC frame with a volatile memset, as their FinalLowerGC passes
the frame's TBAA tag as the volatility flag (fixed in JuliaLang/julia#62061). The ports
zero it as Julia 1.14 does, so dead-store elimination later drops the zeroing of the
header slots, which pushing the frame overwrites. That showed in the differential test on
Julia 1.10 with `--check-bounds=yes`, where the BoundsError constructor of a `@simd`
loop's range keeps a frame. Compare such IR after making the C++ memset non-volatile and
running dead-store elimination and InstCombine on both sides.
The logger truncates the two modules, which left failures on CI hosts
that can't be reproduced locally without the difference.
FinalLowerGC builds the load of the previous frame and the address of the frame's slot
for it as two arguments of one C++ call, so their order depends on the compiler that
built Julia: Julia's aarch64 Linux builds load first, the ports and the x86_64 builds
compute the address first. Before comparing again, put the address first wherever such
an independent pair feeds the store that links the frame.
The IR can live in another LLVM library than Julia's (with the late hand-off to
libLLVMGPU), and LLVM.jl sends calls on its values to the library of the active
context.
The test picks opaque-pointer IR based on `typed_ptrs`, which describes a
`JuliaContext`, but parsed it in a plain `Context`. With the ports on Julia 1.11
the two differ: Julia contexts use opaque pointers, plain ones typed pointers.
JuliaIR.jl 0.1.0 predates changes to Julia's passes that its ports have to follow:
LowerPTLS reads the GC stack from the wrong thread register on AArch64 macOS since
JuliaLang/julia#63921, and RemoveJuliaAddrspaces moves function bodies since
JuliaLang/julia#65016. 0.1.1 follows both; 0.1.2 fixes RemoveJuliaAddrspaces on
modules with inline assembly (the Julia master job's failure), 0.1.3 moves
function bodies the way Julia 1.15 does, and 0.1.4 follows the C++ more closely
in AllocOpt's preserve regions and in debug locations after debug intrinsics.
@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