Skip to content

Compile with GPUCompiler's do-block compile - #1184

Merged
maleadt merged 1 commit into
mainfrom
tb/compile-do-block
Nov 25, 2026
Merged

maleadt merged 1 commit into
mainfrom
tb/compile-do-block

Conversation

@maleadt

@maleadt maleadt commented Nov 25, 2026

Copy link
Copy Markdown
Member

emit_metallib now lowers and inspects the IR inside the callback of GPUCompiler's new do-block compile (JuliaGPU/GPUCompiler.jl#1124), which creates the context, keeps it active around the callback and frees the IR afterwards. The body of the old @dispose block (logging detection, the julia.metal.rng marker, the AIR downgrade and the MetalLibFunction) moved unchanged into lower_to_air, which returns only plain data; the result is still type-asserted so its uses stay statically dispatched and precompiled. The Downgrade to AIR signpost now nests inside Generate LLVM IR. GPUCompiler compat is raised to 2.24.

The reason is GPUCompiler's upcoming llvmgpu toolchain (JuliaGPU/GPUCompiler.jl#1122): with it, the IR compile returns can belong to libLLVMGPU, a newer LLVM than Julia's, and LLVM.jl can only work with that IR while its context is active (for the other back-ends, touching it outside the block segfaults). Metal's IR still comes from Julia's LLVM today, so nothing crashes yet; this prepares Metal for being compiled with libLLVMGPU like the other back-ends. Before that can be enabled, finish_ir!'s arg_eltypes annotation (which uses convert(LLVMType, ...)) and some of GPUCompiler's own Metal passes will need to build their LLVM types directly.

Validated on an M1 (macOS 27, Julia 1.12) with GPUCompiler #1122 plus the do-block form: the execution, compiler and device tests (execution capturing relocations metallib precompilation output closures closure_boxes unions kernelabstractions metalcall device) pass with the default toolchain (6492 tests) and with toolchain = "llvmgpu" (3686 + 2806; the kernelabstractions tests also launch on KernelAbstractions' CPU back-end, which needs JuliaGPU/KernelAbstractions.jl#995 under llvmgpu). Precompilation coverage is unchanged: the first kernel in a fresh session compiles in 0.25 s before and after, and --trace-compile reports the same 68 methods.

Lower and inspect the IR inside the callback of `GPUCompiler.compile(f, :llvm, job)`,
which creates the context, activates it around the callback and frees the IR afterwards,
instead of managing a `JuliaContext` and disposing of the module here.

With GPUCompiler's llvmgpu toolchain, the IR that `compile` returns can belong to
libLLVMGPU (a newer LLVM than Julia's), and LLVM.jl then needs that library's context to
be active to work with it. Metal's IR still comes from Julia's LLVM today, so nothing
breaks yet, but this prepares Metal for being compiled with libLLVMGPU like the other
back-ends. Requires GPUCompiler 2.24.
@maleadt
maleadt force-pushed the tb/compile-do-block branch from 8e59a90 to ced2f91 Compare November 25, 2026 11:31
@codecov

codecov Bot commented Nov 25, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 81.81818% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.82%. Comparing base (7cdead9) to head (ced2f91).
⚠️ Report is 7 commits behind head on main.

Files with missing lines Patch % Lines
src/compiler/compilation.jl 81.81% 4 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1184      +/-   ##
==========================================
+ Coverage   87.39%   89.82%   +2.43%     
==========================================
  Files         105      105              
  Lines       10108     9554     -554     
==========================================
- Hits         8834     8582     -252     
+ Misses       1274      972     -302     

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

@github-actions

Copy link
Copy Markdown
Contributor

Benchmark results

Compared with 7cdead9cb, the merge base.
The environment differs from the baseline's: CompilerCaching 0.5.3 → 0.5.4, GPUArrays 12.2.0 → 12.3.0, GPUCompiler 2.23.4 → 2.24.0, LLVMDowngrader_jll 0.12.2+0 → 0.12.2+1, ObjectiveC 7.2.0 → 7.2.1.

Times are the minimum time of every benchmark. A change is unusual when it exceeds both three times the benchmark's typical change between consecutive runs on the default branch and a minimum per category (performance 3%, latency 5%, applications 3%, other 3%). A benchmark that starts or stops allocating is listed however few the allocations. A single run can still be a fluke, so rerun before drawing conclusions.

Geometric mean Unusual: slower Unusual: faster Compared
Performance −1.4% 2 4 74
Latency −0.7% 0 0 4
Applications −0.4% 0 0 5
Benchmark Baseline This PR Change
Performance
array/reductions/reduce/Float32/dims=1 213 µs 183 µs −13.7% 🟢 faster
array/construct 2.33 µs 2.13 µs −8.9% 🟢 faster
array/private/iteration/scalar 1.40 ms 1.30 ms −7.6% 🟢 faster
array/reductions/reduce/Float32/dims=2 256 µs 236 µs −7.5% 🟢 faster
kernel/rand 243 µs 258 µs +5.9% 🔴 slower
array/shared/iteration/logical 362 µs 383 µs +5.8% 🔴 slower
All benchmarks
Benchmark Baseline This PR Change
Performance
array/accumulate/Float32/1d 398 µs 392 µs −1.7%
array/accumulate/Float32/dims=1 355 µs 377 µs +6.0%
array/accumulate/Float32/dims=1L 8.87 ms 8.87 ms +0.0%
array/accumulate/Float32/dims=2 438 µs 436 µs −0.5%
array/accumulate/Float32/dims=2L 2.62 ms 1.98 ms −24.5%
array/accumulate/Int64/1d 529 µs 517 µs −2.1%
array/accumulate/Int64/dims=1 517 µs 518 µs +0.3%
array/accumulate/Int64/dims=1L 1.65 ms 1.63 ms −0.9%
array/accumulate/Int64/dims=2 551 µs 554 µs +0.5%
array/accumulate/Int64/dims=2L 5.43 ms 5.43 ms +0.0%
array/broadcast 237 µs 226 µs −4.3%
array/construct 2.33 µs 2.13 µs −8.9% 🟢 faster
array/permutedims/2d 341 µs 339 µs −0.7%
array/permutedims/3d 461 µs 461 µs +0.1%
array/permutedims/4d 423 µs 422 µs −0.2%
array/private/copy 227 µs 222 µs −2.6%
array/private/copyto!/cpu_to_gpu 223 µs 216 µs −3.5%
array/private/copyto!/gpu_to_cpu 225 µs 221 µs −1.5%
array/private/copyto!/gpu_to_gpu 217 µs 219 µs +0.9%
array/private/iteration/findall/bool 353 µs 348 µs −1.3%
array/private/iteration/findall/int 417 µs 414 µs −0.8%
array/private/iteration/findfirst/bool 242 µs 238 µs −1.7%
array/private/iteration/findfirst/int 238 µs 240 µs +0.8%
array/private/iteration/findmin/1d 302 µs 302 µs −0.2%
array/private/iteration/findmin/2d 398 µs 398 µs +0.1%
array/private/iteration/logical 357 µs 370 µs +3.7%
array/private/iteration/scalar 1.40 ms 1.30 ms −7.6% 🟢 faster
array/random/rand!/Float32 423 µs 422 µs −0.3%
array/random/rand!/Int64 452 µs 435 µs −3.7%
array/random/rand/Float32 441 µs 438 µs −0.5%
array/random/rand/Int64 520 µs 519 µs −0.1%
array/random/randn!/Float32 350 µs 353 µs +1.1%
array/random/randn/Float32 370 µs 366 µs −1.1%
array/reductions/mapreduce/Float32/1d 215 µs 210 µs −2.6%
array/reductions/mapreduce/Float32/dims=1 206 µs 200 µs −2.9%
array/reductions/mapreduce/Float32/dims=1L 576 µs 580 µs +0.6%
array/reductions/mapreduce/Float32/dims=2 251 µs 247 µs −1.3%
array/reductions/mapreduce/Float32/dims=2L 416 µs 416 µs +0.1%
array/reductions/mapreduce/Int64/1d 223 µs 219 µs −2.0%
array/reductions/mapreduce/Int64/dims=1 230 µs 224 µs −2.3%
array/reductions/mapreduce/Int64/dims=1L 1.01 ms 986 µs −2.0%
array/reductions/mapreduce/Int64/dims=2 262 µs 251 µs −4.3%
array/reductions/mapreduce/Int64/dims=2L 602 µs 595 µs −1.1%
array/reductions/reduce/Float32/1d 208 µs 208 µs −0.1%
array/reductions/reduce/Float32/dims=1 213 µs 183 µs −13.7% 🟢 faster
array/reductions/reduce/Float32/dims=1L 582 µs 577 µs −0.9%
array/reductions/reduce/Float32/dims=2 256 µs 236 µs −7.5% 🟢 faster
array/reductions/reduce/Float32/dims=2L 384 µs 400 µs +3.9%
array/reductions/reduce/Int64/1d 229 µs 217 µs −5.1%
array/reductions/reduce/Int64/dims=1 226 µs 218 µs −3.5%
array/reductions/reduce/Int64/dims=1L 1.01 ms 993 µs −1.4%
array/reductions/reduce/Int64/dims=2 257 µs 256 µs −0.3%
array/reductions/reduce/Int64/dims=2L 595 µs 592 µs −0.5%
array/shared/copy 130 µs 128 µs −1.3%
array/shared/copyto!/cpu_to_gpu 35.6 µs 36.9 µs +3.5%
array/shared/copyto!/gpu_to_cpu 36.5 µs 38.0 µs +4.1%
array/shared/copyto!/gpu_to_gpu 36.7 µs 37.8 µs +3.1%
array/shared/iteration/findall/bool 356 µs 351 µs −1.5%
array/shared/iteration/findall/int 420 µs 418 µs −0.6%
array/shared/iteration/findfirst/bool 238 µs 240 µs +0.8%
array/shared/iteration/findfirst/int 245 µs 235 µs −4.1%
array/shared/iteration/findmin/1d 303 µs 298 µs −1.4%
array/shared/iteration/findmin/2d 401 µs 399 µs −0.5%
array/shared/iteration/logical 362 µs 383 µs +5.8% 🔴 slower
array/shared/iteration/scalar 87.4 ns 87.5 ns +0.0%
array/sorting/1d 1.93 ms 1.95 ms +1.2%
array/sorting/2d 8.41 ms 8.34 ms −0.8%
kernel/indexing 208 µs 184 µs −11.2%
kernel/indexing_checked 245 µs 238 µs −2.7%
kernel/launch 863 ns 854 ns −1.0%
kernel/metalcall 883 ns 904 ns +2.4%
kernel/rand 243 µs 258 µs +5.9% 🔴 slower
metal/synchronization/context 561 ns 579 ns +3.2%
metal/synchronization/stream 359 ns 360 ns +0.3%
Latency
compilation 1.77 s 1.79 s +1.4%
import 1.49 s 1.51 s +1.1%
launch 1.63 s 1.56 s −3.9%
precompile 32.3 s 31.9 s −1.3%
Applications
byval/reference 1.12 ms 1.11 ms −0.7%
byval/slices=1 1.12 ms 1.09 ms −2.8%
byval/slices=2 2.02 ms 2.03 ms +0.2%
byval/slices=3 6.82 ms 6.89 ms +1.0%
metaldevrt 392 µs 393 µs +0.4%

History: https://juliagpu.org/perf/?pkg=metal

@maleadt
maleadt merged commit 3780e7a into main Nov 25, 2026
17 checks passed
@maleadt
maleadt deleted the tb/compile-do-block branch November 25, 2026 13:10
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