Repository navigation
Description
A kernel argument marked @Const that contains a StepRangeLen fails to compile with an InvalidIRError. The same kernel compiles fine without @Const. This came up in CliMA/Oceananigans.jl#5799.
Reproducer
using Metal, KernelAbstractions
@kernel function copy_range!(dst, @Const(r))
i = @index(Global)
@inbounds dst[i] = r[i]
end
r = StepRangeLen(0f0, 0.5f0, 16) # StepRangeLen{Float32, Float32, Float32, Int64}
dst = Metal.zeros(Float32, 16)
copy_range!(MetalBackend())(dst, r; ndrange=16)ERROR: InvalidIRError: compiling MethodInstance for gpu_copy_range!(...) resulted in invalid LLVM IR
Reason: unsupported allocation of an object with references (Tuple{String, Int64})
Stacktrace:
[1] getindex
@ ./tuple.jl:33
[2] print_to_string
@ ./strings/io.jl:140
[3] multiple call sites
@ unknown:0
Reason: unsupported allocation of an object with references (Tuple{String, Int64, String, Int64})
...
Reason: unsupported allocation of an object with references (IOBuffer)
...
Dropping @Const from the signature makes it compile and give [0.0, 0.5, 1.0, 1.5, ...]. UnitRange, StepRange and Base.OneTo arguments compile fine under @Const.
Reproduced with:
- KernelAbstractions v0.9.44 + Metal v1.11.1 + Adapt v4.7.4 (released), Julia 1.13.1
- KernelAbstractions main (1c2be35) + Metal
ka-0.10branch (Implement KernelInterface, and support KernelAbstractions 0.10 Metal.jl#994)
Cause
@Const(x) lowers to x = constify(x), i.e. adapt(ConstAdaptor(), x) run inside the kernel body. Adapt.jl's rule for ranges rebuilds a StepRangeLen through its Base constructor:
adapt_structure(to, r::StepRangeLen{T}) where T =
StepRangeLen{T}(adapt(to, r.ref), adapt(to, r.step), r.len, r.offset)and that constructor's argument checks format strings at runtime ("length cannot be negative, got $len" and "StepRangeLen: offset must be in [1,$len], got $offset", which are exactly the Tuple{String, Int64} and Tuple{String, Int64, String, Int64} allocations above). Since len and offset come from the kernel argument, the error paths cannot be eliminated.
KernelAbstractions already works around the same problem for Base.ReshapedArray and PermutedDimsArray (src/KernelAbstractions.jl, "constify runs inside the kernel, where wrappers must be rebuilt without re-validating them"). StepRangeLen needs the same treatment.
Why it matters
GPUArrays' reductions (through AcceleratedKernels) mark their source @Const(src), so reducing any lazy array that carries a StepRangeLen now fails. In Oceananigans.jl uniform grids store coordinates as OffsetVector{FT, StepRangeLen}, so sum/maximum of any lazy operation on a GPU breaks with the new GPUArrays reductions.
Suggested fix
A range holds nothing to constify, so next to the existing special cases:
Adapt.adapt_structure(::ConstAdaptor, r::StepRangeLen) = ror, more generally, Adapt.adapt_structure(::ConstAdaptor, r::AbstractRange) = r. I verified that defining the StepRangeLen method in a session makes both the reproducer and the Oceananigans reductions compile and return correct results.
Activity
Constify `@Const` arguments when launching instead of in the kernel
Constify `@Const` arguments when launching instead of in the kernel (J…