victory-shader: fold constants and eliminate dead pure ops in bitcode #1715
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1716"
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 bartikele on 2026-11-24T21:11:25Z.
Summary
Verification
Imported from Marvisak/VIctory. Originally posted by catminou4 on 2026-11-24T21:40:01Z.
Review: APPROVED
Tested locally on macOS arm64 (rustc 1.98.1): PR head merged cleanly into current main (
ffe310e), cargo test -p victory-shader --locked → 99 passed, 0 failed. Hardware/valu/waterfall execution tests are #[cfg(all(test, windows))] and run only on Windows; macOS covers lowering/structure unit tests.New bitcode optimize() pass: bounded (4 iterations) constant folding + trivial-phi compression with a step budget, unreachable-block phi pruning, branch folding on constant conditions, then pure DCE. is_pure is conservative (Load/Store/AtomicRmw/CmpXchg and non-ReadNone calls are roots; metadata values and CondBr conditions are roots too), and unused external declarations are dropped with index remapping. +3 macOS tests (99 total).
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T22:55:49Z.
A regression on WARP: RTX 3070 (driver 616.56) plus WARP, Windows 11, debug,
pr1716(ffe310e-based) merged cleanly ontomain70564ae.cargo test -p victory-shader --locked: 201 passed, 11 failed, 2 ignored, the same in 2 of 2 runs.mainalone fails only #1744's 3 (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,a_heap_buffer_read_by_element_takes_the_views_stride,gathers_at_a_level_match_the_hardware). The 8 new ones:bvh_tests::intersections_on_the_gpu_equal_the_reference,…_with_64_bit_pointers_on_the_gpu_…,…_on_nodes_in_title_memory_…valu_tests::image_addresses_of_16_bits_and_clampswaterfall_tests::lanes_sample_the_entry_their_offset_names_in_the_console_waterfall,…_texture_their_arm_loaded_or_picked,…_texture_their_if_else_arm_chose,…_their_own_texture_in_a_waterfall_narrowed_by_v_cmpxEach fails alone too (
--exact), so it isn't one test taking the shared device down for the others. In each, the hardware adapter's half passes (e.g. "hardware: equal (2093 boxes …)"), then the WARP half fails withwarp: dispatch failed: The GPU device instance has been suspended, with nothing logged before it. So the folded DXIL of these programs validates and runs on the NVIDIA driver but removes the WARP device. They share BVH node, 16-bit address and waterfall descriptor arithmetic, which is plausibly where folding changes the IR the most. Update, narrowed to the pure DCE. I put local switches around the parts ofoptimize()and ran three of the eight (intersections_on_the_gpu_equal_the_reference,lanes_sample_the_texture_their_if_else_arm_chose,image_addresses_of_16_bits_and_clamps) alone each time:optimize()variantsimplify_instand noCondBrfolding (loop changes nothing)Calltreated as impureCondBrconditions of unreachable blocks also marked liveCause found.
dxc -dumpbinofimage_addresses_of_16_bits_and_clamps's program with and without the DCE: everything the DCE removes is dead (add x, 0, constant arithmetic, unusedextractvalues). But it removes theextractvalueusers ofReadOnlycalls and keeps the calls, becauseis_puretreats onlyReadNonecalls as removable. The optimised module ends with twodx.op.getDimensionsand fourdx.op.rawBufferLoad.f32calls whose results have no uses; without the DCE each has one. The NVIDIA driver accepts that, and WARP's JIT takes the device down on it.Fix: let the DCE also drop unused calls whose function is
Attr::ReadOnly, since they have no side effects:With exactly that change, the three tests above pass, and the full
cargo test -p victory-shader --lockedis back to 209 passed, 3 failed, 2 ignored on the RTX 3070 plus WARP. Those 3 are #1744's NVIDIA heap-cap failures onmain, so all 8 regressions are gone.AI-assisted (Claude Code). Synthetic tests only.
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-24T23:38:03Z.
Found the cause of the WARP regression, and a one-line fix (details in my comment above): the DCE leaves
ReadOnlydx.opcalls (rawBufferLoad,getDimensions) with no uses after removing theirextractvalues, and WARP fails on those. TreatingAttr::ReadOnlycalls as removable inis_purebrings the full suite back tomain's result on an RTX 3070 plus WARP.Imported from Marvisak/VIctory. Originally posted by akimfomin on 2026-11-25T00:46:26Z.
Independent follow-up on this PR's exact head
758bf7fb. PortBridge generated a live circular-copy fixture from the public compute fixture by alessiodeia and tested a narrow extension to this optimizer for an entry-seeded i32 PHI ring.The one-seed 64-register case went from 66 to 2 PHIs and from 4,168 to 3,724 bytes. Microsoft DXV accepted all 12 A/B outputs. Each two-seed case stayed byte-identical. On RTX 4070 Ti SUPER, all eight before/after 2- and 16-register cases matched all 72 checked output words. Prevalence, compile-time impact, and 64-register GPU execution remain unmeasured.
Patch, opt-in fixture, reproduction and results. This builds on #1716's optimizer; the fixture can also be used on its own. AI-assisted with Codex.
Imported from Marvisak/VIctory. Originally posted by zed6000 on 2026-11-25T01:57:22Z.
Measured offline on the title's shaders (as in #1247): merged onto main
70564aeand compared program by program (victory translate --validate --list,shaders.rpf1.0.2.1, dxil.dll 1.8.2502). This PR gains nothing there and loses 6 programs that validate on main (4 pixel, 2 compute). With the PR, they fail validation with:validator: error: Flags must match usage.(4)validator: error: Internal declaration 'code' is unused.(2)This is separate from upipibi's WARP finding above. The likely cause is that dead-code elimination removes the last use of an operation or a declaration, but the module still declares the matching shader flags, or keeps the now-unused internal global. The validator checks both. Offline counts only; no program names or data.
Imported from Marvisak/VIctory. Originally posted by Marvisak on 2026-11-25T03:35:29Z.
Automated triage for the maintainers.
Thanks for the rebase. On current
main, much of whatModule::optimize(crates/victory-shader/src/emit/bitcode.rs) does is already done when the module is written:Module::livedrops values nothing reads (including unused read-onlydx.opcalls),Module::term/Module::planfold constant branches and leave out unreachable blocks, andModule::keptdrops unused declarations and constant globals (commit286a00e). So the new part is mainly the integer constant folding, the algebraic identities and the trivial-phi collapse. The finallive()+retainpass inoptimizerepeats what the writer already does.What would change the verdict:
main. The 21 % instruction reduction and the 6 programs that failed validation were both measured on940d32f, which is before286a00e. The benefit, and whether any program still fails validation, need to be measured again against today'smain.emit::tests(crates/victory-shader/src/emit/mod.rs), the low-half check now also accepts65535, so it no longer shows that the destination's high half is kept. Please give the test a destination that cannot be folded, so it still checks the-65536mask, rather than accepting either result.optimizeonly folds.I read the folding rules (
simplify_inst: masks per bit width, signed division overflow guarded, float compares left alone) and found no wrong rewrite. The trivial-phi replacement is sound under SSA dominance.CI:
checkpassing.Generated by Claude Code
Imported from Marvisak/VIctory. Originally posted by r4tedx on 2026-11-25T05:28:11Z.
Measured on current
main(940d32f): 21.4 % fewer instructions, and the same 6 programs lostTriage asked for a benefit this can show. I merged head
758bf7fbontomain940d32f(clean merge) together with #2115, which counts the instructions of every emitted shader, and compared the two listings program by program (translate --validate --list --countover the title'sshaders.rpf, dxil.dll 10.0.26100.0).mainmain+ this PRbitcast−22,401,564 (52.4 M → 30.0 M),add−4,778,091,and−3,046,252,extractvalue−2,365,625,icmp−2,074,280,sub−1,465,088,shl−1,342,395. So most of what goes is dead register reads and dead address arithmetic, not folded constants.mainand fail here withFlags must match usage.(4) andInternal declaration 'code' is unused.(2). These are the two reasons @zed6000 found on70564ae; they are still there on940d32f. No program is gained.What this does not show: GPU time. A driver's compiler removes dead code itself, so the likelier gain is pipeline compile time and cache size. I did not measure either.
Local title data, offline, CPU only; counts and validator reasons only. The title was not run.
AI-assisted
Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-25T11:50:29Z.
Rebased onto current main (
940d32f) and addressed all three issues from the reviews (commit ad2fb4):awBufferLoad / getDimensions calls whose extractvalues were eliminated are also removed. All 8 WARP tests (vh_tests, alu_tests::image_addresses_of_16_bits_and_clamps, waterfall_tests) now pass.
Imported from Marvisak/VIctory. Originally posted by upipibi on 2026-11-25T13:32:58Z.
@bartikele Re-run at
bad2fb4(as-is, onmain940d32f), RTX 3070 (616.56) plus WARP, Windows 11, debug: the 8 tests from my WARP report all pass now, 2 runs ofcargo test -p victory-shader --locked.bvh_tests:intersections_on_the_gpu_equal_the_reference, the…_with_64_bit_pointers_on_the_gpu_…and…_on_nodes_in_title_memory_…variants.valu_tests::image_addresses_of_16_bits_and_clamps.waterfall_tests.The suite gives 248 passed, 3 failed, 3 ignored in both runs. The 3 are #1744's NVIDIA failures that
mainhas too (a_heap_buffer_read_where_no_buffer_view_is_reads_zeros,a_heap_buffer_read_by_element_takes_the_views_stride,gathers_at_a_level_match_the_hardware).AI-assisted (Claude Code). Synthetic tests only.
Imported from Marvisak/VIctory. Originally posted by bartikele on 2026-11-25T16:45:15Z.
Rebased onto current main (
67e8ae1, commit436a091), integrating with286a00e:Verified on
67e8ae1:Imported from Marvisak/VIctory. Originally posted by zed6000 on 2026-11-25T18:26:09Z.
Re-measured after the rebase. Head
436a091, merged onto main13f91ca(a clean merge), program by program (shaders.rpf1.0.2.1, dxil.dll 1.8.2502): 0 gained, 0 lost. The 6 programs I reported earlier as failing validation ("Flags must match usage" and "Internal declaration 'code' is unused") now validate.cargo test -p victory-shader --libgives 274 passed and 0 failed on an RX 7900 XTX. Offline counts only; no program names or data.Imported from Marvisak/VIctory. Originally posted by liandraraiane on 2026-11-26T12:49:17Z.
Measured on NVIDIA: what the folding changes in the DXIL, and on the card.
Setup: RTX 3060 12 GB (Ampere), driver 610.88, Windows 11 26200, release. This PR merged onto
main63bcca4(clean), plus two local measurement hooks: one that counts the instructionsModule::writewrites per program, and one that repeats a test's compute dispatch 500 times between GPU timestamps; and an env switch to skipoptimize()so both sides run from one binary.Correctness:
victory-shadersuite with this PR: 302 passed, 3 failed;mainalone: 298 passed, the same 3 failed (the capped-heap failures of #1835 on NVIDIA). No new failure; the 4 new tests pass.DXIL size, over the 1,330 programs the suite translates: instructions written 51,089 → 45,057 (−11.8 %); integer arithmetic −23 %, casts −14 %, compares −6 %, selects −5 %, calls (loads, samples, wave ops) −0.2 %; bitcode bytes −1 %.
mainalready drops unread values and constant branches when it writes (Module::live,Module::term), so the change here is the folding and identities.GPU time: the heaviest programs of the suite (ray/box intersection ~1,090 instructions, title memory at computed addresses ~880, waterfall loops ~370, f16 and gather tests), median of 5 alternated runs, µs per dispatch:
bvh_tests::intersections_on_the_gpu_equal_the_referencebvh_tests::intersections_on_nodes_in_title_memory_…memory_tests::title_memory_is_read_and_written_at_computed_addressessynthetic_tests::relative_moves_in_waterfall_loops_…valu_tests::v_fma_f16_rounds_once_and_preserves_other_halfThose programs do shrink (1,086 → 1,066, 876 → 838, 371 → 334 instructions), but the card's time does not move: NVIDIA's compiler already folds these. So on this card the gain would be smaller modules (and perhaps shorter pipeline compiles, not measured), not GPU time. One block-local count for the next passes: within blocks, 2.1 % of instructions repeat an earlier pure value, 6.5 % of reads repeat an earlier read with no store between, and pure calls almost never repeat.
Not tested: a title; AMD; pipeline creation time.
AI use: Claude Code ran the builds and measurements on this machine and drafted this comment.
Imported from Marvisak/VIctory. Originally posted by andrewpozdnakov7-jpg on 2026-11-28T13:10:26Z.
I combined the exact #1716 head
436a091with #3734e61d843on currentmainin2e12265. Git merged the files cleanly and kept both commits as direct parents. The constant-mask fold runs during emission, followed by your existing optimizer.On Microsoft WARP 1.0.22 with Agility 1.620.2, the combined tree and
mainplus #1716 matched the integer reference and returned the same 6,174 output words. The 12-mask fixture went from 41 to 30 written DXIL instructions and from 2,372 to 2,332 bytes. The two-mask fixture went from 31 to 30 instructions. The version with a second reader stayed at 31 and kept the intermediate result.All 17 bitcode tests passed, with the existing benchmark ignored. Strict all-feature shader Clippy and the asset policy passed. @bartikele, the integration tree is available for merge coordination. Please retain the original Git authors on cherry-pick or rebase. If folding #3734 into another commit, please add
Co-authored-by: andrewpozdnakov7-jpg <[email protected]>.AI-assisted with OpenAI Codex.
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.