Skip to content

Shader: a dot product over a constant multiplicand is taken by hand - #2408

Open
KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:fix/dot4-inline-constant
Open

KainanP wants to merge 1 commit into
Marvisak:mainfrom
KainanP:fix/dot4-inline-constant

Conversation

@KainanP

@KainanP KainanP commented Nov 25, 2026 •

Copy link
Copy Markdown

Part of #1829: this fixes the fspx failure on the Intel UHD from the report there. It also fixes the RTX 5070 symptom from #1896. It keeps main's operand values and uses a by-hand sum when a multiplicand is a constant, as #1896's triage asked.

Root cause

fspx_programs_run_with_the_hardwares_dot_products fails on the Intel UHD (Xe-LP, 8086:a78b, driver 31.0.101.5445) in one case: v_dot4_i32_i8 v0, v1, 2.0 with sel_hi_lo. The result is acc − 128, and RDNA 2 gives acc − 320.

The DXIL is right. dxc -dumpbin of the translation on main shows the op_sel selection as integer arithmetic on the inline constant, which goes straight into the intrinsic:

%22 = lshr i32 1073741824, 16
%23 = and i32 1073741824, 65535
%24 = shl i32 %23, 16
%25 = or i32 %22, %24
%26 = call i32 @dx.op.dot4AddPacked.i32(i32 163, i32 %19, i32 %15, i32 %25)

The driver folds %25 to 0x4000 and then multiplies by 0x40004000. With a = 0x0302FB02, that gives −5·64 + 3·64 = −128.

I ran a probe on that device over constant multiplicands. It uses a = 0x0705FB82 and accumulator 0x7FFFFFF0, both from root constants, so only the multiplicand is known to the compiler. Every constant that fits in 16 bits, signed or unsigned, is read as its low half repeated in both halves:

multiplicand Intel UHD on main read as
inline 0, -1 right (replication gives the same value)
inline 1, 2, 64, -2, -16 wrong 0x00010001, 0x00020002, 0x00400040, 0xFFFEFFFE, 0xFFF0FFF0
literal 0x4000, 0x7F00, 0xFF80, 0xFFFF8000 wrong low half in both halves
inline floats (0x3F000000 … 0x3E22F983), literals 0x00800000, 0x12345678 right
the same values from root constants (run time) right
a constant moved into a VGPR (v_mov_b32 v2, lit) first wrong the driver folds it anyway
v_dot4c_i32_i8 with a literal wrong

The pattern fits a driver that encodes a constant source as a half-word immediate when it fits, which the packed byte dot then reads as a whole dword. #1896 saw the same thing on an RTX 5070 with 0x4000. The same values from root constants give the right result, so what goes wrong is a constant reaching dot4AddPacked.

Fix

Both dot lowerings now go through Lower::dot4_i8(acc, a, b):

  • A multiplicand that 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. If both multiplicands are constant, the products fold to one constant added to the accumulator. This is V_DOT4_I32_I8 (VOP3P opcode 22) and V_DOT4C_I32_I8 (VOP2 opcode 13) exactly as the RDNA 2 ISA defines them (November 2020, pp. 173 and 140): D = Σ S0.i8[k]·S1.i8[k] + S2 (+ D for dot4c), wrapping. So no dot4AddPacked ever sees a constant operand.
  • Run-time multiplicands: they keep the intrinsic, unchanged.

"Can work out" is decided by the new bitcode::Module::known_i32. The builder doesn't fold anything itself. known_i32 folds constants through integer arithmetic, casts and selects, which covers the op_sel half selection the lowering itself emits (the shift/and/or chain above), v_mov/s_mov literals and chains of those.

The constant's value is the same as on main. An inline float is its 32-bit pattern, and op_sel picks its halves. That is what @CaravelaLabs measured for all nine inline floats on RDNA 3.5 in #1896 and what the triage there asked to keep. After the change, the translation of the case above has no dot4AddPacked. It has shl/ashr byte extraction, mul i32 %b1, 64 and add. A register operand still emits dot4AddPacked(acc, %a, %b).

The by-hand sum for constants comes from @kais7654's #1896. This PR keeps main's operand handling, which that PR's triage asked for.

Tests

  • emit::valu_tests::dot4_takes_every_constant_class_whole (new GPU test): it runs on the hardware and on WARP and checks against dot4_ref. Each class is one program with its dot products chained through v0, so the test takes about 2 s on the RTX 4060 and under 1 s on the Intel UHD. The other operand and the accumulator come from root constants (four input sets), so only the constant is known to the compiler. The classes:

    1. inline integers 0, 1, 2, 15, 16, 63, 64, −1, −2 and −16;
    2. the nine inline floats;
    3. literals 0x4000, 0x7F00, 0xFF80, 0xFFFF8000, 0x00800000 and 0x12345678;
    4. constants moved into a VGPR (v_mov_b32) or an SGPR (s_mov_b32) first;
    5. v_dot4c_i32_i8 with literal and inline sources;
    6. constant accumulators;
    7. run-time multiplicands with four op_sel/op_sel_hi choices.

    In classes 1-3, each constant runs in src1 with four op_sel/op_sel_hi choices and in src0.

  • emit::bitcode::tests::known_i32_folds_constant_integer_arithmetic (new unit test): it checks the fold (the op_sel chain, a float bitcast, ashr) and that it stops at run-time values, at selects whose arms differ, and at float arithmetic.

  • fspx_programs_run_with_the_hardwares_dot_products: unchanged. It now passes on the Intel UHD.

Results

Debug build, Windows 11 26200, Rust 1.98.1 MSVC, on main 940d32f. The laptop has an RTX 4060 Laptop GPU (driver 32.0.16.1047, the default adapter) and the Intel UHD 8086:a78b. For the Intel runs I used a local, uncommitted hook in emit::host::hardware_adapter that picks the adapter by name, as in #1829. Its log shows only Intel(R) UHD Graphics (8086:a78b) for every hardware device. The hook is not part of this PR.

run main 940d32f this PR
Intel UHD, cargo test -p victory-shader --lib 239 passed / 8 failed; fspx fails 242 passed / 7 failed; fspx and both new tests pass
Intel UHD, new test, per class classes 1-5 fail (e.g. inline integers: got 0x7fff3c23, want 0x7fff3679); 6 and 7 pass all 7 pass
RTX 4060, cargo test --workspace --locked --no-fail-fast 695 passed / 5 failed 698 passed / the same 5 failed
WARP (VICTORY_WARP=1), same 696 passed / 4 failed 699 passed / the same 4 failed
--all-features (RTX) 700 passed / 5 failed 703 passed / the same 5 failed
cargo clippy --workspace --all-targets --locked -- -D warnings, default and --all-features clean
cargo xtask policy ok

The 7 failures left on the Intel UHD are the ones #1829 lists for other causes: arith, the three bvh tests, heap, permute and atom64. All of them are on main too.

The RTX, WARP and all-features failures are the ones main 940d32f has on this machine:

  • RTX and all-features: dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views, the two stride_tests heap tests and gathers_at_a_level_match_the_hardware.
  • WARP: the same two victory-gpu tests, gathers_at_a_level_match_the_hardware and the wave-32 permutes_….

The new tests and the FSPX test pass on the RTX 4060 and on WARP, both before and after the change.

Not run: AMD or NVIDIA RDNA-class hardware other than the RTX 4060, the RTX 5070 from #1896, and the title. I used no game data.

Overlap:

AI use: Claude Code (Claude Opus 5.5) investigated, wrote the change and the tests, ran the hardware runs on this machine, and wrote this description.

🤖 Generated with 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: merged into main 940d32f, cargo test -p victory-shader --locked on macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — constant-multiplicand dot product folded.

@steven-seagull27

Copy link
Copy Markdown

I ran the FSPX test and the seven packed-dot operand classes on an RTX 5080 (driver 621.40) and built-in Microsoft WARP. Each comparison used the same tests on the PR head and its direct parent. The device was verified by DXGI LUID and every case completed a D3D12 dispatch and readback.

Revision RTX 5080 WARP
Parent 940d32f 32/32 matched 32/32 matched
PR 2644bbc 32/32 matched 32/32 matched

That is four FSPX results plus four inputs for each of the seven operand classes. The inline 2.0 / sel_hi_lo FSPX case returned 0x7ffffeb0 (0x7ffffff0 - 320) on both revisions and devices. This RTX 5080/driver combination therefore passes the parent case that failed on the RTX 5070 in #1896. The results are from synthetic shaders.

@oni-swr

oni-swr commented Nov 25, 2026

Copy link
Copy Markdown

WARP verification

Tested: PR head 2644bbcb5cc5801565dad685b5100dd607591156, on origin/main 940d32f69b22265690a0b5a15d6ded572ba33baf (the PR is one commit on that base, so the merge was a no-op).
Environment: VICTORY_D3D12_WARP=1 (software WARP), Windows 11 Pro 10.0.26200, rustc 1.98.1, DXC 1.9.0.5512 (dxcompiler), dxil.dll 1.8.2502.11 on PATH.

Command Result
cargo test -p victory-shader --lib --locked First run: 248 passed, 1 failed, 3 ignored. Three further full runs: 249 passed, 0 failed, 3 ignored each
cargo clippy -p victory-shader --all-targets --locked -- -D warnings exit 0
cargo xtask policy asset policy: 182 files ok, exit 0

The failure on the first run was emit::memory_tests::writes_through_views_mark_their_pages (page-set assertion at memory_tests.rs:611, backend warp: it got many more pages than the expected [100, 101, 200]). It passed on that PR head in two isolated runs and in the three later full runs. It also passed in isolation on main. I think the failure is intermittent on WARP and not caused by this PR, but I did not find the cause. The test was added recently, by the "shaders mark the pages they write through views" commit, so maintainers may want to check it for flakiness.

New tests emit::bitcode::tests::known_i32_folds_constant_integer_arithmetic and emit::valu_tests::dot4_takes_every_constant_class_whole both ran and passed (not skipped). The second one runs on WARP, so it did execute the by-hand dot product there.

Diff review (bitcode.rs, lower.rs, valu_tests.rs): known_i32 is a bounded-depth constant folder with no side effects. dot4_i8 falls back to dot4AddPacked when neither operand is known. There are no process, network or filesystem actions.

Limits: WARP only. The dot4AddPacked driver behaviour this PR works around (Intel Xe-LP, RTX 5070) was not checked, since there is no physical GPU here. No game files or dumps.

AI use: Claude Code (Claude Sonnet 5.5)

@DenRakEiw DenRakEiw 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 against origin/main (940d32f) on Windows 11 + NVIDIA GeForce RTX 5090 and WARP:

  • Lowering dot products over constant multiplicands by hand passes all live GPU dot-product tests on RTX 5090 and WARP.
  • cargo test -p victory-shader dot: 3/3 passed on RTX 5090, 3/3 passed on WARP (byte_dot_products_match_the_hardware, fspx_programs_run_with_the_hardwares_dot_products, dot4_takes_every_constant_class_whole).
  • cargo clippy -p victory-shader --all-targets -- -D warnings: 0 warnings.
  • cargo xtask policy: 183 files ok.

AI-assisted (Gemini 4 Neon).

Marvisak commented Nov 25, 2026 •

Copy link
Copy Markdown
Owner

Automated triage for the maintainers.

Merge candidate. Both packed byte-dot lowerings in crates/victory-shader/src/emit/lower.rs now go through Lower::dot4_i8. When a multiplicand folds to a constant (bitcode::Module::known_i32), it computes the signed byte products and their sum by hand. A run-time pair keeps dot4AddPacked. This works around drivers that misread a 16-bit constant operand (Intel Xe-LP, RTX 5070), and the evidence in the description (DXIL dump, per-constant probe) supports it. The operand values are unchanged from main, and the products are i8 by i8, so they cannot overflow.

On your reply: the rebase only adds lines next to main's new bitcode.rs helpers and valu_tests.rs test, as you say. The verdict before the conflict was merge candidate, and it stands. CI: passing.


Generated by Claude Code

@kotekompotek

Copy link
Copy Markdown

Verified the semantics: known-side bytes extract as i8 (signed), the other side uses select_bits(sext=true) so i8×i8 signed products sum into acc with wrapping adds — the same value dot4AddPacked computes, expressed as muls a buggy driver can't misread as a replicated half-word (#1829's 0x4000→0x40004000, #1896 on RTX 5070). Both-known folds fully; neither-known keeps the intrinsic. known_int is depth-bounded and the Select same-arm case is a nice touch — the swapped-halves test pins exactly the misread shape.

One note: when the other operand is runtime-valued the expansion emits up to 4 mul+add pairs instead of one op — strictly more instructions, so on non-buggy drivers this is a mild pessimisation. An env/flag gate would let a driver that handles the intrinsic correctly keep it; not a blocker.

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>
@KainanP
KainanP force-pushed the fix/dot4-inline-constant branch from 2644bbc to 9ea9e00 Compare November 25, 2026 23:22
@KainanP

KainanP commented Nov 25, 2026

Copy link
Copy Markdown
Author

Rebased onto main 842b0cb7 (head 9ea9e00d). The two conflicts were additions on both sides: bitcode.rs (main's test-only has_function/has_i32_const next to this PR's known_i32) and valu_tests.rs (main's v_mul_legacy_f32 test next to this PR's dot tests), so both sides are kept unchanged. RTX 4060 Laptop: cargo test -p victory-shader --lib gives 300 passed and 3 failed, the same 3 that fail on main on this NVIDIA card (two stride tests and the gather test). cargo clippy -p victory-shader --all-targets --features host -D warnings is clean.

AI use: Claude Code (Claude Opus 5.5).

BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…t multiplicand is taken by hand

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
BastienRodz added a commit to BastienRodz/VIctory that referenced this pull request Nov 26, 2026
…t multiplicand is taken by hand

# Conflicts:
#	crates/victory-shader/src/emit/valu_tests.rs
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