From b3cbb483b3e09e7fd92d15749f4ef49a22dcd8a4 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Wed, 25 Nov 2026 10:14:29 +0100 Subject: [PATCH] Add a do-block form of compile that manages the IR's context 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. --- src/driver.jl | 31 ++++++++++++++++++++++++++++++- test/native.jl | 22 ++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/driver.jl b/src/driver.jl index 5661bc74..e39848ca 100644 --- a/src/driver.jl +++ b/src/driver.jl @@ -83,7 +83,8 @@ The default [`relocation_lowering`](@ref) strategy resolves Julia-value relocati The IR in the result (the `:llvm` module, or `meta.ir` and `meta.entry`) lives in the LLVM context of the enclosing [`JuliaContext`](@ref) block, and is freed with it. Use it, -and dispose of it, within that block. +and dispose of it, within that block. The do-block form, `compile(f, target, job)`, takes +care of both. """ function compile(target::Symbol, @nospecialize(job::CompilerJob)) run_compile_hook(job) @@ -92,6 +93,34 @@ function compile(target::Symbol, @nospecialize(job::CompilerJob)) end end +""" + compile(f, target::Symbol, job::CompilerJob) + +Compile `job` as `compile(target, job)` does, in an LLVM context of its own, and return +`f(result, meta)`. `result` is the module for `:llvm`, and the code otherwise. + +`f` runs with the context of the IR active, so it can work with the module and its values +directly. The IR and its context are freed when `f` returns: return only values that don't +refer to them, e.g., strings, or the code. + +```julia +compile(:obj, job) do obj, meta + (; obj, entry=meta.entry.name) +end +``` +""" +function compile(f, target::Symbol, @nospecialize(job::CompilerJob)) + return JuliaContext() do _ + result, meta = compile(target, job) + ir = target === :llvm ? result : meta.ir + try + f(result, meta) + finally + dispose(ir) + end + end +end + function report_failure(f, @nospecialize(job::CompilerJob)) hook = compile_failure_hook[] hook === nothing && return f() diff --git a/test/native.jl b/test/native.jl index 3d75abfa..68c6ab41 100644 --- a/test/native.jl +++ b/test/native.jl @@ -27,6 +27,28 @@ import InteractiveUtils end end +@testset "compile do-block" begin + mod = @eval module $(gensym()) + f(x::Int) = x + 1 + end + job, _ = Native.create_job(mod.f, (Int,)) + + # the block runs in the context of the IR + name = GPUCompiler.compile(:llvm, job) do ir, meta + @test context() == context(ir) + @test meta.entry in ir.functions + meta.entry.name + end + asm, entry = GPUCompiler.compile(:asm, job) do asm, meta + @test meta.entry in meta.ir.functions + asm, meta.entry.name + end + @test occursin(r"^(julia|j)_f_\d+$", name) + @test occursin(r"^(julia|j)_f_\d+$", entry) && occursin(entry, asm) + + @test_throws "boom" GPUCompiler.compile((_, _) -> error("boom"), :llvm, job) +end + @testset "compile hook" begin mod = @eval module $(gensym()) f(x::Int) = nothing