Skip to content

Commit a74488e

Browse files
committed
Lower host references through CUDA loader slots
1 parent bc70103 commit a74488e

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

CUDACore/src/compiler/compilation.jl

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ const CUDACompilerConfig = CompilerConfig{PTXCompilerTarget, CUDACompilerParams}
1818
const AnyCUDAJob = CompilerJob{PTXCompilerTarget, <:AbstractCUDACompilerParams}
1919

2020
GPUCompiler.runtime_module(@nospecialize(job::AnyCUDAJob)) = CUDACore
21-
GPUCompiler.supports_host_reference_patching(@nospecialize(job::AnyCUDAJob)) = true
21+
function GPUCompiler.lower_host_references!(@nospecialize(job::AnyCUDAJob), mod::LLVM.Module,
22+
refs::GPUCompiler.HostReferences)
23+
GPUCompiler.emit_host_reference_slots!(mod, refs)
24+
end
2225

2326
# filter out functions from libdevice and cudadevrt
2427
GPUCompiler.isintrinsic(@nospecialize(job::AnyCUDAJob), fn::String) =
@@ -525,8 +528,9 @@ function link_kernel(@nospecialize(job::CompilerJob), image::Vector{UInt8}, entr
525528
mod = CuModule(image)
526529
roots = Any[]
527530
for (name, ref) in refs.slots
528-
CuGlobal{UInt}(mod, name)[] = GPUCompiler.resolve_host_reference(ref)
529-
ref isa GPUCompiler.JuliaObjectRef && push!(roots, ref.value)
531+
slot = CuGlobal{UInt}(mod, name)
532+
slot[] = GPUCompiler.resolve_host_reference(ref)
533+
ref isa GPUCompiler.JuliaValueRef && push!(roots, ref.value)
530534
end
531535
return CuFunction(mod, entry), roots
532536
end

CUDACore/src/precompile.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ end
4545
# kernel launch infrastructure
4646
let CUDACompilerJob = CompilerJob{PTXCompilerTarget, CUDACompilerParams}
4747
precompile(Tuple{typeof(cufunction), typeof(identity), Type{Tuple{Nothing}}})
48-
precompile(Tuple{typeof(link_kernel), CUDACompilerJob, Vector{UInt8}, String})
48+
precompile(Tuple{typeof(link_kernel), CUDACompilerJob, Vector{UInt8}, String,
49+
GPUCompiler.HostReferences})
4950

5051
# GPUCompiler 2.0 caching pipeline (specialized for CUDACore's results struct)
5152
precompile(Tuple{typeof(compile_or_lookup), CUDACompilerJob})

test/core/codegen.jl

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -256,15 +256,26 @@ end
256256
############################################################################################
257257

258258
@testset "host reference patching" begin
259-
@eval begin
259+
runtime_refs = @eval module $(gensym())
260+
using ..CUDACore
260261
const host_reference_type_tag = CUDACore.GPUCompiler.Runtime.type_tag
261262
host_reference_kernel(out) = (out[1] = host_reference_type_tag(Val(:float32)); return)
262263
end
263264

264265
out = CUDA.zeros(UInt, 1)
265-
@cuda threads=1 host_reference_kernel(out)
266+
@cuda threads=1 runtime_refs.host_reference_kernel(out)
266267
expected = UInt(unsafe_load(cglobal(:jl_float32_type, Ptr{UInt})))
267268
@test Array(out)[] == expected
269+
270+
value_refs = @eval module $(gensym())
271+
const host_reference_symbol = Symbol("value#global")
272+
host_reference_symbol_kernel(out, name) =
273+
(out[1] = UInt(name === host_reference_symbol); return)
274+
end
275+
276+
out = CUDA.zeros(UInt, 1)
277+
@cuda threads=1 value_refs.host_reference_symbol_kernel(out, Symbol("value#global"))
278+
@test Array(out)[] == 1
268279
end
269280

270281
############################################################################################

0 commit comments

Comments
 (0)