Skip to content

Commit 1e4ec44

Browse files
committed
Add an experimental opaque closure type.
1 parent 3321fc8 commit 1e4ec44

File tree

2 files changed

+164
-0
lines changed

2 files changed

+164
-0
lines changed

src/compiler/compilation.jl

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,121 @@ function run_and_collect(cmd)
261261

262262
return proc, log
263263
end
264+
265+
266+
267+
## opaque closures
268+
269+
# TODO: once stabilised, move bits of this into GPUCompiler.jl
270+
271+
using Core.Compiler: IRCode
272+
using Core: CodeInfo, MethodInstance, CodeInstance, LineNumberNode
273+
274+
struct OpaqueClosure{F, E, A, R} # func, env, args, ret
275+
env::E
276+
end
277+
278+
# XXX: because we can't call functions from other CUDA modules, we effectively need to
279+
# recompile when the target function changes. this, and because of how GPUCompiler's
280+
# deferred compilation mechanism currently works, is why we have `F` as a type param.
281+
282+
# XXX: because of GPU code requiring specialized signatures, we also need to recompile
283+
# when the environment or argument types change. together with the above, this
284+
# negates much of the benefit of opaque closures.
285+
286+
# TODO: support for constructing an opaque closure from source code
287+
288+
# TODO: complete support for passing an environment. this probably requires a split into
289+
# host and device structures to, e.g., root a CuArray and pass a CuDeviceArray.
290+
291+
function compute_ir_rettype(ir::IRCode)
292+
rt = Union{}
293+
for i = 1:length(ir.stmts)
294+
stmt = ir.stmts[i][:inst]
295+
if isa(stmt, Core.Compiler.ReturnNode) && isdefined(stmt, :val)
296+
rt = Core.Compiler.tmerge(Core.Compiler.argextype(stmt.val, ir), rt)
297+
end
298+
end
299+
return Core.Compiler.widenconst(rt)
300+
end
301+
302+
function compute_oc_signature(ir::IRCode, nargs::Int, isva::Bool)
303+
argtypes = Vector{Any}(undef, nargs)
304+
for i = 1:nargs
305+
argtypes[i] = Core.Compiler.widenconst(ir.argtypes[i+1])
306+
end
307+
if isva
308+
lastarg = pop!(argtypes)
309+
if lastarg <: Tuple
310+
append!(argtypes, lastarg.parameters)
311+
else
312+
push!(argtypes, Vararg{Any})
313+
end
314+
end
315+
return Tuple{argtypes...}
316+
end
317+
318+
function OpaqueClosure(ir::IRCode, @nospecialize env...; isva::Bool = false)
319+
# NOTE: we need ir.argtypes[1] == typeof(env)
320+
ir = Core.Compiler.copy(ir)
321+
nargs = length(ir.argtypes)-1
322+
sig = compute_oc_signature(ir, nargs, isva)
323+
rt = compute_ir_rettype(ir)
324+
src = ccall(:jl_new_code_info_uninit, Ref{CodeInfo}, ())
325+
src.slotnames = Base.fill(:none, nargs+1)
326+
src.slotflags = Base.fill(zero(UInt8), length(ir.argtypes))
327+
src.slottypes = copy(ir.argtypes)
328+
src.rettype = rt
329+
src = Core.Compiler.ir_to_codeinf!(src, ir)
330+
config = compiler_config(device(); kernel=false)
331+
return generate_opaque_closure(config, src, sig, rt, nargs, isva, env...)
332+
end
333+
334+
function OpaqueGPUClosure(src::CodeInfo, @nospecialize env...)
335+
src.inferred || throw(ArgumentError("Expected inferred src::CodeInfo"))
336+
mi = src.parent::Core.MethodInstance
337+
sig = Base.tuple_type_tail(mi.specTypes)
338+
method = mi.def::Method
339+
nargs = method.nargs-1
340+
isva = method.isva
341+
return generate_opaque_closure(config, src, sig, src.rettype, nargs, isva, env...)
342+
end
343+
344+
function generate_opaque_closure(config::CompilerConfig, src::CodeInfo,
345+
@nospecialize(sig), @nospecialize(rt),
346+
nargs::Int, isva::Bool, @nospecialize env...;
347+
mod::Module=@__MODULE__,
348+
file::Union{Nothing,Symbol}=nothing, line::Int=0)
349+
# create a method (like `jl_make_opaque_closure_method`)
350+
meth = ccall(:jl_new_method_uninit, Ref{Method}, (Any,), Main)
351+
meth.sig = Tuple
352+
meth.isva = isva # XXX: probably not supported?
353+
meth.is_for_opaque_closure = 0 # XXX: do we want this?
354+
meth.name = Symbol("opaque gpu closure")
355+
meth.nargs = nargs + 1
356+
meth.file = something(file, Symbol())
357+
meth.line = line
358+
ccall(:jl_method_set_source, Nothing, (Any, Any), meth, src)
359+
360+
# look up a method instance and create a compiler job
361+
full_sig = Tuple{typeof(env), sig.parameters...}
362+
mi = ccall(:jl_specializations_get_linfo, Ref{MethodInstance},
363+
(Any, Any, Any), meth, full_sig, Core.svec())
364+
job = CompilerJob(mi, config) # this captures the current world age
365+
366+
# create a code instance and store it in the cache
367+
ci = CodeInstance(mi, rt, C_NULL, src, Int32(0), meth.primary_world, typemax(UInt),
368+
UInt32(0), UInt32(0), nothing, UInt8(0))
369+
Core.Compiler.setindex!(GPUCompiler.ci_cache(job), ci, mi)
370+
371+
id = length(GPUCompiler.deferred_codegen_jobs) + 1
372+
GPUCompiler.deferred_codegen_jobs[id] = job
373+
return OpaqueClosure{id, typeof(env), sig, rt}(env)
374+
end
375+
376+
# device-side call to an opaque closure
377+
function (oc::OpaqueClosure{F})(a, b) where F
378+
ptr = ccall("extern deferred_codegen", llvmcall, Ptr{Cvoid}, (Int,), F)
379+
assume(ptr != C_NULL)
380+
return ccall(ptr, Int, (Int, Int), a, b)
381+
end

test/execution.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1085,3 +1085,49 @@ end
10851085
end
10861086

10871087
############################################################################################
1088+
1089+
if VERSION >= v"1.10-"
1090+
@testset "opaque closures" begin
1091+
1092+
# basic closure, constructed from IRCode
1093+
let
1094+
ir, rettyp = only(Base.code_ircode(+, (Int, Int)))
1095+
oc = CUDA.OpaqueClosure(ir)
1096+
1097+
c = CuArray([0])
1098+
a = CuArray([1])
1099+
b = CuArray([2])
1100+
1101+
function kernel(oc, c, a, b)
1102+
i = threadIdx().x
1103+
@inbounds c[i] = oc(a[i], b[i])
1104+
return
1105+
end
1106+
@cuda threads=1 kernel(oc, c, a, b)
1107+
1108+
@test Array(c)[] == 3
1109+
end
1110+
1111+
# basic closure, constructed from CodeInfo
1112+
let
1113+
ir, rettyp = only(Base.code_typed(+, (Int, Int)))
1114+
oc = CUDA.OpaqueClosure(ir)
1115+
1116+
c = CuArray([0])
1117+
a = CuArray([1])
1118+
b = CuArray([2])
1119+
1120+
function kernel(oc, c, a, b)
1121+
i = threadIdx().x
1122+
@inbounds c[i] = oc(a[i], b[i])
1123+
return
1124+
end
1125+
@cuda threads=1 kernel(oc, c, a, b)
1126+
1127+
@test Array(c)[] == 3
1128+
end
1129+
1130+
end
1131+
end
1132+
1133+
############################################################################################

0 commit comments

Comments
 (0)