[PR #1277] [CLOSED] rdna2spirv: test each legacy multiply operand once (SHADER-LEGACY-MUL-SUBNORMAL) #1309

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

📋 Pull Request Information

Original PR: https://github.com/ubiquitarianism/leonida/pull/1277
Author: @Moriz82
Created: 11/24/2026
Status: ❌ Closed

Base: main ← Head: split/shader-legacy-mul-subnormal


📝 Commits (1)

  • 819cc92 rdna2spirv: test each legacy multiply operand once (SHADER-LEGACY-MUL-SUBNORMAL)

📊 Changes

3 files changed (+27 additions, -1 deletions)

View changed files

📝 docs/design.md (+1 -1)
📝 tests/semantic/test_valu_mul_legacy_denorm.py (+23 -0)
📝 tools/rdna2spirv.py (+3 -0)

📄 Description

Rebased onto bfef489c at 819cc921. Main already combines the operand denormal checks. This diff keeps the operation-count regression test and explains the calculation. All five focused tests passed.

Claimed in the coordination issue as: SHADER-LEGACY-MUL-SUBNORMAL (claim).

Part of the split of #984 into small pull requests.

Depends on: none
Overlaps: none found. #984 is the parent integration. Searched open and merged pull requests for v_mul_legacy_f32, mul_legacy, legacy mul, legacy f32, denorm, subnormal and flush_f32: #642 (merged, a clamped NaN gives +0) is in the base and its clamp_mods is used unchanged; #1039 adds GPU fixtures for the v_mad_f32 flush and #637 decodes v_mac_legacy_f32, neither touches this lowering or its test. The current diffs of #811, #829, #858, #1039, #1068 and #1112 apply cleanly on this branch (git apply --check).

What and why

v_mul_legacy_f32 is the DX9 multiply: zero times anything is +0, and under the game's MODE an f32 subnormal reads as zero (the contract row "v_mul_legacy_f32 default-mode f32 denormal flushing"). For every such instruction the translator emitted:

  • a signed flush of each operand (FAbs, a compare with FLT_MIN, the sign bit, a bitcast and a select),
  • a compare of each flushed copy with zero for the override,
  • the product, its flush and the select,
  • and a second flush of the result, also when no output modifier had scaled it.

A subnormal operand ends as +0 whatever its sign, so the signed zero made for it is never seen: |x| < FLT_MIN is the override condition itself. This change

  • tests each operand's magnitude once and uses that test as the override,
  • multiplies the operands as they are (the product is thrown away whenever either test holds, and is the product of the flushed operands otherwise),
  • flushes the product once, and
  • flushes the result again only after mul:2, mul:4 or div:2, the one step that can make a new subnormal. A clamp of +0, a flushed product or a NaN cannot.

No result changes. The emitted code gets shorter. For one multiply in the test's pixel shader, in assembly lines over the same program without it:

Form Base This branch
no modifier 37 22
clamp 40 25
mul:2 43 35
mul:2 clamp 46 38

The three GPU fixtures with four legacy multiplies each go from 7,324 to 6,140 bytes (ps_legacy_mul), 7,196 to 6,012 (ps_mul_legacy_denorm) and 7,616 to 6,840 (ps_mul_legacy_denorm_mods, three of its four with an output multiplier). docs/design.md counts 147,499 instructions of the form v_mul_legacy_f32 … clamp alone in the game's listings, so the instruction is common enough for its length to matter.

The contract row now says how the software test is laid out. The rule itself is unchanged.

Tested

CachyOS (kernel 7.3.5), Python 3.14, LLVM llvm-mc 23.1.5 and SPIRV-Tools v2026.4. GPU fixtures on an NVIDIA GeForce RTX 4070, driver 620.73.04.

  • python3 -I tests/semantic/test_valu_mul_legacy_denorm.py: 5 tests pass. With the base commit's translator (LEONIDA_TOOLS_DIR=<export of the base's tools>) the new test_each_operand_is_tested_once fails in all 4 of its subtests: 4 magnitude tests where 3 are expected without an output multiplier, and 3 compares with zero where 1 is expected with one. The other 4 tests pass on the base, including the two operand pairs added here (an infinity times the largest subnormal of the other sign): they pin the FLT_MIN boundary of the new test and do not show a fault in the base.
  • Results before and after: 23 operand bit patterns (zeros, smallest and largest subnormals, FLT_MIN, normals, FLT_MAX, infinities and NaNs of both signs) in every pair, under 7 modifier sets (none, mul:2, mul:4, div:2, clamp, mul:2 clamp, div:2 clamp), run through spirv_mini_interp with each translator: the same word in all 3,703 cases. This sweep is a local script, not part of the pull request.
  • python3 -I scripts/run_semantic_tests.py tests/semantic -p <file> for test_negative_controls.py (1 test), test_ps_semantics.py (7), test_value_semantics.py (41) and test_valu_f32_min3_max3_mad.py (22), and python3 -I on test_mutations.py (3), test_vop3_clamp_nan.py (12), test_alu_vop3_mods_hw.py (2), test_bitcast_round_trip.py (3) and tests/test_f32_fuzz.py (29, 1 skipped): all pass. test_mutations.py still catches the translator with the zero override removed.
  • python3 -I scripts/run_gpu_tests.py --filter legacy: 3 shaders translate and pass spirv-val; ps_legacy_mul, ps_mul_legacy_denorm and ps_mul_legacy_denorm_mods each give 4096/4096 exact pixels, with the base commit's translator and with this one. The harness programs are from a native Linux build of 2026-11-23 on this machine and were not rebuilt.
  • python3 -I scripts/run_semantic_tests.py tests/semantic -j 2: 470 files, 6,169 tests, 0 files failed (the base: 470 files, 6,168 tests, 0 failed).
  • The standard-library and pytest files under tests/ the way ci.yml runs them, and python3 -I -m compileall -q make_game.py tools tests: 520 files, 519 pass. tests/test_imhex_patterns.py fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change. compileall passes.
  • python3 -I scripts/check_no_game_data.py: passes.

Not run: native Windows, macOS, an AMD or Intel GPU, the full GPU suite, the tools/hw_oracle probe (the hardware rule is not changed, so nothing new was measured), the runtime build and a game boot.

GPUs tested: NVIDIA GeForce RTX 4070, driver 620.73.04, Linux (the three legacy multiply fixtures only).

AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named.

Checklist

  • Based on upstream main 6293c1c4f, one commit.
  • One topic: the lowering of one instruction, its test and its contract row.
  • No game data, keys or decryption, including in logs or reports quoted here.
  • The contract row of docs/design.md describes the new layout and names the test that pins it.

🤖 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/1277 **Author:** [@Moriz82](https://github.com/Moriz82) **Created:** 11/24/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `split/shader-legacy-mul-subnormal` --- ### 📝 Commits (1) - [`819cc92`](https://github.com/ubiquitarianism/leonida/commit/819cc9218bd62178731d6b6f3e67e0a7e31b6f05) rdna2spirv: test each legacy multiply operand once (SHADER-LEGACY-MUL-SUBNORMAL) ### 📊 Changes **3 files changed** (+27 additions, -1 deletions) <details> <summary>View changed files</summary> 📝 `docs/design.md` (+1 -1) 📝 `tests/semantic/test_valu_mul_legacy_denorm.py` (+23 -0) 📝 `tools/rdna2spirv.py` (+3 -0) </details> ### 📄 Description **Rebased onto `bfef489c` at `819cc921`.** Main already combines the operand denormal checks. This diff keeps the operation-count regression test and explains the calculation. All five focused tests passed. Claimed in the coordination issue as: SHADER-LEGACY-MUL-SUBNORMAL ([claim](https://github.com/ubiquitarianism/leonida/issues/14#issuecomment-7144630364)). Part of the split of #984 into small pull requests. Depends on: none Overlaps: none found. #984 is the parent integration. Searched open and merged pull requests for `v_mul_legacy_f32`, `mul_legacy`, `legacy mul`, `legacy f32`, `denorm`, `subnormal` and `flush_f32`: #642 (merged, a clamped NaN gives +0) is in the base and its `clamp_mods` is used unchanged; #1039 adds GPU fixtures for the `v_mad_f32` flush and #637 decodes `v_mac_legacy_f32`, neither touches this lowering or its test. The current diffs of #811, #829, #858, #1039, #1068 and #1112 apply cleanly on this branch (`git apply --check`). ### What and why `v_mul_legacy_f32` is the DX9 multiply: zero times anything is +0, and under the game's `MODE` an f32 subnormal reads as zero (the contract row "`v_mul_legacy_f32` default-mode f32 denormal flushing"). For every such instruction the translator emitted: - a signed flush of each operand (`FAbs`, a compare with `FLT_MIN`, the sign bit, a bitcast and a select), - a compare of each flushed copy with zero for the override, - the product, its flush and the select, - and a second flush of the result, also when no output modifier had scaled it. A subnormal operand ends as +0 whatever its sign, so the signed zero made for it is never seen: `|x| < FLT_MIN` is the override condition itself. This change - tests each operand's magnitude once and uses that test as the override, - multiplies the operands as they are (the product is thrown away whenever either test holds, and is the product of the flushed operands otherwise), - flushes the product once, and - flushes the result again only after `mul:2`, `mul:4` or `div:2`, the one step that can make a new subnormal. A clamp of +0, a flushed product or a NaN cannot. **No result changes.** The emitted code gets shorter. For one multiply in the test's pixel shader, in assembly lines over the same program without it: | Form | Base | This branch | |---|---:|---:| | no modifier | 37 | 22 | | `clamp` | 40 | 25 | | `mul:2` | 43 | 35 | | `mul:2 clamp` | 46 | 38 | The three GPU fixtures with four legacy multiplies each go from 7,324 to 6,140 bytes (`ps_legacy_mul`), 7,196 to 6,012 (`ps_mul_legacy_denorm`) and 7,616 to 6,840 (`ps_mul_legacy_denorm_mods`, three of its four with an output multiplier). `docs/design.md` counts 147,499 instructions of the form `v_mul_legacy_f32 … clamp` alone in the game's listings, so the instruction is common enough for its length to matter. The contract row now says how the software test is laid out. The rule itself is unchanged. ### Tested CachyOS (kernel 7.3.5), Python 3.14, LLVM `llvm-mc` 23.1.5 and SPIRV-Tools v2026.4. GPU fixtures on an NVIDIA GeForce RTX 4070, driver 620.73.04. - `python3 -I tests/semantic/test_valu_mul_legacy_denorm.py`: 5 tests pass. With the base commit's translator (`LEONIDA_TOOLS_DIR=<export of the base's tools>`) the new `test_each_operand_is_tested_once` fails in all 4 of its subtests: 4 magnitude tests where 3 are expected without an output multiplier, and 3 compares with zero where 1 is expected with one. The other 4 tests pass on the base, including the two operand pairs added here (an infinity times the largest subnormal of the other sign): they pin the `FLT_MIN` boundary of the new test and do not show a fault in the base. - Results before and after: 23 operand bit patterns (zeros, smallest and largest subnormals, `FLT_MIN`, normals, `FLT_MAX`, infinities and NaNs of both signs) in every pair, under 7 modifier sets (none, `mul:2`, `mul:4`, `div:2`, `clamp`, `mul:2 clamp`, `div:2 clamp`), run through `spirv_mini_interp` with each translator: the same word in all 3,703 cases. This sweep is a local script, not part of the pull request. - `python3 -I scripts/run_semantic_tests.py tests/semantic -p <file>` for `test_negative_controls.py` (1 test), `test_ps_semantics.py` (7), `test_value_semantics.py` (41) and `test_valu_f32_min3_max3_mad.py` (22), and `python3 -I` on `test_mutations.py` (3), `test_vop3_clamp_nan.py` (12), `test_alu_vop3_mods_hw.py` (2), `test_bitcast_round_trip.py` (3) and `tests/test_f32_fuzz.py` (29, 1 skipped): all pass. `test_mutations.py` still catches the translator with the zero override removed. - `python3 -I scripts/run_gpu_tests.py --filter legacy`: 3 shaders translate and pass `spirv-val`; `ps_legacy_mul`, `ps_mul_legacy_denorm` and `ps_mul_legacy_denorm_mods` each give 4096/4096 exact pixels, with the base commit's translator and with this one. The harness programs are from a native Linux build of 2026-11-23 on this machine and were not rebuilt. - `python3 -I scripts/run_semantic_tests.py tests/semantic -j 2`: 470 files, 6,169 tests, 0 files failed (the base: 470 files, 6,168 tests, 0 failed). - The standard-library and pytest files under `tests/` the way `ci.yml` runs them, and `python3 -I -m compileall -q make_game.py tools tests`: 520 files, 519 pass. `tests/test_imhex_patterns.py` fails (6 of its 9 tests) and fails the same way on the base commit, so it is not from this change. `compileall` passes. - `python3 -I scripts/check_no_game_data.py`: passes. Not run: native Windows, macOS, an AMD or Intel GPU, the full GPU suite, the `tools/hw_oracle` probe (the hardware rule is not changed, so nothing new was measured), the runtime build and a game boot. GPUs tested: NVIDIA GeForce RTX 4070, driver 620.73.04, Linux (the three legacy multiply fixtures only). AI-assisted: yes (Claude Code). The numbers above come from runs on the machine named. ### Checklist - [x] Based on upstream main `6293c1c4f`, one commit. - [x] One topic: the lowering of one instruction, its test and its contract row. - [x] No game data, keys or decryption, including in logs or reports quoted here. - [x] The contract row of `docs/design.md` describes the new layout and names the test that pins it. 🤖 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:20:48 +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#1309
No description provided.