Skip to content

Commit 2f2d75f

Browse files
maleadtclaude
andauthored
Fix @device_code_tiled for kernels with Constant arguments (#127)
The compile_hook in emit_ir passed unwrapped types to code_tiled because launch does MI lookup with Constant stripped to its element type. This caused @device_code_tiled to fail with "Unsupported Julia type for Tile IR" for any kernel using Constant args (including the tiled broadcast kernels). Reconstruct Constant{T,V} types from const_argtypes in the hook so code_tiled can recover const-seeded arguments. Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 1ea58d9 commit 2f2d75f

2 files changed

Lines changed: 36 additions & 2 deletions

File tree

src/compiler/interface.jl

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,24 @@ IR where constant values are folded in.
376376
function emit_ir(cache::CacheView, mi::Core.MethodInstance;
377377
const_argtypes::Union{Vector{Any}, Nothing}=nothing)
378378
# Invoke compile hook if set (for @device_code_* reflection)
379-
# Pass (f, tt) tuple to enable direct use with reflection utilities
379+
# Pass (f, tt) tuple to enable direct use with reflection utilities.
380+
# Reconstruct Constant{T,V} types from const_argtypes so that code_tiled
381+
# can recover const-seeded arguments (MI specTypes have them unwrapped).
380382
if compile_hook[] !== nothing
381383
ftype = mi.specTypes.parameters[1]
382384
f = isdefined(ftype, :instance) ? ftype.instance : ftype
383-
tt = Tuple{mi.specTypes.parameters[2:end]...}
385+
arg_types = collect(Any, mi.specTypes.parameters[2:end])
386+
if const_argtypes !== nothing
387+
# const_argtypes is [Const(f), arg2, ...]; arg_types omits f,
388+
# so arg_types[i] corresponds to const_argtypes[i+1].
389+
for i in eachindex(arg_types)
390+
if const_argtypes[i+1] isa CC.Const
391+
val = const_argtypes[i+1].val
392+
arg_types[i] = Constant{typeof(val), val}
393+
end
394+
end
395+
end
396+
tt = Tuple{arg_types...}
384397
compile_hook[](f, tt)
385398
end
386399

test/execution/core.jl

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,27 @@ end # invalidations
197197
ct.@device_code_typed io=buf ct.launch(reflect_vadd, cld(n, 16), a, b, c)
198198
String(take!(buf))
199199
end
200+
201+
# @device_code_tiled with Constant arguments
202+
function reflect_const_vadd(a::ct.TileArray{Float32,1}, b::ct.TileArray{Float32,1},
203+
c::ct.TileArray{Float32,1}, tile_size::Int)
204+
pid = ct.bid(1)
205+
tile_a = ct.load(a, pid, (tile_size,))
206+
tile_b = ct.load(b, pid, (tile_size,))
207+
ct.store(c, pid, tile_a + tile_b)
208+
return
209+
end
210+
211+
c2 = CUDA.zeros(Float32, n)
212+
@test @filecheck begin
213+
@check "entry @reflect_const_vadd"
214+
@check "load_view"
215+
@check "addf"
216+
@check "store_view"
217+
ct.@device_code_tiled ct.launch(reflect_const_vadd, cld(n, 16), a, b, c2,
218+
ct.Constant(16))
219+
end
220+
@test Array(c2) Array(a) + Array(b)
200221
end
201222

202223
@testset "assert" begin

0 commit comments

Comments
 (0)