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

Merged
Andretuta merged 4 commits from upstream-pr/137 into main 2026-12-01 15:13:35 +00:00
Andretuta commented 2026-11-24 02:26:02 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by Andretuta on 2026-11-24T02:26:02Z.


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.)
<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5464913181 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/137). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:26:02Z. --- > 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.)
GeorgiyDemo commented 2026-11-24 07:32:24 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by GeorgiyDemo on 2026-11-24T07:32:24Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7125101880 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/137#issuecomment-7125101880). Originally posted by [GeorgiyDemo](https://github.com/GeorgiyDemo) on 2026-11-24T07:32:24Z. --- 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.
EvgHaze commented 2026-11-25 12:40:40 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by EvgHaze on 2026-11-25T12:40:40Z.


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.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7162703088 --> Imported from [ubiquitarianism/leonida](https://github.com/ubiquitarianism/leonida/pull/137#issuecomment-7162703088). Originally posted by [EvgHaze](https://github.com/EvgHaze) on 2026-11-25T12:40:40Z. --- 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.
MoonEyes commented 2026-11-30 19:10:28 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by MoonEyes on 2026-11-30T19:10:28Z.


The fix still appears relevant: current main does not contain usel_k or the two new image-BVH regressions, while docs/design.md already specifies the required canonical NaN word (0xFFC00000). The independent M3/M5 reports show the KosmicKrisp NaN-word failure fixed and MoltenVK unchanged, and the CPU translation test distinguishes the old select form. I am leaving this open, not merge-ready: it is still a draft and all available CI checks were cancelled. Please rebase against current main, mark ready when complete, and obtain current-base CI plus the relevant GPU/translator checks before merge.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7288958572 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/137#issuecomment-7288958572). Originally posted by [MoonEyes](https://github.com/MoonEyes) on 2026-11-30T19:10:28Z. --- The fix still appears relevant: current `main` does not contain `usel_k` or the two new image-BVH regressions, while `docs/design.md` already specifies the required canonical NaN word (`0xFFC00000`). The independent M3/M5 reports show the KosmicKrisp NaN-word failure fixed and MoltenVK unchanged, and the CPU translation test distinguishes the old select form. I am leaving this open, not merge-ready: it is still a draft and all available CI checks were cancelled. Please rebase against current `main`, mark ready when complete, and obtain current-base CI plus the relevant GPU/translator checks before merge.
aslan2142 commented 2026-12-01 12:00:19 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:00:19Z.

Review: COMMENTED


Draft review against current main: the usel_k bitwise constant-NaN select workaround in tools/rdna2spirv.py:14638-14654 looks clean, clean-room verified, and still relevant, but CI checks are cancelled and the PR is still Draft. Please merge/rebase current main and mark Ready for Review so CI runs green.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130844227 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/137#pullrequestreview-6130844227). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:00:19Z. **Review:** COMMENTED --- Draft review against current `main`: the `usel_k` bitwise constant-NaN select workaround in `tools/rdna2spirv.py:14638-14654` looks clean, clean-room verified, and still relevant, but CI checks are cancelled and the PR is still Draft. Please merge/rebase current `main` and mark Ready for Review so CI runs green. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:09:48 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T12:09:48Z.


Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review.

(AI-assisted triage.)

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7310741791 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/137#issuecomment-7310741791). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:09:48Z. --- Marking Ready for Review since no WIP/blocker reason for draft status is specified on this PR, so it can enter normal CI and review. *(AI-assisted triage.)*
aslan2142 commented 2026-12-01 13:02:32 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T13:02:32Z.

Review: APPROVED


Now ready for review and MERGEABLE. Clean-room verified: the usel_k bitwise select workaround in tools/rdna2spirv.py:14638-14654 keeps NaN-pattern constants out of float-typed selects in image_bvh (independently verified on Apple M3 & M5 Pro KosmicKrisp + MoltenVK, with regression tests in tests/semantic/test_image_bvh_tri.py).

Approving — ready to merge once fresh CI runs green on current main (existing Nov 24 checks were cancelled).

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6131773274 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/137#pullrequestreview-6131773274). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T13:02:32Z. **Review:** APPROVED --- Now ready for review and `MERGEABLE`. Clean-room verified: the `usel_k` bitwise select workaround in `tools/rdna2spirv.py:14638-14654` keeps NaN-pattern constants out of float-typed selects in `image_bvh` (independently verified on Apple M3 & M5 Pro KosmicKrisp + MoltenVK, with regression tests in `tests/semantic/test_image_bvh_tri.py`). Approving — ready to merge once fresh CI runs green on current `main` (existing Nov 24 checks were cancelled). *(AI-assisted review.)*
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
ProjectLeonida/mandy!137
No description provided.