Skip to content

@Const argument containing a StepRangeLen does not compile on the GPU #867

Closed

Description

@giordano

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:

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) = r

or, 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

  1. added a commit that references this issue on Oct 11, 2026
    f95ae43
  2. added a commit that references this issue on Oct 11, 2026
    f34a52b
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions