From dfb34a0f4cafbd417710d60d2ab84a4cb3753382 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Fri, 27 Nov 2026 14:29:09 +0100 Subject: [PATCH] Request the RNG initialization with `GPUCompiler.deferred_codegen!`. Instead of registering the job in `deferred_codegen_jobs` under `length + 1`, which races with concurrent compilations, and emitting the marker by hand. This needs GPUCompiler 2.25. --- Project.toml | 2 +- src/pocl/compiler/compilation.jl | 25 +++++-------------------- 2 files changed, 6 insertions(+), 21 deletions(-) diff --git a/Project.toml b/Project.toml index 76fa02930..ae786ed44 100644 --- a/Project.toml +++ b/Project.toml @@ -53,7 +53,7 @@ AMDGPU = "2" Adapt = "0.4, 1.0, 2.0, 3.0, 4" Atomix = "1.5" EnzymeCore = "0.8.25" -GPUCompiler = "2.24" +GPUCompiler = "2.25" GPUToolbox = "3.6" KernelInterface = "0.4.7" LLVM = "10" diff --git a/src/pocl/compiler/compilation.jl b/src/pocl/compiler/compilation.jl index 36bab0662..a587fd23a 100644 --- a/src/pocl/compiler/compilation.jl +++ b/src/pocl/compiler/compilation.jl @@ -102,11 +102,10 @@ function GPUCompiler.finish_module!( ft, tt, job.world; method_table_view = GPUCompiler.method_table_view(job) ) cfg = CompilerConfig(job.config; kernel = false, name = nothing) - job = CompilerJob(src, cfg, job.world) - id = length(GPUCompiler.deferred_codegen_jobs) + 1 - GPUCompiler.deferred_codegen_jobs[id] = job + rng_job = CompilerJob(src, cfg, job.world) - # generate IR for calls to `deferred_codegen` and the resulting function pointer + # call `initialize_rng_state()` in a new entry block. it returns nothing: inferring + # its return type natively would leave host code for it in sysimages. top_bb = entry.entry bb = BasicBlock(LLVM.before(top_bb), "initialize_rng") @dispose builder = IRBuilder() begin @@ -117,29 +116,15 @@ function GPUCompiler.finish_module!( builder.debug_location = loc end - # call the `deferred_codegen` marker function - # (declared like GPUCompiler's `ccall("extern deferred_codegen", llvmcall, Ptr{Cvoid}, ...)`) - T_ptr = convert(LLVMType, Ptr{Cvoid}) - T_id = convert(LLVMType, Int) - deferred_codegen_ft = LLVM.FunctionType(T_ptr, [T_id]) - deferred_codegen = get!(mod.functions, "deferred_codegen") do - LLVM.Function(mod, "deferred_codegen", deferred_codegen_ft) - end - fptr = call!(builder, deferred_codegen_ft, deferred_codegen, [ConstantInt(id)]) - - # call the `initialize_rng_state` function, which returns nothing. inferring its - # return type natively would leave host code for it in sysimages. llvm_ft = LLVM.FunctionType(LLVM.VoidType()) - fptr = inttoptr!(builder, fptr, LLVM.PointerType(llvm_ft)) + fptr = GPUCompiler.deferred_codegen!(builder, rng_job) + fptr = pointercast!(builder, fptr, LLVM.PointerType(llvm_ft)) call!(builder, llvm_ft, fptr) br!(builder, top_bb) # note the use of the device-side RNG in this kernel push!(entry.function_attributes, StringAttribute("julia.opencl.rng", "")) end - - # XXX: put some of the above behind GPUCompiler abstractions - # (e.g., a compile-time version of `deferred_codegen`) end return entry end