Skip to content

KernelInterface: dynamic local memory - #774

Draft
vchuravy wants to merge 2 commits into
mainfrom
vc/ki-dynamic-localmem
Draft

vchuravy wants to merge 2 commits into
mainfrom
vc/ki-dynamic-localmem

Conversation

@vchuravy

Copy link
Copy Markdown
Member

Implements KernelInterface's dynamic local memory for oneAPIBackend. The KernelInterface side is the ccr-c54f0813-yayzye branch of JuliaGPU/KernelAbstractions.jl; the CUDA counterpart is JuliaGPU/CUDA.jl#3620.

Nothing here has run on a Level Zero device. The machine it was written on has none; CI is the first run. Code generation was checked without a device.

Changes

  • Device: two intrinsics, julia.oneapi.dynamic_localmemory and julia.oneapi.dynamic_localmemory_size.
  • Compiler: a finish_linked_module! hook turns them into the last two kernel arguments with GPUCompiler.add_input_arguments!: a pointer in the Workgroup address space, then the requested size. Only kernels that use the block get them, and those are flagged on oneAPIResults and ZeKernel.
  • Launch: onecall takes localmem::Int=0. For a flagged kernel it sizes the pointer argument with zeKernelSetArgumentValue(kernel, index, localmem + 16, C_NULL) and sets the size argument. Kernels without the arguments ignore localmem.
  • Alignment: the driver's alignment of a local memory argument is unspecified, so the launch allocates 16 bytes more than asked and the kernel rounds the pointer up to 16 bytes. This also keeps the allocation from being empty when a launch asks for none.
  • Limit: KI.max_dynamic_localmemory is maxSharedLocalMemory of the device, less the kernel's localMemSize and the 16 bytes of padding.
  • @oneapi also accepts localmem. This goes beyond KernelInterface and is easy to drop.

What was checked without a device

  • GPUCompiler.compile to LLVM IR and SPIR-V with both back-ends, for five kernels: a kernel that doesn't use the block is unchanged; every kernel that does gets ptr addrspace(3) dynamic_localmemory, i64 dynamic_localmemory_size as its last two parameters. All ten SPIR-V modules pass spirv-val.
  • The new host functions infer concretely. They were not executed.

Assumptions to confirm in CI

  1. Setting an argument with a size and a null value allocates shared local memory. ze_api.h says so; the runtime behaviour is unverified, including that the size persists and that the validation layer accepts the null value.
  2. IGC treats a Workgroup pointer parameter as a local memory argument without OpenCL kernel_arg_addr_space metadata.
  3. The driver numbers the arguments the way onecall does, so the two new ones are at length(args) + 1 and + 2.
  4. localMemSize is the static local memory only and doesn't change once the argument is sized. The new test set asserts the limit formula exactly and launches at the limit; those assertions are the most likely to need adjusting.
  5. maxSharedLocalMemory is in bytes and bounds static plus argument local memory together.

Before merging

  • The last commit is temporary: it points [sources] at the KernelAbstractions branch. Julia 1.10 ignores [sources], so oneAPI.jl fails to load there until KernelInterface is released.
  • KernelInterface has no version bump for this feature yet, so the compat bound here needs raising once it is released.
  • The additional-argument helpers duplicate what OpenCL.jl needs for the same feature; they could move to SPIRVIntrinsics.

🤖 Generated with Claude Code

Support local memory whose size is chosen at launch: the `localmem`
launch keyword, `KI.dynamic_localmemory`, `KI.dynamic_localmemory_size`
and `KI.max_dynamic_localmemory`.

Kernels that use the block get two more arguments, after the others: a
pointer to local memory, and the size the launch asked for. They are
added by `add_input_arguments!` when the module has been linked, so
only kernels that use the block change. Launches of such a kernel size
the pointer argument, which makes Level Zero allocate the memory, also
when they ask for none.

Nothing specifies how the driver aligns that memory, so launches
allocate 16 bytes more than they were asked for, and the kernel aligns
the block itself. That also keeps the allocation from being empty.

A launch can ask for what the device has for a work-group, less the
static local memory of the kernel and those 16 bytes.

`@oneapi` and calls of a `HostKernel` take `localmem` as well.

Assisted-by: Claude Code (Fable 5.2)
…cal memory branch

Dynamic local memory needs the KernelInterface of
JuliaGPU/KernelAbstractions.jl's `ccr-c54f0813-yayzye` branch. To be
dropped when that is released.

Julia 1.10 ignores `[sources]`, so its tests keep using the released
packages.

Assisted-by: Claude Code (Fable 5.2)
@maleadt
maleadt force-pushed the vc/ki-dynamic-localmem branch from 09cb799 to e0ab4ff Compare December 2, 2026 10:40

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant