Skip to content

Shaders: lower binary64 scaling with floating-point modes - #2743

Open
andrewpozdnakov7-jpg wants to merge 3 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/ldexp-f64
Open

andrewpozdnakov7-jpg wants to merge 3 commits into
Marvisak:mainfrom
andrewpozdnakov7-jpg:codex2/ldexp-f64

Conversation

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor

Summary

Lower RDNA 2 V_LDEXP_F64 by adjusting and rounding its binary64 significand and exponent in two 32-bit words. Both register pairs and signed 32-bit scale operands use their normal routes. The translation requires neither host doubles nor int64.

The existing MODE analysis now also tracks the FP16/FP64 denormal field. Scaling handles all four rounding and denormal modes when they are known at translation time. The shader entry uses nearest-even rounding and preserves binary64 subnormals, matching the documented AMDHSA defaults. RDNA 2 ISA · AMDGPU kernel descriptor defaults

Verification

  • On 13f91ca, each of the three synthetic shaders fails translation with Unsupported v_ldexp_f64.
  • With the change, the same tests verify both result words on app-local D3D12 WARP. Cases cover normals, subnormals, signed zeros, overflow, underflow, NaN/Inf, cross-word carry, and explicit S_ROUND_MODE, S_DENORM_MODE and S_SETREG writes.
  • All 33 project VALU tests pass. The existing FP16 mode test also passes on the base and patched builds. Strict shader Clippy and staged asset policy pass.

Coordination claim

Please retain the Git author if cherry-picking or rebasing this commit. If folding it into another commit, add Co-authored-by: andrewpozdnakov7-jpg andrewpozdnakov7-jpg@users.noreply.github.com.

AI-assisted. Verification used synthetic shaders.

@fuzeofficlal

Copy link
Copy Markdown
Contributor

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

Code review

  • Pure 32-bit integer lowering of v_ldexp_f64 (no DOUBLE / INT64 shader capability required).
  • scale_source_words applies abs then neg on the high word, rejects src1 modifiers and clamp/omod/sdwa/dpp, places Literal in the high word, sign-extends inline negative integers, promotes inline floats (with 248 = 0x3fc4_5f30_6dc9_c882), preserves ±0.0/±Inf, and quietens sNaN while retaining payload.
  • Clamping power to [-4096, 4096] avoids signed overflow with i32::MIN/i32::MAX. FpMode16 tracks both MODE[3:2] rounding and MODE[7:6] FP16/FP64 denorm modes cleanly and explicitly refuses unknown/conflicting modes at CFG joins; subnormal input flush vs. FirstbitHi normalization, cross-word FirstbitLo sticky rounding in round_scaled_subnormal, carry-out from max subnormal to min normal before output flush, and overflow rounding (±Inf vs. ±f64::MAX) all check out.

Verification (Windows 11, RTX 5090 + WARP + dxil.dll)

  • cargo test --locked -p victory-shader --lib --features host emit::tests: 17/17 passed; emit::lower::tests: 27/27 passed; emit::valu_tests (excluding slow exhaustive tests): 29/29 passed (all 3 ldexp_f64_* tests pass on both RTX 5090 and WARP).
  • VICTORY_WARP=1 cargo test --locked -p victory-shader --lib --features host ldexp: 3/3 passed.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 185 files ok.
  • Clean git merge-tree against 13f91ca, #2713, and #2728.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

Independent WARP/CPU verification for head 99161fd against origin/main 13f91ca. State check at start and again before posting: open, non-draft, mergeable, head unchanged; no prior oni-swr comment.

Environment: Windows 11 x64, Rust 1.98.1 (48a229cea 2026-09-01), cargo 1.98.1, Windows SDK dxil.dll 10.0.26100.0 (file version 1.8.2502.11). VICTORY_WARP=1 and VICTORY_D3D12_WARP=1 set (WARP only, no physical GPU); no game files. CARGO_TARGET_DIR=D:\victory-warp-2741-target. Tested commit: the PR head itself, which is exactly one commit on top of origin/main (merge-base = origin/main, so no temporary merge was needed). In victory-shader's host tests VICTORY_WARP=1 makes both the "hardware" and "warp" labels run on WARP (Host::new), so no physical GPU was used.

Commands/results:

  • Baseline (origin/main): cargo test --locked -p victory-shader — 267 passed, 3 failed, 3 ignored. Pre-existing failures on main: emit::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. cargo clippy --locked -p victory-shader --all-targets -- -D warnings — pass.
  • PR: cargo test --locked -p victory-shader --lib ldexp_f64 — 3/3 passed. cargo test --locked -p victory-shader — 270 passed (+3), the same 3 failed (identical names to baseline, so no new failure), 3 ignored. cargo clippy --locked -p victory-shader --all-targets -- -D warnings — pass.
  • Negative controls (tests kept, each change restored afterwards, tree clean): "v_ldexp_f64" arm removed -> all 3 new tests FAILED. Round-up direction inverted (1 => negative) -> ldexp_f64_uses_the_round_mode_for_underflow_and_overflow FAILED, the other two passed. Subnormal-result flush disabled (if false) -> ldexp_f64_uses_the_denormal_mode_for_both_operands_and_results FAILED, the other two passed. So each of the three tests pins a different part of the lowering.

Review: no correctness blocker found in the inspected paths. The MODE tracker now follows bits 3:2 and 7:6 independently, joins disagreeing paths to unknown, and v_ldexp_f64 refuses (Err) when either mode is unknown instead of guessing; the test also asserts the SFI0 doubles/int64 feature bits are clear, which is a good guard for the no-native-double goal. Subnormal rounding (sticky across the dword boundary, carry into the minimum normal, flush only if the result is still subnormal), overflow by round mode, NaN quieting and power clamping are all covered by cases. Scope notes: clamp, omod, SDWA, DPP and abs/neg on the exponent operand are rejected as unsupported (explicit errors, not silent); the lowering is only checked against hand-written expected bit patterns, not against a hardware reference, and the new files are not rustfmt-formatted (neither is most of main, so not counted against it).

Not tested: physical GPU, retail title/game behavior, release build (debug profile only).

AI disclosure: Claude Code (Claude Sonnet 5.5).

@kotekompotek

Copy link
Copy Markdown

The FpMode16 refactor is the right shape: INITIAL matches the shader-entry convention (nearest-even, preserve subnormals both ways), the s_setreg field overlap check (offset<=start && end>=start+2) handles partial-field writes correctly, and tracking round/denorm independently unblocks ldexp's denorm handling. ldexp_f64 normalizes subnormal inputs by scaling before exponent adjust - the standard approach. Heads up on merge order: #2782's s_getreg reads self.mode16 as Option; whichever lands second needs mode16.round.

Copy link
Copy Markdown
Owner

Automated triage for the maintainers. This adds a lowering for v_ldexp_f64 in crates/victory-shader/src/emit/lower/ldexp.rs using only 32-bit integer ops, and replaces round16 in emit/lower.rs with FpMode16, which tracks MODE bits 3:2 (round) and 7:6 (FP16/64 denorm) on their own. I checked the subnormal normalization (FirstbitHi counted from the MSB), the right shift into subnormal units with sticky bits across the dword boundary, the directed and nearest-even rounding, the flush rules for the RDNA2 denorm field (bit 0 input, bit 1 output), overflow to Inf or max-finite by round mode, NaN quieting, and the 64-bit literal and inline-constant handling. Things to note: the entry assumption that subnormals are preserved comes from the AMDHSA defaults, not from the title's own shader float mode, and #2782 reads mode16 as Option<u8>, so whichever lands second needs mode16.round. CI: check passing. This is a recommendation for a maintainer to look, not an approval.


Generated by Claude Code

@HAL9000RELOADED

Copy link
Copy Markdown

First Radeon run of 177f0e5: RX 7900 XTX (RDNA3), driver 32.0.32015.7029, Windows 10 22H2 (19045), Agility SDK 1.620 in the shader test binaries through #486. Base: 481d699 (main 842b0cb + #486).

  • victory-shader: 304 passed, 1 failed. The three new tests pass on the card: ldexp_f64_scales_normal_and_subnormal_values, ldexp_f64_uses_the_denormal_mode_for_both_operands_and_results and ldexp_f64_uses_the_round_mode_for_underflow_and_overflow.
  • The one failure is pops_wait_loop_exits_immediately_for_inactive_lanes, which wants an SM 6.6 WARP device (this Windows 10 WARP reports SM 6.2). It fails the same way on the base without this PR.

So the lowered binary64 scaling, denormal and round modes included, matches on RDNA3 hardware as well as on the RTX 5090 and WARP.

@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::ldexp: 3 passed, 0 failed (ldexp_f64_scales_normal_and_subnormal_values, ldexp_f64_uses_the_denormal_mode_for_both_operands_and_results, and ldexp_f64_uses_the_round_mode_for_underflow_and_overflow pass on both RTX 5090 hardware and WARP).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: clean.
  • cargo xtask policy: 192 files ok.

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

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

Looks good — verified locally at 177f0e52:

  • cargo xtask policy (192 files ok) and cargo clippy --all-targets --all-features -- -D warnings pass with 0 warnings.
  • emit::valu_tests::ldexp_f64 passes 3/3 on both RTX 5090 (VICTORY_WARP=0) and WARP (VICTORY_WARP=1), verifying SFI0 & 0x8001 == 0, all 4 rounding modes, and all 4 denormal modes.
  • Core binary64 exponent scaling, subnormal normalization, overflow/underflow saturation, and rounding arithmetic matched an exact integer reference across 3.19M test cases.

(Non-blocking notes):

  1. Pre-existing from round16_after, the whole-wave section loop (lower.rs around for j in i + 1..end { self.section_inst(b, j, i); }) jumps i = end; continue; without advancing self.mode16 = self.mode16.after(&p.blocks[b].insts[j]). Advancing self.mode16 inside that loop keeps intra-block mode16 tracking consistent with fp_modes16 if a s_round_mode / s_denorm_mode appears inside a section.
  2. rustfmt --edition 2024 --check flags minor line-wrapping diffs in crates/victory-shader/src/emit/lower/ldexp.rs and the added lines in lower.rs / valu_tests.rs.

@andrewpozdnakov7-jpg

Copy link
Copy Markdown
Contributor Author

460bea6 tracks the input and output denormal bits independently and processes MODE writes through whole-wave sections.

The new synthetic test exercises both directions. Clearing MODE bit 7 alone preserves a subnormal input that scales into the normal range, and flushes a subnormal result. Clearing bit 6 inside a whole-wave section flushes a subnormal input and still preserves a subnormal result. Both negative cases check the sign bit. The first partial write failed to lower on 177f0e5 with unknown floating-point modes. All four ldexp_f64 tests passed on the updated head with WARP 1.0.22, as did strict all-features shader Clippy and asset policy.

I also tested #2743, #2791 and #2782 together on current main. The integration uses the same separate bits for ldexp and sqrt, and reads the rounding field for s_getreg_b32. All 46 VALU tests passed on WARP, plus strict shader Clippy and asset policy. The branch keeps the three published PR heads.

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.

7 participants