Skip to content
Merged
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
42 changes: 24 additions & 18 deletions src/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ macro oneapi(ex...)
args = call.args[2:end]

code = quote end
vars, var_exprs = assign_args!(code, args)
_, var_exprs = assign_args!(code, args)

# group keyword argument
macro_kwargs, compiler_kwargs, call_kwargs, other_kwargs =
Expand All @@ -113,24 +113,9 @@ macro oneapi(ex...)

# FIXME: macro hygiene wrt. escaping kwarg values (this broke with 1.5)
# we esc() the whole thing now, necessitating gensyms...
@gensym f_var kernel_f kernel_args kernel_tt kernel

# convert the arguments, call the compiler and launch the kernel
# while keeping the original arguments alive
push!(code.args,
quote
$f_var = $f
GC.@preserve $(vars...) $f_var begin
$kernel_f = $kernel_convert($f_var)
$kernel_args = map($kernel_convert, ($(var_exprs...),))
$kernel_tt = Tuple{map(Core.Typeof, $kernel_args)...}
$kernel = $zefunction($kernel_f, $kernel_tt; $(compiler_kwargs...))
if $launch
$kernel($(var_exprs...); $(call_kwargs...))
end
$kernel
end
end)
:($compile_and_launch($f, ($(var_exprs...),), $(Val(launch)),
(; $(call_kwargs...)), (; $(compiler_kwargs...)))))

return esc(quote
let
Expand All @@ -140,6 +125,27 @@ macro oneapi(ex...)
end


# convert the arguments, call the compiler and launch the kernel while keeping the original
# arguments alive. this is a function rather than part of the macro expansion so that the
# arguments have concrete types when they are preserved: `GC.@preserve` of a value that is a
# union of singletons, e.g. `op` in `for op in (max, min)`, crashes codegen on Julia 1.12 and
# 1.13.0-1.13.1 (JuliaLang/julia#63482).
@inline function compile_and_launch(f::F, args::Tuple, ::Val{launch},
call_kwargs::NamedTuple,
compiler_kwargs::NamedTuple) where {F,launch}
GC.@preserve f args begin
kernel_f = kernel_convert(f)
kernel_args = map(kernel_convert, args)
kernel_tt = Tuple{map(Core.Typeof, kernel_args)...}
kernel = zefunction(kernel_f, kernel_tt; compiler_kwargs...)
if launch
kernel(args...; call_kwargs...)
end
kernel
end
end


## argument conversion

struct KernelAdaptor end
Expand Down
26 changes: 26 additions & 0 deletions test/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,32 @@ end
end


@testset "arguments of a union of singleton types" begin
# JuliaLang/julia#63482: `GC.@preserve` of such a value crashed codegen
function kernel(a, op)
@inbounds a[1] = op(a[1], Int16(1))
return
end

function compile(a)
kernels = []
for op in (max, min)
push!(kernels, @oneapi launch=false kernel(a, op))
end
kernels
end
@test length(compile(oneArray(Int16[5]))) == 2

function launch(a)
for op in (max, min)
@oneapi kernel(a, op)
end
Array(a)
end
@test launch(oneArray(Int16[5])) == Int16[1]
end


@testset "immutables" begin
# issue #15: immutables not passed by pointer

Expand Down
Loading