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

Closed
opened 2026-11-26 20:18:41 +01:00 by 4ld0 · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/642
Author: @aadheel
Created: 11/24/2026
Status: ✅ Merged
Merged: 11/24/2026
Merged by: @ubiquitarianism

Base: main ← Head: fix/vop3-clamp-nan-zero


📝 Commits (1)

  • a9bed90 rdna2spirv: VOP3 clamp of a NaN gives +0 (DX10_CLAMP)

📊 Changes

11 files changed (+173 additions, -23 deletions)

View changed files

📝 docs/design.md (+2 -2)
📝 docs/shader_validation.md (+2 -1)
📝 tests/semantic/f16_unary_cases.py (+3 -3)
📝 tests/semantic/test_alu_f32_min_max.py (+5 -4)
📝 tests/semantic/test_alu_min3_max3.py (+7 -8)
📝 tests/semantic/test_alu_vop3_mods_hw.py (+2 -1)
📝 tests/semantic/test_valu_f32_min3_max3_mad.py (+2 -2)
➕ tests/semantic/test_vop3_clamp_nan.py (+137 -0)
📝 tests/test_f64_conversion.py (+3 -0)
📝 tests/test_rcp_iflag_mods.py (+3 -0)
📝 tools/rdna2spirv.py (+7 -2)

📄 Description

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


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/ubiquitarianism/leonida/pull/642 **Author:** [@aadheel](https://github.com/aadheel) **Created:** 11/24/2026 **Status:** ✅ Merged **Merged:** 11/24/2026 **Merged by:** [@ubiquitarianism](https://github.com/ubiquitarianism) **Base:** `main` ← **Head:** `fix/vop3-clamp-nan-zero` --- ### 📝 Commits (1) - [`a9bed90`](https://github.com/ubiquitarianism/leonida/commit/a9bed902d2121ac581f707b2827c30c42fd095c0) rdna2spirv: VOP3 clamp of a NaN gives +0 (DX10_CLAMP) ### 📊 Changes **11 files changed** (+173 additions, -23 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+2 -2) 📝 `docs/shader_validation.md` (+2 -1) 📝 `tests/semantic/f16_unary_cases.py` (+3 -3) 📝 `tests/semantic/test_alu_f32_min_max.py` (+5 -4) 📝 `tests/semantic/test_alu_min3_max3.py` (+7 -8) 📝 `tests/semantic/test_alu_vop3_mods_hw.py` (+2 -1) 📝 `tests/semantic/test_valu_f32_min3_max3_mad.py` (+2 -2) ➕ `tests/semantic/test_vop3_clamp_nan.py` (+137 -0) 📝 `tests/test_f64_conversion.py` (+3 -0) 📝 `tests/test_rcp_iflag_mods.py` (+3 -0) 📝 `tools/rdna2spirv.py` (+7 -2) </details> ### 📄 Description 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) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
4ld0 2026-11-26 20:18:41 +01:00
Sign in to join this conversation.
No labels
bug
pull-request
No milestone
No project
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.

Dependencies

No dependencies set

Reference
starred/leonida#682
No description provided.