Skip to content

Commit 1f46889

Browse files
authored
Allow controlling compilation target versions. (#430)
1 parent d6ca5c2 commit 1f46889

File tree

4 files changed

+42
-12
lines changed

4 files changed

+42
-12
lines changed

src/compiler/compilation.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,15 +75,22 @@ function compiler_config(dev; kwargs...)
7575
end
7676
return config
7777
end
78-
@noinline function _compiler_config(dev; kernel=true, name=nothing, always_inline=false, kwargs...)
79-
# TODO: configure the compiler target based on the device
80-
81-
macos = macos_version()
82-
metal = metal_support()
83-
# we support down to macOS 13, which supports AIR 2.5
84-
# so always target that version for now
85-
air = v"2.5"
86-
@assert air <= air_support()
78+
@noinline function _compiler_config(dev; kernel=true, name=nothing, always_inline=false,
79+
macos=nothing, air=nothing, metal=nothing,
80+
kwargs...)
81+
# determine the versions of things to target
82+
if macos === nothing
83+
macos = macos_version()
84+
end
85+
if metal === nothing
86+
metal = metal_support()
87+
end
88+
if air === nothing
89+
# we support down to macOS 13, which supports AIR 2.5
90+
# so always target that version for now
91+
air = v"2.5"
92+
@assert air <= air_support()
93+
end
8794

8895
# create GPUCompiler objects
8996
target = MetalCompilerTarget(; macos, air, metal, kwargs...)

src/compiler/execution.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ export @metal
44
## high-level @metal interface
55

66
const MACRO_KWARGS = [:launch]
7-
const COMPILER_KWARGS = [:kernel, :name, :always_inline]
7+
const COMPILER_KWARGS = [:kernel, :name, :always_inline, :macos, :air, :metal]
88
const LAUNCH_KWARGS = [:groups, :threads, :queue]
99

1010
"""
@@ -171,6 +171,10 @@ const mtlfunction_lock = ReentrantLock()
171171
Low-level interface to compile a function invocation for the currently-active GPU, returning
172172
a callable kernel object. For a higher-level interface, use [`@metal`](@ref).
173173
174+
The following keyword arguments are supported:
175+
- `macos`, `metal` and `air`: to override the macOS OS, Metal language and AIR bitcode
176+
versions used during compilation. Value should be a valid version number.
177+
174178
The output of this function is automatically cached, i.e. you can simply call `mtlfunction`
175179
in a hot path without degrading performance. New code will be generated automatically when
176180
the function changes, or when different types or keyword arguments are provided.

src/compiler/reflection.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ signature to `io` which defaults to `stdout`.
2727
2828
See also: [`@device_code_agx`](@ref)
2929
"""
30-
function code_agx(io::IO, @nospecialize(func), @nospecialize(types),
30+
function code_agx(io::IO, @nospecialize(func::Base.Callable), @nospecialize(types),
3131
kernel::Bool=true; kwargs...)
3232
compiler_kwargs, kwargs = split_kwargs_runtime(kwargs, COMPILER_KWARGS)
3333
source = methodinstance(typeof(func), Base.to_tuple_type(types))
@@ -170,7 +170,7 @@ function disassemble(path)
170170
return String(take!(io))
171171
end
172172

173-
code_agx(@nospecialize(func), @nospecialize(types); kwargs...) =
173+
code_agx(@nospecialize(func::Base.Callable), @nospecialize(types); kwargs...) =
174174
code_agx(stdout, func, types; kwargs...)
175175

176176
const code_native = code_agx

test/execution.jl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ dummy() = return
2121
@metal groups=(1,1) dummy()
2222
@metal groups=(1,1,1) dummy()
2323

24+
@metal macos=Metal.macos_version() dummy()
25+
@metal metal=Metal.metal_support() dummy()
26+
@metal air=Metal.air_support() dummy()
27+
2428
@test_throws InexactError @metal threads=(-2) dummy()
2529
@test_throws InexactError @metal groups=(-2) dummy()
2630
@test_throws ArgumentError @metal threads=(1025) dummy()
@@ -95,6 +99,21 @@ end
9599
@metal name="mykernel" dummy()
96100
end)))
97101

102+
# set macOS, AIR, and Metal versions
103+
let
104+
@test occursin("""!{!"Metal", i32 3, i32 2, i32 1}""",
105+
sprint(io->Metal.code_llvm(io, dummy, Tuple{}; metal=v"3.2.1",
106+
dump_module=true, kernel=true)))
107+
108+
@test occursin("!{i32 3, i32 2, i32 1}",
109+
sprint(io->Metal.code_llvm(io, dummy, Tuple{}; air=v"3.2.1",
110+
dump_module=true, kernel=true)))
111+
112+
@test occursin("""!"SDK Version", [3 x i32] [i32 3, i32 2, i32 1]}""",
113+
sprint(io->Metal.code_llvm(io, dummy, Tuple{}; macos=v"3.2.1",
114+
dump_module=true, kernel=true)))
115+
end
116+
98117
@test Metal.return_type(identity, Tuple{Int}) === Int
99118
@test Metal.return_type(sin, Tuple{Float32}) === Float32
100119
@test Metal.return_type(getindex, Tuple{MtlDeviceArray{Float32,1,1},Int32}) === Float32

0 commit comments

Comments
 (0)