Skip to content

Shaders: whole-wave sections follow binary64 sums (WaveActiveSum of a double) - #2978

Open
Anzymanz wants to merge 5 commits into
Marvisak:mainfrom
Anzymanz:wave-f64-sums
Open

Anzymanz wants to merge 5 commits into
Marvisak:mainfrom
Anzymanz:wave-f64-sums

Conversation

@Anzymanz

Copy link
Copy Markdown

AI disclosure: written with Claude Code (Claude Opus 5.5) at the request of the account owner, who reviews what goes out. The measurements below were run on our machine; no game data is included (hashes, counts and error text only).

Stacked on #2348 (v_cvt_f32_f64): its three commits come first; the last commit is this change.

Problem

WaveActiveSum of a double expands, on the console, into a whole-wave section that works on dword pairs: v_cndmask puts each lane's pair (or +0.0) in two VGPRs, each DPP step moves the two dwords with two v_mov_b32, v_add_f64 adds the pairs, v_permlanex16 joins the rows, v_readlane reads the result (on wave64, the two halves' results, added once more), and an s_mov_b32 copies each dword to the register that leaves the section. The section simulator (emit/wave.rs) follows 32-bit operations only, so both dwords are unknown, and a program that reads them is refused:

a whole-wave section step the simulation does not follow is not supported

That's what five programs in this title hit once #2348 lowers the v_cvt_f32_f64 that reads the sum (before #2348 they stop at that instruction).

Change

  • emit/wave.rs: Sym::Red64 { lo, hi, mask, lanes, high } is one dword of the binary64 sum over lanes of mask ? (lo, hi) : +0.0. v_add_f64 reads both of its sources as (low, high) pairs: register pairs, SGPR pairs, inline 0. It joins two terms of one masked sum when their lanes are disjoint, and writes both destination dwords. v_mov_b32 with DPP, v_permlanex16_b32 and v_readlane_b32 move these symbols a dword at a time already. s_mov_b32 now keeps what its source holds, where it used to be unknown. A sum over every lane is Shape::Sum64.
  • emit/lower/sum64.rs (new, keeping lower.rs under the 512 KiB policy): Shape::Sum64 becomes one waveActiveOp.f64 (sum) over mask ? double(lo, hi) : 0.0. Both dwords share it, one per block. It sets FLOAT64, INT64 and WAVE_OPS.
  • Shortcut (noted in the code): the host adds in its own order, the console in its tree of DPP steps, so terms that aren't exact in binary64 may round differently. Sums of small integers are exact either way.

Tests

  • wave::tests::a_binary64_sum_is_followed_through_its_dwords: the compiler's wave32 expansion (four DPP steps, v_permlanex16, v_readlane, s_mov_b32 copies) gives Sum64 for both read dwords and both copies.
  • wave::tests::a_binary64_sum_counting_a_lane_twice_is_not_a_sum: the same, with a lane added to itself first, gives no shape.
  • valu_tests::a_whole_wave_binary64_sum_reaches_the_host (GPU): the same section run end to end on the host, then v_cvt_f32_f64 of the copied pair; five values, x as f32 expected. Passes on an RX 9070 XT (RADV through vkd3d-proton, GE-Proton 11-14, DXC's dxil.dll).

Measured over the title's shader pack

All 25,889 distinct containers in shaders.rpf went through emit::translate_with offline, on main 63bcca4 + #2348 and then with this commit added:

main + #2348 + this
translated / refused 25,669 / 220 25,670 / 219
programs whose output changes 1 (5dc3b25986622949, refused before)
refused on "a whole-wave section step the simulation does not follow" 5 0

Every other program's output is byte-identical: the s_mov_b32 change gives nothing else a new shape. The new program's DXIL converts with dxil-spirv (master). The other four (6f225b641c5b0708, 6871d6e4b3ac9a10, 926c46bab3b86bd6, a10179ab7eda0dc3) get past the section and stop later at a lane-mask join of an untracked mask, a separate class.

Windows test binary under Proton on the RX 9070 XT: 306 passed and 2 failed. The 2 failures (hosts_that_do_not_fuse_are_named, stub_pipeline_is_created) fail on main in the same environment too. cargo clippy -p victory-shader -- -D warnings is clean, cargo xtask policy is ok, and lower.rs is 523,410 bytes.

Not run: the differential tests (no console compiler here) and NVIDIA.


🤖 Generated with Claude Code

Calvix2k and others added 4 commits November 26, 2026 01:48
The console's 64-bit to 32-bit conversion: the VGPR pair is bitcast to a
`double` and truncated with the host's `fptrunc`, round to nearest even,
which is MODE's default. The toward-zero 64-bit mode has no DXIL form and is
refused by name rather than mis-rounded.

The IR had no 64-bit float type: it gains `double` (bitcode type 4) and the
`fptrunc` cast (LLVM opcode 7). A module that uses it declares the doubles
feature — SFI0 0x1 and the module's feature-info bit 2 — which dxil.dll
checks against the module.

Ten cases run on the card, checked against Rust's `as f32`: 1.0, -2.5, 0.1,
a tie that rounds to even, the two smallest subnormals (0), f64 max (inf),
±inf and a NaN (payload kept). WARP tops out at SM 6.2 here, so the hardware
device is the one exercised; the DXIL is validated by dxil.dll.

(cherry picked from commit 0439ea5)
…nverts

Two findings from the review (tyr0nsur):

- A literal source of `v_cvt_f32_f64` is the **high** word of the binary64
  (RDNA 2 ISA 6.2.1), the low word zero: 0xC0020000 is -2.25. `src64` reads a
  literal as the sign-extended integer its bits are, so the value became
  garbage there. The literal is handled in the lowering now, and
  `a_64_bit_literal_source_is_the_high_word` runs it on the card.
- The round-mode check admitted every mode but toward-zero: the directed
  modes (1, 2) and a mode written at run time (`None`) have no DXIL form, and
  `fptrunc` rounds nearest-even regardless. Only MODE's default (0) converts
  now; anything else is refused by name.

Lib suite 249 passed, 0 failed, 3 ignored; clippy `-D warnings` clean.

(cherry picked from commit 1ab5616)
Review feedback on Marvisak#2348: `v_cvt_f32_f64` lowers an inline float constant
(operand codes 240-248) through `src64`'s fallback, which zero-extends the
**f32** bit pattern — `1.0` became 0x000000003F800000, a subnormal that
converts to 0, where the hardware uses the binary64 of the value (RDNA 2 ISA
6.2.1).

The instruction now maps 240-248 to the same values as binary64 (including
1/(2π)) and builds the bits with an integer constant; register pairs and
literals keep their paths. `a_64_bit_inline_constant_source_is_the_same_value`
converts code 242 (1.0) and expects 1.0; before this it got 0.

(cherry picked from commit f956a66)
… double)

The console compiler expands a wave sum of doubles into a whole-wave section
on dword pairs: v_cndmask per dword, two v_mov_b32 per DPP step, v_add_f64 on
the pairs, v_permlanex16 per dword, v_readlane, and s_mov_b32 copies out. The
simulator followed 32-bit steps only, so both dwords were unknown and a
program reading them was refused ("a whole-wave section step the simulation
does not follow").

- wave.rs: Sym::Red64 is one dword of the binary64 sum over some lanes of
  mask ? (lo, hi) : +0.0; v_add_f64 joins two terms of one masked sum over
  disjoint lanes and writes both destination dwords; s_mov_b32 keeps what
  its source holds; a sum over every lane is Shape::Sum64.
- lower/sum64.rs: Shape::Sum64 is one waveActiveOp.f64 per block, shared by
  both dwords (FLOAT64, INT64, WAVE_OPS).

Tests: two simulator tests (the wave32 expansion is followed through its
dwords and copies; a lane counted twice is not a sum) and a GPU test running
the section end to end into v_cvt_f32_f64.

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

oni-swr commented Nov 26, 2026

Copy link
Copy Markdown

AI disclosure: tested and written by Claude Code (Claude Opus 5.5), coordinated by Codex, for the claim in #1633. Synthetic programs and data only, no game files.

Independent Windows WARP check of head 7de694fe56020fc3d32f3ef945a829485455b5b3. The control was its exact stack parent 9ead47e28fddc16979529f55588e71b2ef362eec (main 63bcca4 + the three #2348 commits, as stacked here).

Result: the simulator and lowering part holds up. The GPU test a_whole_wave_binary64_sum_reaches_the_host fails on WARP, both here and in this PR's CI at this same head.

Environment

  • Windows 11 Pro 10.0.26200, rustc/cargo 1.98.1, cargo test -p victory-shader --locked -j 4, --test-threads=1 --nocapture
  • VICTORY_WARP=1, VICTORY_D3D12_WARP=1, VICTORY_DXIL = Windows SDK 10.0.26100.0 x64\dxil.dll (1.8.2502.11)
  • WARP: d3d10warp.dll / D3D12Core.dll 10.0.26100.9818. Both test labels ("hardware" and "warp") resolved to Microsoft Basic Render Driver (vendor 0x1414, device 0x008c, software flag set, WaveLaneCountMin..Max = 4..4). I printed this from inside the test process.
  • The physical GPU wasn't used. In every pass, sampling the test process's modules showed d3d10warp.dll and the SDK dxil.dll loaded, and no vendor user-mode driver.

Why the GPU test fails

hardware: compute_pipeline failed: The parameter is incorrect.

With the D3D12 debug layer on, WARP gives the reason:

ID3D12Device::CreateComputeShader: Shader wants wave lane count of 32 but maximum wave lane count for device is 4.

The lowered section asks for WaveSize(32), and this WARP only offers 4-lane waves. CI run 45739979316 at this head fails on the same test with the same message. run_cases panics when compute_pipeline fails. synthetic_tests instead skips a WARP pipeline with a fixed wave size ("no pipeline for a wave of 32; skipped"). So nothing in this environment checks the end-to-end binary64 result. I make no claim either way about whether the value is correct on the host.

Focused (PR tests plus my synthetic cases)

parent 9ead47e head 7de694f
wave::tests::a_binary64_sum_is_followed_through_its_dwords absent ok
wave::tests::a_binary64_sum_counting_a_lane_twice_is_not_a_sum absent ok
valu_tests::a_whole_wave_binary64_sum_reaches_the_host absent FAILED (WaveSize 32 refused by WARP)
synthetic sections lower and pass dxil.dll validation (3 forms) refused: a whole-wave section step the simulation does not follow ok (full EXEC, v_cmpx guard, s_and_saveexec branch)
same 3 forms run over 128 lanes (4 waves of 32) refused at lowering not runnable: pipeline refused as above

My synthetic cases use the same wave32 expansion as the PR's test: s_or_saveexec_b32 -1, v_cndmask per dword, four DPP steps, v_permlanex16, v_add_f64, v_readlane lane 31, then s_mov_b32 copies. The section is wrapped three ways: full EXEC, a straight-line v_cmpx_ne_u32 guard, and a v_cmp + s_and_saveexec_b32 + s_cbranch_execz branch. Inputs are dyadic, so every partial sum is exact in any order: 1 + l·2⁻³² (low dword varies), 2^l (high dword varies), mixed-sign multiples of 0.25, and negative values plus a −0.0. All three forms lower at the head and pass dxil.dll validation. The parent refuses them all with the message this PR addresses. These cases were local only and aren't pushed.

Matched suite (victory-shader lib, same environment)

  • Parent 9ead47e: 301 passed, 4 failed, 3 ignored (not counting my local tests).
  • Head 7de694f: 303 passed, 5 failed, 3 ignored. The two extra passes are the PR's two simulator tests. The extra failure is a_whole_wave_binary64_sum_reaches_the_host.
  • The same 4 failures appear at both commits and at main 63bcca4: synthetic_tests::{a_mask_joined_with_data_at_a_uniform_loop_is_its_number, gathers_at_a_level_match_the_hardware, permutes_mask_high_address_bits_and_divergent_branches, permutes_read_the_lane_named_and_zero_from_lanes_left_out}. They come from VICTORY_WARP=1 sending the "hardware" label to WARP, so they're environmental and not from this stack or this PR.
  • Skips: 44 skip messages at every commit, mostly 32-lane hardware cross-lane tests and console-compiler differential tests. None of them is counted above.

Limitations

  • No wave64 form: the semantics harness is wave32.
  • No physical GPU, so no host result for the sum. The only GPU-side evidence here is the pipeline refusal.
  • Rounding order: the PR notes that inputs which aren't exact may round differently on the host. I didn't test that, since all my inputs are exact by design.

Suggestion: skip on WARP the way synthetic_tests does (or require a 32-lane adapter in a_whole_wave_binary64_sum_reaches_the_host). That would let Windows CI pass, but WARP still wouldn't check the result.

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

Verified: correct LLVM bitcode encodings (FpTrunc=7, TYPE_DOUBLE record 4, FeatureFlag doubles=0x1), inline-constant table matches GCN's f64 set including 1/(2pi), literal sources read as high-word-only per RDNA rules, and only MODE's default round-to-nearest is accepted (directed modes refused rather than mis-rounded). Sum64 is right too: masked lanes contribute +0.0 and one waveActiveOp.f64 serves both dwords via the per-block cache.

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

Reviewed and tested against main (63bcca43) on Windows 11 (RTX 5090 + WARP + dxil.dll, VICTORY_D3D12_DEBUG=1). There is a blocking test failure on WARP plus a source-modifier bug in v_cvt_f32_f64:

  1. Blocking WARP / CI failure (emit::valu_tests::a_whole_wave_binary64_sum_reaches_the_host):

    • The new test passes dxil.dll and RTX 5090, but fails on WARP at valu_tests.rs:209 with E_INVALIDARG (0x80070057).
    • D3D12 Debug Layer ID3D12InfoQueue reports:
      ID3D12Device::CreateComputeShader: Shader wants wave lane count of 32 but maximum wave lane count for device is 4.
    • WARP supports dx.op.waveActiveOp.f64 itself, so the refusal comes from whole-wave sections emitting WaveSize=32 (lower.rs:608) while valu_tests::run_cases panics unconditionally on WARP (unlike synthetic_tests.rs:345-349, which skips WaveSize=32 pipelines on WARP).
  2. Silent source-modifier drop in v_cvt_f32_f64 (crates/victory-shader/src/emit/lower.rs:6405-6427):

    • The Operand::Const(240..=248) and Operand::Literal(_) arms bypass self.src64(...) and do not check v.abs, v.neg, v.sdwa, or v.dpp. A VOP3-encoded v_cvt_f32_f64_e64 v0, -1.0 (inline constant with neg/abs) silently drops the modifier instead of applying or rejecting it.
  3. Two non-blocking notes:

    • a_whole_wave_binary64_sum_reaches_the_host currently runs threads: [1, 1, 1] (1 active lane); testing multi-lane reduction with inactive lanes (+0.0) and NaN/Inf/-0.0 would exercise WaveActiveSum much more thoroughly.
    • On 63bcca43, emit/lower.rs grows to 523,410 B (only 878 B below the 512 KiB xtask policy limit — merging alongside #2977 or #2104 will exceed 524,288 B).

…a 32-lane sum counts the active lanes only

The section declares the console's wave of 32, which WARP (4-lane waves)
refuses: run_cases_wave32 skips WARP there, as synthetic_tests does. A
second test runs the section over 32 lanes with only lanes below s2 active;
lane l adds 2^l, so the sum is 2^s2 - 1 in any order and the inactive
lanes' +0.0 must not count.

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

Copy link
Copy Markdown
Author

Thanks both, and thanks for chasing the WARP message down. ab08acb:

  • WARP. The section declares the console's wave of 32, so on WARP's 4-lane waves the pipeline is refused. The test now goes through run_cases_wave32, which skips WARP when it refuses the pipeline, as synthetic_tests does. Any other failure still panics, and run_cases behaves as before for every other test.
  • More than one lane (@fuzeofficlal's note). New a_whole_wave_binary64_sum_counts_the_active_lanes_only runs the section over a wave of 32 with only the lanes below s2 active (v_cmp_gt_u32 + s_and_saveexec before it). Lane l adds 2^l, so the sum is 2^n − 1 in any order, and the 32 − n inactive lanes' +0.0 must not count. Cases n = 1, 5, 17, 31, 32; with n = 32 the f32 result rounds, which is checked as well. Both GPU tests pass on an RX 9070 XT (RADV via vkd3d-proton). I haven't added NaN/Inf/−0.0 cases. Those don't depend on the order, but a NaN payload's propagation through the host's sum isn't something the console fixes either, so I'd rather not assert it.
  • v_cvt_f32_f64 source modifiers. That code is Shaders: lower v_cvt_f32_f64 #2348's (the commits this PR is stacked on), so I'll point its author at your finding there instead of changing it under their PR.
  • lower.rs size. Yes, 878 B of room on main. This PR only adds the Shape::Sum64 arm and a cache field there; the lowering is in emit/lower/sum64.rs. If something else lands first and it no longer fits, I'll move more out.

AI-assisted (Claude Code).

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Measured offline on the title's shaders (the method in #1247), head 7de694f on main 63bcca4 (a clean merge), RX 7900 XTX:

Build vs main the 5 v_cvt_f32_f64 programs
#2978 +1, 0 lost 1 validates, 4 stop at emit: a lane-mask join of an untracked mask
#2978 + #385 + #1065 +766, 0 lost (25,616 validate) all 5 validate

So this does what you said: with #385 and #1065 on top, all five programs translate and validate. I didn't measure #2978 + #385 without #1065. On #385 alone, the 13 join programs it unblocks on main then hit the validator's stage error that #1065 fixes, so the four likely need it too. The other 761 of the +766 are #385 + #1065's own (+761 together on 63bcca4).

Two things for the merge order:

Offline counts and unit tests only; no program names or data.

@zed6000

zed6000 commented Nov 26, 2026

Copy link
Copy Markdown

Crossed with your ab08acb. On it, the 4 binary64 tests pass on the RX 7900 XTX hardware, and the built-in WARP (10.0.26100.9818) is skipped as intended ("no pipeline for a wave of 32"). So the WARP point in my note is handled. The census numbers above don't change, since ab08acb only touches tests.

@steven-seagull27

Copy link
Copy Markdown

Both result dwords at ab08acb matched an exact integer reference on an RTX 5080 and app-local WARP 1.0.22. The sparse mask 0x80010041 (lanes 0, 6, 16, 31) gave 0x40100000_00000d60 for one input. Converting that result to f32 gives exactly 4.0, so a zeroed low dword would pass the existing check.

I ran 36 wave32 dispatches per adapter with positive and negative inputs and nine masks, including empty, full, alternating and lane 31 alone. Each active lane contributed ±(1 + (seed + 256 × lane) × 2⁻⁵²). The inputs are multiples of 32 units of 2⁻⁵², so every intermediate sum is exactly representable. Each dispatch read back two pairs per lane. The first pair kept both sentinel words in masked-off lanes; after restoring EXEC, all 32 lanes copied out the expected scalar pair. The empty mask gave +0. All 1,152 lane rows matched on each adapter.

Adapter Reported wave range Wave32 result
NVIDIA GeForce RTX 5080 32–32 Passed
App-local Microsoft WARP 1.0.22.0.20261027.1 4–128 Passed
Windows inbox WARP 10.0.26100.10068 4–4 Skipped before dispatch

I verified the loaded WARP DLL paths and validated the shader with Windows SDK dxil.dll (1.9.2602.17). These finite, exact cases leave the existing caveats about floating-point reassociation and NaN payload selection open. Test-only cherry-pick on this head

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. The core of the change reads well: Sym::Red64 in emit/wave.rs follows a binary64 wave sum through the console compiler's dword-pair expansion, and emit/lower/sum64.rs lowers Shape::Sum64 to one waveActiveOp.f64 (sum) over mask ? double(lo, hi) : 0.0, cached per block so both dwords share it. The FLOAT64 flag (bit 2) and the 0x1 feature bit match DXIL's double-precision flags, and the ordering difference is marked as a // shortcut:. Independent runs in the thread check both result dwords on an RTX 5080 and app-local WARP.

What is in the way:

  • CI is failing on ab08acb. Please get the check job green, or say what fails if it is outside this PR.
  • It carries Shaders: lower v_cvt_f32_f64 #2348's three commits (v_cvt_f32_f64). Those should be reviewed and land in Shaders: lower v_cvt_f32_f64 #2348 first, then this PR rebased to its own commit. One point there: in the v_cvt_f32_f64 arm of lower.rs the literal and inline-constant paths skip src64's modifier check, so abs/neg on those forms are silently ignored; they should be applied or refused.
  • A commenter reports conflicts with Shaders: track every incoming edge of a lane-mask phi #385 (mod sum64; next to mod phi;, and tests at the end of valu_tests.rs); whichever lands second will need a rebase.

CI: failing.


Generated by Claude Code

@Venom2054

Copy link
Copy Markdown

Follow-up on the rounding and guest Wave64 cases discussed above. On an RTX 5070 Ti we tested a separate implementation built on #439 (e2d3cfd) and the FP64 conversion from #416. It recognizes the guest addition tree and preserves operand order across two physical Wave32 waves, including when one half leaves a masked loop first.

  • A synthetic GPU readback matched an independent CPU implementation for all 122,688 output words. Cases included only the upper half active, only the lower half active and lane 63 alone. All 45,952 guest shared-memory sentinel words were preserved.
  • In that local stack, 5dc3b25986622949 and 6f225b641c5b0708 both produced SDK-validated Wave32 DXIL and D3D12 compute pipelines. In a matched title capture the control dropped both targeted commands. The candidate recorded two indirect dispatch commands for each shader. The capture did not include the indirect group counts or a visible water surface.

This was a separate implementation on a different base from ab08acb. Would the synthetic masked-loop regression or the ordered lowering be useful as a follow-up here?

Implementation and testing were assisted by Codex.

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.

9 participants