Skip to content

GPU: a kernel's read past an array's end gives 0 and is reported, as a write is - #1109

Open
ASDAlexander77 wants to merge 1 commit into
mainfrom
gpu-read-faults
Open

ASDAlexander77 wants to merge 1 commit into
mainfrom
gpu-read-faults

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

A kernel's read past an array's end is now reported, the same way a write past the end is.

What changes

  • The new rule. This applies to a @kernel that does not wait at a barrier. Before, such a read returned whatever GPU memory followed the array, or crashed the driver. Now:
    • the read gives 0;
    • the first one is recorded in the kernel's fault record;
    • after the kernel, the program stops with exit code 1 and prints:
      tslang gpu: the kernel 'k' read past the end of an array: element 4 of 4 (GPU thread 3)
      
  • What counts as a read: an element, a field of an element, or a typed array's view read past its own length.
  • Read and write in one fault record. The record's flag says which it was: 1 for a write, 2 for a read.
    • The runtime prints "read" or "wrote" accordingly.
    • a[i] += v past the end is reported as a read, because the read happens first.
  • Unchanged:
    • The CPU still does not check reads past the end.
    • Barrier kernels still give 0 silently, since their threads past n read their tiles that way.

Cost

A naive branch around each read reloaded the array's data pointer on every iteration of a loop. That made a matrix-vector kernel 1.6x slower on NVIDIA and 2.1x slower on Intel.

Two changes bring the cost down:

  • The read always happens. It loads from select(inside, element, zeroes), and only the cold record call sits behind a branch. The data pointer and length are then loaded once, before the loop.
  • The record call has its own alias scope. LLVM can then see that the call never touches an array header.

Matrix-vector, 2048 x 2048 floats, 200 launches:

GPU Unchecked Checked
NVIDIA RTX 5080 37 ms 37 ms
Intel iGPU 246 ms 405 ms

A stencil kernel is 3% slower on Intel and unchanged on NVIDIA.

Tests

  • New fault programs: faults/read.ts, read_field.ts, read_typed_array.ts.
  • All 26 GPU programs, under the JIT and as executables, and all 14 fault cases pass on:
    • NVIDIA, Windows;
    • Intel iGPU, Windows;
    • NVIDIA through WSL Linux.
  • The GPU and f16 ctests pass (90).
  • docs/gpu.md and faults/README.md are updated.

🤖 Generated with Claude Code

…a write is

A kernel that does not wait at a barrier read past an array's end unchecked: whatever GPU memory was there, or a
driver fault. Now such a read (an element, a field of one, a typed array's view past its own length) gives 0, the
first is recorded in the fault global, and the program stops after the kernel:

  tslang gpu: the kernel 'k' read past the end of an array: element 4 of 4 (GPU thread 3)

The record's flag says which (1 write, 2 read). The CPU still does not check a read past the end; barrier kernels
keep giving 0 silently (their threads past n read their tiles).

Cost, kept down: the read is always made, of the element or of a block of zeroes (a select), and only the record
is branched to, so the array's data pointer and length are loaded once before a loop; the record's call has an
alias scope of its own, never an array header. A matrix * vector kernel (2048 x 2048 floats, 200 launches):
NVIDIA 37 ms unchecked, 37 ms checked; Intel's iGPU 246 ms -> 405 ms (a branch around the read was 60 ms and 524).

Tests: faults/read.ts, read_field.ts, read_typed_array.ts; all 14 fault cases and all 26 programs pass on NVIDIA
and Intel, JIT and exe; GPU ctests pass.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

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