Skip to content

shader: lower wave prefixes of min and max - #470

Open
Loggerten wants to merge 9 commits into
Marvisak:mainfrom
Loggerten:fix/wave-prefix-minmax
Open

Loggerten wants to merge 9 commits into
Marvisak:mainfrom
Loggerten:fix/wave-prefix-minmax

Conversation

@Loggerten

Copy link
Copy Markdown

What

The wave prefix path lowered wavePrefixOp for sums only. Every other operation fell out at a wave prefix of an operation other than a sum, even though the active-reduction arm right below it already mapped min and max.

This widens the prefix arm to MinU/MaxU/MinI/MaxI/MinF/MaxF, with a (kind, sop) table that mirrors the active-reduction one. Exclusive prefixes pass through. Inclusive min/max fold the lane's own term back in with an ordering compare plus select, because wavePrefixOp excludes it.

Bitwise prefixes stay refused, with a corrected message: DXIL WaveOpKind has no bitwise member (Sum/Product/Min/Max), so wavePrefixOp cannot express them at all.

Signature verified against the DXIL spec: dx.op.wavePrefixOp.<i32|f32>(121, value, i8 kind, i8 sop).

Verified

  • New test emit::lower::tests::a_wave_prefix_of_min_lowers: a wave32 whole-wave section that leaves a per-lane inclusive MinU prefix, consumed by an ds_write_b32. Without the fix the store reads the missing value and the program fails with a wave prefix of an operation other than a sum (offset 160). With the fix it lowers.
  • cargo test -p victory-shader: 57 passed, 0 failed (56 baseline + 1).
  • cargo fmt --check -p victory-shader clean; cargo clippy -p victory-shader --all-targets no new warnings.
  • cargo xtask policy: 151 files ok.
  • Checked against open PR Shaders: follow lane writes and scalar steps in whole-wave sections #308 (lane writes and scalar steps in whole-wave sections): it changes emit/wave.rs tracking, not this refusal arm, so no overlap.

AI-assisted (Hermes Agent); I reviewed the diff and ran the tests and gates myself.

Wave prefixes now take the reduction's kind and signedness, as the
active one does: AddU/AddF keep kind 0, and MinU/MaxU/MinI/MaxI/MinF/
MaxF pass kinds 2 and 3 with the matching sop. Inclusive prefixes of a
min or max fold the lane's own term back in with the ordering compare
and a select rather than an add.

The new test leaves a min prefix in a value a store after the section
reads, so the value cannot stay missing: on main the store is refused
with "a wave prefix of an operation other than a sum".

@towiice towiice 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:

  • Hardware: NVIDIA GeForce GTX 1650 SUPER (Turing TU116, 4 GB VRAM, 32-wide subgroups / wave32, driver 610.88), Windows 11 x64 (26H2 / 10.0.26300), toolchain stable-x86_64-pc-windows-gnullvm (llvm-mingw 23.1.6).
  • Test command: cargo +stable-x86_64-pc-windows-gnullvm test -p victory-shader
  • Result: 70 passed, 0 failed (6.55 s).
    • Verified emit::lower::tests::a_wave_prefix_of_min_lowers passes cleanly and lowers the wave32 inclusive min prefix without the previous refusal panic.
      Clean diff, well-scoped mapping matching the DXIL WaveOpKind specification.

@DaviTostes

Copy link
Copy Markdown

Verified 6dd6d5f on Arch Linux x86_64 (rustc 1.98.1, cargo test -p victory-shader): 57 passed, 0 failed, 0 ignored.

Not covered: wave-prefix min/max on real hardware and title shaders (no game data).

@upipibi

upipibi commented Nov 24, 2026

Copy link
Copy Markdown

Two things on 6dd6d5f, from reading the code (not run):

  1. NaN in the inclusive fold (lower.rs:797-799). The float min/max fold uses select(r < arg, r, arg) (OLT / OGT), so when either side is NaN the compare is false and arg wins. The sequence this replaces is built from v_min_f32 / v_max_f32, which return the non-NaN operand. Example, inclusive min: lane 0 = 1.0, lane 1 = NaN → lane 1 should be 1.0, this gives NaN. If arg is the one that isn't NaN, r (NaN) is dropped the right way only by luck of the order. Using the same FMin / FMax the translator already emits for v_min_f32 / v_max_f32 would match.

  2. wavePrefixOp with kind 2 / 3 (Min / Max). DXIL documents WavePrefixOp only for Sum and Product (HLSL has no WavePrefixMin / WavePrefixMax), so drivers have never been handed this, and the validator or a driver may refuse it. a_wave_prefix_of_min_lowers only checks that the program lowers. Running that module through dxil.dll validation and on a GPU, as the valu_tests harness does, would show whether it's accepted. If it isn't, an exclusive prefix min/max can be built from WaveReadLaneAt / a log-step scan instead.

AI-assisted (Claude Code), read-only review.

@mercuryVM mercuryVM 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 6dd6d5f on Windows 11, RTX 5070, MSVC: cargo test --workspace --no-fail-fast passes (265 passed, 0 failed).

  • (kind, sop) matches the DXIL enums: Sum 0, Product 1, Min 2, Max 3, and signed 0, unsigned 1. Inclusive min and max fold the lane's own term back with the right compare for each type (ULT/UGT, SLT/SGT, OLT/OGT). For floats, select(olt(r, arg), r, arg) returns arg when r is NaN. That differs from fmin's NaN rule, but matches what a scan built from v_min_f32 sees only if the input is NaN-free. That's acceptable.
  • HLSL only has WavePrefixSum and WavePrefixProduct, so DXC never emits wavePrefixOp with Min or Max. I checked whether the validator accepts it anyway. I temporarily made the test's lowers_with run the emitted container through dxil.dll, and a_wave_prefix_of_min_lowers validated OK. Validation is not execution, though: drivers have never seen prefix Min/Max from DXC, so a GPU test (valu_tests::run_cases) comparing lanes on hardware and WARP would be the real proof.

AI-assisted (Claude Code).

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 24, 2026
…d max

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings the branch up to date with origin/main (ffe310e), 131 commits on.

Conflict resolution, all in crates/victory-shader/src/emit/lower.rs and
confined to the #[cfg(test)] module:
- kept main's single test helper `lowers(words, rsrc2)` and dropped the
  branch's `lowers`/`lowers_with` pair, so the helper takes rsrc2 directly;
- ported the branch's new test call from `lowers_with(&words, 1 << 15)` to
  `lowers(&words, 1 << 15)`;
- checked every `lowers(` call site for arity (all now pass rsrc2) and every
  `resource::track` call for main's fourth `Some(&st)` argument; the test
  helper already passed it after the merge.

No lowering logic was changed: the diff against origin/main is this PR's
wave-prefix min/max lowering plus its test.
@Halfect

Halfect commented Nov 24, 2026

Copy link
Copy Markdown

Independent validation of current head 95212d9 on Windows:

  • cargo test --locked -p victory-shader --lib -- --test-threads=1: 121 passed, 0 failed, 1 ignored.
  • I locally extended a_wave_prefix_of_min_lowers to pass the generated container through dxil.dll validation, create a compute pipeline on the default hardware D3D12 adapter, and dispatch one synthetic group. Validation, pipeline creation, and dispatch all succeeded.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings: passed.
  • cargo xtask policy: 154 files ok.

This confirms that the existing synthetic MinU module is accepted by the validator and this driver; it does not establish numerical correctness for all six min/max operations. In particular, the earlier floating-point NaN concern remains untested: the current inclusive float fold still uses OLT/OGT plus select, while this test has no result readback and does not exercise NaN inputs. A float NaN readback case is still needed to clear that part.

An inclusive wave-prefix min/max folded the lane's own term back in with a
compare and select (`OLT`/`OGT`). When that term is the NaN the compare is
false and the select returns the NaN, where the `v_min_f32`/`v_max_f32`
sequence being lowered keeps the non-NaN operand: inclusive min of lane 0 =
1.0 and lane 1 = NaN gave NaN, and must give 1.0.

Use `max_min`, the same `FMin`/`FMax` plus signed-zero fix the translator
already emits for those instructions. Integer min/max keep the compare and
select, where a false compare is the right answer.

`a_wave_prefix_of_min_lowers` also validates its module through `dxil.dll`
where one is loadable: no compiler emits `wavePrefixOp` kind 2/3, so nothing
else checks that the instruction is well formed.
@Loggerten

Copy link
Copy Markdown
Author

@upipibi Pushed a754127 (on top of the merge with main ffe310e).

1. NaN in the inclusive fold — fixed. You were right. The inclusive fold now calls max_min(false/true, r, arg), which is the same FMin/FMax plus signed-zero fix the translator already emits for v_min_f32/v_max_f32. Integer min/max keep the compare and select, where a false compare is the right answer. So inclusive min of lane 0 = 1.0 and lane 1 = NaN now yields 1.0, and the NaN is dropped whichever side it is on.

2. WavePrefixOp kind 2/3 — half answered, and it is the weaker half. Your reading of the producers is correct: LLVM's DXIL.td defines WaveOpKind_Min = 2 and WaveOpKind_Max = 3, but the only intrinsics on opcode 121 are sum, usum, product and uproduct. Nothing emits prefix min/max.

What I could settle from source: dxil.dll has no kind rule to reject them with. lib/DxilValidation/DxilValidation.cpp in the DXC tree (main) contains no WavePrefixOp handling at all; the kind operand is only type-checked as i8. So validation passing would tell us the module is well formed, not that anything supports the instruction.

The part I have not settled is the driver, and I have no hardware run here. I have not claimed it works.

I added a validation step to a_wave_prefix_of_min_lowers: it lowers the module again, and where dxil.dll is loadable it validates it, asserting success and the container hash. That runs on the Windows CI job and on any Windows tester's box, and would catch a malformed instruction even though it cannot catch an unsupported one.

If you think an undocumented kind should not ship at all, the fix is a WaveReadLaneAt scan instead of wavePrefixOp, which rewrites the lowering rather than correcting it. I would want a hardware result before choosing that.

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

Test-merge with current main (ffe310e) conflicts — needs a rebase. Main moved recently (PR #566 waterfall loops touched lower.rs/resource.rs/valu_tests.rs), so shader PRs opened before it need rebasing. I'll re-review once it merges cleanly.

nazmi pushed a commit to nazmi/VIctory that referenced this pull request Nov 24, 2026
Applied as the net diff of upstream PR Marvisak#470 (head a754127), 3 commits: a754127,95212d93,6dd6d5ff.
@rouges78

Copy link
Copy Markdown
Contributor

Hardware readback for a754127, from a synthetic test (no game data), on two GPUs.

Setup. One group of 32 threads, WaveSize(32) declared, dxil.dll validation on. Each lane loads its own dword from a structured buffer, a whole-wave section shaped like the one in a_wave_prefix_of_min_lowers scans it (sums use row_shr 1, 2, 4, 8), and each lane stores the result to a raw UAV over a sentinel. Compared lane by lane with the section's instructions run on the CPU. Inputs are non-monotonic, never zero and order differently as signed and unsigned; float sets add NaNs in lanes 9 and 20, and in lane 0. An exclusive form is built too: it stores the raw wavePrefixOp result, with no fold.

  • NVIDIA: RTX 4080, driver 621.14 (32.0.16.2114), waves of 32 to 32.
  • AMD: Radeon iGPU of a Ryzen 7 7700X, driver 32.0.21030.2001, waves of 32 to 64.
operation kind, sign RTX 4080 Radeon iGPU
sum u32 (control) 0, 1 exact exact
sum f32 (control) 0, 0 exact exact (see the note on NaN)
min, max u32 2 / 3, 1 exact wrong
min, max i32 2 / 3, 0 wrong: the unsigned result wrong
min, max f32 2 / 3, 0 pipeline refused wrong

Each cell holds for both the inclusive and the exclusive form.

  • RTX 4080, signed: the sign operand is ignored. The exclusive readback equals the unsigned prefix on all 32 lanes of both input sets, with 0xFFFFFFFF (min) and 0 (max) in lane 0.
  • RTX 4080, float: CreateComputePipelineState fails with E_OUTOFMEMORY on every run, and later tests in the same run pass. With kind 0 in place of 2 or 3 the same programs create and run.
  • Radeon: every pipeline is created, but kinds 2 and 3 return nothing usable for any type. The exclusive form stores 0 in all 32 lanes. The inclusive form gives values unrelated to the inputs that change from run to run.
  • Radeon, float sum: with a NaN input, that lane's own exclusive prefix reads NaN instead of the sum of the lanes below. That is the sum kind already on main, not this PR.
  • Not a driver matter: with a NaN in lane 0, the console keeps the NaN there for an inclusive float min/max (no scan step folds lane 0), while FMin/FMax(prefix, term) returns the prefix. Neither driver could show it.
  • WARP is skipped: it has no wave of 32 lanes.

So on these two drivers only the unsigned min/max on the RTX 4080 work as lowered. This is one GPU and one driver version per vendor, and the Radeon driver dates from September 2025.

Test: wave_prefix_tests.rs (16 tests on top of a754127, run on every hardware adapter; 13 fail on this machine).

AI-assisted (Claude Code), run on my own hardware.

`wavePrefixOp` kinds 2 and 3 are what HLSL uses for a min or max prefix,
and no driver implements them. Hardware readback on PR Marvisak#470 shows NVIDIA
returning the unsigned prefix for the signed kinds and failing
`CreateComputePipelineState` for the float ones, and AMD returning values
unrelated to the inputs.

Lower them as a Hillis-Steele scan of `waveReadLaneAt` steps instead,
with each step merged by the same operation the translator emits for
`v_min_u32` and friends, so a float fold keeps the non-NaN operand. An
exclusive prefix reads the inclusive one a lane down and puts the
identity in lane 0. A step that would read below lane 0 clamps to lane 0
and its result is discarded.

Sum prefixes keep `wavePrefixOp`, whose kind 0 both vendors implement.

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Needs work. Update after 265c736. Lower::wave_prefix_scan (crates/victory-shader/src/emit/lower.rs) now ballots the active lanes and uses the identity where the source lane is inactive. The test comment no longer names the PR, and the #[test] indentation is fixed. What is still open:

  1. The ballot guard does not make the scan correct under divergence. In a Hillis-Steele scan, the value read at step k from lane L-k stands for the whole window [L-2k+1, L-k]. Replacing an inactive source lane with the identity drops every active lane in that window, not just the inactive one. Example with 4 lanes, where lanes 0 and 3 are active: step 1 reads lane 2 (inactive), step 2 reads lane 1 (inactive), so lane 3 ends up with v3 instead of min(v0, v3). The exclusive step (reading lane L-1) has the same issue. If host lanes can be inactive in this section, the scan needs a form that only ever reads active lanes. One option is a fold over lanes j < L that are in the ballot (one WaveReadLaneAt per lane, 32 or 64 reads). The other is to show that the section always runs with a full host wave and drop the guard.
  2. Still no value check. a_wave_prefix_of_min_lowers only asserts that a scan was emitted and that the module validates. A readback (the valu_tests harness, or rouges78's tests linked above) for unsigned, signed and float, inclusive and exclusive, with a NaN case and a mask with holes, would show the scan computes what the console does.
  3. Small things. The comment on the min/max arm still says kinds 2 and 3 are "what HLSL uses for a min or max prefix" (HLSL has none) and still cites "PR shader: lower wave prefixes of min and max #470". Please reword it. The float-sum sop change from 1 to 0 is still unrelated to this PR and unexplained.

Points 1 and 2 would change the verdict.

CI: passing (check).


Generated by Claude Code

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026
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