Skip to content

Add a do-block form of compile that manages the IR's context - #1124

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

maleadt merged 1 commit into
mainfrom
tb/compile-do

Conversation

@maleadt

@maleadt maleadt commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Most callers of compile create a JuliaContext only to read a few values from the IR (the entry point's name, an attribute, the undefined functions) and then dispose of it:

JuliaContext() do ctx
    obj, meta = GPUCompiler.compile(:obj, job)
    @dispose ir = meta.ir begin
        (; obj, entry = meta.entry.name)
    end
end

compile(f, target, job) does that for them:

GPUCompiler.compile(:obj, job) do obj, meta
    (; obj, entry = meta.entry.name)
end

It compiles in a context of its own, runs f(result, meta) with the IR's context active, and frees the IR and its context afterwards. result is the module for :llvm, and the code otherwise.

The reason is #1122. With its llvmgpu toolchain, the IR can belong to libLLVMGPU rather than to the Julia context the caller created. LLVM.jl then passes values such as meta.entry to Julia's LLVM unless the caller activates the IR's context first, and that crashes or hangs. That is what fails on #1122's Buildkite: KernelAbstractions' POCL back-end reads meta.entry.name in its precompile workload. With this form, #1122 only has to run f in the IR's context, and back-ends don't need to know which LLVM library ran.

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