Shader: a dot product over a constant multiplicand is taken by hand #2406
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/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?
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-25T13:06:40Z.
Part 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
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-25T13:25:23Z.
Review: APPROVED
Tested locally: merged into main
940d32f69b,cargo test -p victory-shader --lockedon macOS arm64 — 145 passed, 0 failed, 2 ignored (Windows-gated hardware execution tests not run here). Approving — constant-multiplicand dot product folded.Imported from Marvisak/VIctory. Originally posted by DenRakEiw on 2026-11-25T14:06:36Z.
Review: APPROVED
Verified against
origin/main(940d32f) on Windows 11 + NVIDIA GeForce 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).
Imported from Marvisak/VIctory. Originally posted by steven-seagull27 on 2026-11-25T13:28:48Z.
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.Imported from Marvisak/VIctory. Originally posted by oni-swr on 2026-11-25T13:34:41Z.
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)
upstream-sync referenced this pull request2026-11-25 16:09:14 +00:00
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T15:31:38Z.
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
Imported from Marvisak/VIctory. Originally posted by kotekompotek on 2026-11-25T16:55:14Z.
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.
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-25T23:22:58Z.
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).
upstream-sync referenced this pull request2026-11-26 01:04:15 +00:00
Imported from Marvisak/VIctory. Originally posted by fuzeofficlal on 2026-11-26T18:42:36Z.
Review: CHANGES_REQUESTED
Verified
9ea9e00de2d2a843b0f854e608cadfe250547347merged ontomain(63bcca43) on Windows 11 x64 (NVIDIA GeForce RTX 5090 + WARP + Windows SDKdxil.dll). The signed-byte arithmetic (Lower::dot4_i8andselect_bits(..., true)) and wrapping accumulation are sound, and all tests pass, but there is one algorithmic blocker inknown_int:Blocker
Module::known_inton shared DAG expressions (crates/victory-shader/src/emit/bitcode.rs:326-360):known_i32starts atdepth = 32and recurses into both operands of everyOp::Bin(op, a, b)without memoization. A modest 31-instruction chain that reuses the same value on both sides (e.g., repeatedv_add_nc_u32 v2, v2, v2or unrolled bit-twiddling feedingdot4_i8) triggers2^32 - 1recursive evaluations in a singleknown_i32call, effectively hanging shader translation (dot4_i8can also re-invokeknown_i32(x)per nonzero byte). Memoizing visited values (or capping total visits) eliminates the blowup.Non-blocking note
known_intskipsOp::Cmp(andUDiv/SDiv/URem), so a constant compare feedingSelect(e.g.,s_cmp_eq_u32 1, 1->s_cselect_b32 sN, 0x4000, 0) returnsNoneand still falls through todot4AddPacked, where a driver folding the select could still trigger the 16-bit constant duplication bug.Verified locally (merged onto
63bcca43)emit/lower.rssize: 521,946 B (under the 524,288 B limit);cargo test -p xtask --locked policy::tests::repository_passes_policy:1 passed(asset policy: 191 files ok).cargo clippy --locked -p victory-shader --all-targets -- -D warnings(default and--all-features): clean.emit::bitcode::tests::known_i32_folds_constant_integer_arithmetic:1 passed;emit::tests::constant_color_validates(dxil.dll):1 passed.emit::valu_tests::dot4_takes_every_constant_class_whole(RTX 5090) andfspx_programs_run_with_the_hardwares_dot_products(RTX 5090): passed.emit::valu_tests::(36 passed, 0 failed) andemit::lower::tests::(33 passed, 0 failed) pass on both RTX 5090 (VICTORY_WARP=0) and WARP (VICTORY_WARP=1).Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-27T13:21:17Z.
@fuzeofficlal thanks, good catch. You were right:
known_intwent over every path, so a value used on both sides of each step cost 2^n visits.Fixed in
23d9be48(rebased onto63bcca43):known_i32now goes through a smallKnownwalker with aHashMap<V, (Option<u32>, depth)>. A known value is the same at any depth, and a value that is unknown with d steps left is also unknown with fewer. So a value is only looked at again when the walk reaches it with more depth left than before. Anything the old walk resolved within the 32-step depth limit gives the same result, as long as it stays under the cap below.KNOWN_VISITS) and then calls the value unknown. A wide graph of distinct values stays cheap too. Theop_seland select expressions this exists for take only a few visits.dot4_i8already had the known value of both operands, so it now reuses that for each byte instead of callingknown_i32again.EQ..SLE, signed ones read at the operand's own width) of known values are now known too. Sos_cmp_eq_u32 1, 1→s_cselect_b32 sN, 0x4000, 0folds and takes the by-hand path. Float compares andUDiv/SDiv/URemare still unknown. I left division out because nothing feeding a dot product seems to use it. Happy to add it if you have a case.Regression test
emit::bitcode::tests::known_i32_works_out_a_shared_value_oncecounts visits with a test-only counter instead of timing:add x, xSome(0x8000_0000)None(the first operand is already unknown, so?stops the walk)NoneNone(cap)I ran the "before" column on the old head with a temporary counter. It doubles with each step: 8 → 511, 16 → 131,071, 20 → 2,097,151.
known_i32_folds_integer_comparescovers all ten integer predicates, an i8 signed compare, and the select over each one.Local results (Windows 11; RTX 4060 Laptop, WARP, Intel UHD a78b):
cargo test -p victory-shader --lib, PR vsmain(63bcca43) on the same machine:memory_tests::heap_direct_texture_reads_mark_their_heap_index, which crashes the test process with an access violation onmaintoo): PR 302 passed / 3 failed,main298 / 3. The same three fail on both (stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride,stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,synthetic_tests::gathers_at_a_level_match_the_hardware).VICTORY_WARP=1): PR 298 / 4,main298 / 4, the same foursynthetic_tests.main292 / 10. The difference isvalu_tests::fspx_programs_run_with_the_hardwares_dot_products, which this PR fixes. The other nine fail onmainas well.cargo clippy -p victory-shader --all-targets -- -D warnings, default and--all-features: clean.xtaskpolicy: pass.emit/lower.rsis 521,941 B.AI use: Claude Code (Claude Opus 5.5).
Could you take another look when you have a moment?
Imported from Marvisak/VIctory. Originally posted by KainanP on 2026-11-29T12:06:04Z.
@fuzeofficlal gentle ping for a re-review when you have time. Both points from your review are on the current head
23d9be48, which still merges cleanly onmain63bcca43with green CI:known_int): eachknown_i32query now memoises values per depth and stops after 512 visits.known_i32_works_out_a_shared_value_oncepins it: 31 doublings ofadd x, xtake 32 visits instead of 2^32 − 1.s_cmp_eq_u32 1, 1→s_cselect_b32takes the by-hand path (known_i32_folds_integer_compares).Details and per-adapter results are in my earlier comment. Thanks!
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.Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.