shader: lower wave prefixes of min and max #470

Open
github-bot wants to merge 9 commits from github-bot/gh-470 into main AGit
Owner

@⁠Loggerten on GitHub · 2026-11-24 03:47 UTC · original
Loggerten:fix/wave-prefix-minmax → main · +223 −3 · 2 files · 9 commits · open

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

**@⁠Loggerten** on GitHub · 2026-11-24 03:47 UTC · [original](https://github.com/Marvisak/VIctory/pull/470) `Loggerten:fix/wave-prefix-minmax` → `main` · +223 −3 · 2 files · 9 commits · open ## 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 #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".
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.
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.
`wavePrefixOp` kinds 2 and 3 are what HLSL uses for a min or max prefix,
and no driver implements them. Hardware readback on PR #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.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:05:09 +00:00
Author
Owner

@⁠DaviTostes on GitHub · 2026-11-24 03:51 UTC · original

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

**@⁠DaviTostes** on GitHub · 2026-11-24 03:51 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7121480263) 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).
Author
Owner

@⁠upipibi on GitHub · 2026-11-24 05:30 UTC · original

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.

**@⁠upipibi** on GitHub · 2026-11-24 05:30 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7123017397) 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.
Author
Owner

@⁠Halfect on GitHub · 2026-11-24 11:52 UTC · original

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.

**@⁠Halfect** on GitHub · 2026-11-24 11:52 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7130667678) 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.
Author
Owner

@⁠Loggerten on GitHub · 2026-11-24 11:57 UTC · original

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

**@⁠Loggerten** on GitHub · 2026-11-24 11:57 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7130770086) @⁠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.
Author
Owner

@⁠rouges78 on GitHub · 2026-11-24 16:05 UTC · original

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.

**@⁠rouges78** on GitHub · 2026-11-24 16:05 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7137097592) 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`](https://github.com/rouges78/VIctory/blob/50b8fe2c936e076a1269cdc11f9bd62738ef55d4/crates/victory-shader/src/emit/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.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 00:19 UTC · original · edited 2026-11-25 15:57 UTC

Automated triage for the maintainers.

This pull request no longer merges cleanly into main (conflicts in crates/victory-shader/src/emit/lower.rs). It will be reviewed once it is rebased onto the current main. The earlier verdict (needs-work) was for an older state of main.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 00:19 UTC · [original](https://github.com/Marvisak/VIctory/pull/470#issuecomment-7148395938) · edited 2026-11-25 15:57 UTC <!-- victory-triage v1 head=265c736dcc72181f55d12d36c16f0b045b02ed15 verdict=needs-rebase at=2026-11-25T15:57:10Z --> Automated triage for the maintainers. This pull request no longer merges cleanly into `main` (conflicts in `crates/victory-shader/src/emit/lower.rs`). It will be reviewed once it is rebased onto the current `main`. The earlier verdict (needs-work) was for an older state of `main`. --- _Generated by [Claude Code](https://claude.ai/code)_
This pull request has changes conflicting with the target branch.
  • crates/victory-shader/src/emit/lower.rs
View command line instructions

Manual merge helper

Use this merge commit message when completing the merge manually.

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/470/head:github-bot/gh-470'
git switch 'github-bot/gh-470'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-470'
git switch 'github-bot/gh-470'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-470'
git switch 'github-bot/gh-470'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-470'
git switch 'main'
git merge --squash 'github-bot/gh-470'
git switch 'main'
git merge --ff-only 'github-bot/gh-470'
git switch 'main'
git merge 'github-bot/gh-470'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!470
No description provided.