Skip to content

GPU: Uint8ClampedArray/Float16Array kernel args, f16 promotion fix, and the first run on an Intel GPU - #1108

Merged
ASDAlexander77 merged 2 commits into
mainfrom
gpu-clamped-half
Dec 2, 2026
Merged

ASDAlexander77 merged 2 commits into
mainfrom
gpu-clamped-half

Conversation

@ASDAlexander77

@ASDAlexander77 ASDAlexander77 commented Dec 2, 2026 •

Copy link
Copy Markdown
Owner

Uint8ClampedArray and Float16Array can now be kernel arguments.

What changes

  • How they are passed. Both are the default library's ConvertingTypedArray<S> wrapping a TypedArray<S>. gpuLaunch, gpuReduce, gpuBatch and CPU calls pass the inner TypedArray's view:
    • a Float16Array's u16 store becomes f16[];
    • a Uint8ClampedArray becomes u8[].
  • Clamped writes. Writes to a Uint8ClampedArray parameter's elements are clamped, as you ruled: =, the compound assignments, and ++/--. Each goes through ToUint8Clamp, a generic source helper .u8_clamp: values go into 0..255, ties round to even, and NaN becomes 0. A postfix expression's value is the element before the write.
  • Atomics. Using Atomics on a Uint8ClampedArray is a compile error, as in JavaScript.

Language fix: f16 with an integer is f16

The numeric promotion ladder ranked f16 below s32, so f16 * 2 was an s32 multiply: 0.3 * 2 gave 0. f16 now sits next to f32, above the 32-bit integers, so an f16 combined with an integer is an f16, the same as other floats. New asserts in 00f16_conversions.ts cover this.

Intel: first run on hardware

The Intel iGPU (Core Ultra 9 285K, Level Zero on Windows) ran the GPU programs for the first time. 19 of 26 passed; the fixes below (a47cf73) make all 26 pass, as executables and under the JIT, and all 11 fault cases.

  • Shared-memory kernels failed to load. The barrier guard's to_local(p) has no Intel driver builtin for the pointer types the SPIR-V backend produces.
    • When optimization shows which array a pointer points into, whether it is shared memory is decided at compile time.
    • Otherwise, an address-range check against the shared arrays of the kernels that reach the call replaces it.
  • Barrier kernels hung. O3's loop unswitching and jump threading copied OpenCL's barrier() into branches that threads take differently, and an Intel subgroup then waits forever at one copy.
    • Every device function is now convergent, as clang does for CUDA and OpenCL.
    • NVIDIA's bar.sync counts threads, which is why it worked there.
  • gpuWarpSize() differed between kernels. The driver picks 8, 16 or 32 lanes for each kernel, so each Intel kernel is now pinned to 16 (intel_reqd_sub_group_size).
  • Float sqrt wasn't correctly rounded. OpenCL allows an error of up to 3 ulps, so modules are now built with -cl-fp32-correctly-rounded-divide-sqrt.
  • Math.ceil of a value in (-1, -0] gave 0 instead of -0. It is now copysign(ceil(v), v), which is exact on every GPU.

Tests

  • gpu_clamped_half.ts. Runs each kernel on the GPU and on the CPU (the default library's typed arrays), and checks that the two agree. It covers:
    • clamping: ties, both ends, NaN, ++ at 255;
    • half precision: binary16 rounding, overflow to Infinity;
    • gpuReduce and gpuBatch.
  • Compile tests. compile/clamped_half.ts checks the launch kinds and the clamp calls on every GPU target. compile/clamped_atomics.ts checks the Atomics error.
  • docs/gpu.md has a new section on these arrays.

Verified

  • Full Windows ctest: 10494/10494 passed.
  • RTX 5080 on Windows and on WSL Linux: all 26 GPU programs pass under both the JIT and as executables, and all 11 fault cases pass.
  • Intel iGPU on Windows: all 26 programs pass, JIT and exe, and all 11 fault cases.
  • NVIDIA rerun after the Intel fixes: all 26 programs pass, JIT and exe; the 90 GPU and f16 ctests pass.
  • AMD is compile-checked only.

🤖 Generated with Claude Code

ASDAlexander77 and others added 2 commits December 2, 2026 17:25
…n integer is an f16

The two typed arrays the default library makes as ConvertingTypedArray<S> around a TypedArray<S> are taken by a
launch, gpuReduce, gpuBatch and a CPU call as the inner TypedArray's view: a Float16Array's u16 store seen as
f16[], a Uint8ClampedArray's as u8[]. A kernel's Uint8ClampedArray parameter keeps JavaScript's rule: each write
to its elements (=, +=..., ++/--, the postfix's value the element before) goes through ToUint8Clamp (a generic
source helper, .u8_clamp: into 0..255, half to even, NaN 0); Atomics on one is an error, as in JavaScript.

Language fix found on the way: the promotion ladder had f16 below s32, so `f16 * 2` was an s32 multiply (0.3 * 2
was 0). f16 now sits with f32, above the 32-bit integers: an f16 with an integer is an f16, as a float is a float.
00f16_conversions.ts checks it.

gpu_clamped_half.ts runs each kernel on the GPU and on the CPU (the default library's typed arrays) and they agree:
ties, ends, NaN, ++ at 255, binary16 rounding, overflow to Infinity, gpuReduce and gpuBatch; RTX 5080, JIT and exe.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…rs no longer hang, warps of 16, exact float sqrt and ceil

First run on an Intel GPU (Core Ultra 9 285K's integrated one, Level Zero on Windows). 19 of the 26 GPU programs passed;
these fixes make all 26 pass, as executables and under the JIT, and the 11 fault cases:

- Shared-memory kernels did not load: the barrier guard asks to_local(p), and the Intel driver has no to_local for
  the pointer types the backend gives it. After O3 a pointer followed to its object is known (shared, or not); one
  known only on the GPU is checked against the shared arrays of the kernels that reach it.
- Barrier kernels hung: O3's loop unswitching and jump threading copied OpenCL's barrier, a plain function to them,
  into branches a block's threads take apart, and an Intel subgroup waits at one copy for its lanes at the other.
  Every device function is now convergent, as clang has a CUDA or OpenCL one (NVIDIA's bar.sync counts threads, so
  it ran there).
- gpuWarpSize() differed between kernels (the driver picks 8, 16 or 32 per kernel): each Intel kernel's subgroup
  is pinned at 16 (intel_reqd_sub_group_size), which every Intel GPU has.
- A float's sqrt was not correctly rounded (OpenCL allows 3 ulps): the module is built with
  -cl-fp32-correctly-rounded-divide-sqrt.
- Math.ceil of a v in (-1, -0] gave 0, not -0: copysign(ceil(v), v), exact on every GPU.

NVIDIA rerun: all 26 programs pass, JIT and exe. GPU and f16 ctests pass (90).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77 ASDAlexander77 changed the title GPU: Uint8ClampedArray and Float16Array as kernel arguments; an f16 with an integer is an f16 GPU: Uint8ClampedArray/Float16Array kernel args, f16 promotion fix, and the first run on an Intel GPU Dec 2, 2026
@ASDAlexander77
ASDAlexander77 merged commit d70be55 into main Dec 2, 2026
8 checks passed
@ASDAlexander77
ASDAlexander77 deleted the gpu-clamped-half branch December 2, 2026 19:11
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