rdna2spirv: VOP3 clamp of a NaN gives +0 under DX10_CLAMP (SHADER-CLAMP-NAN) #642

Manually merged
github merged 1 commit from gh-pr/642/head into gh-pr/642/base 2026-11-26 09:13:28 +00:00
Owner

@⁠aadheel on GitHub · 2026-11-24 10:45 UTC · original
aadheel:fix/vop3-clamp-nan-zero → main · +173 −23 · 11 files · 1 commits · merged 2026-11-24 15:50 UTC

Claimed in the coordination issue as: SHADER-CLAMP-NAN

What and why

Gen.clamp_mods applied the VOP3 clamp bit as a bare OpExtInst %float %glsl FClamp v %f0 %f1. GLSL.std.450 leaves FClamp undefined when an operand is NaN, so any NaN reaching the clamp (a NaN operand, inf - inf, -inf * 0, an all-NaN min/max) gave SPIR-V whose result is up to the driver. The CPU interpreter refuses it (UnsupportedConstruct), and tests/spirv_interp.py passes the NaN through.

The game's MODE has DX10_CLAMP=1. Under it the hardware turns a NaN result into +0. This is already measured on gfx1036 in docs/shader_validation.md: the v_rcp_iflag_f32 run (a NaN result is +0 except with DX10_CLAMP off, 169 programs) and SHADER-186 (v_fmac_f16, v_fma_mixlo_f16, v_fma_mix_f32). docs/design.md listed it as "Not modelled". test_alu_vop3_mods_hw.py leaves out those hardware rows because the translation was undefined, and design.md counts most of its 6,430 undefined cases as this one.

The fix selects +0 for a NaN before FClamp (OpIsNan + OpSelect), the same pattern the translator already uses for the v_div_fixup_f32, v_cvt_pk_u8_f32 and v_cvt_pknorm_* clamps. Every f32/f16 op that applies clamp through clamp_mods/clamp_f16 gets it: the F1/F2 ALU tables, fma/fmac/mad and the legacy ops, min/max/min3/max3/med3, conversions, mix ops, interp, f16 arithmetic and transcendentals. Non-NaN values are unchanged: FClamp sees the same operand as before, and omod still applies before the clamp.

Not in this PR:

  • clamp_mods_f64 and the packed v_pk_* clamp still emit a bare FClamp. #475 is rewriting clamp_mods_f64, and the f64 row in design.md plus that docstring still say "as for f32 ... left to the driver". Whichever of the two lands second should drop "as for f32". I left those lines alone to avoid a conflict with #475.
  • v_dot2_f32_f16 also routes its clamp through clamp_mods. #410 measured that clamp as a hardware no-op and removes it. The two PRs merge cleanly and their tests pass together.
  • tools/mode_diff.py, div_fixup_diff.py and f16misc_diff.py still mark clamp of a NaN as "not judged". Follow-up: they can now judge it as +0.

Tested

  • New tests/semantic/test_vop3_clamp_nan.py, 12 tests. Covered: quiet, negative and signalling NaN operands and invalid operations under clamp, for f32 ops, legacy mul/mad, f16 ops writing the low half and the high half (the full word is checked, so the other half must be kept), all-NaN v_med3_f16, v_cvt_f32_f16/v_cvt_f16_f32 with and without omod, and v_fma_mix_f32/v_fma_mixlo_f16/v_fma_mixhi_f16. Each case expects +0. Controls without a NaN check that the clamp is unchanged. The test fails 33 subtests on main and passes with the change.
  • Changed to expect +0: the two negative controls that asserted the undefined FClamp was refused (test_alu_f32_min_max, test_alu_min3_max3, including the min3/max3 reference model), and the f16 unary harness (f16_unary_cases.py), which now checks clamp of a NaN result instead of skipping it. v_sqrt_f16/v_rsq_f16 of a negative now exercise it.
  • tests/test_rcp_iflag_mods.py and tests/test_f64_conversion.py match the SPIR-V text and now expect the OpIsNan/OpSelect before FClamp.
  • python -I scripts/run_semantic_tests.py tests/semantic: 466 files, 6,120 tests, all pass except test_int32_to_f64. That one also fails on unmodified main on this machine, because Ubuntu 24.04's spirv-opt 2025.1~rc1 segfaults on it.
  • tests/test_translate.py: 666 passed, 1 failed (ps_sdwa_f16_const.asm), the same failure as on main here (Ubuntu's LLVM 18 llvm-mc). The other translator unit tests (test_f16misc_diff, test_div_fixup_diff, test_mode_diff, test_rdna2spirv_cli, test_text_write_line_endings and the rest of the 122 tests/test_*.py files that use the translator) pass.
  • Translated programs with the new clamp pass spirv-val (Vulkan 1.2).
  • scripts/check_no_game_data.py on every changed file: clean.
  • No conflicts with open PRs: git merge-tree against the heads of the 82 open PRs that touch these files adds no conflicted file. #475, #410, #270 and #309 also pass their own tests and this one when merged with it.

GPUs tested: n/a. CPU interpreter and spirv-val only; no GPU fixture was run (no llvm-mc with the AMDGPU target on this machine).

AI-assisted: yes (Claude Code)

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.

🤖 Generated with Claude Code

**@⁠aadheel** on GitHub · 2026-11-24 10:45 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/642) `aadheel:fix/vop3-clamp-nan-zero` → `main` · +173 −23 · 11 files · 1 commits · merged 2026-11-24 15:50 UTC Claimed in the coordination issue as: SHADER-CLAMP-NAN ### What and why `Gen.clamp_mods` applied the VOP3 `clamp` bit as a bare `OpExtInst %float %glsl FClamp v %f0 %f1`. GLSL.std.450 leaves `FClamp` undefined when an operand is NaN, so any NaN reaching the clamp (a NaN operand, `inf - inf`, `-inf * 0`, an all-NaN min/max) gave SPIR-V whose result is up to the driver. The CPU interpreter refuses it (`UnsupportedConstruct`), and `tests/spirv_interp.py` passes the NaN through. The game's MODE has DX10_CLAMP=1. Under it the hardware turns a NaN result into +0. This is already measured on gfx1036 in `docs/shader_validation.md`: the `v_rcp_iflag_f32` run (a NaN result is +0 except with DX10_CLAMP off, 169 programs) and SHADER-186 (`v_fmac_f16`, `v_fma_mixlo_f16`, `v_fma_mix_f32`). `docs/design.md` listed it as "Not modelled". `test_alu_vop3_mods_hw.py` leaves out those hardware rows because the translation was undefined, and `design.md` counts most of its 6,430 undefined cases as this one. The fix selects +0 for a NaN before `FClamp` (`OpIsNan` + `OpSelect`), the same pattern the translator already uses for the `v_div_fixup_f32`, `v_cvt_pk_u8_f32` and `v_cvt_pknorm_*` clamps. Every f32/f16 op that applies `clamp` through `clamp_mods`/`clamp_f16` gets it: the F1/F2 ALU tables, fma/fmac/mad and the legacy ops, min/max/min3/max3/med3, conversions, mix ops, interp, f16 arithmetic and transcendentals. Non-NaN values are unchanged: `FClamp` sees the same operand as before, and omod still applies before the clamp. Not in this PR: - `clamp_mods_f64` and the packed `v_pk_*` clamp still emit a bare `FClamp`. #475 is rewriting `clamp_mods_f64`, and the f64 row in `design.md` plus that docstring still say "as for f32 ... left to the driver". Whichever of the two lands second should drop "as for f32". I left those lines alone to avoid a conflict with #475. - `v_dot2_f32_f16` also routes its clamp through `clamp_mods`. #410 measured that clamp as a hardware no-op and removes it. The two PRs merge cleanly and their tests pass together. - `tools/mode_diff.py`, `div_fixup_diff.py` and `f16misc_diff.py` still mark clamp of a NaN as "not judged". Follow-up: they can now judge it as +0. ### Tested - New `tests/semantic/test_vop3_clamp_nan.py`, 12 tests. Covered: quiet, negative and signalling NaN operands and invalid operations under `clamp`, for f32 ops, legacy mul/mad, f16 ops writing the low half and the high half (the full word is checked, so the other half must be kept), all-NaN `v_med3_f16`, `v_cvt_f32_f16`/`v_cvt_f16_f32` with and without omod, and `v_fma_mix_f32`/`v_fma_mixlo_f16`/`v_fma_mixhi_f16`. Each case expects +0. Controls without a NaN check that the clamp is unchanged. **The test fails 33 subtests on `main`** and passes with the change. - Changed to expect +0: the two negative controls that asserted the undefined `FClamp` was refused (`test_alu_f32_min_max`, `test_alu_min3_max3`, including the min3/max3 reference model), and the f16 unary harness (`f16_unary_cases.py`), which now checks clamp of a NaN result instead of skipping it. `v_sqrt_f16`/`v_rsq_f16` of a negative now exercise it. - `tests/test_rcp_iflag_mods.py` and `tests/test_f64_conversion.py` match the SPIR-V text and now expect the `OpIsNan`/`OpSelect` before `FClamp`. - `python -I scripts/run_semantic_tests.py tests/semantic`: 466 files, 6,120 tests, all pass except `test_int32_to_f64`. That one also fails on unmodified `main` on this machine, because Ubuntu 24.04's `spirv-opt` 2025.1~rc1 segfaults on it. - `tests/test_translate.py`: 666 passed, 1 failed (`ps_sdwa_f16_const.asm`), the same failure as on `main` here (Ubuntu's LLVM 18 `llvm-mc`). The other translator unit tests (`test_f16misc_diff`, `test_div_fixup_diff`, `test_mode_diff`, `test_rdna2spirv_cli`, `test_text_write_line_endings` and the rest of the 122 `tests/test_*.py` files that use the translator) pass. - Translated programs with the new clamp pass `spirv-val` (Vulkan 1.2). - `scripts/check_no_game_data.py` on every changed file: clean. - No conflicts with open PRs: `git merge-tree` against the heads of the 82 open PRs that touch these files adds no conflicted file. #475, #410, #270 and #309 also pass their own tests and this one when merged with it. GPUs tested: n/a. CPU interpreter and `spirv-val` only; no GPU fixture was run (no `llvm-mc` with the AMDGPU target on this machine). AI-assisted: yes (Claude Code) ### 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. - [x] A new translator rule is in the contract table of `docs/design.md`, with how it was confirmed. 🤖 Generated with [Claude Code](https://claude.com/claude-code)
Gen.clamp_mods applied the VOP3 clamp bit as a bare GLSL.std.450
FClamp, which is undefined for a NaN operand. The game's MODE has
DX10_CLAMP=1, under which the hardware turns a NaN result into +0
(measured on gfx1036: the v_rcp_iflag_f32 run and SHADER-186 for
v_fmac_f16 / v_fma_mix*). Select +0 for a NaN before FClamp, as the
div_fixup, cvt_pk_u8 and cvt_pknorm clamps already do. This covers
every f32/f16 op that applies clamp through clamp_mods/clamp_f16.

- tests/semantic/test_vop3_clamp_nan.py: NaN operands (quiet,
  negative, signalling) and invalid operations (inf-inf, -inf*0)
  under clamp give +0 for f32, f16 (low and high half), conversions,
  legacy and mix ops; controls without a NaN are unchanged. It fails
  33 subtests on the old translator.
- The two negative controls that expected the undefined FClamp
  (test_alu_f32_min_max, test_alu_min3_max3) now expect +0, and the
  f16 unary harness checks clamp of a NaN result instead of skipping.
- test_rcp_iflag_mods / test_f64_conversion match the new SPIR-V.
- docs/design.md and docs/shader_validation.md no longer list the
  f32/f16 clamp of a NaN as unmodelled. Still a bare FClamp: the f64
  clamp (open PR #475) and the packed v_pk_* clamp.

AI-assisted (Claude Code).

Co-Authored-By: Claude Opus 5.5 <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 09:13:26 +00:00
github manually merged commit a9bed902d2 into gh-pr/642/base 2026-11-26 09:13:28 +00:00
Author
Owner

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

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

  • At this head: test_alu_f32_min_max, test_alu_min3_max3, test_alu_vop3_mods_hw and test_valu_f32_min3_max3_mad pass, and the new tests/semantic/test_vop3_clamp_nan.py passes 12 tests.
  • Against main's translator the new file errors 33 times with spirv_mini_interp.UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined), which is exactly the undefined-on-NaN clamp this PR replaces, and the two modified files error once and twice. So the added cases do distinguish the two revisions.
  • No GPU run: the semantic interpreter is the oracle for the words here, and the fixture set has no clamp-of-NaN GPU case on this machine.

llvm-mc 23.1.6. Synthetic fixtures only, no game data. AI-assisted.

**@⁠GeorgiyDemo** on GitHub · 2026-11-24 11:25 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/642#issuecomment-7130073517) Checked `a9bed902` on Apple M5 Pro (16-core GPU), macOS 26.7.3 (25G342), against `main` `d42773d07`: - At this head: `test_alu_f32_min_max`, `test_alu_min3_max3`, `test_alu_vop3_mods_hw` and `test_valu_f32_min3_max3_mad` pass, and the new `tests/semantic/test_vop3_clamp_nan.py` passes 12 tests. - Against `main`'s translator the new file errors 33 times with `spirv_mini_interp.UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined)`, which is exactly the undefined-on-NaN clamp this PR replaces, and the two modified files error once and twice. So the added cases do distinguish the two revisions. - No GPU run: the semantic interpreter is the oracle for the words here, and the fixture set has no clamp-of-NaN GPU case on this machine. llvm-mc 23.1.6. Synthetic fixtures only, no game data. AI-assisted.
Author
Owner

@⁠hamdog0203 on GitHub · 2026-11-24 11:35 UTC · original

Independent code review and Windows 11 + RTX 4060 verification on commit a9bed902d (compared against main c9485def3, no game files, AI-assisted: yes).

Environment

  • OS / GPU: Windows 11 x64 (26200.9974), NVIDIA GeForce RTX 4060 (driver 621.35, subgroup 32..32)
  • Tools: llvm-mc 23.1.6, Khronos spirv-as / spirv-val, vk_compute_test / GPU harness built with MSVC 14.44.35207

Test results on a9bed902d

  • All 12 related semantic/unit test files passed:
    • tests/semantic/test_vop3_clamp_nan.py: 12/12 tests OK (negative control against unpatched main failed with 33 errors: UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined))
    • tests/semantic/test_alu_f32_min_max.py (43 OK), test_alu_min3_max3.py (20 OK), test_valu_f32_min3_max3_mad.py (22 OK), test_alu_vop3_mods_hw.py (2 OK), tests/test_f64_conversion.py (1 OK), tests/test_rcp_iflag_mods.py (5 OK), and all 5 test_valu_*_f16.py suites (fract, pack_b32, rcp, rsq, sqrt: all OK).
  • Synthetic 10-case compute readback on RTX 4060 (vk_compute_test):
    • Lowered shader went from 1 OpIsNan guard on main to 10 on a9bed902d; passed spirv-val and returned exact expected bits on all 10/10 slots on RTX 4060 (note: NVIDIA's driver already evaluated unguarded FClamp(NaN, 0, 1) to +0.0, while spirv_mini_interp confirms the guard eliminates the undefined SPIR-V cases).
  • Full GPU test suite (scripts/run_gpu_tests.py):
    • 257/257 fixtures translated and passed spirv-val.
    • 197/248 passed, 49 skipped (ps_f32_mods, ps_outmods_f16, ps_f16_med3_mods, ps_f16_fnan_med3, ps_fnan_med3, ps_fnan_med3_s2, ps_mul_legacy_denorm_mods, ps_vop3_const_mods, ps_fmac_f16_mods, etc. all 4096/4096 pixels exact).
    • Only cs_imgload_tfe and cs_imgload_d16_tfe failed, which also fail identically (0/2 passed, texel(5,3) = 2071) on unmodified main here.

Non-blocking code-review note

  • Gen.clamp_mods covers scalar f32/f16 VOP3 paths cleanly. Two other FClamp(0, 1) sites still emit unguarded FClamp on potential NaN results if targeted separately later: Gen.clamp_mods_f64 (tools/rdna2spirv.py:6805-6818) and packed v_pk_*_f16 clamp (tools/rdna2spirv.py:9369-9375).
**@⁠hamdog0203** on GitHub · 2026-11-24 11:35 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/642#issuecomment-7130288963) Independent code review and Windows 11 + RTX 4060 verification on commit `a9bed902d` (compared against `main` `c9485def3`, no game files, AI-assisted: yes). ### Environment - **OS / GPU:** Windows 11 x64 (`26200.9974`), NVIDIA GeForce RTX 4060 (driver `621.35`, subgroup `32..32`) - **Tools:** `llvm-mc 23.1.6`, Khronos `spirv-as` / `spirv-val`, `vk_compute_test` / GPU harness built with MSVC `14.44.35207` ### Test results on `a9bed902d` - **All 12 related semantic/unit test files passed:** - `tests/semantic/test_vop3_clamp_nan.py`: **12/12 tests OK** (negative control against unpatched `main` failed with **33 errors**: `UnsupportedConstruct: GLSL.std.450 FClamp with these operands (undefined)`) - `tests/semantic/test_alu_f32_min_max.py` (43 OK), `test_alu_min3_max3.py` (20 OK), `test_valu_f32_min3_max3_mad.py` (22 OK), `test_alu_vop3_mods_hw.py` (2 OK), `tests/test_f64_conversion.py` (1 OK), `tests/test_rcp_iflag_mods.py` (5 OK), and all 5 `test_valu_*_f16.py` suites (`fract`, `pack_b32`, `rcp`, `rsq`, `sqrt`: all OK). - **Synthetic 10-case compute readback on RTX 4060 (`vk_compute_test`):** - Lowered shader went from `1` `OpIsNan` guard on `main` to `10` on `a9bed902d`; passed `spirv-val` and returned exact expected bits on all 10/10 slots on RTX 4060 (note: NVIDIA's driver already evaluated unguarded `FClamp(NaN, 0, 1)` to `+0.0`, while `spirv_mini_interp` confirms the guard eliminates the undefined SPIR-V cases). - **Full GPU test suite (`scripts/run_gpu_tests.py`):** - `257/257` fixtures translated and passed `spirv-val`. - **197/248 passed, 49 skipped** (`ps_f32_mods`, `ps_outmods_f16`, `ps_f16_med3_mods`, `ps_f16_fnan_med3`, `ps_fnan_med3`, `ps_fnan_med3_s2`, `ps_mul_legacy_denorm_mods`, `ps_vop3_const_mods`, `ps_fmac_f16_mods`, etc. all `4096/4096 pixels exact`). - Only `cs_imgload_tfe` and `cs_imgload_d16_tfe` failed, which also fail identically (`0/2 passed`, `texel(5,3) = 2071`) on unmodified `main` here. ### Non-blocking code-review note - `Gen.clamp_mods` covers scalar `f32`/`f16` VOP3 paths cleanly. Two other `FClamp(0, 1)` sites still emit unguarded `FClamp` on potential NaN results if targeted separately later: `Gen.clamp_mods_f64` (`tools/rdna2spirv.py:6805-6818`) and packed `v_pk_*_f16` clamp (`tools/rdna2spirv.py:9369-9375`).
Author
Owner

@⁠qhukz13 on GitHub · 2026-11-24 11:36 UTC · original · reviewed

Reviewed on head a9bed902d (Linux, no GPU: CPU interpreter and the unit tests only).

Checked:

  • Every FClamp the translator emits at this head: besides clamp_mods, the only unguarded ones are the packed v_pk_* clamp (tools/rdna2spirv.py:9379) and clamp_mods_f64 (:6821), both called out as out of scope. The remaining sites (v_cvt_{i,u}16_f16, v_cvt_norm_*, v_cvt_pknorm_*, div_fixup) already select +0 for a NaN first, and clamp_f16 goes through clamp_mods, so the f16 paths are covered.
  • Pass: test_vop3_clamp_nan.py, test_alu_f32_min_max.py, test_alu_min3_max3.py, test_alu_vop3_mods_hw.py, test_valu_f32_min3_max3_mad.py, the five users of f16_unary_cases.py (test_valu_{rcp,rsq,sqrt,fract}_f16.py, test_valu_pack_b32_f16.py), tests/test_rcp_iflag_mods.py, tests/test_f64_conversion.py.
  • tools/semantic_mutants.py --lines 6595-6603 tests/semantic/test_vop3_clamp_nan.py: the one mutant (OpSelect arms swapped) is caught (2 failures, 33 errors).

A suggestion, not a blocker: the new file's expectations are +0 by the DX10_CLAMP rule, with the hardware evidence cited from docs/shader_validation.md. test_alu_vop3_mods_hw.py says it left out the measured clamp-of-NaN rows because the translation was undefined; now that it is defined, replaying those gfx1036 rows there (in a follow-up if you prefer) would check the +0 against the measured words rather than against the rule.

Looks good to me.

**@⁠qhukz13** on GitHub · 2026-11-24 11:36 UTC · [original](https://github.com/ubiquitarianism/leonida/pull/642#pullrequestreview-6038491047) · **reviewed** Reviewed on head a9bed902d (Linux, no GPU: CPU interpreter and the unit tests only). Checked: - Every `FClamp` the translator emits at this head: besides `clamp_mods`, the only unguarded ones are the packed `v_pk_*` clamp (`tools/rdna2spirv.py:9379`) and `clamp_mods_f64` (`:6821`), both called out as out of scope. The remaining sites (`v_cvt_{i,u}16_f16`, `v_cvt_norm_*`, `v_cvt_pknorm_*`, div_fixup) already select +0 for a NaN first, and `clamp_f16` goes through `clamp_mods`, so the f16 paths are covered. - Pass: `test_vop3_clamp_nan.py`, `test_alu_f32_min_max.py`, `test_alu_min3_max3.py`, `test_alu_vop3_mods_hw.py`, `test_valu_f32_min3_max3_mad.py`, the five users of `f16_unary_cases.py` (`test_valu_{rcp,rsq,sqrt,fract}_f16.py`, `test_valu_pack_b32_f16.py`), `tests/test_rcp_iflag_mods.py`, `tests/test_f64_conversion.py`. - `tools/semantic_mutants.py --lines 6595-6603 tests/semantic/test_vop3_clamp_nan.py`: the one mutant (OpSelect arms swapped) is caught (2 failures, 33 errors). A suggestion, not a blocker: the new file's expectations are +0 by the DX10_CLAMP rule, with the hardware evidence cited from `docs/shader_validation.md`. `test_alu_vop3_mods_hw.py` says it left out the measured clamp-of-NaN rows because the translation was undefined; now that it is defined, replaying those gfx1036 rows there (in a follow-up if you prefer) would check the +0 against the measured words rather than against the rule. Looks good to me.
This discussion has been locked. Commenting is limited to contributors.
No description provided.