|
| 1 | +# Host references |
| 2 | + |
| 3 | +```@meta |
| 4 | +DocTestSetup = quote |
| 5 | + using GPUCompiler |
| 6 | +end |
| 7 | +``` |
| 8 | + |
| 9 | +Generated IR can refer to either a Julia value or a libjulia C data global. GPUCompiler |
| 10 | +normalizes both forms into `HostReferences`: a Julia value becomes a `JuliaValueRef`, and a C |
| 11 | +global becomes a `CGlobalRef`. This metadata follows the LLVM module through deferred linking, |
| 12 | +optimization, and final code generation. |
| 13 | + |
| 14 | +The default `lower_host_references!` resolves every live reference in the current Julia session |
| 15 | +before machine-code generation. A backend with a loader can instead leave writable word-sized |
| 16 | +symbols in its object, load the object, resolve and patch every symbol, then expose the callable |
| 17 | +function. A Julia value whose address is written into device storage must stay rooted for as |
| 18 | +long as that storage can be reached. |
| 19 | + |
| 20 | +CUDA.jl implements that loader path as follows: |
| 21 | + |
| 22 | +```julia |
| 23 | +function GPUCompiler.lower_host_references!(job::AnyCUDAJob, mod::LLVM.Module, |
| 24 | + refs::GPUCompiler.HostReferences) |
| 25 | + GPUCompiler.emit_host_reference_slots!(mod, refs) |
| 26 | +end |
| 27 | + |
| 28 | +roots = Any[] |
| 29 | +for (name, ref) in refs.slots |
| 30 | + slot = CuGlobal{UInt}(mod, name) |
| 31 | + slot[] = GPUCompiler.resolve_host_reference(ref) |
| 32 | + ref isa GPUCompiler.JuliaValueRef && push!(roots, ref.value) |
| 33 | +end |
| 34 | +``` |
| 35 | + |
| 36 | +See [CUDA.jl's implementation](https://github.com/JuliaGPU/CUDA.jl) for the complete loader |
| 37 | +path. `embedded_pointer` is independent of unresolved loader slots: it remains a conservative |
| 38 | +signal that a session pointer was embedded and the cached artifact must be evicted before |
| 39 | +package-image serialization. |
| 40 | + |
| 41 | +```@docs |
| 42 | +GPUCompiler.JuliaValueRef |
| 43 | +GPUCompiler.CGlobalRef |
| 44 | +GPUCompiler.HostReference |
| 45 | +GPUCompiler.HostReferences |
| 46 | +GPUCompiler.lower_host_references! |
| 47 | +GPUCompiler.emit_host_reference_slots! |
| 48 | +GPUCompiler.resolve_host_reference |
| 49 | +``` |
0 commit comments