Skip to content

GPU: gpuBatch keeps typed arrays on the GPU, by their elements - #1106

Merged
ASDAlexander77 merged 3 commits into
mainfrom
gpu-batch-views
Dec 2, 2026
Merged

ASDAlexander77 merged 3 commits into
mainfrom
gpu-batch-views

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

gpuBatch keeps typed arrays on the GPU too

Inside gpuBatch, plain arrays stay on the GPU between launches, but a typed array was copied in and out at every launch. Each launch builds the typed array its own view header, so the batch never recognised it as the same array. This change makes the batch recognise it.

  • Compiler: a typed-array argument's kind is now elementSize | 0x80000 (GPU_ARRAY_VIEW) instead of the per-launch flag.
  • Runtime: inside a batch, a view is kept by its elements, meaning its first element, length, element size and GPU.
    • The same typed array, or a new subarray over the same elements, is the GPU's copy at every launch after the first.
    • Subarrays of one buffer that don't overlap are separate arrays on the GPU.
    • A view that overlaps one the GPU already holds first waits for that one and copies it back, then is copied in itself. It therefore sees what kernels wrote to the other, and the two copies never drift apart.
    • An empty view stays per-launch.
  • Keeping arrays alive: the batch's table holds a pointer into the elements, and the collector (built with ALL_INTERIOR_POINTERS) treats that as a reference. A typed array created for one step and then dropped therefore stays alive until the batch ends.
  • Also fixed: a stale "(on the GPU that is a trap)" in __tslang_gpu_past_end's message, left over from GPU: a kernel's write past an array's end is reported by name on every GPU #1105.

Tested

  • gpu_batch_views.ts covers four cases:
    • 100 saxpy launches on the same Float32Arrays, where only the first copies anything in (checked with TSLANG_GPU_PROFILE);
    • non-overlapping subarrays;
    • overlapping subarrays;
    • temporary Int32Arrays collected between steps.
  • All 25 GPU programs and the 11 fault cases pass under the JIT and as executables, on the RTX 5080 on Windows and on Linux (WSL).
  • Full Windows ctest before the merge of main: 10,491 tests run, none failing. The 72 GPU ctests pass after the merge.

🤖 Generated with Claude Code

ASDAlexander77 and others added 3 commits December 2, 2026 14:53
…ry GPU, instead of a bare driver error

A trap made it "running the kernel failed (719 unspecified launch failure)" on NVIDIA and AMD, and undefined on
Intel. Now each element write in a kernel that does not wait at a barrier (a store, +=, Atomics, a tuple's field
or whole element, in the kernel or a function it calls) is made only inside the array; the first one past the end
is recorded in the module's __tslang_gpu_fault (a cmpxchg on its flag, then thread, index, length), and the
runtime reads it where it waits for the kernel (the launch's own sync, gpuWait, the program's end) and stops:
"tslang gpu: the kernel 'k' wrote past the end of an array: element 1000 of 1000 (GPU thread 995)", exit 1.
The compiler's own check before a write goes only where that write is guarded; any other stays a trap. Drivers
look the record up with cuModuleGetGlobal_v2 / hipModuleGetGlobal / zeModuleGetGlobalPointer, optional (missing:
no report, the driver still runs). Barrier kernels keep dropping such writes silently. The write costs the one
comparison it had; a launch's time is unchanged (0.066 ms median either way). tslang/test/gpu/faults/ has a
program per way of writing: all 11 report right on the RTX 5080, JIT and exe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A typed array reaches a launch as a view header of its own (made for the launch), so gpuBatch could not find it
again and copied it each launch (GPU_ARRAY_PER_LAUNCH). Now its kind says GPU_ARRAY_VIEW (0x80000) and the batch
keeps it by its first element, length and element size: the same typed array, or a new subarray of the same
elements, is the GPU's copy at every launch after the first. A view whose elements overlap another the GPU holds
(subarrays of one buffer) sends that one back first (after the GPU is done with it) and is copied in from there,
so the two copies never part. The table's pointer into the elements keeps the typed array's store alive (the
collector takes interior pointers). gpu_batch_views.ts: 100 saxpy launches copy in once; apart, overlapping and
temporary (collected between steps) typed arrays all right on the RTX 5080. The stale "on the GPU that is a trap"
in __tslang_gpu_past_end's message is gone.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 4cae358 into main Dec 2, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the gpu-batch-views branch December 2, 2026 16:39
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