Shaders: track rounding modes set from constant SGPRs #2891

Open
github-bot wants to merge 1 commit from github-bot/gh-2891 into main AGit
Owner

@⁠andrewpozdnakov7-jpg on GitHub · 2026-11-25 23:11 UTC · original
andrewpozdnakov7-jpg:codex2/round-mode-sgpr-constant → main · +40 −14 · 2 files · 1 commits · open

S_SETREG_B32 can take the MODE value from a scalar register. Reuse the lowerer's constant proof for that operand, so the FP16/FP64 rounding analysis sees values written through s_mov_b32 and s_movk_i32. Both block-entry analysis and instruction lowering use the same result.

The same synthetic V_CVT_F16_F32 test runs on app-local WARP SM 6.6 before and after. It sets round-to-zero through two scalar moves, converts, resets to nearest-even through s_movk_i32, and converts again. On 842b0cb four of six inputs differ from the CPU model. For input 0x3f801800, the two low halves are [0x3c01, 0x3c01] with [0x3c00, 0x3c01] expected. With this commit all six match, including positive and negative values, overflow, subnormal rounding, and signed zero. The existing literal MODE test, 35 VALU tests, DXIL validation, strict shader Clippy and the asset policy passed.

I also combined the patch locally with #2886 at 2aa1767, keeping both fixtures and passing the lowerer to its whole-wave update. Both regression tests passed on WARP.

RDNA 2 ISA · Coordination claim

Please retain the Git author when moving the patch. If you fold it into another commit, please add Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.

**@⁠andrewpozdnakov7-jpg** on GitHub · 2026-11-25 23:11 UTC · [original](https://github.com/Marvisak/VIctory/pull/2891) `andrewpozdnakov7-jpg:codex2/round-mode-sgpr-constant` → `main` · +40 −14 · 2 files · 1 commits · open `S_SETREG_B32` can take the MODE value from a scalar register. Reuse the lowerer's constant proof for that operand, so the FP16/FP64 rounding analysis sees values written through `s_mov_b32` and `s_movk_i32`. Both block-entry analysis and instruction lowering use the same result. The same synthetic `V_CVT_F16_F32` test runs on app-local WARP SM 6.6 before and after. It sets round-to-zero through two scalar moves, converts, resets to nearest-even through `s_movk_i32`, and converts again. On `842b0cb` four of six inputs differ from the CPU model. For input `0x3f801800`, the two low halves are `[0x3c01, 0x3c01]` with `[0x3c00, 0x3c01]` expected. With this commit all six match, including positive and negative values, overflow, subnormal rounding, and signed zero. The existing literal MODE test, 35 VALU tests, DXIL validation, strict shader Clippy and the asset policy passed. I also combined the patch locally with #2886 at `2aa1767`, keeping both fixtures and passing the lowerer to its whole-wave update. Both regression tests passed on WARP. [RDNA 2 ISA](https://www.amd.com/content/dam/amd/en/documents/radeon-tech-docs/instruction-set-architectures/rdna2-shader-instruction-set-architecture.pdf) · [Coordination claim](https://git.gtahub.st/github/Marvisak--VIctory/issues/1633) Please retain the Git author when moving the patch. If you fold it into another commit, please add `Co-authored-by: andrewpozdnakov7-jpg <[email protected]>`.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:16:51 +00:00
Author
Owner

@⁠pawrenity on GitHub · 2026-11-25 23:31 UTC · original

VERIFY-2891: verified dbfe2c21 (base main 842b0cb) on NVIDIA GeForce RTX 4070, Windows 11. The semantics harness ran both the hardware host (the 4070) and WARP.

Review

  • The field check is unchanged: the round mode is known only when the write covers MODE bits 3:2 (off <= 2 && off + size >= 4), and input >> (2 - off) & 3 extracts those bits. Partial overlaps still give None, and writes that miss bits 3:2 keep the current mode.
  • konst_operand handles Literal itself, so s_setreg_imm32_b32 behaves as before. For an SGPR source it walks unguarded s_mov_b32/s_movk_i32 defs through def_block, which is filled in Lower::new. That makes it safe to call from round16 at the start of run(), before any block is lowered. Other constant producers (s_or_b32 and so on), phis and guarded moves still give None. That is conservative and matches the old behaviour.
  • I checked the test encodings by hand. 0xB9800881 | reg<<16 is SOPK op 0x13 (s_setreg_b32), and simm16 0x0881 is hwreg(id 1 = MODE, offset 2, size 2). 0xB0020000 is s_movk_i32 s2, 0. The harness disassembly confirms this. I also checked the expected values: 0x3F801800 is above the midpoint between 1.0 and the next f16 (RTZ gives 0x3c00, RNE gives 0x3c01). 0x47800000 = 65536 gives max finite 0x7bff under RTZ and inf under RNE. 0x337FFFFF is just below 2^-24, so RTZ gives 0 and RNE gives 0x0001.
  • No defects found. Note: #2886 adds another round16_after(inst, mode) call site, and this PR changes the function's signature. Whichever merges second needs the trivial rebase that the PR description mentions.

Tests (PR head)

  • cargo test -p victory-shader --locked --lib -- emit::semantics_tests::f16_: 4 passed, including the new f16_rounding_follows_a_constant_setreg_source.
  • cargo test -p victory-shader --locked --lib -- emit::semantics_tests: 35 passed, 1 ignored.
  • cargo test -p victory-shader --locked --lib -- emit::lower: 33 passed.
  • cargo clippy -p victory-shader --all-targets --locked -- -D warnings: clean.

Without the fix (842b0cb plus only the semantics_tests.rs hunk, --test-threads=1): the new test fails, with 4 of 6 inputs mismatching on both hardware and WARP. Both conversions come out RNE. For example, 3f801800 gives [3c01, 3c01] where [3c00, 3c01] is expected, and 47800000 gives [7c00, 7c00] where [7bff, 7c00] is expected. This matches the PR description.

AI-assisted (Claude Code).

**@⁠pawrenity** on GitHub · 2026-11-25 23:31 UTC · [original](https://github.com/Marvisak/VIctory/pull/2891#issuecomment-7177685196) VERIFY-2891: verified `dbfe2c21` (base `main` `842b0cb`) on NVIDIA GeForce RTX 4070, Windows 11. The semantics harness ran both the hardware host (the 4070) and WARP. **Review** - The field check is unchanged: the round mode is known only when the write covers MODE bits 3:2 (`off <= 2 && off + size >= 4`), and `input >> (2 - off) & 3` extracts those bits. Partial overlaps still give `None`, and writes that miss bits 3:2 keep the current mode. - `konst_operand` handles `Literal` itself, so `s_setreg_imm32_b32` behaves as before. For an SGPR source it walks unguarded `s_mov_b32`/`s_movk_i32` defs through `def_block`, which is filled in `Lower::new`. That makes it safe to call from `round16` at the start of `run()`, before any block is lowered. Other constant producers (`s_or_b32` and so on), phis and guarded moves still give `None`. That is conservative and matches the old behaviour. - I checked the test encodings by hand. `0xB9800881 | reg<<16` is SOPK op 0x13 (`s_setreg_b32`), and simm16 `0x0881` is hwreg(id 1 = MODE, offset 2, size 2). `0xB0020000` is `s_movk_i32 s2, 0`. The harness disassembly confirms this. I also checked the expected values: `0x3F801800` is above the midpoint between 1.0 and the next f16 (RTZ gives `0x3c00`, RNE gives `0x3c01`). `0x47800000` = 65536 gives max finite `0x7bff` under RTZ and inf under RNE. `0x337FFFFF` is just below 2^-24, so RTZ gives 0 and RNE gives `0x0001`. - No defects found. Note: #2886 adds another `round16_after(inst, mode)` call site, and this PR changes the function's signature. Whichever merges second needs the trivial rebase that the PR description mentions. **Tests (PR head)** - `cargo test -p victory-shader --locked --lib -- emit::semantics_tests::f16_`: 4 passed, including the new `f16_rounding_follows_a_constant_setreg_source`. - `cargo test -p victory-shader --locked --lib -- emit::semantics_tests`: 35 passed, 1 ignored. - `cargo test -p victory-shader --locked --lib -- emit::lower`: 33 passed. - `cargo clippy -p victory-shader --all-targets --locked -- -D warnings`: clean. **Without the fix** (`842b0cb` plus only the `semantics_tests.rs` hunk, `--test-threads=1`): the new test fails, with 4 of 6 inputs mismatching on both hardware and WARP. Both conversions come out RNE. For example, `3f801800` gives `[3c01, 3c01]` where `[3c00, 3c01]` is expected, and `47800000` gives `[7c00, 7c00]` where `[7bff, 7c00]` is expected. This matches the PR description. AI-assisted (Claude Code).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-26 04:28 UTC · original

Automated triage for the maintainers.

Merge candidate. FP16/FP64 round-mode tracking in emit/lower.rs (round16_after, round16) now accepts an s_setreg_b32 MODE source from a scalar register holding a proven constant via the existing konst_operand, not only a literal; the field-coverage check is unchanged and def_block is built in Lower::new, so the block-entry modes are safe to compute at the start of run. The test checks against an independent CPU model (to_f16) using synthetic encodings.

CI: passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-26 04:28 UTC · [original](https://github.com/Marvisak/VIctory/pull/2891#issuecomment-7182622241) <!-- victory-triage v1 head=dbfe2c2154383c7d6b617454f13e8b3b133760a6 verdict=merge-candidate at=2026-11-26T04:28:03Z --> Automated triage for the maintainers. **Merge candidate.** FP16/FP64 round-mode tracking in `emit/lower.rs` (`round16_after`, `round16`) now accepts an `s_setreg_b32` MODE source from a scalar register holding a proven constant via the existing `konst_operand`, not only a literal; the field-coverage check is unchanged and `def_block` is built in `Lower::new`, so the block-entry modes are safe to compute at the start of `run`. The test checks against an independent CPU model (`to_f16`) using synthetic encodings. CI: passing. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request can be merged automatically.
This branch is out-of-date with the base branch
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2891/head:github-bot/gh-2891'
git switch 'github-bot/gh-2891'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2891'
git switch 'github-bot/gh-2891'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2891'
git switch 'github-bot/gh-2891'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2891'
git switch 'main'
git merge --squash 'github-bot/gh-2891'
git switch 'main'
git merge --ff-only 'github-bot/gh-2891'
git switch 'main'
git merge 'github-bot/gh-2891'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2891
No description provided.