Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions .github/workflows/Test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -85,3 +85,38 @@ jobs:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: false
files: lcov.info

handoff_test:
# the late hand-off to libLLVMGPU, for the targets that support it (the others are
# tested as usual)
name: Julia ${{ matrix.version }} with libLLVMGPU - ${{ matrix.os }}
runs-on: ${{ matrix.os }}
timeout-minutes: 30
strategy:
fail-fast: false
matrix:
version: ['1.12', '1.13']
os: [ubuntu-24.04]
steps:
- uses: actions/checkout@v7

- name: Install Julia
uses: julia-actions/setup-julia@v3
with:
version: ${{ matrix.version }}

- name: Set-up cache
uses: julia-actions/cache@v3

- name: Select the late hand-off
run: |
julia --project=test -e 'using Pkg; Pkg.add(name="LLVMGPU_jll", version="1")'
for dir in . test; do
printf '[GPUCompiler]\nhandoff = "late"\n' >> $dir/LocalPreferences.toml
done

- name: Run tests
uses: julia-actions/julia-runtest@v1
env:
# (checked by the tests, so that they don't silently test without the hand-off)
GPUCOMPILER_TEST_HANDOFF: late
1 change: 1 addition & 0 deletions src/GPUCompiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ include("mangling.jl")

# compiler interface and implementations
include("interface.jl")
include("handoff.jl")
include("relocation.jl")
include("atomics.jl")
include("error.jl")
Expand Down
58 changes: 48 additions & 10 deletions src/driver.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,25 @@ export JuliaContext
# this helper to a regular `Context()` call.
function JuliaContext(; opaque_pointers=nothing)
# XXX: remove
ThreadSafeContext(; opaque_pointers)
ThreadSafeContext(; opaque_pointers, library=:julia)
end
function JuliaContext(f; kwargs...)
ThreadSafeContext(; kwargs...) do ts_ctx
ThreadSafeContext(; library=:julia, kwargs...) do ts_ctx
# for now, also activate the underlying context
# XXX: this is wrong; we can't expose the underlying LLVM context, but should
# instead always go through the callback in order to unlock it properly.
# rework this once we depend on Julia 1.9 or later.
ctx = context(ts_ctx)
context!(ctx) do
f(ctx)
# the block also owns the context of the late hand-off's library, if it's used
owner = ContextOwner(current_task(), nothing)
try
with(context_owner => owner) do
context!(ctx) do
f(ctx)
end
end
finally
dispose_handoff_context(owner)
end
end
end
Expand Down Expand Up @@ -83,7 +91,9 @@ 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. With the late hand-off (see [`CompilerConfig`](@ref)),
it belongs to another LLVM library than Julia's, in a context that the block also owns:
work with it inside `context!(context(ir)) do ... end`.
"""
function compile(target::Symbol, @nospecialize(job::CompilerJob))
run_compile_hook(job)
Expand Down Expand Up @@ -368,7 +378,7 @@ const __llvm_initialized = Ref(false)
end
end

exception_sites = ExceptionSite[]
resolve_early = false
@tracepoint "IR post-processing" begin
# mark the kernel entry-point functions (optimization may need it)
if job.config.kernel
Expand Down Expand Up @@ -452,11 +462,36 @@ const __llvm_initialized = Ref(false)
end
end
end
end
end

# the rest of the pipeline runs with the LLVM library that generates code: Julia's,
# or after the late hand-off, a newer one (see `handoff.jl`)
if job.config.handoff === :late
# (in a new variable, which the closures above don't capture)
handed_off = @tracepoint "hand-off" late_handoff(job, ir)
return finish_llvm_in!(context(handed_off), job, handed_off, entry_fn, entry,
entrypoints, compiled, relocations, resolve_early)
else
return finish_llvm!(job, ir, entry_fn, entry, entrypoints, compiled, relocations,
resolve_early)
end
end

finish_llvm_in!(ctx::LLVM.Context, args...) = context!(() -> finish_llvm!(args...), ctx)

# finalize the IR for the target, validate it and collect its relocations
function finish_llvm!(@nospecialize(job::CompilerJob), ir::LLVM.Module, entry_fn::String,
entry::LLVM.Function, entrypoints, compiled, relocations::Relocations,
resolve_early::Bool)
exception_sites = ExceptionSite[]
@tracepoint "IR finalization" begin
if job.config.toplevel
# Do not expose dead sites to loaders.
resolve_early || prune_dead_relocations!(ir, relocations)

# optimization may have replaced functions, so look the entry point up again
# optimization (or the hand-off) may have replaced functions, so look the
# entry point up again
entry = ir.functions[entry_fn]

# finish the module
Expand Down Expand Up @@ -543,10 +578,13 @@ emit_asm(@nospecialize(job::CompilerJob), ir::LLVM.Module,
@tracepoint "Debug info removal" strip_debuginfo!(ir)
end

@tracepoint "LLVM back-end" begin
@tracepoint "preparation" prepare_execution!(job, ir, relocs)
# (the module may belong to another library than Julia's, after the late hand-off)
code = context!(context(ir)) do
@tracepoint "LLVM back-end" begin
@tracepoint "preparation" prepare_execution!(job, ir, relocs)

code = @tracepoint "machine-code generation" mcgen(job, ir, format)
@tracepoint "machine-code generation" mcgen(job, ir, format)
end
end

return code, (;)
Expand Down
65 changes: 63 additions & 2 deletions src/gcn.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ llvm_triple(::GCNCompilerTarget) = "amdgcn-amd-amdhsa"

source_code(target::GCNCompilerTarget) = "gcn"

# the hand-off replaces the external back-end, whose datalayout it shares
supports_handoff(@nospecialize(target::GCNCompilerTarget)) = target.backend === :external

function llvm_machine(target::GCNCompilerTarget)
is_handoff_library() && return handoff_machine(target)
@static if :AMDGPU ∉ LLVM.backends()
return nothing
end
Expand All @@ -49,6 +53,61 @@ function llvm_machine(target::GCNCompilerTarget)
return tm
end

# the processors that the AMDGPU back-end of LLVM 23.1 knows (GCNProcessors.td)
const gcn_processors = Set([
"generic", "generic-hsa", "gfx600", "tahiti", "gfx601", "pitcairn", "verde", "gfx602",
"hainan", "oland", "gfx700", "kaveri", "gfx701", "hawaii", "gfx702", "gfx703",
"kabini", "mullins", "gfx704", "bonaire", "gfx705", "gfx801", "carrizo", "gfx802",
"iceland", "tonga", "gfx803", "fiji", "polaris10", "polaris11", "gfx805", "tongapro",
"gfx810", "stoney", "gfx900", "gfx902", "gfx904", "gfx906", "gfx908", "gfx909",
"gfx90a", "gfx90c", "gfx942", "gfx950", "gfx9-generic", "gfx9-4-generic", "gfx1010",
"gfx1011", "gfx1012", "gfx1013", "gfx1030", "gfx1031", "gfx1032", "gfx1033", "gfx1034",
"gfx1035", "gfx1036", "gfx10-1-generic", "gfx10-3-generic", "gfx1100", "gfx1101",
"gfx1102", "gfx1103", "gfx1150", "gfx1151", "gfx1152", "gfx1153", "gfx1154", "gfx1170",
"gfx1171", "gfx1172", "gfx11-generic", "gfx11-7-generic", "gfx1200", "gfx1201",
"gfx12-generic", "gfx1250", "gfx1251", "gfx12-5-generic", "gfx1310", "gfx13-generic"])

# after the hand-off, a target machine of the library that generates code. like
# AMDGPU_LLVM_Backend_jll did, reject processors and features that LLVM would only warn
# about, and keep the 8-byte alignment of i128 that Julia needs before 1.12.
function handoff_machine(target::GCNCompilerTarget)
triple = llvm_triple(target)
t = LLVM.Target(; triple)
cpu = target.dev_isa
features = target.features
# (LLVM only warns about unknown processors, and falls back to the oldest one)
cpu in gcn_processors || error("Unsupported processor $cpu for the AMDGPU back-end")
@dispose sti=LLVM.SubtargetInfo(t, triple; cpu) begin
try
LLVM.hasfeatures(sti, features)
catch err
err isa ArgumentError || rethrow()
error("Unsupported features $(repr(features)) for the AMDGPU back-end: $(err.msg)")
end
end

tm = if Base.datatype_alignment(Int128) == 8
# the ABI name selects that alignment (a patch of the library's AMDGPU back-end),
# which LLVM.jl's `TargetMachine` constructor cannot set
lib = LLVM.library()
options = LLVM.API.LLVMCreateTargetMachineOptions(lib)
try
LLVM.API.LLVMTargetMachineOptionsSetCPU(lib, options, cpu)
LLVM.API.LLVMTargetMachineOptionsSetFeatures(lib, options, features)
LLVM.API.LLVMTargetMachineOptionsSetRelocMode(lib, options, LLVM.API.LLVMRelocPIC)
LLVM.API.LLVMTargetMachineOptionsSetABI(lib, options, "i128:64")
ref = LLVM.API.LLVMCreateTargetMachineWithOptions(lib, t, triple, options)
LLVM.mark_alloc(LLVM.TargetMachine(ref; library=lib))
finally
LLVM.API.LLVMDisposeTargetMachineOptions(lib, options)
end
else
LLVM.TargetMachine(t, triple; cpu, features, reloc=LLVM.RelocMode.PIC)
end
LLVM.asm_verbosity!(tm, true)
return tm
end

# the external back-end replaces the module's datalayout with its own, which aligns i128 to
# 16 bytes. it does keep a 64-bit alignment if the module asks for one (since
# AMDGPU_LLVM_Backend_jll 23.1.1+4), which Julia needs before 1.12 to lay out kernel
Expand Down Expand Up @@ -257,7 +316,9 @@ const AMDGPUObjectFile = Cint(1)
mod::LLVM.Module, format=LLVM.CodeGenFileType.Assembly)
target = job.config.target

if target.backend === :inprocess
if is_handoff_library(mod.library)
return String(handoff_mcgen(job, mod, format))
elseif target.backend === :inprocess
if :AMDGPU ∉ LLVM.backends()
error("The in-process LLVM lacks the AMDGPU target; cannot compile to GCN. " *
"Load AMDGPU_LLVM_Backend_jll and use `backend=:external` instead.")
Expand Down Expand Up @@ -301,7 +362,7 @@ end
# the version of the LLVM that generates code for the target, which can differ from the one
# GPUCompiler runs in. only the release matters, so JLL rebuilds are ignored.
function backend_llvm_version(target::GCNCompilerTarget)
target.backend === :external || return LLVM.version()
(target.backend === :external && !is_handoff_library()) || return LLVM.version()
jll = get(Base.loaded_modules, getfield(AMDGPU_LLVM_Backend_jll, :pkg), nothing)
# (without the back-end, `mcgen` reports a clearer error)
jll === nothing && return LLVM.version()
Expand Down
Loading
Loading