Skip to content

GPU: a kernel's write past an array's end is reported by name on every GPU - #1105

Merged
ASDAlexander77 merged 1 commit into
mainfrom
gpu-faults
Dec 2, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
gpu-faults

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

A kernel's write past an array's end is reported by name

Before this, a kernel that wrote past an array's end trapped. On NVIDIA and AMD the only report was a bare running the kernel failed (719 unspecified launch failure); on Intel the behaviour was undefined. Now the program stops after the kernel, on every GPU, with:

tslang gpu: the kernel 'k' wrote past the end of an array: element 1000 of 1000 (GPU thread 995)

and exit code 1, which matches what a kernel called on the CPU already does.

How

  • Guarded writes. In kernels that don't wait at a barrier, and in the functions they call, every element write is made only inside the array. That covers a store, +=, Atomics, a tuple's field and a whole tuple element. A write past the end is skipped, and the first one is recorded in the module's __tslang_gpu_fault global: a cmpxchg on its flag, then the thread, index and length.
  • Traps that stay. The compiler's existing check before a write is removed only when the write it protects was guarded (the compare's index is the guarded write's index). Any other check stays a trap, so no unguarded write past the end goes unnoticed.
  • Runtime. Each driver looks the record up when the module loads, with cuModuleGetGlobal_v2, hipModuleGetGlobal or zeModuleGetGlobalPointer. These entry points are optional: if one is missing, the driver still runs, just without the report. The runtime reads the record wherever it already waits for the GPU: a copying launch's own sync, gpuWait (gpuRead, gpuWrite, gpuFree, the end of a batch) and the program's end.
  • Barrier kernels are unchanged. They still drop such writes silently, as documented.

Cost

A write still costs the one compare it already had; the old past-end check folds into it in the PTX. A launch's total time is unchanged. Medians over 150 launches each, using executables (the JIT's object cache had reused objects built against the old runtime, so my first numbers compared the same code): 0.34 ms with the record read and 0.36 ms without at 1000 elements, 0.52 / 0.52 ms at 100k, 8.64 / 8.57 ms at 4M. Before this change (main) they are 0.36, 0.53 and 8.59 ms. The read only takes over a wait that copying the arrays back would otherwise pay.

Tested

  • tslang/test/gpu/faults/ has one program per way of writing: a store, +=, Atomics, a write in a helper, a field, a whole element, a negative index, a typed-array view, a GpuArray reported at gpuRead and at the program's end, and gpuBatch. All 11 report correctly with exit code 1 under the JIT and as executables, on the RTX 5080 on Windows and on Linux (WSL).
  • All 24 GPU programs still pass on both, with no false faults.
  • New ctest test-gpu-compile-faults checks for the record global, 6 record calls and no trap left, plus spirv-val on the Intel output.
  • The record global is visible on every target: .visible .global in the PTX, in the AMD object's dynamic symbols (protected), and with Export linkage in the SPIR-V.
  • Full Windows ctest: 10,491 tests run, none failing.
  • The AMD and Intel fault paths are compile-checked only.

🤖 Generated with Claude Code

…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>
@ASDAlexander77
ASDAlexander77 merged commit db866b6 into main Dec 2, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the gpu-faults branch December 2, 2026 15:54
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