Skip to content

Allow return or return nothing in kernels #443

Closed

Description

@eschnett

I use JuliaFormatter to format my source code. This adds explicit return statements. This leads to

ERROR: LoadError: Return statement not permitted in a kernel function sum2_kernel!

even if the return statement is just return nothing.

At the same time, ending a kernel with an expression (which is equivalent to a return statement) is allowed.

Could this check be changed to allow only empty return or return nothing statements?

Activity

  1. vchuravy commented on Dec 18, 2023

    @vchuravy
    Member

    At the same time, ending a kernel with an expression (which is equivalent to a return statement) is allowed.

    KernelAbstractions will politly ignore those and insert a return nothing for you.
    But yes we could explicitly allow return or return nothing

  2. pxl-th commented on Jan 9, 2024

    @pxl-th
    Member

    Closed by #446.

  3. AntonReinhard commented on Oct 22, 2025

    @AntonReinhard

    Related to this, I ran into a problem (or at least inconvenience) when defining local anonymous functions inside kernels:

    @kernel function foo(v)
        bar = x -> begin y = 2*x; return y end
        ID = @index(Global)
        v[ID] = bar(v[ID])
    end
    

    Is not allowed by KernelAbstractions and tells me that I can't use return inside a kernel, even though this return clearly doesn't return from the kernel. This problem is fixed if I use instead:

    @kernel function foo(v)
        bar = x -> begin y = 2*x; y end
        ID = @index(Global)
        v[ID] = bar(v[ID])
    end
    

    Which is valid, compiles, and runs as expected.

  4. szabo137 commented on Oct 22, 2025

    @szabo137

    +1 on that

  5. added
    kernels
    on Oct 19, 2026
  6. maleadt commented on Oct 19, 2026

    @maleadt
    Member

    Fixed by #556, which removed the check: return and return nothing are accepted in 0.10, including early returns and return inside closures defined in the kernel.

    @kernel function f!(A)
        i = @index(Global)
        bar = x -> begin y = 2x; return y end
        i > 2 && return
        A[i] = bar(i)
        return nothing
    end
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

    kernels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions