JuliaGPU / JuliaGPU/KernelAbstractions.jl

UndefVarError when variable is defined

Open
#647 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Julia
Stars
523
Forks
88
Avg merge
1d 11h
Merged PRs (30d)
25

Description

using KernelAbstractions

@kernel function mykernel(y)
    z = length(y)

    for i in 1:z
        @synchronize
    end
end

x = zeros(3)
backend = get_backend(x)

mykernel(backend)(x; ndrange=length(x))

KernelAbstractions.synchronize(backend)

This MWE yields:

ERROR: UndefVarError: `z` not defined in `Main`
Suggestion: check for spelling errors or missing imports.
Stacktrace:
 [1] cpu_mykernel
   @ ~/.julia/packages/KernelAbstractions/lGrz7/src/macros.jl:307 [inlined]
 [2] cpu_mykernel(__ctx__::KernelAbstractions.CompilerMetadata{…}, x::Vector{…})
   @ Main ./none:0
 [3] __thread_run(tid::Int64, len::Int64, rem::Int64, obj::KernelAbstractions.Kernel{…}, ndrange::Tuple{…}, iterspace::KernelAbstractions.NDIteration.NDRange{…}, args::Tuple{…}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:145
 [4] __run(obj::KernelAbstractions.Kernel{…}, ndrange::Tuple{…}, iterspace::KernelAbstractions.NDIteration.NDRange{…}, args::Tuple{…}, dynamic::KernelAbstractions.NDIteration.NoDynamicCheck, static_threads::Bool)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:112
 [5] (::KernelAbstractions.Kernel{…})(args::Vector{…}; ndrange::Int64, workgroupsize::Nothing)
   @ KernelAbstractions ~/.julia/packages/KernelAbstractions/lGrz7/src/cpu.jl:46
 [6] top-level scope
   @ Untitled-2:14
Some type information was truncated. Use `show(err)` to see complete types.

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

Run the Julia reproducer and inspect the generated path around macros.jl:307, especially the cpu_mykernel entry shown in the stack trace. Trace how the local variable z is handled across the kernel macro expansion; done means the example runs without the UndefVarError and preserves the loop behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
julia
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.