Skip to content

Commit 7be0d40

Browse files
committed
Keep minimal support for non-BDA capable drivers.
1 parent 08fd080 commit 7be0d40

File tree

10 files changed

+33
-32
lines changed

10 files changed

+33
-32
lines changed

.github/workflows/Test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ jobs:
2626
os: [ubuntu-24.04, ubuntu-24.04-arm, macOS-13, macOS-15, windows-2025]
2727
arch: [x64, arm64]
2828
pocl: [jll, local]
29-
memory_backend: [usm, bda, svm]
29+
memory_backend: [usm, svm, buffer]
3030
exclude:
3131
# unsupported combinations
3232
- os: ubuntu-24.04

LocalPreferences.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[OpenCL]
22
# Which memory back-end to use for unspecified CLArray allocations. This can be:
3-
# - "usm": Unified Shared Memory (`cl_intel_unified_shared_memory`)
4-
# - "bda": plain buffers (`cl_mem` + `cl_ext_buffer_device_address`)
5-
# - "svm": Shared Virtual Memory (coarse-grained)
3+
# - "buffer": plain buffers (using pointers if `cl_ext_buffer_device_address` is available)
4+
# - "usm": Unified Shared Memory (requiring `cl_intel_unified_shared_memory`)
5+
# - "svm": Shared Virtual Memory (requiring coarse-grained SVM support)
66
# If unspecified, the default will be used based on the platform and device capabilities.
77
#default_memory_backend="..."

lib/cl/device.jl

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,7 @@ function exec_capabilities(d::Device)
190190
)
191191
end
192192

193-
function bda_supported(d::Device)
194-
"cl_ext_buffer_device_address" in d.extensions || return false
195-
return true
196-
end
197-
198-
function usm_supported(d::Device)
199-
"cl_intel_unified_shared_memory" in d.extensions || return false
200-
return true
201-
end
193+
usm_supported(d::Device) = "cl_intel_unified_shared_memory" in d.extensions
202194

203195
function usm_capabilities(d::Device)
204196
usm_supported(d) || throw(ArgumentError("Unified Shared Memory not supported on this device"))
@@ -261,6 +253,8 @@ function svm_capabilities(d::Device)
261253
)
262254
end
263255

256+
bda_supported(d::Device) = false#"cl_ext_buffer_device_address" in d.extensions
257+
264258
function cl_device_type(dtype::Symbol)
265259
if dtype == :all
266260
cl_dtype = CL_DEVICE_TYPE_ALL

lib/cl/kernel.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ function set_arg!(k::Kernel, idx::Integer, arg::CLPtr{T}) where {T}
6969
end
7070

7171
# raw memory
72-
function set_arg!(k::Kernel, idx::Integer, arg::AbstractMemory)
72+
function set_arg!(k::Kernel, idx::Integer, arg::AbstractPointerMemory)
7373
# XXX: this assumes that the receiving argument is pointer-typed, which is not the case
7474
# with Julia's `Ptr` ABI. Instead, one should reinterpret the pointer as a
7575
# `Core.LLVMPtr`, which _is_ pointer-valued. We retain this handling for `Ptr` for

lib/cl/memory.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,6 @@ Base.unsafe_convert(::Type{cl_mem}, mem::AbstractMemoryObject) = mem.id
8383
# for passing buffers to kernels: pass the private device pointer
8484
Base.convert(::Type{CLPtr{T}}, mem::AbstractMemoryObject) where {T} =
8585
convert(CLPtr{T}, pointer(mem))
86-
# XXX: for passing buffers directly, we can support non-BDA drivers
87-
# by postponing the conversion to `cl.set_arg!`
88-
#Base.unsafe_convert(::Type{<:Ptr}, mem::AbstractMemoryObject) = mem
8986

9087
include("memory/buffer.jl")
9188

lib/cl/memory/buffer.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ end
99

1010
Buffer() = Buffer(C_NULL, CL_NULL, 0, context())
1111

12-
Base.pointer(buf::Buffer) = @something buf.ptr error("Buffer does not have a device private address")
12+
Base.pointer(buf::Buffer) = @something buf.ptr error("Conversion of a buffer to a pointer is not supported by this device")
1313
Base.sizeof(buf::Buffer) = buf.bytesize
1414
context(buf::Buffer) = buf.context
1515

@@ -18,7 +18,7 @@ context(buf::Buffer) = buf.context
1818

1919
# for internal use
2020
function Buffer(sz::Int, flags::Integer, hostbuf=nothing;
21-
device=:rw, host=:rw, device_private_address=false)
21+
device=:rw, host=:rw, device_private_address=bda_supported(cl.device()))
2222
if device == :rw
2323
flags |= CL_MEM_READ_WRITE
2424
elseif device == :r

lib/cl/state.jl

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ end
161161
abstract type AbstractMemoryBackend end
162162
struct SVMBackend <: AbstractMemoryBackend end
163163
struct USMBackend <: AbstractMemoryBackend end
164-
struct BDABackend <: AbstractMemoryBackend end
164+
struct BufferBackend <: AbstractMemoryBackend end
165165

166166
function supported_memory_backends(dev::Device)
167167
backends = AbstractMemoryBackend[]
@@ -175,10 +175,10 @@ function supported_memory_backends(dev::Device)
175175
end
176176
end
177177

178-
# plain old device buffers are second choice, but require an extension to support being
179-
# referenced by raw pointers.
178+
# plain old buffers are always supported, but we only want to use them if we have the
179+
# buffer device address extension, which allows us to reference them by raw pointers.
180180
if bda_supported(dev)
181-
push!(backends, BDABackend())
181+
push!(backends, BufferBackend())
182182
end
183183

184184
# shared virtual memory is last, because it comes at a performance cost.
@@ -187,12 +187,17 @@ function supported_memory_backends(dev::Device)
187187
push!(backends, SVMBackend())
188188
end
189189

190+
if isempty(backends)
191+
# as a last resort, use plain buffers without the ability to reference by pointer.
192+
# this severely limits compatibility, but it's better than nothing.
193+
push!(backends, BufferBackend())
194+
end
195+
190196
return backends
191197
end
192198

193199
function default_memory_backend(dev::Device)
194200
supported_backends = supported_memory_backends(dev)
195-
isempty(supported_backends) && return nothing
196201

197202
backend_str = load_preference(OpenCL, "default_memory_backend")
198203
backend_str === nothing && return first(supported_backends)
@@ -201,8 +206,8 @@ function default_memory_backend(dev::Device)
201206
USMBackend()
202207
elseif backend_str == "svm"
203208
SVMBackend()
204-
elseif backend_str == "bda"
205-
BDABackend()
209+
elseif backend_str == "buffer"
210+
BufferBackend()
206211
else
207212
error("Unknown memory backend '$backend_str' requested")
208213
end
@@ -212,9 +217,14 @@ end
212217

213218
function memory_backend()
214219
return get!(task_local_storage(), :CLMemoryBackend) do
215-
backend = default_memory_backend(device())
220+
dev = device()
221+
backend = default_memory_backend(dev)
216222
if backend === nothing
217-
error("Device $(device()) does not support any of the available memory backends")
223+
error("Device $(dev) does not support any of the available memory backends")
224+
end
225+
if backend === BufferBackend() && !bda_supported(dev)
226+
@warn """Your device $(dev.name) does not support the necessary extensions for OpenCL.jl's memory management (requiring either USM, coarse-grained SVM, or BDA).
227+
Falling back to plain OpenCL buffers, which severely limits compatibility with other OpenCL.jl, only supporting OpenCL C kernels.""" maxlog=1 _id="memory_backend_$(dev.name)"
218228
end
219229
backend
220230
end

src/array.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ function memory_type()
9898
return cl.UnifiedDeviceMemory
9999
elseif cl.memory_backend() == cl.SVMBackend()
100100
return cl.SharedVirtualMemory
101-
elseif cl.memory_backend() == cl.BDABackend()
101+
elseif cl.memory_backend() == cl.BufferBackend()
102102
return cl.Buffer
103103
end
104104
end

src/memory.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ end
144144

145145
function alloc(::Type{cl.Buffer}, bytes::Int; alignment::Int = 0)
146146
# TODO: use alignment
147-
buf = cl.Buffer(bytes; device_private_address = true)
147+
buf = cl.Buffer(bytes)
148148
return Managed(buf)
149149
end
150150

src/util.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ function versioninfo(io::IO=stdout)
8787
push!(tags, "svm"*suffix)
8888
elseif backend isa cl.USMBackend
8989
push!(tags, "usm"*suffix)
90-
elseif backend isa cl.BDABackend
91-
push!(tags, "bda"*suffix)
90+
elseif backend isa cl.BufferBackend
91+
push!(tags, "buffer"*suffix)
9292
end
9393
end
9494
## relevant extensions

0 commit comments

Comments
 (0)