Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/jlgen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@ export methodinstance, generic_methodinstance

@inline function signature_type_by_tt(ft::Type, tt::Type)
u = Base.unwrap_unionall(tt)::DataType
return Base.rewrap_unionall(Tuple{dispatch_key(ft), map(dispatch_key, u.parameters)...}, tt)
# `methodinstance` calls this on every kernel launch, so for constant `ft` and `tt`
# this needs to fold to a constant, which `map` over the parameters prevents by
# allocating a `Vector`. Without `Type{...}` elements, plain splatting is cheapest.
sig = if Base.isType(ft) || any(Base.isType, u.parameters)
Tuple{dispatch_key(ft), (dispatch_key(p) for p in u.parameters)...}
else
Tuple{ft, u.parameters...}
end
return Base.rewrap_unionall(sig, tt)
end
else
@inline function signature_type_by_tt(ft::Type, tt::Type)
Expand Down
16 changes: 16 additions & 0 deletions test/native.jl
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,22 @@ end
@test Base.isdispatchtuple(mi.specTypes)
end

# on 1.10, `methodinstance` is the generic, allocating lookup
VERSION >= v"1.11.0-DEV.1552" && @testset "method instance lookups don't allocate" begin
# back-ends look up the method instance on every kernel launch
mod = @eval module $(gensym())
import GPUCompiler
kernel(args...) = nothing
lookup() = GPUCompiler.methodinstance(typeof(kernel),
$(Tuple{Vector{Int}, ntuple(_ -> Int, 40)...}))
lookup_type() = GPUCompiler.methodinstance(typeof(kernel), Tuple{Type{Int}, Int})
end
mod.lookup()
@test (@allocated mod.lookup()) == 0
mod.lookup_type()
@test (@allocated mod.lookup_type()) == 0
end

@testset "compilation" begin
@testset "callable structs" begin
mod = @eval module $(gensym())
Expand Down
Loading