Skip to content

GPU: arrays of tuples and object types as kernel arguments (+ boolean[]/byte[]) - #1103

Merged
ASDAlexander77 merged 4 commits into
mainfrom
gpu-structs
Dec 2, 2026
Merged

ASDAlexander77 merged 4 commits into
mainfrom
gpu-structs

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Arrays of tuples and object types as kernel arguments

Stacked on #1101 (warp builtins): merge that first. After it merges, I'll rebase this branch onto main so only the last two commits remain.

type Particle = { x: number, y: number, vx: number, vy: number };

@kernel
function step(i: int, ps: Particle[], dt: number) {
    ps[i].vy = ps[i].vy - 9.8 * dt;
    ps[i].x = ps[i].x + ps[i].vx * dt;
}

gpuLaunch(step, n, ps, 0.1);
  • { x: number, y: number }[] and [number, int][] already hold their elements by value as naturally aligned LLVM structs, which is also what a GPU wants. A kernel can read and write fields (ps[i].x), nested fields (bodies[i].pos.x) and whole elements (ps[i] = { x, y }).
  • gpuElementSize computes an element's size with LLVM's natural layout: numbers, integers, floats, booleans and nested tuples, each at its alignment. The launch, gpuReduce, gpuBatch and GpuArray all use it.
  • An element with a string, array or class field is refused, because those fields are pointers into the CPU's memory. Arrays of class objects are not supported either, since they hold pointers.
  • In a kernel that waits at gpuBarrier, the bounds guard now looks through field GEPs, so ps[i].y = ... from a thread past n is dropped.
  • Fixes a bug where arrays of 1-byte elements (boolean[], byte[]) were refused. The check meant for plain scalars also caught any array whose elements were 1 byte.

Tested

  • gpu_structs.ts runs on the RTX 5080 under the JIT and as an executable, on Windows and on Linux (WSL). It covers:
    • 10,000 particles stepped on the GPU and on the CPU, with identical results;
    • fields of every size with padding between them;
    • tuples, nested objects and whole-element writes;
    • GpuArray, gpuReduce, and a barrier kernel with an array longer than n;
    • boolean[] and byte[].
  • All 24 GPU programs pass on Windows, and on Linux (WSL) at the first commit. The 1-byte fix commit was run on Windows only.
  • New ctests:
    • a compile of compile/structs.ts for NVIDIA, AMD and Intel, checking the element sizes 32 (with padding), 16 (read-only) and 24, plus spirv-val;
    • an error test for an element with a string field.
  • Full Windows ctest: 10,481 tests run, none failing.
  • AMD and Intel are compile-checked only.

🤖 Generated with Claude Code

ASDAlexander77 and others added 4 commits December 2, 2026 12:23
… gpuBallot, gpuAll, gpuAny

Stubs lowered per GPU: NVIDIA shfl.sync/vote.sync over activemask, AMD ds_bpermute and ballot (wave32 from
gfx10; gpuBallot refused on wave64), Intel OpSubgroupShuffleINTEL (SPV_INTEL_subgroups, given to the SPIR-V
backend through its -spirv-ext option) and sub-group reduce/all/any. A kernel that uses them runs as a barrier
kernel (every thread; writes past n dropped); its votes leave out the threads past n. Its block must be whole
warps (a multiple of 32), gpuReduce refuses it, and a CPU call is an error. gpu_warp.ts and gpu_scan.ts (a prefix
sum of shuffles and shared memory) pass on the RTX 5080, JIT and exe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…x (WSL) too

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…: number }[], [number, int][]

Their elements are structs held by value, one after another, laid out alike on the CPU and every GPU: the launch
passes each element's size (gpuElementSize: numbers, integers, floats, booleans and nested tuples, each at its
alignment), and gpuReduce, GpuArray and gpuBatch take them as they take numbers. A barrier kernel's guard sees
through a field's GEP to the element. A tuple with a string, an array or a class in it is refused.
gpu_structs.ts (particles, padded fields, nested, whole elements, GpuArray, gpuReduce, a barrier kernel past n)
passes on the RTX 5080, JIT and exe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…k no longer refuses them; docs for struct arrays

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit e60ca38 into main Dec 2, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the gpu-structs branch December 2, 2026 13:53
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