rdna2spirv: keep NaN-pattern constants out of float-typed selects in image_bvh (OLD-9868) #1212

Open
github-bot wants to merge 4 commits from github-bot/gh-1212 into main AGit
Owner

@⁠ambroseautomation on GitHub · 2026-11-24 20:24 UTC · original
ambroseautomation:carry/old-9868-bvh-nan-select → main · +52 −3 · 2 files · 4 commits · open

Claimed in the coordination issue as: OLD-9868 (#14)

What and why

Carries forward old-pr/9868 (draft #137, originally authored by Conrage, all 4 original commits preserved; indexed in #17), rebased cleanly onto current main.

Keeps NaN-pattern integer constants (0xFFFFFFFF box-miss and 0xFFC00000 canonical NaN) out of float-typed OpSelect instructions in image_bvh*_intersect_ray lowering (tools/rdna2spirv.py):

  • Builds those selects via bitwise (x & keep) | (k & pick) (usel_k) where keep/pick select between two integer constants, preventing drivers like KosmicKrisp (Mesa 26.3.1) from typing the select as float and emitting (NAN) (0x7FC00000) instead of 0xFFFFFFFF / 0xFFC00000.
  • Adds regression tests in tests/semantic/test_image_bvh_tri.py verifying canonical NaN payload handling and asserting that no OpSelect pairs the NaN/miss constants with a computed word.

Tested

  • python -m unittest discover -s tests/semantic -p test_image_bvh_tri.py -v: 9 tests, OK.
  • python -I scripts/run_semantic_tests.py tests/semantic: 470 files, 6,170 tests, 0 files failed.
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_image_bvh_tri.py: exit 0 (no game data).

GPUs tested: n/a (CPU SPIR-V interpreter semantic suite on Windows 11 Pro 64-bit, Python 3.12.10)

AI-assisted: yes

Checklist

  • Based on current main and merges cleanly (a conflicting pull request is labeled needs rebase).
  • One topic; follow-up work goes in a new pull request.
  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed. (n/a — preserves identical integer words for existing image_bvh lowering)
**@⁠ambroseautomation** on GitHub · 2026-11-24 20:24 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1212) `ambroseautomation:carry/old-9868-bvh-nan-select` → `main` · +52 −3 · 2 files · 4 commits · open Claimed in the coordination issue as: OLD-9868 (https://git.gtahub.st/github/ubiquitarianism--leonida/issues/14) ### What and why Carries forward `old-pr/9868` (draft #137, originally authored by `Conrage`, all 4 original commits preserved; indexed in #17), rebased cleanly onto current `main`. Keeps NaN-pattern integer constants (`0xFFFFFFFF` box-miss and `0xFFC00000` canonical NaN) out of float-typed `OpSelect` instructions in `image_bvh*_intersect_ray` lowering (`tools/rdna2spirv.py`): - Builds those selects via bitwise `(x & keep) | (k & pick)` (`usel_k`) where `keep`/`pick` select between two integer constants, preventing drivers like KosmicKrisp (Mesa 26.3.1) from typing the select as float and emitting `(NAN)` (`0x7FC00000`) instead of `0xFFFFFFFF` / `0xFFC00000`. - Adds regression tests in `tests/semantic/test_image_bvh_tri.py` verifying canonical NaN payload handling and asserting that no `OpSelect` pairs the NaN/miss constants with a computed word. ### Tested - `python -m unittest discover -s tests/semantic -p test_image_bvh_tri.py -v`: 9 tests, OK. - `python -I scripts/run_semantic_tests.py tests/semantic`: 470 files, 6,170 tests, 0 files failed. - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_image_bvh_tri.py`: exit 0 (no game data). GPUs tested: n/a (CPU SPIR-V interpreter semantic suite on Windows 11 Pro 64-bit, Python 3.12.10) AI-assisted: yes ### Checklist - [x] Based on current `main` and merges cleanly (a conflicting pull request is labeled `needs rebase`). - [x] One topic; follow-up work goes in a new pull request. - [x] No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here. - [ ] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. (n/a — preserves identical integer words for existing `image_bvh` lowering)
KosmicKrisp (Mesa 26.3.1) types an OpSelect by the first concrete type that
reaches it. In the image_bvh*_intersect_ray lowering the result words share
registers with bitcast floats, so the box branch's 0xFFFFFFFF "no hit" select
and canon()'s 0xFFC00000 select were typed float and their constants printed
as the MSL literal (NAN), which Metal reads as 0x7FC00000. cs_bvh and
cs_bvh32 returned 0x7fc00000 for the miss word.

Build those two selects as (x & keep) | (k & pick) with keep/pick selects
between two constants. Bitwise ops never take a float type in KosmicKrisp's
inference, so the constants stay integers. The result words are the same on
every driver.

Tested on an Apple M5 (macOS): cs_bvh/cs_bvh32 0/2 -> 2/2 on KosmicKrisp
and 2/2 -> 2/2 on MoltenVK 1.4.2; full run_gpu_tests.py unchanged otherwise
on both; tests/semantic unchanged.

AI-assisted: yes
Review on #7595: every NaN input in the BVH semantic tests was 0x7FC00000, so
a usel_k that dropped its mask still passed. A payload NaN in v0.x catches it
on the CPU.

AI-assisted: yes
AI-assisted: yes
A translation-only check of the #6623 workaround's form: it fails on
main's OpSelect form (8 such selects) and passes with usel_k, on any
machine, unlike the GPU and interpreter runs that can't see the
KosmicKrisp typing.

AI-assisted: yes
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:43:04 +00:00
Author
Owner

@⁠tiguan26 on GitHub · 2026-11-25 06:40 UTC · original · approved these changes

Approving. Merged into current main (42537e7) with no conflicts. The full translator suite, including this PR's test, gives 487 files / 6,281 tests with only test_int32_to_f64 failing, as on main here (Linux, Python 3.13). That replaces my earlier run, whose worktree was shared with another job.

I checked usel_k by hand against the selects it replaces. (x & (c ? 0 : ~0)) | (c ? k : 0) is c ? k : x, and the invert form is c ? x : k. The k == 0xFFFFFFFF shortcut x | km gives the same in both directions. So hb ? child : 0xFFFFFFFF and the NaN canonicalisation are unchanged on drivers that type OpSelect correctly.

AI-assisted (Claude).

**@⁠tiguan26** on GitHub · 2026-11-25 06:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1212#pullrequestreview-6052983617) · **approved these changes** Approving. Merged into current main (42537e7) with no conflicts. The full translator suite, including this PR's test, gives 487 files / 6,281 tests with only `test_int32_to_f64` failing, as on main here (Linux, Python 3.13). That replaces my earlier run, whose worktree was shared with another job. I checked `usel_k` by hand against the selects it replaces. `(x & (c ? 0 : ~0)) | (c ? k : 0)` is `c ? k : x`, and the `invert` form is `c ? x : k`. The `k == 0xFFFFFFFF` shortcut `x | km` gives the same in both directions. So `hb ? child : 0xFFFFFFFF` and the NaN canonicalisation are unchanged on drivers that type OpSelect correctly. AI-assisted (Claude).
Author
Owner

@⁠PierreEbele on GitHub · 2026-11-25 07:09 UTC · original

Reviewed 82fde44: ran test_image_bvh_tri.py, test_image_bvh_box.py, test_image_bvh_tri_hw.py, test_image_bvh_box_hw.py, and test_bvh_tri_probe.py in the sandbox on PR and main; all pass on PR (9/4/3/2/17 OK), the new form test fails on main with the 8 expected mixed selects, and a 464-case probe (32/64-bit, both sort flags, static/dynamic EXEC, poked NaN payloads/specials, box16/32, types 6–7) gives bit-identical interpreter output on both trees.
Non-blocking note: in dynamic-EXEC shaders (e.g. cs_bvh.asm, cs_bvh32.asm), L['oth'] (tools/rdna2spirv.py:14977, node types 6–7) still emits 4 OpSelect %uint %active %cffffffff %old per call via Gen.dst() (8 remain in each shader; missed by test_no_select_mixes... because its shader has static EXEC); worth converting via eager=True/usel_k and adding a dynamic-EXEC test if types 6–7 ever run on KosmicKrisp.
GPU tests not run here (no GPU/KosmicKrisp in this environment); commit d01324cda reports cs_bvh/cs_bvh32 0/2 -> 2/2 on Apple M5 KosmicKrisp, so the docs/testing.md row can be updated in a follow-up.
AI-assisted review.

Recommendation: Merge

**@⁠PierreEbele** on GitHub · 2026-11-25 07:09 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1212#issuecomment-7155551361) Reviewed 82fde44: ran `test_image_bvh_tri.py`, `test_image_bvh_box.py`, `test_image_bvh_tri_hw.py`, `test_image_bvh_box_hw.py`, and `test_bvh_tri_probe.py` in the sandbox on PR and main; all pass on PR (`9/4/3/2/17 OK`), the new form test fails on main with the 8 expected mixed selects, and a 464-case probe (32/64-bit, both sort flags, static/dynamic EXEC, poked NaN payloads/specials, box16/32, types 6–7) gives bit-identical interpreter output on both trees. Non-blocking note: in dynamic-EXEC shaders (e.g. `cs_bvh.asm`, `cs_bvh32.asm`), `L['oth']` (`tools/rdna2spirv.py:14977`, node types 6–7) still emits 4 `OpSelect %uint %active %cffffffff %old` per call via `Gen.dst()` (8 remain in each shader; missed by `test_no_select_mixes...` because its shader has static EXEC); worth converting via `eager=True`/`usel_k` and adding a dynamic-EXEC test if types 6–7 ever run on KosmicKrisp. GPU tests not run here (no GPU/KosmicKrisp in this environment); commit `d01324cda` reports `cs_bvh`/`cs_bvh32` `0/2 -> 2/2` on Apple M5 KosmicKrisp, so the `docs/testing.md` row can be updated in a follow-up. AI-assisted review. **Recommendation:** Merge
Author
Owner

@⁠Tauwni on GitHub · 2026-11-25 10:16 UTC · original

Tested on an Apple M3 (macOS 26.5.1), this branch at 82fde4444, test programs built from main 626d1281a: cs_bvh and cs_bvh32 fail on main with KosmicKrisp (Mesa 26.3.1), out[3] = 0x7fc00000, and pass with this PR (out[0..7] = 104 72 64 4294967295 ...). They also still pass on MoltenVK 1.4.2. AI-assisted (Claude Code).

**@⁠Tauwni** on GitHub · 2026-11-25 10:16 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/1212#issuecomment-7159471619) Tested on an Apple M3 (macOS 26.5.1), this branch at `82fde4444`, test programs built from `main` `626d1281a`: `cs_bvh` and `cs_bvh32` fail on `main` with KosmicKrisp (Mesa 26.3.1), `out[3]` = `0x7fc00000`, and pass with this PR (`out[0..7] = 104 72 64 4294967295 ...`). They also still pass on MoltenVK 1.4.2. AI-assisted (Claude Code).
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/1212/head:github-bot/gh-1212'
git switch 'github-bot/gh-1212'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-1212'
git switch 'github-bot/gh-1212'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-1212'
git switch 'github-bot/gh-1212'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-1212'
git switch 'main'
git merge --squash 'github-bot/gh-1212'
git switch 'main'
git merge --ff-only 'github-bot/gh-1212'
git switch 'main'
git merge 'github-bot/gh-1212'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No description provided.