Skip to content

Shaders: read known FP16/FP64 rounding mode bits - #2782

Open
andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/read-mode-round16
Open

andrewpozdnakov7-jpg wants to merge 2 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/read-mode-round16

Conversation

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

A shader that reads the FP16/FP64 rounding field through S_GETREG_B32 stops translation on main. The lowering now extracts the requested bits from the same statically known MODE value used by FP16 arithmetic. It accepts a read of both rounding bits or either bit individually. AMD specifies that hardware-register reads place the result in the low bits of the destination. RDNA 2 ISA reference

The same synthetic readback on 13f91ca stops with unsupported s_getreg_b32. With this commit, all 12 cases return the expected values on app-local WARP SM 6.6 with the DXIL validator. The test sets all four rounding modes, reads the full field and its two one-bit slices, and gives FP32 a different rounding value.

All 32 VALU tests in the app-local run passed, including the runtime identity check. The existing FP16 rounding test passed before and after. Strict shader Clippy and staged asset policy passed. Coordination claim

Please retain the Git author on cherry-pick or rebase. If you fold this change into another commit, please include Co-authored-by: andrewpozdnakov7-jpg <andrewpozdnakov7-jpg@users.noreply.github.com>.

@kotekompotek

Copy link
Copy Markdown

Selector decode matches the hwreg encoding (regid[5:0] | offset[10:6] | (size-1)[15:11]), MODE=1, and the [2,4) field bound correctly restricts reads to the tracked FP16/FP64 rounding bits while refusing unmodeled fields. The test's cross-check (FP32 set to mode^3) would catch reading the wrong field.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification of head c89141382fdec3e7051650dfee5b4af78569c0e1 merged cleanly onto origin/main 13f91caee25f2fdf2c062be5f7a27f058e51cbca.

Environment: Windows 11 x64, WARP/CPU only (VICTORY_WARP=1, VICTORY_D3D12_WARP=1), no physical GPU, no game files; Windows SDK 10.0.26100.0 x64 dxil.dll 1.8.2502.11.

Tests (cargo test --no-fail-fast): victory-shader: 268 passed, 3 failed, 3 ignored (baseline 267 passed, 3 failed, 3 ignored; +1 new test, which executes on the WARP device for all four rounding modes and three field slices). victory-shader baseline on origin/main: 267 passed, 3 failed, 3 ignored; the 3 failures (synthetic_tests::a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_read_the_lane_named_and_zero_from_lanes_left_out) fail identically on origin/main under WARP, so they are not caused by this PR.

Negative control: disabling the s_getreg_b32 branch makes read_fp_round_mode_field_after_each_setting fail (lowering error); restored afterwards.

Clippy: cargo clippy -p <affected crates> --all-targets -- -D warnings clean (exit 0).
fmt: cargo fmt --check is not clean on origin/main itself (891 diff hunks across many files); this PR: 893 hunks (+2 vs main, in the touched files).

Review: Selector decode (id[5:0], offset[10:6], size-1[15:11]) is correct; only MODE[3:2] is modelled and any other register/field is rejected, and an unknown mode16 is rejected rather than guessed. Diff contains no game files, dumps, shader names or retail hashes.

AI disclosure: this verification was done by Claude Code (Claude Sonnet 5.5).

@fuzeofficlal

Copy link
Copy Markdown
Contributor

Reviewed and verified c8914138 on Windows 11 (RTX 5090 + WARP + Windows SDK 10.0.26100.0 dxil.dll) against main (13f91ca).

  • Decoding s_getreg_b32 (register, offset, width) and strictly accepting only register == 1 (MODE), offset >= 2, offset + width <= 4 while rejecting unknown/unmodeled fields and unknown dynamic/joined modes is clean and minimal.
  • (Merge-order note): textually clean git merge-tree against 13f91ca, #2743, and #2791, but since #2743 and #2791 change Lower::mode16 from Option<u8> to FpMode16 { round, denorm }, self.mode16.ok_or(...) (lower.rs:3384) will need to become self.mode16.round.ok_or(...) if merged after either of them.
  • cargo test --locked -p victory-shader --features host --lib -- valu_tests::: 31 passed, 0 failed (including read_fp_round_mode_field_after_each_setting across all 4 rounding modes × 3 bit slices on both RTX 5090 and WARP).
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 184 files ok.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

This lowers s_getreg_b32 in emit/lower.rs for reads of MODE[3:2], the FP16/FP64 rounding field, from the statically tracked mode16. It refuses any other register or field, and also refuses when the mode is unknown. I checked the selector decode (id[5:0], offset[10:6], size-1[15:11], MODE = 1) against the public RDNA2 ISA and against round16_after, which decodes s_setreg the same way. The new test sets the FP32 field to a different value so that a read of the wrong field fails. One note: the value read is only as good as mode16's starting value (Some(0), which the FP16 arithmetic lowering already assumes). If #2743 or #2791 lands first, self.mode16.ok_or(...) needs to follow their FpMode16 change. CI passes.

This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

@Dukestar69 Dukestar69 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested merged onto origin/main (63bcca4) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP, dxil.dll validator active):

  • cargo test -p victory-shader --lib emit::valu_tests::read_fp_round_mode_field_after_each_setting: 1 passed, 0 failed (RTX 5090 hardware + WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 191 files ok.

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

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…d merged tests with the branch

Marvisak#2811 on this branch turned `Lower::mode16` into `FpMode16 { round,
denorm }` and moved the MODE update into `FpMode16::after`. Three
refreshed PRs still used the old `Option<u8>` and the removed
`round16_after`, so victory-shader did not build:

- Marvisak#2782 reads MODE[3:2] for `s_getreg_b32`: it now reads `mode16.round`.
- Marvisak#2886 updates the mode inside whole-wave sections: it now calls
  `mode16.after`, as the main lowering loop does.
- Marvisak#2917 rounds toward zero when FP_ROUND_16_64 is 3: it now checks
  `mode16.round`.

Tests from Marvisak#2152, Marvisak#2817, Marvisak#2952, Marvisak#3038 and a `mod.rs` test used the older
`lower::Parts`, `HostShader`, `Translated` and `emit::emit` signatures;
they get the branch's fields (`Target::ANY`, no console flow, no native
f16, `written_root_uavs`, `threads`, `atomics64`) as the other tests do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@fuzeofficlal fuzeofficlal left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s_getreg_b32 selector decoding (MODE[3:2] offsets (2, 1), (3, 1), (2, 2)) and bit extraction match RDNA2, and read_fp_round_mode_field_after_each_setting passes on both RTX 5090 and WARP (clippy and xtask policy also pass).

Two functional issues need fixing before merge (plus a minor formatting nit):

  1. Whole-wave sections skip mode16 updates (crates/victory-shader/src/emit/lower.rs:1367-1375)

    • When self.sections emits j in i + 1..end via section_inst and jumps i = end, round16_after is not applied to instructions inside the section. A s_round_mode inside a whole-wave section (e.g., s_or_saveexec_b32; s_round_mode 0xc; restore EXEC; s_getreg_b32) leaves self.mode16 stale, so s_getreg_b32 silently reads 0 instead of 3. Updating self.mode16 across i + 1..end avoids stale reads.
  2. Destination guard misses EXEC_HI (lower.rs:3566, :3581)

    • inst.write(Reg::S(EXEC)).is_some() || self.is_mask(lo) only checks EXEC_LO (EXEC) and st.masks, missing EXEC_HI / st.highs in Wave64, and writing a numeric constant into VCC can turn a downstream s_cbranch_vccnz into a per-lane bit test rather than a wave-wide nonzero check.

Minor formatting nit: rustfmt --edition 2024 --check flags crates/victory-shader/src/emit/lower.rs:3586 and crates/victory-shader/src/emit/valu_tests.rs:252.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor Author

Thanks @fuzeofficlal. 7102a71 advances the rounding mode through whole-wave sections. The added shader reads MODE[3:2] after s_round_mode 0xc inside one of those sections. It returned 0 on c891413 and now returns 3 on WARP.

The lowering also rejects s_getreg_b32 directly into either half of EXEC or VCC before simulating a section, and checks high halves of tracked lane masks. The tests cover all four architectural destinations, including the high halves under wave64, and the existing MODE field selections.

All three focused tests, strict all-features shader Clippy, and the asset policy check passed locally. AI-assisted with Codex. Synthetic shaders only.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants