Shader: the packed dot over an inline constant is taken by hand from … #1896

Closed
github-bot wants to merge 1 commit from gh-pr/1896/head into gh-pr/1896/base
Owner

@⁠kais7654 on GitHub · 2026-11-25 00:17 UTC · original
kais7654:shader/dot-packed-const → main · +46 −11 · 2 files · 1 commits · closed without merging

Problem

fspx_programs_run_with_the_hardwares_dot_products fails on NVIDIA (RTX 5070):
one v_dot4_i32_i8 case returns acc - 128 instead of acc - 320 when one of
the dot sources is an inline constant.

The emitted DXIL is semantically correct. Disassembling the dumped shader with
dxc -dumpbin shows the faulting call:

%14 = call i32 @⁠dx.op.dot4AddPacked.i32(i32 163, i32 2147483632, i32 50526978, i32 16384)

b = 16384 is the f16 pattern in the low half alone, which is exactly the
wanted value. But the driver feeds the low half into BOTH halves of its native
dot instruction (broadcast), so the hardware result drifts. WARP passes the
same test with the fold in place.

Fix

In the v_dot4_i32_i8 lowering (crates/victory-shader/src/emit/lower.rs):

  1. When a source operand is a foldable inline constant (240..=248), emit its
    f16 pattern in the low half through f16_operand() and mark the
    instruction as folded.
  2. When folded, compute the dot by hand — 4 signed-byte products via
    select_bits() accumulated with Mul/Add — instead of emitting the
    dot4AddPacked.i32 intrinsic, which sidesteps the driver bug entirely.
  3. Otherwise the intrinsic is emitted unchanged.

Testing

  • fspx_programs_run_with_the_hardwares_dot_products PASSES on RTX 5070
    (laptop) and on WARP.
  • Full suite cargo test -p victory-shader --lib -- --test-threads=1:
    203 passed, 6 failed — the 6 failures are pre-existing on main (70564ae);
    the packed-dot failure is gone (7 → 6).
  • cargo clippy clean.
  • No temporary instrumentation remains; the diff only touches lower.rs
    (32 insertions, 8 deletions).
**@⁠kais7654** on GitHub · 2026-11-25 00:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896) `kais7654:shader/dot-packed-const` → `main` · +46 −11 · 2 files · 1 commits · closed without merging ## Problem `fspx_programs_run_with_the_hardwares_dot_products` fails on NVIDIA (RTX 5070): one `v_dot4_i32_i8` case returns `acc - 128` instead of `acc - 320` when one of the dot sources is an inline constant. The emitted DXIL is semantically correct. Disassembling the dumped shader with `dxc -dumpbin` shows the faulting call: %14 = call i32 @⁠dx.op.dot4AddPacked.i32(i32 163, i32 2147483632, i32 50526978, i32 16384) `b = 16384` is the f16 pattern in the low half alone, which is exactly the wanted value. But the driver feeds the low half into BOTH halves of its native dot instruction (broadcast), so the hardware result drifts. WARP passes the same test with the fold in place. ## Fix In the `v_dot4_i32_i8` lowering (`crates/victory-shader/src/emit/lower.rs`): 1. When a source operand is a foldable inline constant (240..=248), emit its f16 pattern in the low half through `f16_operand()` and mark the instruction as `folded`. 2. When `folded`, compute the dot **by hand** — 4 signed-byte products via `select_bits()` accumulated with Mul/Add — instead of emitting the `dot4AddPacked.i32` intrinsic, which sidesteps the driver bug entirely. 3. Otherwise the intrinsic is emitted unchanged. ## Testing - `fspx_programs_run_with_the_hardwares_dot_products` PASSES on RTX 5070 (laptop) and on WARP. - Full suite `cargo test -p victory-shader --lib -- --test-threads=1`: 203 passed, 6 failed — the 6 failures are pre-existing on main (70564ae); the packed-dot failure is gone (7 → 6). - `cargo clippy` clean. - No temporary instrumentation remains; the diff only touches `lower.rs` (32 insertions, 8 deletions).
The dot4 lowering sent an inline constant the f16-operand way, its
pattern alone in the low half; AMD reads a constant like any operand —
the f32 pattern through the half selection (measured across the nine
constants and the selections) — so the constant goes the ordinary way
again. A host driver still reads a constant's low half in both halves
of its dot, so a dot over one is taken by hand from the byte products
as before. Two cases were added: 1.0 at the default selection and 4.0
with the halves swapped.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <[email protected]>
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 04:47:59 +00:00
github-bot closed this pull request 2026-11-26 04:47:59 +00:00
Author
Owner

@⁠oni-swr on GitHub · 2026-11-25 01:06 UTC · original

AMD RDNA3 (RX 7800 XT) test report for #1896 at head 51f3cb87 merged with main 42363ab6 (clean merge).

Tested

  • AMD Radeon RX 7800 XT 16 GB (RDNA3, driver 32.0.31035.1003), Ryzen 7 5700X3D, 64 GB RAM, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug build.
  • Baseline on origin/main 42363ab6:
    • cargo test -p victory-shader --lib fspx_programs_run_with_the_hardwares_dot_products -- --test-threads=1 --nocapture: 1 passed, 0 failed (0.40s; AMD did not hit the NVIDIA inline-constant dot4 broadcast bug).
  • PR #1896 (51f3cb87 merged with 42363ab6):
    • cargo test -p victory-shader --lib fspx_programs_run_with_the_hardwares_dot_products -- --test-threads=1 --nocapture: 1 passed, 0 failed (0.40s).
    • cargo test -p victory-shader --lib -- --test-threads=1 --nocapture: 246 passed, 0 failed, 3 ignored (33.19s; 6 no console [or PC] compiler; skipped and 1 warp: no pipeline for a wave of 32; skipped).
    • cargo clippy -p victory-shader --all-targets: clean.

Not tested

  • Release build, full workspace outside victory-shader, or a game run.

AI use

  • T3 Code (Antigravity), Gemini 4 Neon: checked out the PR in a temporary worktree, ran the baseline and PR tests on @⁠oni-swr's machine, and wrote this report.
**@⁠oni-swr** on GitHub · 2026-11-25 01:06 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7149208418) AMD RDNA3 (RX 7800 XT) test report for #1896 at head `51f3cb87` merged with `main` `42363ab6` (clean merge). **Tested** - AMD Radeon RX 7800 XT 16 GB (RDNA3, driver 32.0.31035.1003), Ryzen 7 5700X3D, 64 GB RAM, Windows 11 Pro 26200, Rust 1.100.0 MSVC, debug build. - Baseline on `origin/main` `42363ab6`: - `cargo test -p victory-shader --lib fspx_programs_run_with_the_hardwares_dot_products -- --test-threads=1 --nocapture`: **1 passed, 0 failed** (0.40s; AMD did not hit the NVIDIA inline-constant dot4 broadcast bug). - PR #1896 (`51f3cb87` merged with `42363ab6`): - `cargo test -p victory-shader --lib fspx_programs_run_with_the_hardwares_dot_products -- --test-threads=1 --nocapture`: **1 passed, 0 failed** (0.40s). - `cargo test -p victory-shader --lib -- --test-threads=1 --nocapture`: **246 passed, 0 failed, 3 ignored** (33.19s; 6 `no console [or PC] compiler; skipped` and 1 `warp: no pipeline for a wave of 32; skipped`). - `cargo clippy -p victory-shader --all-targets`: clean. **Not tested** - Release build, full workspace outside `victory-shader`, or a game run. **AI use** - T3 Code (Antigravity), Gemini 4 Neon: checked out the PR in a temporary worktree, ran the baseline and PR tests on @⁠oni-swr's machine, and wrote this report.
Author
Owner

@⁠CaravelaLabs on GitHub · 2026-11-25 01:07 UTC · original · edited 2026-11-25 03:53 UTC

@⁠kais7654 thanks for catching that RTX 5070 miss. For this encoding the dump is the right dword: 16384 is sel_hi_lo(2.0), and acc−320 is the signed dot of those bytes. A driver that copies that low half into both halves is a host bug. The by-hand path matches that dword only for this one constant.

An inline float in v_dot4_i32_i8 is the 32-bit value. inline returns 1.0 as 0x3F800000 (and 2.0 as 0x40000000), and op_sel / op_sel_hi pick the halves. That is what main does, and what the other packed integer ops do. f16_operand is the packed-float table (1.0 → 0x3C00). Those two agree only for ±2.0 with sel_hi_lo.

That is the only inline constant in fspx_programs_run_with_the_hardwares_dot_products (code 244, op_sel 0b010, op_sel_hi 0b101, expected 0x4000). The test passing cannot see the rest. @⁠oni-swr's RX 7800 XT run of that same test passing is this case, on main 42363ab and on this head. Thank you for that data point.

On 51f3cb87 the new arm sends codes 240–248 through f16_operand and skips op_sel. With the test's a = 0x0302FB02 and accumulator 0x7FFFFFF0, identity 1.0 (llvm-mc gfx1030 v_dot4_i32_i8 v0, v1, 1.0, v2, encoding [0x00,0x40,0x16,0xcc,0x01,0xe5,0x09,0x1c], op_sel=0, op_sel_hi=7) should be 0x7FFFFFAD. This path produces 0x7FFFFEC4. sel_hi_lo(4.0) should be 0x7FFFFDB0, not 0x7FFFFE9C. The 2.0 case you fixed stays 0x7FFFFEB0 either way. Those are the project's own dot4_ref values; a GPU run is below. The lowering on main 42363ab is unchanged from 4118f33.

Separate from #1782 (an RDNA1 driver folding that intrinsic's constant as unsigned). No game data. I did not re-run the RTX 5070.

Update, now on AMD hardware (Radeon 8060S, RDNA3.5, so not RDNA2 itself). I ran the same VOP3P opcode (0x16; v_dot4_i32_iu8 with both operands signed on this generation) with each inline float 240-248 and four op_sel/op_sel_hi settings, and read the results back. All 36 match the 32-bit float pattern with op_sel picking the halves. The f16 pattern with op_sel ignored matches 2 of the 36: ±2.0 with sel_hi_lo. Same output on every repeat.

src1 op_sel / op_sel_hi hardware 51f3cb87 (computed)
1.0 default 0x7FFFFFAD 0x7FFFFEC4
4.0 sel_hi_lo 0x7FFFFDB0 0x7FFFFE9C
2.0 sel_hi_lo 0x7FFFFEB0 0x7FFFFEB0

@⁠mercuryVM, you read RDNA2 as ignoring op_sel for these. I can't run RDNA2 here, so if you have a reference or an RDNA2 card that shows it, I'd gladly be corrected. Your point that the by-hand sum avoids the NVIDIA fold still stands; only the constant's value is in question.

AI-assisted check, run on this machine.

**@⁠CaravelaLabs** on GitHub · 2026-11-25 01:07 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7149235327) · edited 2026-11-25 03:53 UTC @⁠kais7654 thanks for catching that RTX 5070 miss. For this encoding the dump is the right dword: `16384` is `sel_hi_lo(2.0)`, and acc−320 is the signed dot of those bytes. A driver that copies that low half into both halves is a host bug. The by-hand path matches that dword only for this one constant. An inline float in `v_dot4_i32_i8` is the 32-bit value. `inline` returns `1.0` as `0x3F800000` (and `2.0` as `0x40000000`), and `op_sel` / `op_sel_hi` pick the halves. That is what main does, and what the other packed integer ops do. `f16_operand` is the packed-float table (`1.0` → `0x3C00`). Those two agree only for ±2.0 with `sel_hi_lo`. That is the only inline constant in `fspx_programs_run_with_the_hardwares_dot_products` (code `244`, `op_sel` `0b010`, `op_sel_hi` `0b101`, expected `0x4000`). The test passing cannot see the rest. @⁠oni-swr's RX 7800 XT run of that same test passing is this case, on main `42363ab` and on this head. Thank you for that data point. On `51f3cb87` the new arm sends codes 240–248 through `f16_operand` and skips `op_sel`. With the test's `a = 0x0302FB02` and accumulator `0x7FFFFFF0`, identity `1.0` (llvm-mc gfx1030 `v_dot4_i32_i8 v0, v1, 1.0, v2`, encoding `[0x00,0x40,0x16,0xcc,0x01,0xe5,0x09,0x1c]`, `op_sel=0`, `op_sel_hi=7`) should be `0x7FFFFFAD`. This path produces `0x7FFFFEC4`. `sel_hi_lo(4.0)` should be `0x7FFFFDB0`, not `0x7FFFFE9C`. The `2.0` case you fixed stays `0x7FFFFEB0` either way. Those are the project's own `dot4_ref` values; a GPU run is below. The lowering on main `42363ab` is unchanged from `4118f33`. Separate from #1782 (an RDNA1 driver folding that intrinsic's constant as unsigned). No game data. I did not re-run the RTX 5070. **Update, now on AMD hardware** (Radeon 8060S, RDNA3.5, so not RDNA2 itself). I ran the same VOP3P opcode (`0x16`; `v_dot4_i32_iu8` with both operands signed on this generation) with each inline float 240-248 and four `op_sel`/`op_sel_hi` settings, and read the results back. All 36 match the 32-bit float pattern with `op_sel` picking the halves. The f16 pattern with `op_sel` ignored matches 2 of the 36: ±2.0 with `sel_hi_lo`. Same output on every repeat. | `src1` | `op_sel` / `op_sel_hi` | hardware | `51f3cb87` (computed) | |---|---|---|---| | `1.0` | default | `0x7FFFFFAD` | `0x7FFFFEC4` | | `4.0` | `sel_hi_lo` | `0x7FFFFDB0` | `0x7FFFFE9C` | | `2.0` | `sel_hi_lo` | `0x7FFFFEB0` | `0x7FFFFEB0` | @⁠mercuryVM, you read RDNA2 as ignoring `op_sel` for these. I can't run RDNA2 here, so if you have a reference or an RDNA2 card that shows it, I'd gladly be corrected. Your point that the by-hand sum avoids the NVIDIA fold still stands; only the constant's value is in question. AI-assisted check, run on this machine.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 08:22 UTC · original · edited 2026-11-25 14:51 UTC

Automated triage for the maintainers.

Merge candidate. Re-reviewed at 9b0824a. In the v_dot4_i32_i8 lowering (crates/victory-shader/src/emit/lower.rs), inline constants 240-248 now go through the ordinary operand path again: s(self, k) gives the 32-bit float pattern, and op_sel/op_sel_hi pick the halves, exactly as on main. Only when a multiplicand is such a constant is the dot taken by hand: four signed byte products from select_bits(.., true), added to the accumulator with wrapping i32 arithmetic. That is the same math as dot4AddPacked.i32, so the console semantics do not change; it only avoids the RTX 5070 driver reading the constant's low half in both halves. The two new cases in fspx_programs_run_with_the_hardwares_dot_products (1.0 at default selection, 4.0 with sel_hi_lo) take their expected values from dot4_ref, and they match the AMD hardware readbacks @⁠CaravelaLabs posted above (0x7FFFFFAD, 0x7FFFFDB0).

Reply to @⁠kais7654: thanks, both requested changes are in, so the verdict moves from needs-work. A small optional follow-up: folded only looks at float inline constants (240-248). If the same driver also folds integer inline constants (128-208) or a literal operand, those still reach the intrinsic; a case with one of them would show whether that matters.

CI: passing (check).


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 08:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7156939720) · edited 2026-11-25 14:51 UTC <!-- victory-triage v1 head=9b0824ae8d3e08440bd34169550d2396fc198154 verdict=merge-candidate at=2026-11-25T14:51:02Z --> Automated triage for the maintainers. **Merge candidate.** Re-reviewed at `9b0824a`. In the `v_dot4_i32_i8` lowering (`crates/victory-shader/src/emit/lower.rs`), inline constants 240-248 now go through the ordinary operand path again: `s(self, k)` gives the 32-bit float pattern, and `op_sel`/`op_sel_hi` pick the halves, exactly as on `main`. Only when a multiplicand is such a constant is the dot taken by hand: four signed byte products from `select_bits(.., true)`, added to the accumulator with wrapping i32 arithmetic. That is the same math as `dot4AddPacked.i32`, so the console semantics do not change; it only avoids the RTX 5070 driver reading the constant's low half in both halves. The two new cases in `fspx_programs_run_with_the_hardwares_dot_products` (`1.0` at default selection, `4.0` with `sel_hi_lo`) take their expected values from `dot4_ref`, and they match the AMD hardware readbacks @⁠CaravelaLabs posted above (`0x7FFFFFAD`, `0x7FFFFDB0`). Reply to @⁠kais7654: thanks, both requested changes are in, so the verdict moves from needs-work. A small optional follow-up: `folded` only looks at float inline constants (240-248). If the same driver also folds integer inline constants (128-208) or a literal operand, those still reach the intrinsic; a case with one of them would show whether that matters. CI: passing (`check`). --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠sinan-cmyk on GitHub · 2026-11-25 11:38 UTC · original

Offline measurement on a retail dump

Result: no count change against main.

I merged head 51f3cb87 onto main 940d32f (clean merge) and ran victory translate --validate --list on shaders.rpf (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with main program by program (25,889 programs). Validated: 23,918 on main, 23,918 with this PR.

No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right.

No shader names or data are included, per the asset policy. AI-assisted (Claude Code).

**@⁠sinan-cmyk** on GitHub · 2026-11-25 11:38 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7161319024) ### Offline measurement on a retail dump **Result: no count change against `main`.** I merged head 51f3cb87 onto `main` 940d32f (clean merge) and ran `victory translate --validate --list` on `shaders.rpf` (1.0.2.1), with dxil.dll 1.8.2502.11. I compared the result with `main` program by program (25,889 programs). Validated: 23,918 on `main`, 23,918 with this PR. No program changes its failure reason. The count shows that the change rejects nothing on this data. It does not show whether the output is right. No shader names or data are included, per the asset policy. AI-assisted (Claude Code).
Author
Owner

@⁠kais7654 on GitHub · 2026-11-25 13:34 UTC · original

Fixed at 9b0824a: the f16-operand branch is gone — an inline constant
goes the ordinary operand way again (the f32 pattern, through the
op_sel / op_sel_hi half selection), like main; only a dot over a
constant is still taken by hand from the byte products, for the driver
that reads the constant's low half in both halves. Two cases were added
to fspx_programs_run_with_the_hardwares_dot_products: 1.0 at the
default selection (0x3F800000 read whole → 0x7FFFFFAD) and 4.0 with the
halves swapped (0x4080 → 0x7FFFFDB0). All six dot4 cases pass on RTX
5070 Laptop GPU (driver 616.87) and WARP; CI is green.

AI-assisted (Mistral Vibe).

**@⁠kais7654** on GitHub · 2026-11-25 13:34 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7163972805) Fixed at 9b0824a: the f16-operand branch is gone — an inline constant goes the ordinary operand way again (the f32 pattern, through the op_sel / op_sel_hi half selection), like main; only a dot over a constant is still taken by hand from the byte products, for the driver that reads the constant's low half in both halves. Two cases were added to fspx_programs_run_with_the_hardwares_dot_products: 1.0 at the default selection (0x3F800000 read whole → 0x7FFFFFAD) and 4.0 with the halves swapped (0x4080 → 0x7FFFFDB0). All six dot4 cases pass on RTX 5070 Laptop GPU (driver 616.87) and WARP; CI is green. AI-assisted (Mistral Vibe).
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 22:03 UTC · original

Automated triage for the maintainers, second look (a review against current main, with builds and tests where they could change the verdict).

Closing in favour of #2408, which takes the same by-hand sum (crediting this PR) for every multiplicand a host compiler can fold: inline floats, inline integers, literals and constants moved through a register, and also v_dot4c_i32_i8. Integer inline constants and literals were measured to be misread too, and this PR matches only inline floats on v_dot4_i32_i8. The sum itself is right.

If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

**@⁠Marvisak** on GitHub · 2026-11-25 22:03 UTC · [original](https://github.com/Marvisak/VIctory/pull/1896#issuecomment-7175889898) Automated triage for the maintainers, second look (a review against current `main`, with builds and tests where they could change the verdict). Closing in favour of #2408, which takes the same by-hand sum (crediting this PR) for every multiplicand a host compiler can fold: inline floats, inline integers, literals and constants moved through a register, and also `v_dot4c_i32_i8`. Integer inline constants and literals were measured to be misread too, and this PR matches only inline floats on `v_dot4_i32_i8`. The sum itself is right. If this is mistaken, reply here with what was missed and it will be looked at again. Thank you for the contribution.

Pull request closed

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!1896
No description provided.