From 1917eb52451e69510b006bceb7a71dea12ab9244 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 25 Nov 2026 11:36:45 +0100 Subject: [PATCH] Compile with GPUCompiler's do-block `compile` GPUCompiler 2.24 can run a callback with the IR of a compilation and dispose of it afterwards. With GPUCompiler's llvmgpu toolchain, the IR can belong to libLLVMGPU, a newer LLVM than Julia's; the do-block activates the IR's context before the callback reads the entry point's name, which crashes from a `JuliaContext` of our own. For the same reason, `add_heap!` (run from `finish_ir!`, after optimization) can no longer use Julia's code generator to lower `KernelState` and its heap field to LLVM types. Take them from the kernel's state parameter instead, and only on optimized kernels, as optimization is what adds that parameter. --- Project.toml | 2 +- src/compiler/compilation.jl | 34 ++++++++++++++++++---------------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/Project.toml b/Project.toml index 5641e891..3f79b2e7 100644 --- a/Project.toml +++ b/Project.toml @@ -41,7 +41,7 @@ Adapt = "4" CEnum = "0.5, 1" Crayons = "4" GPUArrays = "12.1" -GPUCompiler = "2.23" +GPUCompiler = "2.24" GPUToolbox = "3.5" IntelITT = "0.2" KernelAbstractions = "0.10" diff --git a/src/compiler/compilation.jl b/src/compiler/compilation.jl index 49d48bde..8e5a8479 100644 --- a/src/compiler/compilation.jl +++ b/src/compiler/compilation.jl @@ -88,8 +88,9 @@ end function GPUCompiler.finish_ir!(job::oneAPICompilerJob, mod::LLVM.Module, entry::LLVM.Function) - # Initialize the heap before SPIR-V lowering converts the state to a reference. - job.config.kernel && add_heap!(mod, entry) + # GPUCompiler adds the kernel state parameter during optimization. Initialize the heap + # before SPIR-V lowering converts the state to a reference. + job.config.kernel && job.config.optimize && add_heap!(mod, entry) entry = invoke(GPUCompiler.finish_ir!, Tuple{CompilerJob{SPIRVCompilerTarget}, typeof(mod), typeof(entry)}, @@ -100,22 +101,28 @@ end # Reserve a private arena only when device code reads the heap pointer. At this point, # GPUCompiler has threaded the state through callees as a leading by-value argument. +# +# The LLVM types are taken from that argument rather than converted from `KernelState`: +# with GPUCompiler's llvmgpu toolchain the IR can belong to libLLVMGPU, a newer LLVM than +# Julia's, whose context Julia's code generator cannot create types in. function add_heap!(mod::LLVM.Module, entry::LLVM.Function) - T_state = convert(LLVMType, KernelState) heap_field = Base.fieldindex(KernelState, :heap) - 1 - uses_heap(mod, T_state, heap_field) || return false - params = entry.parameters - if isempty(params) || params[1].value_type != T_state - error("kernel `$(entry.name)` allocates but has no kernel state to hold the heap") + T_state = isempty(params) ? nothing : params[1].value_type + if !(T_state isa LLVM.StructType && + length(T_state.elements) == fieldcount(KernelState) && + T_state.elements[heap_field+1] isa LLVM.PointerType) + error("kernel `$(entry.name)` does not take the kernel state as its first argument") end + uses_heap(mod, T_state, heap_field) || return false + state = params[1] # collect the users now, before the insertvalue below becomes one of them users = collect(state.users) - T_size = convert(LLVMType, Csize_t) + T_size = LLVM.IntType(8 * sizeof(Csize_t)) T_heap = LLVM.StructType([T_size, T_size, LLVM.ArrayType(LLVM.Int8Type(), HEAP_SIZE)]) - T_ptr = convert(LLVMType, fieldtype(KernelState, :heap)) + T_ptr = T_state.elements[heap_field+1] @dispose builder = IRBuilder() begin position!(builder, LLVM.at_begin(entry.entry)) @@ -256,13 +263,8 @@ end # run inference + LLVM codegen + SPIR-V emission. returns `(image, entry, exception_sites)`, # all session-portable so they survive precompilation when stored on a cached `CodeInstance`. function compile_to_obj(@nospecialize(job::CompilerJob)) - # TODO: on 1.9, this actually creates a context. cache those. - return JuliaContext() do ctx - asm, meta = GPUCompiler.compile(:obj, job) - # the IR belongs to us: dispose of it, or it leaks along with the context - @dispose ir = meta.ir begin - (image = asm, entry = meta.entry.name, meta.exception_sites) - end + return GPUCompiler.compile(:obj, job) do asm, meta + (image = asm, entry = meta.entry.name, meta.exception_sites) end end