Skip to content

Commit 07bbb7a

Browse files
committed
Fix loading on unsupported platforms (#459)
* Gate getpagesize call behind function * Make `functional` require a fully-supported GPU
1 parent 83037d0 commit 07bbb7a

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

lib/mtl/buffer.jl

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,23 @@ function MTLBuffer(dev::MTLDevice, bytesize::Integer, ptr::Ptr;
4949
return MTLBuffer(ptr)
5050
end
5151

52-
const PAGESIZE = ccall(:getpagesize, Cint, ())
52+
const _page_size::Ref{Int} = Ref{Int}(0)
53+
function page_size()
54+
if _page_size[] == 0
55+
_page_size[] = Int(ccall(:getpagesize, Cint, ()))
56+
end
57+
_page_size[]
58+
end
59+
5360
function can_alloc_nocopy(ptr::Ptr, bytesize::Integer)
5461
# newBufferWithBytesNoCopy has several restrictions:
5562
## the pointer has to be page-aligned
56-
if Int64(ptr) % PAGESIZE != 0
63+
if Int(ptr) % page_size() != 0
5764
return false
5865
end
5966
## the new buffer needs to be page-aligned
6067
## XXX: on macOS 14, this doesn't seem required; is this a documentation issue?
61-
if bytesize % PAGESIZE != 0
68+
if bytesize % page_size() != 0
6269
return false
6370
end
6471
return true

src/initialization.jl

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
const _functional = Ref{Bool}(false)
2-
functional() = _functional[]
1+
# Starts at `nothing`. Only false when it's determined
2+
const _functional = Ref{Union{Nothing,Bool}}(false)
3+
4+
function functional()
5+
if isnothing(_functional[])
6+
dev = device()
7+
8+
_functional[] =
9+
supports_family(dev, MTL.MTLGPUFamilyApple7) &&
10+
supports_family(dev, MTL.MTLGPUFamilyMetal3)
11+
end
12+
_functional[]
13+
end
314

415
function __init__()
516
precompiling = ccall(:jl_generating_output, Cint, ()) != 0
@@ -39,7 +50,10 @@ function __init__()
3950
load_framework("CoreGraphics")
4051
ver = MTL.MTLCompileOptions().languageVersion
4152
@debug "Successfully loaded Metal; targeting v$ver."
42-
_functional[] = true
53+
54+
# Successful loading of CoreGraphics means there's a
55+
# chance the graphics device is supported
56+
_functional[] = nothing
4357
catch err
4458
@error "Failed to load Metal" exception=(err,catch_backtrace())
4559
return

0 commit comments

Comments
 (0)