feat: shader census counts the translation's own costs per shader hash #570

Open
KainanP wants to merge 3 commits from upstream-pr/570 into main
KainanP commented 2026-11-27 23:46:10 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-27T23:46:10Z.


What

VICTORY_SHADER_CENSUS=<file> already made victory translate append one line per emitted program, with its instruction kinds. The line now also carries what the translator knows to be costly on the host. These are counts only, with no code:

Field What it counts
added_barrier group barriers the translation adds where a console wave64 program runs as two host waves of 32 and they exchange (LDS slot + 2 barriers per exchange, lane reads across halves, the thread ticket)
indexed_select selects of M0-indexed register moves (v_movrels/d/sd_b32): one per register M0 may name, up to the program's VGPR count
wide64 instructions with a 64-bit integer or double result
loops branches back to a block on the depth-first path from the entry (loops) in the written module
lds_bytes groupshared bytes declared (the title's LDS plus the exchange slots)
threads, wave_size thread-group size and the WaveSize the shader requires (0 = none)
c_minmax, c_minmax_k, c_round, c_trig, c_perm, c_pk16, c_legacy console instructions the lowering spells out in several host operations: float min/max/median (_k: with an inline or literal constant), v_rndne_f32, v_sin/cos_f32, v_perm_b32, packed f16 arithmetic, legacy multiplies. Each took 2-6x the GPU time of the closest HLSL in a chain bench on the 4060 Laptop
form= the form a wave64 program was emitted in: as is, without early-outs, local waves (APPROXIMATE), loose LDS (APPROXIMATE), wave-wide branches...
root_cbvs/srvs/uavs, user_dwords, samplers host bindings

The existing kinds (guard_select, clamp, loop_counter, wave_op, barrier, LDS/buffer/texture traffic...) are unchanged. Each line starts with the label, which ends in #<16-digit hash>, the same hash the run log uses. VICTORY_TRANSLATE_ONLY=<hash,...> limits the run to those shaders.

Why

YUYING234's GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop) puts 92 % of the card's time in the title's own commands. Almost all of it is in a handful of compute shaders (3cf39f44…, 82a2ad6d…, 4518151a…, dd24ff46…, and 7b2c3c1d… at 50 ms a dispatch). We cannot look at those shaders and must not. A tester who owns the game can run this and post the counts for those hashes. That tells us which translation cost to attack: wave64 exchanges, indexed-register select chains, loop counters, and so on.

How

  • Two new Tags (Meeting on group_sync barriers, Indexed on movrel selects) are counted in Module::census. Tags only exist while counting and are never written, so the DXIL does not change.
  • census::with(f) turns counting on for one thread. Tests use it, and so can tools that want one program's counts.
  • census::form(): emit notes which retry produced the program.
  • Census::default is now written out, because 33 kinds is past Default for arrays.

The translator's source files change, so the shader cache key changes once (as with any translator change). The translation itself is identical.

Tests

  • census_counts_the_barriers_and_lds_two_host_waves_add: a wave64 program with a lane-mask count, on a 32-lane host, has ≥ 2 added barriers, ≥ 8 LDS bytes and 64 threads. The same program on a 64-lane host, and a variant without the exchange, have 0 and 0.
  • census_counts_indexed_register_selects_and_64_bit_results: v_movrels_b32 v5, v1 in a 16-VGPR program gives exactly 15 indexed selects, and v_lshlrev_b64 gives wide64 ≥ 1. A plain move gives 0 for all three.
  • Both fail before this change: the counts and census::with do not exist.
  • cargo test -p victory-shader --lib on the RTX 4060 Laptop: 340 passed, 6 ignored. cargo check -p victory passes. Full pytest (WARP): 1454 passed, 529 skipped.

Update (2026-11-28): two commits after the first census

YUYING234 posted the census lines for the five hashes on #3505. Two things came out of them:

  • a2b484b4 fix: loops counted forward edges. The census counted every branch to a lower-numbered block. The lowering appends its own blocks (critical-edge blocks, the joins of guarded stores), so their forward branches into the next console block counted as loops: 64-247 for skinning programs that have 1-4 loops (loop_counter is 3 per loop). It now counts the back edges on the depth-first path (Module::back_edges, which single_latches uses). Test: census_counts_loops_not_forward_edges_into_earlier_blocks (a diamond whose edge block was made last, a loop whose latch was made last, and a header numbered after its latch) fails before (left: 1, right: 0).
  • a7767758 feat: c_minmax ... c_legacy (row above), counted from the console program. Test: spelled_out_instructions_by_class.

cargo test -p victory-shader --lib --release on the RTX 4060 Laptop: 371 passed, 24 ignored (with the med3 PR on top). cargo xtask policy: 381 files ok. The translation is unchanged.

AI use: written and tested by Claude Code (Claude Opus 5.5) for KainanP. The tests were run on synthetic programs only; no game shader was used.

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5534582307 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/570). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-27T23:46:10Z. --- ## What `VICTORY_SHADER_CENSUS=<file>` already made `victory translate` append one line per emitted program, with its instruction kinds. The line now also carries what the translator knows to be costly on the host. These are counts only, with no code: | Field | What it counts | | --- | --- | | `added_barrier` | group barriers the translation adds where a console wave64 program runs as two host waves of 32 and they exchange (LDS slot + 2 barriers per exchange, lane reads across halves, the thread ticket) | | `indexed_select` | selects of M0-indexed register moves (`v_movrels/d/sd_b32`): one per register M0 may name, up to the program's VGPR count | | `wide64` | instructions with a 64-bit integer or double result | | `loops` | branches back to a block on the depth-first path from the entry (loops) in the written module | | `lds_bytes` | groupshared bytes declared (the title's LDS plus the exchange slots) | | `threads`, `wave_size` | thread-group size and the `WaveSize` the shader requires (0 = none) | | `c_minmax`, `c_minmax_k`, `c_round`, `c_trig`, `c_perm`, `c_pk16`, `c_legacy` | console instructions the lowering spells out in several host operations: float min/max/median (`_k`: with an inline or literal constant), `v_rndne_f32`, `v_sin/cos_f32`, `v_perm_b32`, packed f16 arithmetic, legacy multiplies. Each took 2-6x the GPU time of the closest HLSL in a chain bench on the 4060 Laptop | | `form=` | the form a wave64 program was emitted in: `as is`, `without early-outs`, `local waves (APPROXIMATE)`, `loose LDS (APPROXIMATE)`, `wave-wide branches`... | | `root_cbvs/srvs/uavs`, `user_dwords`, `samplers` | host bindings | The existing kinds (`guard_select`, `clamp`, `loop_counter`, `wave_op`, `barrier`, LDS/buffer/texture traffic...) are unchanged. Each line starts with the label, which ends in `#<16-digit hash>`, the same hash the run log uses. `VICTORY_TRANSLATE_ONLY=<hash,...>` limits the run to those shaders. ## Why YUYING234's GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop) puts 92 % of the card's time in the title's own commands. Almost all of it is in a handful of compute shaders (`3cf39f44…`, `82a2ad6d…`, `4518151a…`, `dd24ff46…`, and `7b2c3c1d…` at 50 ms a dispatch). We cannot look at those shaders and must not. A tester who owns the game can run this and post the counts for those hashes. That tells us which translation cost to attack: wave64 exchanges, indexed-register select chains, loop counters, and so on. ## How - Two new `Tag`s (`Meeting` on `group_sync` barriers, `Indexed` on movrel selects) are counted in `Module::census`. Tags only exist while counting and are never written, so the DXIL does not change. - `census::with(f)` turns counting on for one thread. Tests use it, and so can tools that want one program's counts. - `census::form()`: `emit` notes which retry produced the program. - `Census::default` is now written out, because 33 kinds is past `Default` for arrays. The translator's source files change, so the shader cache key changes once (as with any translator change). The translation itself is identical. ## Tests - `census_counts_the_barriers_and_lds_two_host_waves_add`: a wave64 program with a lane-mask count, on a 32-lane host, has ≥ 2 added barriers, ≥ 8 LDS bytes and 64 threads. The same program on a 64-lane host, and a variant without the exchange, have 0 and 0. - `census_counts_indexed_register_selects_and_64_bit_results`: `v_movrels_b32 v5, v1` in a 16-VGPR program gives exactly 15 indexed selects, and `v_lshlrev_b64` gives `wide64 ≥ 1`. A plain move gives 0 for all three. - Both fail before this change: the counts and `census::with` do not exist. - `cargo test -p victory-shader --lib` on the RTX 4060 Laptop: 340 passed, 6 ignored. `cargo check -p victory` passes. Full pytest (WARP): 1454 passed, 529 skipped. ## Update (2026-11-28): two commits after the first census YUYING234 posted the census lines for the five hashes on #3505. Two things came out of them: - `a2b484b4` **fix: `loops` counted forward edges.** The census counted every branch to a lower-numbered block. The lowering appends its own blocks (critical-edge blocks, the joins of guarded stores), so their forward branches into the next console block counted as loops: 64-247 for skinning programs that have 1-4 loops (`loop_counter` is 3 per loop). It now counts the back edges on the depth-first path (`Module::back_edges`, which `single_latches` uses). Test: `census_counts_loops_not_forward_edges_into_earlier_blocks` (a diamond whose edge block was made last, a loop whose latch was made last, and a header numbered after its latch) **fails before** (`left: 1, right: 0`). - `a7767758` **feat: `c_minmax` ... `c_legacy`** (row above), counted from the console program. Test: `spelled_out_instructions_by_class`. `cargo test -p victory-shader --lib --release` on the RTX 4060 Laptop: 371 passed, 24 ignored (with the med3 PR on top). `cargo xtask policy`: 381 files ok. The translation is unchanged. _AI use: written and tested by Claude Code (Claude Opus 5.5) for KainanP. The tests were run on synthetic programs only; no game shader was used._ 🤖 Generated with [Claude Code](https://claude.com/claude-code)
LooperSalty commented 2026-11-28 00:15:43 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by LooperSalty on 2026-11-28T00:15:43Z.


Main just took #563 (the VIctory copy moved to Vice-Tay 4f15f09e) and #564 (memory stack), and this PR now conflicts in third_party/victory/crates/victory-shader/src/emit/bitcode.rs, third_party/victory/crates/victory-shader/src/emit/mod.rs. Please rebase on current main, rerun the tests for the files you touch, and force-push.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7230774033 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/570#issuecomment-7230774033). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-28T00:15:43Z. --- Main just took #563 (the VIctory copy moved to Vice-Tay `4f15f09e`) and #564 (memory stack), and this PR now conflicts in `third_party/victory/crates/victory-shader/src/emit/bitcode.rs`, `third_party/victory/crates/victory-shader/src/emit/mod.rs`. Please rebase on current main, rerun the tests for the files you touch, and force-push.
KainanP commented 2026-11-28 00:59:00 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T00:59:00Z.


Rebased on main 132da61 and force-pushed: the head is now a37407f1, one commit, and it shows MERGEABLE.

  • bitcode.rs: Module::tag keeps main's clamp exception: tag == Tag::Clamp || census::counting().
  • mod.rs: census::set_form("as is") now comes after main's new WAVE64-PAIRS-ONLY block, just before the first emit_program.

The translation itself is unchanged. On the RTX 4060 Laptop, cargo test -p victory-shader --lib (with #571 on top) gives 369 passed, 0 failed, 21 ignored, and cargo check -p victory passes.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7231332796 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/570#issuecomment-7231332796). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T00:59:00Z. --- Rebased on main `132da61` and force-pushed: the head is now `a37407f1`, one commit, and it shows MERGEABLE. - `bitcode.rs`: `Module::tag` keeps main's clamp exception: `tag == Tag::Clamp || census::counting()`. - `mod.rs`: `census::set_form("as is")` now comes after main's new `WAVE64-PAIRS-ONLY` block, just before the first `emit_program`. The translation itself is unchanged. On the RTX 4060 Laptop, `cargo test -p victory-shader --lib` (with #571 on top) gives 369 passed, 0 failed, 21 ignored, and `cargo check -p victory` passes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/570:upstream-pr/570'
git switch 'upstream-pr/570'

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.

git switch 'main'
git merge --no-ff 'upstream-pr/570'
git switch 'upstream-pr/570'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/570'
git switch 'upstream-pr/570'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/570'
git switch 'main'
git merge --squash 'upstream-pr/570'
git switch 'main'
git merge --ff-only 'upstream-pr/570'
git switch 'main'
git merge 'upstream-pr/570'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
LooperSalty/AnyXboxX!570
No description provided.