Repository navigation
Constify @Const arguments when launching instead of in the kernel - #872
Merged
Merged
Conversation
The POCL device array code came from OpenCL.jl, where `reinterpret` validates its arguments with `GPUArrays._reinterpret_exception`. KernelAbstractions doesn't depend on GPUArrays, so the call referred to an undefined name and every kernel using `reinterpret` failed to compile. Inline the same checks; the thrown value is dropped on the device anyway. Fixes #869
`@Const(B)` expanded to `B = constify(B)` at the top of the kernel body, so Adapt.jl's rules for whatever the argument contained ran in device code. Several of them rebuild wrappers through constructors that check their arguments and format the error message eagerly, which doesn't compile for GPUs: passing a `StepRangeLen` or `LinRange` as `@Const` failed on POCL, Metal and (for `LinRange`) CUDA, and #793 had to add device-safe rules for reshaped and permuted arrays. Instead, the launch wraps `@Const` arguments in a `ConstArg`, whose Adapt rule constifies the argument after the backend converted it. Adapt's rules now run on the host, and the backend still does its own conversion (and e.g. memory tracking). To find out which arguments to wrap, the launch calls the kernel function with `ConstArgs()` in place of the context: every kernel method starts by answering that with its mask, so the answer comes from the method the converted arguments dispatch to, and the check folds away for the actual context. Default arguments get explicit forwarding methods that answer before evaluating the defaults. The kernel sees the same constified types as before, so annotations on `@Const` arguments, which already had to accept them, keep working. `@Const` on varargs, which did not work before, is now rejected when expanding the kernel. Fixes #867
maleadt
force-pushed
the
tb/const-at-launch
branch
from
October 11, 2026 09:23
1f57668 to
f95ae43
Compare
Contributor
Benchmark ResultsShow table
Benchmark PlotsA plot of the benchmark results have been uploaded as an artifact to the workflow run for this PR. |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #872 +/- ##
==========================================
+ Coverage 79.89% 80.49% +0.60%
==========================================
Files 26 26
Lines 2149 2210 +61
==========================================
+ Hits 1717 1779 +62
+ Misses 432 431 -1 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
vchuravy
approved these changes
Oct 11, 2026
Member
|
Also matches well with JuliaGPU/GPUCompiler.jl#1002 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
@Const(B)used to expand toB = constify(B)at the top of the kernel body, so Adapt.jl's rules for whateverBcontains ran in device code. Several of those rules rebuild wrappers through Base constructors that validate their arguments and format the error message eagerly ("length cannot be negative, got $len"), which doesn't compile for GPUs. #867 hits this with aStepRangeLen, which Oceananigans grids carry and GPUArrays' reductions pass as@Const:This fails on POCL and Metal;
LinRangefails on CUDA too (CUDA.jl only happens to overrideStepRangeLen's constructor). #793 already had to add device-safe rules for reshaped and permuted arrays, and AcceleratedKernels dropped@Constfrom its mapreduce for the same reason.Running
constifyin the kernel goes back to the old CPU backend, which neededBase.Experimental.Constinside an@aliasscopein the same function; nothing requires it anymore since #785. So this PR constifies@Constarguments when launching instead, as CUDA (const __restrict__), SYCL (read accessors) and Kokkos (View<const T, RandomAccess>) put read-only-ness in the argument's type at the launch boundary:@Constarguments in aConstArg, whose Adapt rule constifies the argument after the backend converted it. Adapt's rules now run on the host, where validation is harmless, and the backend still does its own conversion (and memory tracking). This requires a backend's argument conversion to recurse with Adapt.jl, which all of them do; the backend notes now say so.ConstArgs()in place of the context. Every kernel method starts by answering that with its@Constmask, so the answer comes from the method the converted arguments dispatch to, and the check folds away for the real context. Separate metadata methods would not work for kernels defined in local scope (global definitions aren't allowed there, and extra methods box the closure, Do not capture recursive local function's self-identifier (to avoid box) JuliaLang/julia#53295). Default arguments get explicit forwarding methods that answer before evaluating the next default, so that defaults are only evaluated on the device.The kernel body sees the same constified types as before. Since the old expansion re-asserted an argument's type annotation on the constified value (
B::T = constify(B::T)), annotations on@Constarguments already had to accept the read-only type, so dispatch doesn't change for kernels that worked; the ones that change are those that failed before (e.g.@Const(B::P) where {P}). Which arguments are@Constis decided by the method the arguments select before being constified, so kernel methods that dispatch on the read-only type aren't supported; the docstring says so. Agenerated=truekernel's generator now sees the read-only type for@Constarguments, as the body already did.@Conston varargs, which didn't work before, is now rejected when expanding the kernel.The first commit fixes device-side
reinterpreton POCL, found along the way: it referred to GPUArrays'_reinterpret_exceptionwithout depending on GPUArrays (#869).Tested with the full test suite on POCL, CUDA.jl's and Metal.jl's (on an M1)
ka-0.10KernelAbstractions tests, with the new range tests failing on main (POCL, Metal) and passing here. The per-backend constructor quirks that would make these ranges work when constructed in device code are tracked in JuliaGPU/GPUToolbox.jl#29.Fixes #867, fixes #869