Skip to content

Commit 5fc8d91

Browse files
authored
Metal: Support vector-valued intrinsics. (#515)
1 parent 947507e commit 5fc8d91

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/metal.jl

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -880,18 +880,23 @@ function lower_llvm_intrinsics!(@nospecialize(job::CompilerJob), fun::LLVM.Funct
880880

881881
# determine type of the intrinsic
882882
typ = value_type(call)
883-
if typ isa LLVM.IntegerType
884-
fn *= signed::Bool ? ".s" : ".u"
885-
fn *= ".$(width(typ))"
886-
elseif typ == LLVM.HalfType()
887-
fn *= ".f16"
888-
elseif typ == LLVM.FloatType()
889-
fn *= ".f32"
890-
elseif typ == LLVM.DoubleType()
891-
fn *= ".f64"
892-
else
893-
error("Unsupported intrinsic type: $typ")
883+
function type_suffix(typ)
884+
# XXX: can't we use LLVM to do this kind of mangling?
885+
if typ isa LLVM.IntegerType
886+
(signed::Bool ? "s" : "u") * "$(width(typ))"
887+
elseif typ == LLVM.HalfType()
888+
"f16"
889+
elseif typ == LLVM.FloatType()
890+
"f32"
891+
elseif typ == LLVM.DoubleType()
892+
"f64"
893+
elseif typ isa LLVM.VectorType
894+
"v$(size(typ))$(type_suffix(eltype(typ)))"
895+
else
896+
error("Unsupported intrinsic type: $typ")
897+
end
894898
end
899+
fn *= "." * type_suffix(typ)
895900

896901
new_intr = LLVM.Function(mod, fn, call_ft)
897902
@dispose builder=IRBuilder() begin

test/metal_tests.jl

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ end
7777
@test !occursin(r"call i32 @julia.air.thread_position_in_threadgroup.i32", ir)
7878
end
7979

80+
@testset "vector intrinsics" begin
81+
foo(x, y) = ccall("llvm.smax.v2i64", llvmcall, NTuple{2, VecElement{Int64}},
82+
(NTuple{2, VecElement{Int64}}, NTuple{2, VecElement{Int64}}), x, y)
83+
84+
ir = sprint(io->Metal.code_llvm(io, foo, (NTuple{2, VecElement{Int64}}, NTuple{2, VecElement{Int64}})))
85+
@test occursin("air.max.v2s64", ir)
86+
end
87+
8088
end
8189

8290
end

0 commit comments

Comments
 (0)