rdna2spirv: support directed MODE.FP_ROUND[3:2] (+inf/-inf) and dynamic s_setreg_b32 #247

Closed
Andretuta wants to merge 1 commit from upstream-pr/247 into main
Andretuta commented 2026-11-24 02:53:04 +00:00 (Migrated from github.com)

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


Carried over from old PR 9982 by Lewandox in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch old-pr/9982, 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.
Lewandox: 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):

Adds full IEEE-754 directed rounding support (RTP / +inf = mode 1 and RTN / -inf = mode 2) and dynamic SGPR s_setreg_b32 writes for MODE.FP_ROUND[3:2] (f16 and f64 rounding) in tools/rdna2spirv.py.

What changed

  • Added Gen._round_mode_flags(is_neg, mode_val) to map all four MODE.FP_ROUND[3:2] encodings (0=RNE, 1=RTP, 2=RTN, 3=RTZ) into sign-aware is_trunc (toward zero) and is_away (away from zero) predicates.
  • Gen.f16_bits / Gen.f16_res: extended integer-only f32-to-f16 narrowing to handle all four rounding modes across normals (bias 0x1FFF when is_away), subnormals & tiny non-zero underflow (all_ones guarded on e != 0 so ±0.0 stays ±0.0 while 2^-126 <= |x| < 2^-24 rounds away to ±0x0001), and overflow boundaries (> 65504.0 overflows to ±inf in is_away, clamps to ±65504 in is_trunc).
  • Gen.f16_omod: finite mul:2 / mul:4 overflow now uses sign-aware is_trunc (±65504 vs ±inf).
  • Gen.sum_toward_zero & Gen.f64_add_mode: extended TwoSum / FastTwoSum error adjustment to step 1 ulp away from zero when is_away (including DBL_MAX + 1.0 -> +inf under RTP) and produce -0.0 on opposite-sign exact cancellation under RTN (mode == 2), per IEEE-754.
  • Gen.mode_write & Gen.write_half_round_mode: accepted modes 1 and 2 in s_round_mode and s_setreg_imm32_b32, and lowered dynamic SGPR s_setreg_b32 hwreg(HW_REG_MODE, 2, 2), sX (sX & 3 -> %mode_r16). Writes targeting other HW_REG_MODE bit ranges remain Unsupported.
  • Added exhaustive bit-exact semantic tests (against a Python fractions.Fraction IEEE-754 oracle + spirv_asm.assemble()) in tests/semantic/test_mode_fp_round_directed.py and updated tests/semantic/test_scalar_noop_waitcnt.py to move modes 1/2 and s_round_mode 4/8 into the modelled set.

Verification

  • python -I -m unittest discover -s tests/semantic -p "test_mode_fp_round_directed.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_scalar_noop_waitcnt.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_valu_add_f64.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_f16_omod_rdna2.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_f16_sum_narrowing.py" -v (PASS)
  • python -I -m unittest discover -s tests/semantic -p "test_valu_mul_f16_mode_round.py" -v (PASS)
  • python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py (PASS)
  • python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py (PASS)

Note: AI-assisted implementation, verified locally against the semantic/assembler test suites above (no game data used).

<!-- greyfinch-sync:ubiquitarianism/leonida:issue:5465182015 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/247). Originally posted by [Andretuta](https://github.com/Andretuta) on 2026-11-24T02:53:04Z. --- > Carried over from **old PR 9982** by `Lewandox` in the original Leonida repository, which went private on 23 Nov 2026. The code is the branch `old-pr/9982`, 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. > `Lewandox`: 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): Adds full IEEE-754 directed rounding support (`RTP` / `+inf` = mode 1 and `RTN` / `-inf` = mode 2) and dynamic SGPR `s_setreg_b32` writes for `MODE.FP_ROUND[3:2]` (f16 and f64 rounding) in `tools/rdna2spirv.py`. ### What changed - Added `Gen._round_mode_flags(is_neg, mode_val)` to map all four `MODE.FP_ROUND[3:2]` encodings (`0=RNE`, `1=RTP`, `2=RTN`, `3=RTZ`) into sign-aware `is_trunc` (toward zero) and `is_away` (away from zero) predicates. - **`Gen.f16_bits` / `Gen.f16_res`**: extended integer-only f32-to-f16 narrowing to handle all four rounding modes across normals (bias `0x1FFF` when `is_away`), subnormals & tiny non-zero underflow (`all_ones` guarded on `e != 0` so `±0.0` stays `±0.0` while `2^-126 <= |x| < 2^-24` rounds away to `±0x0001`), and overflow boundaries (`> 65504.0` overflows to `±inf` in `is_away`, clamps to `±65504` in `is_trunc`). - **`Gen.f16_omod`**: finite `mul:2` / `mul:4` overflow now uses sign-aware `is_trunc` (`±65504` vs `±inf`). - **`Gen.sum_toward_zero` & `Gen.f64_add_mode`**: extended TwoSum / FastTwoSum error adjustment to step 1 ulp away from zero when `is_away` (including `DBL_MAX + 1.0 -> +inf` under `RTP`) and produce `-0.0` on opposite-sign exact cancellation under `RTN` (`mode == 2`), per IEEE-754. - **`Gen.mode_write` & `Gen.write_half_round_mode`**: accepted modes `1` and `2` in `s_round_mode` and `s_setreg_imm32_b32`, and lowered dynamic SGPR `s_setreg_b32 hwreg(HW_REG_MODE, 2, 2), sX` (`sX & 3 -> %mode_r16`). Writes targeting other `HW_REG_MODE` bit ranges remain `Unsupported`. - Added exhaustive bit-exact semantic tests (against a Python `fractions.Fraction` IEEE-754 oracle + `spirv_asm.assemble()`) in `tests/semantic/test_mode_fp_round_directed.py` and updated `tests/semantic/test_scalar_noop_waitcnt.py` to move modes `1`/`2` and `s_round_mode 4`/`8` into the modelled set. ### Verification - `python -I -m unittest discover -s tests/semantic -p "test_mode_fp_round_directed.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_scalar_noop_waitcnt.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_valu_add_f64.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_f16_omod_rdna2.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_f16_sum_narrowing.py" -v` (PASS) - `python -I -m unittest discover -s tests/semantic -p "test_valu_mul_f16_mode_round.py" -v` (PASS) - `python -m compileall -q tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py` (PASS) - `python -I scripts/check_no_game_data.py tools/rdna2spirv.py tests/semantic/test_mode_fp_round_directed.py tests/semantic/test_scalar_noop_waitcnt.py` (PASS) Note: AI-assisted implementation, verified locally against the semantic/assembler test suites above (no game data used).
aslan2142 commented 2026-12-01 11:50:13 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T11:50:13Z.

Review: COMMENTED


Draft review note: carried over from old PR 9982 (MODE.FP_ROUND[3:2] directed rounding + dynamic s_setreg_b32). Currently mergeable on main, but still a draft with cancelled CI. If someone picks this up, please refresh against current main, run CI to green, and mark ready for review.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6130662633 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/247#pullrequestreview-6130662633). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T11:50:13Z. **Review:** COMMENTED --- Draft review note: carried over from old PR 9982 (`MODE.FP_ROUND[3:2]` directed rounding + dynamic `s_setreg_b32`). Currently mergeable on `main`, but still a draft with cancelled CI. If someone picks this up, please refresh against current `main`, run CI to green, and mark ready for review. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 12:16:40 +00:00 (Migrated from github.com)

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


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:7310902819 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/247#issuecomment-7310902819). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:16:40Z. --- 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 12:58:47 +00:00 (Migrated from github.com)

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

Review: CHANGES_REQUESTED


Now ready for review, requesting changes before merge:

  • Missing contract table documentation (AGENTS.md rule): this PR promotes directed MODE.FP_ROUND[3:2] (+inf/-inf) and dynamic SGPR s_setreg_b32 (tools/rdna2spirv.py:1280-1292) from Unsupported to translated rules, but does not add a row to the translator contract table in docs/design.md recording the new rules and how they were confirmed (also note docs/design.md:305 still says s_setreg is ignored).
  • CI: all checks from 2026-11-24 were cancelled. Please add the docs/design.md contract entry and run fresh green CI.

(AI-assisted review.)

<!-- greyfinch-sync:ubiquitarianism/leonida:review:6131714558 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/247#pullrequestreview-6131714558). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T12:58:47Z. **Review:** CHANGES_REQUESTED --- Now ready for review, requesting changes before merge: - **Missing contract table documentation (`AGENTS.md` rule)**: this PR promotes directed `MODE.FP_ROUND[3:2]` (`+inf`/`-inf`) and dynamic SGPR `s_setreg_b32` (`tools/rdna2spirv.py:1280-1292`) from `Unsupported` to translated rules, but does not add a row to the translator contract table in `docs/design.md` recording the new rules and how they were confirmed (also note `docs/design.md:305` still says `s_setreg` is ignored). - **CI**: all checks from 2026-11-24 were cancelled. Please add the `docs/design.md` contract entry and run fresh green CI. *(AI-assisted review.)*
aslan2142 commented 2026-12-01 16:29:22 +00:00 (Migrated from github.com)

Imported from ubiquitarianism/leonida. Originally posted by aslan2142 on 2026-12-01T16:29:22Z.


Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (d3d12/) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, d3d12/, or general docs), please feel free to reopen it.

<!-- greyfinch-sync:ubiquitarianism/leonida:comment:7317691097 --> Imported from [ubiquitarianism/leonida](https://github.com/ProjectLeonida/mandy/pull/247#issuecomment-7317691097). Originally posted by [aslan2142](https://github.com/aslan2142) on 2026-12-01T16:29:22Z. --- Closing this PR because the legacy C++ Vulkan runtime (and legacy Python shader compiler) is now considered deprecated in favor of the new Rust Vulkan runtime (in progress) and the Rust DX12 runtime (`d3d12/`) available now. Thank you for the contribution! If you believe this PR was closed by mistake (for example, if it still applies to active tools, `d3d12/`, or general docs), please feel free to reopen it.

Pull request closed

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!247
No description provided.