rdna2spirv: keep NaN-pattern constants out of float-typed selects in image_bvh (#6623) #137

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

@⁠Andretuta on GitHub · 2026-11-24 02:26 UTC · original
ubiquitarianism:old-pr/9868 → main · +52 −3 · 2 files · 4 commits · draft

Carried over from old PR 9868 by Conrage in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9868, exactly that PR's last commit. All carried-over PRs are listed in #17.
Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it.
Conrage: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one.


Original description (mentions and #N references are disabled; the numbers refer to the old repository):

Claimed in the coordination issue as: #6623, in #7496

Replaces #7595, closed in the PR cleanup; same change, rebased onto main 20cff7e36.

Fixes #6623

What and why

On KosmicKrisp (Mesa 26.3.1, Vulkan on Metal), cs_bvh and cs_bvh32 return 0x7fc00000 where the box miss word should be 0xffffffff. KosmicKrisp's MSL type inference gives an OpSelect the first concrete type that reaches it. In the image_bvh*_intersect_ray lowering that is float (the result words share registers with bitcast floats), so the select's NaN-pattern constant is printed as (NAN), which Metal reads as 0x7FC00000. Two selects are affected: the box branch's 0xFFFFFFFF miss index and canon()'s 0xFFC00000 NaN word.

KosmicKrisp's bitwise ops never take a float type, so a local helper usel_k(c, k, x) builds c ? k : x as (x & keep) | (k & pick), where keep and pick are selects between two constants (just x | pick for k = 0xFFFFFFFF). Only those two call sites change. The result words are the same on every driver. This is a workaround; the real fix belongs in Mesa's nir_to_msl.

Tests

  • test_a_nan_with_a_payload_returns_the_canonical_nan: a payload NaN vertex must return ff800000 ffc00000 ffc00000 ffc00000; a usel_k that drops its mask fails it.
  • test_no_select_mixes_the_nan_or_miss_constant_with_a_computed_word (asked for by @kapitche): no OpSelect in the generated module pairs 0xFFC00000/0xFFFFFFFF with a computed operand. It fails on main (8 such selects) and passes here, on any machine.

Tested

  • Apple M5, KosmicKrisp (Mesa 26.3.1): run_gpu_tests.py --filter cs_bvh 0/2 → 2/2; (NAN) literals in the MSL of cs_bvh/cs_bvh32 16 → 0; full suite 174 → 176 passed, only cs_bvh/cs_bvh32 changed. MoltenVK 1.4.2: no differences.
  • On #7595 others found no change against main on RTX 3080/3080 Ti/4060 Ti/5060 Laptop/5070/5070 Ti/5090, GTX 1660 SUPER, RX 6650 XT/6800 XT/6900 XT/7800 XT, Radeon 660M and other RDNA2 iGPUs, Steam Deck (RADV), Intel Arc B580 and Raptor Lake, and llvmpipe.
  • This rebase, Linux: tests/semantic/test_image_bvh_{tri,box,tri_hw,box_hw}.py 18 passed.

AI-assisted: yes

Checklist

  • 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. (No new instruction rule: the same words are produced through other SPIR-V ops.)
**@⁠Andretuta** on GitHub · 2026-11-24 02:26 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/137) `ubiquitarianism:old-pr/9868` → `main` · +52 −3 · 2 files · 4 commits · draft > Carried over from **old PR 9868** by `Conrage` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9868`, exactly that PR's last commit. All carried-over PRs are listed in #17. > Opened as a draft, and the CI runs the opening started were cancelled so 250 PRs don't fill the queue. Re-run CI here when you review it. > `Conrage`: this is your work. Feel free to take it over, by pushing to the branch or opening your own PR and closing this one. --- Original description (mentions and `#N` references are disabled; the numbers refer to the old repository): Claimed in the coordination issue as: `#6623`, in `#7496` Replaces `#7595`, closed in the PR cleanup; same change, rebased onto main 20cff7e36. Fixes `#6623` ### What and why On KosmicKrisp (Mesa 26.3.1, Vulkan on Metal), `cs_bvh` and `cs_bvh32` return `0x7fc00000` where the box miss word should be `0xffffffff`. KosmicKrisp's MSL type inference gives an `OpSelect` the first concrete type that reaches it. In the `image_bvh*_intersect_ray` lowering that is float (the result words share registers with bitcast floats), so the select's NaN-pattern constant is printed as `(NAN)`, which Metal reads as `0x7FC00000`. Two selects are affected: the box branch's `0xFFFFFFFF` miss index and `canon()`'s `0xFFC00000` NaN word. KosmicKrisp's bitwise ops never take a float type, so a local helper `usel_k(c, k, x)` builds `c ? k : x` as `(x & keep) | (k & pick)`, where `keep` and `pick` are selects between two constants (just `x | pick` for `k = 0xFFFFFFFF`). Only those two call sites change. The result words are the same on every driver. This is a workaround; the real fix belongs in Mesa's `nir_to_msl`. ### Tests - `test_a_nan_with_a_payload_returns_the_canonical_nan`: a payload NaN vertex must return `ff800000 ffc00000 ffc00000 ffc00000`; a `usel_k` that drops its mask fails it. - `test_no_select_mixes_the_nan_or_miss_constant_with_a_computed_word` (asked for by `@kapitche`): no `OpSelect` in the generated module pairs `0xFFC00000`/`0xFFFFFFFF` with a computed operand. It fails on main (8 such selects) and passes here, on any machine. ### Tested - Apple M5, KosmicKrisp (Mesa 26.3.1): `run_gpu_tests.py --filter cs_bvh` 0/2 → 2/2; `(NAN)` literals in the MSL of `cs_bvh`/`cs_bvh32` 16 → 0; full suite 174 → 176 passed, only `cs_bvh`/`cs_bvh32` changed. MoltenVK 1.4.2: no differences. - On `#7595` others found no change against main on RTX 3080/3080 Ti/4060 Ti/5060 Laptop/5070/5070 Ti/5090, GTX 1660 SUPER, RX 6650 XT/6800 XT/6900 XT/7800 XT, Radeon 660M and other RDNA2 iGPUs, Steam Deck (RADV), Intel Arc B580 and Raptor Lake, and llvmpipe. - This rebase, Linux: `tests/semantic/test_image_bvh_{tri,box,tri_hw,box_hw}.py` 18 passed. AI-assisted: yes ### Checklist - [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. (No new instruction rule: the same words are produced through other SPIR-V ops.)
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 08:46:02 +00:00
Author
Owner

@⁠GeorgiyDemo on GitHub · 2026-11-24 07:32 UTC · original

Tested cc05917a on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against main d42773d07:

  • KosmicKrisp (Mesa 26.3.1, subgroup 32..32): main fails cs_bvh and cs_bvh32 with out[0..7] = 104 72 64 2143289344 ... (0x7fc00000 at word 3); this head passes 2/2.
  • MoltenVK 1.4.2 (subgroup 4..32): 2/2 pass at both revisions, so no regression.
  • tests/semantic/test_image_bvh_tri.py: 9 tests OK at this head; the same file against main's translator fails 1 of 9, so the new tests catch the bug rather than only passing here.

Binaries from main (only the translator changed), llvm-mc 23.1.6, SPIRV-Tools v2026.4. Synthetic fixtures only, no game data. AI-assisted.

**@⁠GeorgiyDemo** on GitHub · 2026-11-24 07:32 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/137#issuecomment-7125101880) Tested `cc05917a` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `d42773d07`: - **KosmicKrisp** (Mesa 26.3.1, subgroup 32..32): `main` fails `cs_bvh` and `cs_bvh32` with `out[0..7] = 104 72 64 2143289344 ...` (`0x7fc00000` at word 3); this head passes 2/2. - **MoltenVK 1.4.2** (subgroup 4..32): 2/2 pass at both revisions, so no regression. - `tests/semantic/test_image_bvh_tri.py`: 9 tests OK at this head; the same file against `main`'s translator fails 1 of 9, so the new tests catch the bug rather than only passing here. Binaries from `main` (only the translator changed), llvm-mc 23.1.6, SPIRV-Tools v2026.4. Synthetic fixtures only, no game data. AI-assisted.
Author
Owner

@⁠EvgHaze on GitHub · 2026-11-25 12:40 UTC · original

Apple-side check of this draft, on the hardware the failure was reported from.

Machine:  Apple M3, 10-core (MacBook Air 13", Mac15,13), macOS 27.0 (26A5406e), arm64
Drivers:  KosmicKrisp, Mesa 26.3.1 (Homebrew), through the Vulkan loader
          MoltenVK 1.4.2 (Homebrew), ICD loaded directly, subgroup 4..32
Base:     main `0d80bc5` + this diff applied cleanly (`git apply`, no fuzz)
Build:    `cmake -S runtime -B runtime/build -G Ninja` (198 targets, 33 `vk_*` tools)

Before (main, 0d80bc5):

KosmicKrisp  cs_bvh   bvh out = 00000068 00000048 00000040 7fc00000 | ...
             1 groups x 64: 1 mismatches (first at 3: got 0x7fc00000)
KosmicKrisp  cs_bvh32 same word, same mismatch

With this diff:

KosmicKrisp  cs_bvh   bvh out = 00000068 00000048 00000040 ffffffff | ...  0 mismatches
KosmicKrisp  cs_bvh32                                                 0 mismatches
MoltenVK     cs_bvh                                                  0 mismatches (unchanged)

So the fix does what it says on a third Apple machine (the report was an M2 Ultra): the KosmicKrisp words are exact now, and MoltenVK is not disturbed.

MSL, the same fixture (MESA_KK_DEBUG=msl): on main the dump contains r10 = as_type<uint>((as_type<float>(r9))) with r9 = 0xFFFFFFFF, which is where 0x7fc00000 came from. With this diff that double cast is gone from the dump (as_type<uint>((as_type<float> appears 0 times), which matches the measured words.

Tests: python3 -I scripts/run_semantic_tests.py tests/semantic → 489 files, 6290 tests, 0 failed (8 jobs, 150 s).

One small nit on the docstring: python -I tests/semantic/test_image_bvh_tri.py cannot work, because -I removes the working directory from sys.path and the from support import ... then fails (ModuleNotFoundError: support). The form in the docstring (python -m unittest tests/semantic/test_image_bvh_tri.py -v) or the runner above does not have that problem.

AI-assisted. No game data: the fixtures are hand-written .asm, and every number above is test output.

**@⁠EvgHaze** on GitHub · 2026-11-25 12:40 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/137#issuecomment-7162703088) Apple-side check of this draft, on the hardware the failure was reported from. ```text Machine: Apple M3, 10-core (MacBook Air 13", Mac15,13), macOS 27.0 (26A5406e), arm64 Drivers: KosmicKrisp, Mesa 26.3.1 (Homebrew), through the Vulkan loader MoltenVK 1.4.2 (Homebrew), ICD loaded directly, subgroup 4..32 Base: main `0d80bc5` + this diff applied cleanly (`git apply`, no fuzz) Build: `cmake -S runtime -B runtime/build -G Ninja` (198 targets, 33 `vk_*` tools) ``` **Before (main, `0d80bc5`):** ```text KosmicKrisp cs_bvh bvh out = 00000068 00000048 00000040 7fc00000 | ... 1 groups x 64: 1 mismatches (first at 3: got 0x7fc00000) KosmicKrisp cs_bvh32 same word, same mismatch ``` **With this diff:** ```text KosmicKrisp cs_bvh bvh out = 00000068 00000048 00000040 ffffffff | ... 0 mismatches KosmicKrisp cs_bvh32 0 mismatches MoltenVK cs_bvh 0 mismatches (unchanged) ``` So the fix does what it says on a third Apple machine (the report was an M2 Ultra): the KosmicKrisp words are exact now, and MoltenVK is not disturbed. **MSL, the same fixture (`MESA_KK_DEBUG=msl`):** on main the dump contains `r10 = as_type<uint>((as_type<float>(r9)))` with `r9 = 0xFFFFFFFF`, which is where `0x7fc00000` came from. With this diff that double cast is gone from the dump (`as_type<uint>((as_type<float>` appears 0 times), which matches the measured words. **Tests:** `python3 -I scripts/run_semantic_tests.py tests/semantic` → **489 files, 6290 tests, 0 failed** (8 jobs, 150 s). One small nit on the docstring: `python -I tests/semantic/test_image_bvh_tri.py` cannot work, because `-I` removes the working directory from `sys.path` and the `from support import ...` then fails (`ModuleNotFoundError: support`). The form in the docstring (`python -m unittest tests/semantic/test_image_bvh_tri.py -v`) or the runner above does not have that problem. AI-assisted. No game data: the fixtures are hand-written `.asm`, and every number above is test output.
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/137/head:github-bot/gh-137'
git switch 'github-bot/gh-137'

Merge

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