Shaders: track rounding modes set from constant SGPRs #2889

Open
andrewpozdnakov7-jpg wants to merge 1 commit from upstream-pr/2891 into main
andrewpozdnakov7-jpg commented 2026-11-25 23:11:37 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-25T23:11:37Z.


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]>.

<!-- greyfinch-sync:Marvisak/VIctory:issue:6615657025 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891). Originally posted by [andrewpozdnakov7-jpg](https://github.com/andrewpozdnakov7-jpg) on 2026-11-25T23:11:37Z. --- `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://github.com/Marvisak/VIctory/issues/1633#issuecomment-7177164416) 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]>`.
fuzeofficlal commented 2026-11-26 00:03:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-25T23:31:28Z.

Review: COMMENTED


Verified head dbfe2c21 cleanly merged onto origin/main (842b0cb7) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 dxil.dll).

Code & Cross-PR Review

  • Sound constant tracking: Running round16 / round16_after inside Lower::run (after self.def_block and self.x.st.guards are built) and resolving s_setreg_b32 / s_setreg_imm32_b32 writes covering hwreg(MODE=1) bits 3:2 via lower.konst_operand handles inline constants, literals, and unguarded s_mov_b32 / s_movk_i32 chains while conservatively falling back to None on guarded definitions, partial bitfield writes, dynamic ALU/phi sources, or conflicting CFG joins.
  • File size: emit/lower.rs is 520,513 B (3,775 B below the 512 KiB limit; cleanly compatible with #2893 at 521,232 B combined).
  • Cross-PR note (#2886): Because #2891 updates round16_after to take lower: &Lower<'_> as its first parameter, if #2886 lands alongside #2891, #2886's whole-wave section call site will need &self added to match.

Local Verification

  • cargo test --locked -p victory-shader --lib semantics_tests: 35/35 passed (1 ignored) on RTX 5090 + WARP + dxil.dll, including f16_rounding_follows_a_constant_setreg_source.
  • cargo test --locked -p victory-shader --lib emit::lower::tests: 33/33 passed.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default and --all-features): clean.
  • cargo xtask policy: 191 files ok.

Looks clean and ready to merge.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066445195 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891#pullrequestreview-6066445195). Originally posted by [fuzeofficlal](https://github.com/fuzeofficlal) on 2026-11-25T23:31:28Z. **Review:** COMMENTED --- Verified head `dbfe2c21` cleanly merged onto `origin/main` (`842b0cb7`) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + D3D12 WARP + Windows SDK 10.0.26100.0 `dxil.dll`). ### Code & Cross-PR Review - **Sound constant tracking**: Running `round16` / `round16_after` inside `Lower::run` (after `self.def_block` and `self.x.st.guards` are built) and resolving `s_setreg_b32` / `s_setreg_imm32_b32` writes covering `hwreg(MODE=1)` bits `3:2` via `lower.konst_operand` handles inline constants, literals, and unguarded `s_mov_b32` / `s_movk_i32` chains while conservatively falling back to `None` on guarded definitions, partial bitfield writes, dynamic ALU/phi sources, or conflicting CFG joins. - **File size**: `emit/lower.rs` is `520,513` B (`3,775` B below the 512 KiB limit; cleanly compatible with `#2893` at `521,232` B combined). - **Cross-PR note (`#2886`)**: Because `#2891` updates `round16_after` to take `lower: &Lower<'_>` as its first parameter, if `#2886` lands alongside `#2891`, `#2886`'s whole-wave section call site will need `&self` added to match. ### Local Verification - `cargo test --locked -p victory-shader --lib semantics_tests`: **35/35 passed** (1 ignored) on RTX 5090 + WARP + `dxil.dll`, including `f16_rounding_follows_a_constant_setreg_source`. - `cargo test --locked -p victory-shader --lib emit::lower::tests`: **33/33 passed**. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default and `--all-features`): clean. - `cargo xtask policy`: `191 files ok`. Looks clean and ready to merge.
kotekompotek commented 2026-11-26 00:03:04 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T23:34:40Z.

Review: COMMENTED


Sound widening: a MODE write whose source is a constant SGPR is just as constant as a literal — resolving through konst_operand picks those up without treating runtime values as modes. The round16 field removal keeping mode16 as the live state is consistent. LGTM.

<!-- greyfinch-sync:Marvisak/VIctory:review:6066474702 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891#pullrequestreview-6066474702). Originally posted by [kotekompotek](https://github.com/kotekompotek) on 2026-11-25T23:34:40Z. **Review:** COMMENTED --- Sound widening: a MODE write whose source is a constant SGPR is just as constant as a literal — resolving through konst_operand picks those up without treating runtime values as modes. The round16 field removal keeping mode16 as the live state is consistent. LGTM.
pawrenity commented 2026-11-26 00:03:32 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by pawrenity on 2026-11-25T23:31:38Z.


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).

<!-- greyfinch-sync:Marvisak/VIctory:comment:7177685196 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891#issuecomment-7177685196). Originally posted by [pawrenity](https://github.com/pawrenity) on 2026-11-25T23:31:38Z. --- 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).
Marvisak commented 2026-11-26 05:04:11 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-26T04:28:10Z.


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

<!-- greyfinch-sync:Marvisak/VIctory:comment:7182622241 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891#issuecomment-7182622241). Originally posted by [Marvisak](https://github.com/Marvisak) on 2026-11-26T04:28:10Z. --- <!-- 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)_
Dukestar69 commented 2026-11-26 09:05:24 +00:00 (Migrated from github.com)

Imported from Marvisak/VIctory. Originally posted by Dukestar69 on 2026-11-26T08:12:29Z.

Review: APPROVED


Verified on RTX 5090 & WARP against origin/main (63bcca4):

  • cargo test -p victory-shader --lib f16_rounding -- --test-threads=1: passed on RTX 5090 and WARP (f16_rounding_follows_a_constant_setreg_source).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

Cleanly resolves constant SGPR sources in round16_after via lower.konst_operand rather than matching only literal operands.

Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).

<!-- greyfinch-sync:Marvisak/VIctory:review:6070506300 --> Imported from [Marvisak/VIctory](https://github.com/Marvisak/VIctory/pull/2891#pullrequestreview-6070506300). Originally posted by [Dukestar69](https://github.com/Dukestar69) on 2026-11-26T08:12:29Z. **Review:** APPROVED --- Verified on RTX 5090 & WARP against `origin/main` (`63bcca4`): - `cargo test -p victory-shader --lib f16_rounding -- --test-threads=1`: passed on RTX 5090 and WARP (`f16_rounding_follows_a_constant_setreg_source`). - `cargo clippy -p victory-shader --all-targets -- -D warnings`: clean. - `cargo xtask policy`: 191 files ok. Cleanly resolves constant SGPR sources in `round16_after` via `lower.konst_operand` rather than matching only literal operands. Synthetic tests only (no game files used). AI-assisted (Gemini 4 Neon).
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/2891:upstream-pr/2891'
git switch 'upstream-pr/2891'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/2891'
git switch 'upstream-pr/2891'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/2891'
git switch 'upstream-pr/2891'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/2891'
git switch 'main'
git merge --squash 'upstream-pr/2891'
git switch 'main'
git merge --ff-only 'upstream-pr/2891'
git switch 'main'
git merge 'upstream-pr/2891'
git push origin 'main'
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
Marvisak/VIctory!2889
No description provided.