Skip to content

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

Closed
kais7654 wants to merge 1 commit into
Marvisak:mainfrom
kais7654:shader/dot-packed-const
Closed

kais7654 wants to merge 1 commit into
Marvisak:mainfrom
kais7654:shader/dot-packed-const

Conversation

@kais7654

Copy link
Copy Markdown

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

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

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 Shader: the packed dot over an inline constant is taken by hand from … #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.

@CaravelaLabs

CaravelaLabs commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

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

mercuryVM added a commit to mercuryVM/VIctory that referenced this pull request Nov 25, 2026
…e constant is taken by hand from …

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

@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 51f3cb8 on Windows 11, RTX 5070, MSVC:

  • cargo test -p victory-shader -- --test-threads=1 passes the dot-product test on the head, and the remaining failures are the same 5 as on main.
  • Merged into our fork with #1900: the same results, and clippy is clean.

The change is right. An inline f16 constant ignores op_sel/op_sel_hi on RDNA2, so its pattern sits in the low half. The host's dot4AddPacked with a constant operand reads it differently, so taking that one case by hand from the four byte products keeps the console's result. Non-constant operands still use the intrinsic.

This and #1900 approach the same failure from both sides. Both are worth keeping: #1900 makes the test run what a title runs, and this PR covers titles that do fold a constant.

AI-assisted (Claude Code).

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

Tested locally: macOS arm64, rustc 1.98.1, PR head merged into main 940d32f, cargo test -p victory-shader --locked.

144 passed / 0 failed / 2 ignored (baseline-identical). Packed dot with an inline constant now reads the constant pool by hand instead of relying on the immediate path — closes a real encoding gap.

github-actions Bot added a commit to KevinPratap/VIctory that referenced this pull request Nov 25, 2026

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

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

@sinan-cmyk

Copy link
Copy Markdown

Offline measurement on a retail dump

Result: no count change against main.

I merged head 51f3cb8 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).

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 <vibe@mistral.ai>
@kais7654
kais7654 force-pushed the shader/dot-packed-const branch from 51f3cb8 to 9b0824a Compare November 25, 2026 13:23
@kais7654

Copy link
Copy Markdown
Author

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

@Marvisak

Copy link
Copy Markdown
Owner

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 Marvisak closed this Nov 25, 2026
KainanP added a commit to KainanP/VIctory that referenced this pull request Nov 25, 2026
On an Intel UHD (Xe-LP, 8086:a78b, driver 31.0.101.5445),
`fspx_programs_run_with_the_hardwares_dot_products` gives acc - 128 where
RDNA 2 gives acc - 320 (Marvisak#1829). The case is `v_dot4_i32_i8 v0, v1, 2.0`
with `sel_hi_lo`, a multiplicand of 0x4000. The DXIL is right: the op_sel
selection is integer arithmetic on 0x40000000, and the driver folds it to
`dot4AddPacked(acc, a, 0x4000)`. The device then multiplies by 0x40004000:
-5 * 64 + 3 * 64 = -128.

A probe over constant multiplicands on that device shows the rule. Every
constant that fits in 16 bits (signed or not) is read as its low half in
both halves: 1, 2, 64, -2, -16, 0x4000, 0x7F00, 0xFF80 and 0xFFFF8000 all
give the replicated value. 0 and -1 are right only because their
replication is themselves. Constants wider than 16 bits (the inline floats
as 32-bit patterns, 0x00800000, 0x12345678) are right, and so is every
run-time multiplicand. A constant moved into a VGPR or an SGPR first
fails the same way, and so does `v_dot4c_i32_i8`. Marvisak#1896 reports the same
symptom on an RTX 5070.

Both dot lowerings now go through `dot4_i8`. When a multiplicand is
something the host compiler can work out before the program runs, the
lowering sums the other operand's sign-extended bytes times the
constant's non-zero bytes by hand, which is V_DOT4_I32_I8 and
V_DOT4C_I32_I8 in the RDNA 2 ISA (November 2020, pp. 140 and 173), so no
`dot4AddPacked` sees a constant. `bitcode::Module::known_i32` decides
this. It folds constants through integer arithmetic, casts and selects,
which covers the op_sel half selection the lowering itself emits. Run-time
multiplicands keep the intrinsic. The constant's value is unchanged: an
inline float is its 32-bit pattern and op_sel picks its halves, as on
`main`.

`dot4_takes_every_constant_class_whole` checks against `dot4_ref`, one
program per class with its dot products chained through v0: inline
integers 0, 1, 2, 15, 16, 63, 64, -1, -2 and -16; the nine inline floats;
16-bit and wider literals (each constant in src1 with four op_sel
choices and in src0); constants in a VGPR or an SGPR; `v_dot4c` with
literal and inline sources; constant accumulators; and run-time operands
with four op_sel choices. With `main`'s lowering the Intel UHD fails the
first six classes and passes the last two. With this change it passes
all of them, and the FSPX test passes too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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