JuliaArrays / JuliaArrays/StaticArrays.jl

CUDA: `reinterpret` results in dynamic function invocation

Open
#1,312 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
844
Forks
159
Avg merge
3d 21h
Merged PRs (30d)
3

Description

Ref: https://github.com/JuliaGPU/AMDGPU.jl/issues/808
MWE (fails on Julia 1.10, works on 1.11+):

using CUDA, StaticArrays

@inline function add!(a::MMatrix{NC,NC,Complex{T},NC2}) where {NC,NC2,T}
    ar = reinterpret(reshape, T, a)
    @inbounds ar[1] += 1.0
    return a
end

function kernel!(a)
    i = threadIdx().x
    @inbounds a[i] = SMatrix(add!(MMatrix(a[i])))
    return
end

a = CUDA.zeros(SMatrix{3,3,ComplexF64,9}, 1)
@cuda threads=1 kernel!(a)

Error:

ERROR: LoadError: InvalidIRError: compiling MethodInstance for kernel!(::CuDeviceVector{SMatrix{3, 3, ComplexF64, 9}, 1}) resulted in invalid LLVM IR
Reason: unsupported dynamic function invocation (call to padding(T::DataType, baseoffset::Int64) @ Base reinterpretarray.jl:706)
Stacktrace:
 [1] padding
   @ ./reinterpretarray.jl:707
 [2] CyclePadding
   @ ./reinterpretarray.jl:727
 [3] array_subpadding
   @ ./reinterpretarray.jl:736
 [4] reinterpret
   @ ./reinterpretarray.jl:115
 [5] add!
   @ ~/code/t.jl:4
 [6] kernel!
   @ ~/code/t.jl:11
Reason: unsupported call to an unknown function (call to jl_f__svec_ref)
Stacktrace:
 [1] getindex
   @ ./essentials.jl:769
 [2] iterate (repeats 2 times)
   @ ./essentials.jl:779
 [3] iterate
   @ ./reinterpretarray.jl:693
 [4] array_subpadding
   @ ./reinterpretarray.jl:740
 [5] reinterpret
   @ ./reinterpretarray.jl:115
 [6] add!
   @ ~/code/t.jl:4
 [7] kernel!
   @ ~/code/t.jl:11
Hint: catch this exception as `err` and call `code_typed(err; interactive = true)` to introspect the erroneous code with Cthulhu.jl
Stacktrace:
  [1] check_ir(job::GPUCompiler.CompilerJob{GPUCompiler.PTXCompilerTarget, CUDA.CUDACompilerParams}, args::LLVM.Module)
    @ GPUCompiler ~/.julia/packages/GPUCompiler/3QaEh/src/validation.jl:167
...

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the MWE on Julia 1.10 and inspect Base's reinterpretarray.jl around padding, CyclePadding, and array_subpadding, along with GPUCompiler's validation.jl. Compare behavior with Julia 1.11+, then verify that the CUDA kernel compiles without dynamic function invocation.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
32/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.