Shader: a dot product over a constant multiplicand is taken by hand #2408
No reviewers
Labels
No labels
accessibility
area: build
area: formats
area: gpu
area: loader
area: runtime
area: shader
bug
documentation
duplicate
enhancement
good first issue
gpu: amd
gpu: intel
gpu: nvidia
help wanted
host: wine
invalid
out of scope
question
request
run report
triage: confirmed
triage: has patch
triage: merge candidate
triage: needs info
triage: needs rebase
triage: needs work
triage: proposed close
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/Marvisak--VIctory!2408
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-2408"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
@KainanP on GitHub · 2026-11-25 13:06 UTC · original
KainanP:fix/dot4-inline-constant→main· +320 −18 · 3 files · 1 commits · openPart of #1829: this fixes the
fspxfailure on the Intel UHD from the report there. It also fixes the RTX 5070 symptom from #1896. It keepsmain'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_productsfails on the Intel UHD (Xe-LP,8086:a78b, driver 31.0.101.5445) in one case:v_dot4_i32_i8 v0, v1, 2.0withsel_hi_lo. The result isacc − 128, and RDNA 2 givesacc − 320.The DXIL is right.
dxc -dumpbinof the translation onmainshows the op_sel selection as integer arithmetic on the inline constant, which goes straight into the intrinsic:The driver folds
%25to0x4000and then multiplies by0x40004000. Witha = 0x0302FB02, that gives −5·64 + 3·64 = −128.I ran a probe on that device over constant multiplicands. It uses
a = 0x0705FB82and accumulator0x7FFFFFF0, 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:main0,-11,2,64,-2,-160x00010001,0x00020002,0x00400040,0xFFFEFFFE,0xFFF0FFF00x4000,0x7F00,0xFF80,0xFFFF80000x3F000000…0x3E22F983), literals0x00800000,0x12345678v_mov_b32 v2, lit) firstv_dot4c_i32_i8with a literalThe 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 reachingdot4AddPacked.Fix
Both dot lowerings now go through
Lower::dot4_i8(acc, a, b):D = Σ S0.i8[k]·S1.i8[k] + S2(+ Dfordot4c), wrapping. So nodot4AddPackedever sees a constant operand."Can work out" is decided by the new
bitcode::Module::known_i32. The builder doesn't fold anything itself.known_i32folds 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_movliterals 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 nodot4AddPacked. It hasshl/ashrbyte extraction,mul i32 %b1, 64andadd. A register operand still emitsdot4AddPacked(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 againstdot4_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:0x4000,0x7F00,0xFF80,0xFFFF8000,0x00800000and0x12345678;v_mov_b32) or an SGPR (s_mov_b32) first;v_dot4c_i32_i8with literal and inline sources;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
main940d32f. The laptop has an RTX 4060 Laptop GPU (driver 32.0.16.1047, the default adapter) and the Intel UHD8086:a78b. For the Intel runs I used a local, uncommitted hook inemit::host::hardware_adapterthat picks the adapter by name, as in #1829. Its log shows onlyIntel(R) UHD Graphics (8086:a78b)for every hardware device. The hook is not part of this PR.main940d32fcargo test -p victory-shader --libfspxfailsfspxand both new tests pass0x7fff3c23, want0x7fff3679); 6 and 7 passcargo test --workspace --locked --no-fail-fastVICTORY_WARP=1), same--all-features(RTX)cargo clippy --workspace --all-targets --locked -- -D warnings, default and--all-featurescargo xtask policyThe 7 failures left on the Intel UHD are the ones #1829 lists for other causes:
arith, the threebvhtests,heap,permuteandatom64. All of them are onmaintoo.The RTX, WARP and all-features failures are the ones
main940d32fhas on this machine:dynamic_slots_are_held_until_released,lists_share_a_table_and_copies_keep_their_views, the twostride_testsheap tests andgathers_at_a_level_match_the_hardware.victory-gputests,gathers_at_a_level_match_the_hardwareand the wave-32permutes_….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:
dot8forms. They would meet a textual conflict here, and their constant operands intodot4AddPacked(op 164 for the unsigned forms included) would need the same handling.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
@steven-seagull27 on GitHub · 2026-11-25 13:28 UTC · original
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.
940d32f2644bbcThat is four FSPX results plus four inputs for each of the seven operand classes. The inline
2.0/sel_hi_loFSPX case returned0x7ffffeb0(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 on GitHub · 2026-11-25 13:34 UTC · original
WARP verification
Tested: PR head
2644bbcb5cc5801565dad685b5100dd607591156, onorigin/main940d32f69b22265690a0b5a15d6ded572ba33baf(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 onPATH.cargo test -p victory-shader --lib --lockedcargo clippy -p victory-shader --all-targets --locked -- -D warningscargo xtask policyasset policy: 182 files ok, exit 0The failure on the first run was
emit::memory_tests::writes_through_views_mark_their_pages(page-set assertion atmemory_tests.rs:611, backendwarp: 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_arithmeticandemit::valu_tests::dot4_takes_every_constant_class_wholeboth 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_i32is a bounded-depth constant folder with no side effects.dot4_i8falls back todot4AddPackedwhen neither operand is known. There are no process, network or filesystem actions.Limits: WARP only. The
dot4AddPackeddriver 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)
@Marvisak on GitHub · 2026-11-25 15:31 UTC · original · edited 2026-11-26 00:20 UTC
Automated triage for the maintainers.
Merge candidate. Both packed byte-dot lowerings in
crates/victory-shader/src/emit/lower.rsnow go throughLower::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 keepsdot4AddPacked. 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 frommain, and the products are i8 by i8, so they cannot overflow.On your reply: the rebase only adds lines next to
main's newbitcode.rshelpers andvalu_tests.rstest, as you say. The verdict before the conflict was merge candidate, and it stands. CI: passing.Generated by Claude Code
@kotekompotek on GitHub · 2026-11-25 16:55 UTC · original
Verified the semantics: known-side bytes extract as
i8(signed), the other side usesselect_bits(sext=true)so i8×i8 signed products sum into acc with wrapping adds — the same valuedot4AddPackedcomputes, 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_intis 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.
@KainanP on GitHub · 2026-11-25 23:22 UTC · original
Rebased onto
main842b0cb7(head9ea9e00d). The two conflicts were additions on both sides:bitcode.rs(main's test-onlyhas_function/has_i32_constnext to this PR'sknown_i32) andvalu_tests.rs(main'sv_mul_legacy_f32test next to this PR's dot tests), so both sides are kept unchanged. RTX 4060 Laptop:cargo test -p victory-shader --libgives 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 warningsis clean.AI use: Claude Code (Claude Opus 5.5).
View command line instructions
Checkout
From your project repository, check out a new branch and test the changes.Merge
Merge the changes and update on Forgejo.