From 5ed8112399a1c9aebed3eebddd6bdecf52c42e30 Mon Sep 17 00:00:00 2001 From: sasmith-14 <210313780+sasmith-14@users.noreply.github.com> Date: Sat, 7 Nov 2026 14:25:18 +0530 Subject: [PATCH 1/3] Accept `Memory` as a kernel argument on the POCL backend `get_backend(::Memory)` returns the POCL backend, but only `Array`s were converted to device arrays, so passing a `Memory` (or a view of one) to a kernel failed with "passing non-bitstype argument". Wrap it in an `Array`, which does not copy, and reuse that conversion. --- src/pocl/pocl.jl | 6 ++++++ test/runtests.jl | 28 ++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+) diff --git a/src/pocl/pocl.jl b/src/pocl/pocl.jl index 8d707fd34..50e4ea448 100644 --- a/src/pocl/pocl.jl +++ b/src/pocl/pocl.jl @@ -218,6 +218,12 @@ function Adapt.adapt_storage(to::KernelAdaptor, xs::Array{T, N}) where {T, N} return CLDeviceArray{T, N, AS.CrossWorkgroup}(size(xs), reinterpret(LLVMPtr{T, AS.CrossWorkgroup}, ptr), maxsize) end +# VERSION >= v"1.11" +@static if isdefined(Core, :Memory) + # wrapping `Memory` in an `Array` doesn't copy it + Adapt.adapt_storage(to::KernelAdaptor, xs::Memory) = adapt(to, Base.wrap(Array, xs, length(xs))) +end + import KernelInterface include("backend.jl") import .POCLKernels: POCLBackend diff --git a/test/runtests.jl b/test/runtests.jl index 6d48a8474..19b6d90a2 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -1363,6 +1363,34 @@ end end end +# `Memory` belongs to the POCL back-end too, so kernels take it like an `Array` +@kernel function add_one_memory!(B, @Const(A)) + i = @index(Global) + @inbounds B[i] = coalesce(A[i], 0) + 1 +end +# VERSION >= v"1.11" +@static if isdefined(Core, :Memory) + @testset "POCL Memory arguments" begin + A = Memory{Int32}(undef, 5) + A .= 1:5 + B = Memory{Float32}(undef, 5) + add_one_memory!(POCLBackend())(B, A; ndrange = 5) + synchronize(POCLBackend()) + @test B == 2:6 + + C = zeros(Int32, 3) + add_one_memory!(POCLBackend())(C, view(A, 2:4); ndrange = 3) + synchronize(POCLBackend()) + @test C == 3:5 + + U = Memory{Union{Missing, Int32}}(undef, 4) + U .= [1, missing, 3, missing] + add_one_memory!(POCLBackend())(C, U; ndrange = 3) + synchronize(POCLBackend()) + @test C == [2, 1, 4] + end +end + # a struct with a union field is not isbits, but is stored inline with its own selector struct UnionField x::Union{Int, Float32} From e1b4eb5d6bb0224cc8d3a17e7d0ed16f50cbb261 Mon Sep 17 00:00:00 2001 From: sasmith-14 <210313780+sasmith-14@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:02:08 +0530 Subject: [PATCH 2/3] Build the device array straight from a `Memory` Instead of wrapping it in an `Array` first, which allocated a small `Array` on every launch. --- src/pocl/pocl.jl | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/pocl/pocl.jl b/src/pocl/pocl.jl index 50e4ea448..cba54ea1a 100644 --- a/src/pocl/pocl.jl +++ b/src/pocl/pocl.jl @@ -220,8 +220,16 @@ end # VERSION >= v"1.11" @static if isdefined(Core, :Memory) - # wrapping `Memory` in an `Array` doesn't copy it - Adapt.adapt_storage(to::KernelAdaptor, xs::Memory) = adapt(to, Base.wrap(Array, xs, length(xs))) + function Adapt.adapt_storage(to::KernelAdaptor, xs::Memory{T}) where {T} + ptr = pointer(xs) + # memory of isbits unions stores its selector bytes past its data + maxsize = if Base.isbitsunion(T) + Int(UInt(@ccall(jl_genericmemory_typetagdata(xs::Any)::Ptr{UInt8})) - UInt(ptr)) + else + length(xs) * aligned_sizeof(T) + end + return CLDeviceArray{T, 1, AS.CrossWorkgroup}((length(xs),), reinterpret(LLVMPtr{T, AS.CrossWorkgroup}, ptr), maxsize) + end end import KernelInterface From 2200005d9e9c283e2cd22461127d66de9060ac7c Mon Sep 17 00:00:00 2001 From: sasmith-14 <210313780+sasmith-14@users.noreply.github.com> Date: Mon, 16 Nov 2026 00:11:40 +0530 Subject: [PATCH 3/3] Find the selector bytes of a `Memory` with `Base.elsize` Instead of calling the runtime function `jl_genericmemory_typetagdata`, which places them `length * elsize` bytes past the data. --- src/pocl/pocl.jl | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/pocl/pocl.jl b/src/pocl/pocl.jl index cba54ea1a..04786d73b 100644 --- a/src/pocl/pocl.jl +++ b/src/pocl/pocl.jl @@ -221,14 +221,9 @@ end # VERSION >= v"1.11" @static if isdefined(Core, :Memory) function Adapt.adapt_storage(to::KernelAdaptor, xs::Memory{T}) where {T} - ptr = pointer(xs) - # memory of isbits unions stores its selector bytes past its data - maxsize = if Base.isbitsunion(T) - Int(UInt(@ccall(jl_genericmemory_typetagdata(xs::Any)::Ptr{UInt8})) - UInt(ptr)) - else - length(xs) * aligned_sizeof(T) - end - return CLDeviceArray{T, 1, AS.CrossWorkgroup}((length(xs),), reinterpret(LLVMPtr{T, AS.CrossWorkgroup}, ptr), maxsize) + # memory of isbits unions stores its selector bytes right after its data + maxsize = length(xs) * Base.elsize(xs) + return CLDeviceArray{T, 1, AS.CrossWorkgroup}((length(xs),), reinterpret(LLVMPtr{T, AS.CrossWorkgroup}, pointer(xs)), maxsize) end end