Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vchuravy committed Feb 5, 2025
1 parent 7edafde commit 6fb1cea
Show file tree
Hide file tree
Showing 14 changed files with 345 additions and 392 deletions.
18 changes: 7 additions & 11 deletions src/nditeration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,18 +138,14 @@ needs to perform dynamic bounds-checking.
"""
@inline function partition(ndrange, __workgroupsize)
@assert length(__workgroupsize) <= length(ndrange)
if length(__workgroupsize) < length(ndrange)
# pad workgroupsize with ones
workgroupsize = ntuple(Val(length(ndrange))) do I
Base.@_inline_meta
if I > length(__workgroupsize)
return 1
else
return __workgroupsize[I]
end
# pad workgroupsize with ones
workgroupsize = ntuple(Val(length(ndrange))) do I
Base.@_inline_meta
if I > length(__workgroupsize) || __workgroupsize[I] == 0
return 1

Check warning on line 145 in src/nditeration.jl

View check run for this annotation

Codecov / codecov/patch

src/nditeration.jl#L142-L145

Added lines #L142 - L145 were not covered by tests
else
return __workgroupsize[I]

Check warning on line 147 in src/nditeration.jl

View check run for this annotation

Codecov / codecov/patch

src/nditeration.jl#L147

Added line #L147 was not covered by tests
end
else
workgroupsize = __workgroupsize
end
let workgroupsize = workgroupsize
dynamic = Ref(false)
Expand Down
8 changes: 4 additions & 4 deletions src/pocl/backend.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,26 @@ KA.allocate(::POCLBackend, ::Type{T}, dims::Tuple) where {T} = Array{T}(undef, d

function KA.zeros(backend::POCLBackend, ::Type{T}, dims::Tuple) where {T}
arr = KA.allocate(backend, T, dims)
kernel = init_kernel(backend)
kernel = KA.init_kernel(backend)
kernel(arr, zero, T, ndrange = length(arr))
return arr

Check warning on line 30 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L26-L30

Added lines #L26 - L30 were not covered by tests
end
function KA.ones(backend::POCLBackend, ::Type{T}, dims::Tuple) where {T}
arr = KA.allocate(backend, T, dims)
kernel = init_kernel(backend)
kernel = KA.init_kernel(backend)
kernel(arr, one, T; ndrange = length(arr))
return arr

Check warning on line 36 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L32-L36

Added lines #L32 - L36 were not covered by tests
end

function KA.copyto!(backend::POCLBackend, A, B)
if get_backend(A) == get_backend(B) && get_backend(A) isa POCLBackend
if KA.get_backend(A) == KA.get_backend(B) && KA.get_backend(A) isa POCLBackend
if length(A) != length(B)
error("Arrays must match in length")

Check warning on line 42 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L39-L42

Added lines #L39 - L42 were not covered by tests
end
if Base.mightalias(A, B)
error("Arrays may not alias")

Check warning on line 45 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L44-L45

Added lines #L44 - L45 were not covered by tests
end
kernel = copy_kernel(backend)
kernel = KA.copy_kernel(backend)
kernel(A, B, ndrange = length(A))
return A

Check warning on line 49 in src/pocl/backend.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/backend.jl#L47-L49

Added lines #L47 - L49 were not covered by tests
else
Expand Down
20 changes: 11 additions & 9 deletions src/pocl/compiler/compilation.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,20 @@

struct OpenCLCompilerParams <: AbstractCompilerParams end
const OpenCLCompilerConfig = CompilerConfig{SPIRVCompilerTarget, OpenCLCompilerParams}
const OpenCLCompilerJob = CompilerJob{SPIRVCompilerTarget,OpenCLCompilerParams}
const OpenCLCompilerJob = CompilerJob{SPIRVCompilerTarget, OpenCLCompilerParams}

GPUCompiler.runtime_module(::CompilerJob{<:Any,OpenCLCompilerParams}) = POCL
GPUCompiler.runtime_module(::CompilerJob{<:Any, OpenCLCompilerParams}) = POCL

Check warning on line 7 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L7

Added line #L7 was not covered by tests

GPUCompiler.method_table(::OpenCLCompilerJob) = method_table

Check warning on line 9 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L9

Added line #L9 was not covered by tests

# filter out OpenCL built-ins
# TODO: eagerly lower these using the translator API
GPUCompiler.isintrinsic(job::OpenCLCompilerJob, fn::String) =

Check warning on line 13 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L13

Added line #L13 was not covered by tests
invoke(GPUCompiler.isintrinsic,
Tuple{CompilerJob{SPIRVCompilerTarget}, typeof(fn)},
job, fn) ||
invoke(
GPUCompiler.isintrinsic,
Tuple{CompilerJob{SPIRVCompilerTarget}, typeof(fn)},
job, fn
) ||
in(fn, opencl_builtins)


Expand Down Expand Up @@ -42,14 +44,14 @@ function compiler_config(dev::cl.Device; kwargs...)
end
return config

Check warning on line 45 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L45

Added line #L45 was not covered by tests
end
@noinline function _compiler_config(dev; kernel=true, name=nothing, always_inline=false, kwargs...)
@noinline function _compiler_config(dev; kernel = true, name = nothing, always_inline = false, kwargs...)
supports_fp16 = "cl_khr_fp16" in dev.extensions
supports_fp64 = "cl_khr_fp64" in dev.extensions

Check warning on line 49 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L47-L49

Added lines #L47 - L49 were not covered by tests

# create GPUCompiler objects
target = SPIRVCompilerTarget(; supports_fp16, supports_fp64, kwargs...)
params = OpenCLCompilerParams()
CompilerConfig(target, params; kernel, name, always_inline)
return CompilerConfig(target, params; kernel, name, always_inline)

Check warning on line 54 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L52-L54

Added lines #L52 - L54 were not covered by tests
end

# compile to executable machine code
Expand All @@ -59,7 +61,7 @@ function compile(@nospecialize(job::CompilerJob))
GPUCompiler.compile(:obj, job)

Check warning on line 61 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L60-L61

Added lines #L60 - L61 were not covered by tests
end

(;obj, entry=LLVM.name(meta.entry))
return (; obj, entry = LLVM.name(meta.entry))

Check warning on line 64 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L64

Added line #L64 was not covered by tests
end

# link into an executable kernel
Expand All @@ -70,5 +72,5 @@ function link(@nospecialize(job::CompilerJob), compiled)
error("Your device does not support SPIR-V, which is currently required for native execution.")

Check warning on line 72 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L72

Added line #L72 was not covered by tests
end
cl.build!(prog)
cl.Kernel(prog, compiled.entry)
return cl.Kernel(prog, compiled.entry)

Check warning on line 75 in src/pocl/compiler/compilation.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/compilation.jl#L74-L75

Added lines #L74 - L75 were not covered by tests
end
66 changes: 36 additions & 30 deletions src/pocl/compiler/execution.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const LAUNCH_KWARGS = [:global_size, :local_size, :queue]

macro opencl(ex...)
call = ex[end]
kwargs = map(ex[1:end-1]) do kwarg
kwargs = map(ex[1:(end - 1)]) do kwarg
if kwarg isa Symbol
:($kwarg = $kwarg)
elseif Meta.isexpr(kwarg, :(=))
Expand All @@ -31,14 +31,14 @@ macro opencl(ex...)
macro_kwargs, compiler_kwargs, call_kwargs, other_kwargs =
split_kwargs(kwargs, MACRO_KWARGS, COMPILER_KWARGS, LAUNCH_KWARGS)
if !isempty(other_kwargs)
key,val = first(other_kwargs).args
key, val = first(other_kwargs).args
throw(ArgumentError("Unsupported keyword argument '$key'"))
end

# handle keyword arguments that influence the macro's behavior
launch = true
for kwarg in macro_kwargs
key,val = kwarg.args
key, val = kwarg.args
if key == :launch
isa(val, Bool) || throw(ArgumentError("`launch` keyword argument to @opencl should be a constant value"))
launch = val::Bool
Expand All @@ -56,7 +56,8 @@ macro opencl(ex...)

# convert the arguments, call the compiler and launch the kernel
# while keeping the original arguments alive
push!(code.args,
push!(
code.args,
quote
$f_var = $f
GC.@preserve $(vars...) $f_var begin
Expand All @@ -69,13 +70,16 @@ macro opencl(ex...)
end
$kernel
end
end)
end
)

return esc(quote
let
$code
return esc(
quote
let
$code
end
end
end)
)
end


Expand All @@ -101,21 +105,23 @@ end
# Base.RefValue isn't GPU compatible, so provide a compatible alternative
# TODO: port improvements from CUDA.jl
struct CLRefValue{T} <: Ref{T}
x::T
x::T
end
Base.getindex(r::CLRefValue) = r.x
Adapt.adapt_structure(to::KernelAdaptor, r::Base.RefValue) = CLRefValue(adapt(to, r[]))

Check warning on line 111 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L110-L111

Added lines #L110 - L111 were not covered by tests

# broadcast sometimes passes a ref(type), resulting in a GPU-incompatible DataType box.
# avoid that by using a special kind of ref that knows about the boxed type.
struct CLRefType{T} <: Ref{DataType} end
Base.getindex(r::CLRefType{T}) where T = T
Adapt.adapt_structure(to::KernelAdaptor, r::Base.RefValue{<:Union{DataType,Type}}) =
Base.getindex(r::CLRefType{T}) where {T} = T
Adapt.adapt_structure(to::KernelAdaptor, r::Base.RefValue{<:Union{DataType, Type}}) =

Check warning on line 117 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L116-L117

Added lines #L116 - L117 were not covered by tests
CLRefType{r[]}()

# case where type is the function being broadcasted
Adapt.adapt_structure(to::KernelAdaptor,
bc::Broadcast.Broadcasted{Style, <:Any, Type{T}}) where {Style, T} =
Adapt.adapt_structure(

Check warning on line 121 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L121

Added line #L121 was not covered by tests
to::KernelAdaptor,
bc::Broadcast.Broadcasted{Style, <:Any, Type{T}}
) where {Style, T} =
Broadcast.Broadcasted{Style}((x...) -> T(x...), adapt(to, bc.args), bc.axes)

Check warning on line 125 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L125

Added line #L125 was not covered by tests

"""
Expand All @@ -131,29 +137,30 @@ register methods for the the `OpenCL.KernelAdaptor` type.
The `pointers` argument is used to collect pointers to indirect SVM buffers, which need to
be registered with OpenCL before invoking the kernel.
"""
function clconvert(arg, pointers::Vector{Ptr{Cvoid}}=Ptr{Cvoid}[])
adapt(KernelAdaptor(pointers), arg)
function clconvert(arg, pointers::Vector{Ptr{Cvoid}} = Ptr{Cvoid}[])
return adapt(KernelAdaptor(pointers), arg)

Check warning on line 141 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L140-L141

Added lines #L140 - L141 were not covered by tests
end



## abstract kernel functionality

abstract type AbstractKernel{F,TT} end
abstract type AbstractKernel{F, TT} end

@inline @generated function (kernel::AbstractKernel{F,TT})(args...;
call_kwargs...) where {F,TT}
@inline @generated function (kernel::AbstractKernel{F, TT})(

Check warning on line 149 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L149

Added line #L149 was not covered by tests
args...;
call_kwargs...
) where {F, TT}
sig = Tuple{F, TT.parameters...} # Base.signature_type with a function type
args = (:(kernel.f), (:( clconvert(args[$i], svm_pointers) ) for i in 1:length(args))...)
args = (:(kernel.f), (:(clconvert(args[$i], svm_pointers)) for i in 1:length(args))...)

Check warning on line 154 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L153-L154

Added lines #L153 - L154 were not covered by tests

# filter out ghost arguments that shouldn't be passed
predicate = dt -> GPUCompiler.isghosttype(dt) || Core.Compiler.isconstType(dt)
to_pass = map(!predicate, sig.parameters)
call_t = Type[x[1] for x in zip(sig.parameters, to_pass) if x[2]]
call_args = Union{Expr,Symbol}[x[1] for x in zip(args, to_pass) if x[2]]
call_t = Type[x[1] for x in zip(sig.parameters, to_pass) if x[2]]
call_args = Union{Expr, Symbol}[x[1] for x in zip(args, to_pass) if x[2]]

Check warning on line 160 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L157-L160

Added lines #L157 - L160 were not covered by tests

# replace non-isbits arguments (they should be unused, or compilation would have failed)
for (i,dt) in enumerate(call_t)
for (i, dt) in enumerate(call_t)
if !isbitstype(dt)
call_t[i] = Ptr{Any}
call_args[i] = :C_NULL

Check warning on line 166 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L163-L166

Added lines #L163 - L166 were not covered by tests
Expand All @@ -163,17 +170,16 @@ abstract type AbstractKernel{F,TT} end
# finalize types
call_tt = Base.to_tuple_type(call_t)

Check warning on line 171 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L171

Added line #L171 was not covered by tests

quote
return quote
svm_pointers = Ptr{Cvoid}[]
$cl.clcall(kernel.fun, $call_tt, $(call_args...); svm_pointers, call_kwargs...)

Check warning on line 175 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L173-L175

Added lines #L173 - L175 were not covered by tests
end
end



## host-side kernels

struct HostKernel{F,TT} <: AbstractKernel{F,TT}
struct HostKernel{F, TT} <: AbstractKernel{F, TT}
f::F
fun::cl.Kernel
end
Expand All @@ -183,7 +189,7 @@ end

const clfunction_lock = ReentrantLock()

function clfunction(f::F, tt::TT=Tuple{}; kwargs...) where {F,TT}
function clfunction(f::F, tt::TT = Tuple{}; kwargs...) where {F, TT}
ctx = context()
dev = device()

Check warning on line 194 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L192-L194

Added lines #L192 - L194 were not covered by tests

Expand All @@ -200,10 +206,10 @@ function clfunction(f::F, tt::TT=Tuple{}; kwargs...) where {F,TT}
kernel = get(_kernel_instances, h, nothing)
if kernel === nothing

Check warning on line 207 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L205-L207

Added lines #L205 - L207 were not covered by tests
# create the kernel state object
kernel = HostKernel{F,tt}(f, fun)
kernel = HostKernel{F, tt}(f, fun)
_kernel_instances[h] = kernel

Check warning on line 210 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L209-L210

Added lines #L209 - L210 were not covered by tests
end
return kernel::HostKernel{F,tt}
return kernel::HostKernel{F, tt}

Check warning on line 212 in src/pocl/compiler/execution.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/execution.jl#L212

Added line #L212 was not covered by tests
end
end

Expand Down
13 changes: 7 additions & 6 deletions src/pocl/compiler/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,28 @@ for method in (:code_typed, :code_warntype, :code_llvm, :code_native)
args = method == :code_typed ? (:job,) : (:io, :job)

@eval begin
function $method(io::IO, @nospecialize(func), @nospecialize(types);
kernel::Bool=false, kwargs...)
function $method(

Check warning on line 27 in src/pocl/compiler/reflection.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/reflection.jl#L27

Added line #L27 was not covered by tests
io::IO, @nospecialize(func), @nospecialize(types);
kernel::Bool = false, kwargs...
)
compiler_kwargs, kwargs = split_kwargs_runtime(kwargs, COMPILER_KWARGS)
source = methodinstance(typeof(func), Base.to_tuple_type(types))
config = compiler_config(device(); kernel, compiler_kwargs...)
job = CompilerJob(source, config)
GPUCompiler.$method($(args...); kwargs...)
return GPUCompiler.$method($(args...); kwargs...)

Check warning on line 35 in src/pocl/compiler/reflection.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/reflection.jl#L31-L35

Added lines #L31 - L35 were not covered by tests
end
$method(@nospecialize(func), @nospecialize(types); kwargs...) =

Check warning on line 37 in src/pocl/compiler/reflection.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/reflection.jl#L37

Added line #L37 was not covered by tests
$method(stdout, func, types; kwargs...)
end
end



#
# @device_code_* functions
#

export @device_code_lowered, @device_code_typed, @device_code_warntype, @device_code_llvm,
@device_code_native, @device_code
@device_code_native, @device_code

# forward to GPUCompiler
@eval $(Symbol("@device_code_lowered")) = $(getfield(GPUCompiler, Symbol("@device_code_lowered")))
Expand All @@ -70,5 +71,5 @@ function return_type(@nospecialize(func), @nospecialize(tt))
job = CompilerJob(source, config)
interp = GPUCompiler.get_interpreter(job)
sig = Base.signature_type(func, tt)
Core.Compiler.return_type(interp, sig)
return Core.Compiler.return_type(interp, sig)

Check warning on line 74 in src/pocl/compiler/reflection.jl

View check run for this annotation

Codecov / codecov/patch

src/pocl/compiler/reflection.jl#L68-L74

Added lines #L68 - L74 were not covered by tests
end
Loading

0 comments on commit 6fb1cea

Please sign in to comment.