perf: M0-indexed register reads as a select tree on M0's bits (up to 7.6x on a 4060 Laptop) #571

Open
KainanP wants to merge 4 commits from upstream-pr/571 into main
KainanP commented 2026-11-28 00:02:32 +00:00 (Migrated from github.com)

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


Stacked on #570. This PR uses its census in one test. Merge #570 first, then this PR is the commits b9adbab8 (the change) and 446fb7a6 (more test cases).

What

v_movrels_b32 (and the source side of v_movrelsd_b32) reads the register M0 names, v[src + M0]. It was lowered as one icmp eq M0, k and one select for every register M0 may name, up to the program's VGPR count. Each select took the one before as its operand, so it was one dependent chain as long as the program's VGPR count.

It is now a tree of selects on M0's bits, log2(count) deep, followed by one M0 < 2^levels select for M0 past the registers. M0 past the registers still reads 0, as before. That is still one select per register, but each read waits for about 8 levels instead of up to 129. The write side (v_movreld) already used independent selects, one per destination register, and is unchanged.

Why

This is part of the follow-up to the GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop). 92 % of the card's time is the title's own compute dispatches, so how we translate expensive constructs matters more than our passes. GCN/RDNA compilers emit v_movrel* for dynamically indexed register arrays. In a loop, the chain was the costliest construct I found in a synthetic bench. Whether the title's slow shaders use it will show in the indexed_select count of #570's census (requested on #3505).

Bench (RTX 4060 Laptop, driver as installed, synthetic program only)

cargo test -p victory-shader --lib movrel_bench -- --ignored --nocapture. The test runs 2048 groups × 64 threads × 256 rounds of acc += v[1 + (round & s2)]. Both the round count and the mask are root constants, so no compiler can unroll the loop or prune the range. The figures are median GPU timestamps over 30 dispatches, and every run checks the sums.

VGPRs Chain (before) Tree (this PR) Gain Same loop, plain move HLSL indexed array (dxc) HLSL switch (dxc)
20 0.385 ms 0.160 ms 2.4× 0.040 ms 0.048 ms 0.086 ms
34 0.645 ms 0.245 ms 2.6× 0.040 ms 0.054 ms 0.094 ms
66 1.250 ms 0.284 ms 4.4× 0.040 ms 0.076 ms 0.132 ms
130 2.565 ms 0.339 ms 7.6× 0.040 ms 0.109 ms 0.239 ms

The tree is now within about 1.4-1.9× of what dxc makes of an HLSL switch. An indexed local array (alloca) would be closer to HLSL, but the bitcode writer has no alloca yet, and storing every candidate register at each read would cost as much as the selects.

Tests

  • an_indexed_read_takes_the_register_m0_names_and_0_past_them (GPU) runs programs whose M0 may name 9, 19 and 129 registers. None is a power of two, and 129 makes the tree 8 levels deep. Each run tries every M0 from 0 to one past the last register (the exact upper bound), the values at, below and past the next power of two, and 1000, 0x80000000 and 0xFFFFFFFF. It compares 64 threads with v[1+M0], or 0 past the program's registers. The console's result past the allocated registers is not specified; 0 is the translation's choice. akimfomin suggested the 9- and 129-register cross-check. It passes on the RTX 4060 Laptop, Intel UHD and WARP, before and after the change, so it guards correctness.
  • an_indexed_read_compares_m0_bits_not_every_register: the read adds at most 126 (fills it keeps alive) + 3 × 8 + 2 arithmetic instructions. It fails before (256: one compare per register) and passes after (151).
  • movrel_bench (ignored): the table above.
  • cargo test -p victory-shader --lib on the RTX 4060 Laptop: all pass (342 + 7 ignored). Full pytest (WARP): 1454 passed, 529 skipped.

The translator changes, so the shader cache key changes once.

AI use: written, benched and tested by Claude Code (Claude Opus 5.5) for KainanP, on synthetic programs only (no game shader was used).

🤖 Generated with Claude Code

<!-- greyfinch-sync:LooperSalty/AnyXboxX:issue:5534717567 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T00:02:32Z. --- **Stacked on #570.** This PR uses its census in one test. Merge #570 first, then this PR is the commits `b9adbab8` (the change) and `446fb7a6` (more test cases). ## What `v_movrels_b32` (and the source side of `v_movrelsd_b32`) reads the register M0 names, `v[src + M0]`. It was lowered as one `icmp eq M0, k` and one `select` for every register M0 may name, up to the program's VGPR count. Each select took the one before as its operand, so it was one dependent chain as long as the program's VGPR count. It is now a tree of selects on M0's bits, `log2(count)` deep, followed by one `M0 < 2^levels` select for M0 past the registers. M0 past the registers still reads 0, as before. That is still one select per register, but each read waits for about 8 levels instead of up to 129. The write side (`v_movreld`) already used independent selects, one per destination register, and is unchanged. ## Why This is part of the follow-up to the GPU-timed run on Marvisak/VIctory#3505 (4060 Laptop). 92 % of the card's time is the title's own compute dispatches, so how we translate expensive constructs matters more than our passes. GCN/RDNA compilers emit `v_movrel*` for dynamically indexed register arrays. In a loop, the chain was the costliest construct I found in a synthetic bench. Whether the title's slow shaders use it will show in the `indexed_select` count of #570's census (requested on #3505). ## Bench (RTX 4060 Laptop, driver as installed, synthetic program only) `cargo test -p victory-shader --lib movrel_bench -- --ignored --nocapture`. The test runs 2048 groups × 64 threads × 256 rounds of `acc += v[1 + (round & s2)]`. Both the round count and the mask are root constants, so no compiler can unroll the loop or prune the range. The figures are median GPU timestamps over 30 dispatches, and every run checks the sums. | VGPRs | Chain (before) | Tree (this PR) | Gain | Same loop, plain move | HLSL indexed array (dxc) | HLSL switch (dxc) | |---|---|---|---|---|---|---| | 20 | 0.385 ms | 0.160 ms | 2.4× | 0.040 ms | 0.048 ms | 0.086 ms | | 34 | 0.645 ms | 0.245 ms | 2.6× | 0.040 ms | 0.054 ms | 0.094 ms | | 66 | 1.250 ms | 0.284 ms | 4.4× | 0.040 ms | 0.076 ms | 0.132 ms | | 130 | 2.565 ms | 0.339 ms | 7.6× | 0.040 ms | 0.109 ms | 0.239 ms | The tree is now within about 1.4-1.9× of what dxc makes of an HLSL `switch`. An indexed local array (alloca) would be closer to HLSL, but the bitcode writer has no `alloca` yet, and storing every candidate register at each read would cost as much as the selects. ## Tests - `an_indexed_read_takes_the_register_m0_names_and_0_past_them` (GPU) runs programs whose M0 may name 9, 19 and 129 registers. None is a power of two, and 129 makes the tree 8 levels deep. Each run tries every M0 from 0 to one past the last register (the exact upper bound), the values at, below and past the next power of two, and 1000, 0x80000000 and 0xFFFFFFFF. It compares 64 threads with `v[1+M0]`, or 0 past the program's registers. The console's result past the allocated registers is not specified; 0 is the translation's choice. akimfomin suggested the 9- and 129-register cross-check. It passes on the RTX 4060 Laptop, Intel UHD and WARP, before and after the change, so it guards correctness. - `an_indexed_read_compares_m0_bits_not_every_register`: the read adds at most 126 (fills it keeps alive) + 3 × 8 + 2 arithmetic instructions. It **fails before** (256: one compare per register) and passes after (151). - `movrel_bench` (ignored): the table above. - `cargo test -p victory-shader --lib` on the RTX 4060 Laptop: all pass (342 + 7 ignored). Full pytest (WARP): 1454 passed, 529 skipped. The translator changes, so the shader cache key changes once. _AI use: written, benched and tested by Claude Code (Claude Opus 5.5) for KainanP, 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:44 +00:00 (Migrated from github.com)

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


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:7230774409 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571#issuecomment-7230774409). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-28T00:15:44Z. --- 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.
akimfomin commented 2026-11-28 00:26:04 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by akimfomin on 2026-11-28T00:26:04Z.


PortBridge checked the #570 → #571 stack at both exact heads. The two heads combine cleanly with each other, but each conflicts against current main 132da61 in emit/bitcode.rs and emit/mod.rs. Rebase the census layer first, then replay the M0 change. No build was run.

I found Marvisak/VIctory #3630 independently working on the same bit-tree mechanism. Their WARP test has a nine-register case. An independent CPU model also checked all legal indexes at sizes 1–256. Once #570 is rebased, the practical cross-check is a non-power-of-two range such as 9 or 129 and the exact upper boundary, using your expected guest behavior only where it is specified. That keeps the comparison useful without importing either implementation. PortBridge merge-check entry.

The VIctory control now has an independent execution result. On WARP with required DXIL validation, sizes 7, 8 and 9 hit the first and last legal registers, returned zero at the first excluded index, and did not alias on the large index tested. Keep it scoped to the translated-code result on those two VIctory revisions. Exact result and settings.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7230907652 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571#issuecomment-7230907652). Originally posted by [akimfomin](https://github.com/akimfomin) on 2026-11-28T00:26:04Z. --- PortBridge checked the #570 → #571 stack at both exact heads. The two heads combine cleanly with each other, but each conflicts against [current main `132da61`](https://github.com/LooperSalty/AnyXboxX/commit/132da61504cc2c2d086796c468a8ace1759aba22) in `emit/bitcode.rs` and `emit/mod.rs`. Rebase the census layer first, then replay the M0 change. No build was run. I found [Marvisak/VIctory #3630](https://github.com/Marvisak/VIctory/pull/3630) independently working on the same bit-tree mechanism. Their WARP test has a nine-register case. An independent CPU model also checked all legal indexes at sizes 1–256. Once #570 is rebased, the practical cross-check is a non-power-of-two range such as 9 or 129 and the exact upper boundary, using your expected guest behavior only where it is specified. That keeps the comparison useful without importing either implementation. [PortBridge merge-check entry](https://github.com/akimfomin/PortBridge/issues/98). **The VIctory control now has an independent execution result.** On WARP with required DXIL validation, sizes 7, 8 and 9 hit the first and last legal registers, returned zero at the first excluded index, and did not alias on the large index tested. Keep it scoped to the translated-code result on those two VIctory revisions. [Exact result and settings](https://github.com/Marvisak/VIctory/pull/3630#issuecomment-7231030168).
LooperSalty commented 2026-11-28 00:49:04 +00:00 (Migrated from github.com)

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


Nice speedup, and the tree keeps the old semantics (0 when M0 matches no register, including above the tree). Two points:

  1. TRANSLATOR_VERSION. indexed_read in lower.rs changes the emitted DXIL of every shader that uses v_movrels_b32 / v_movrelsd_b32, but victory-shader/src/lib.rs:29 stays at 16, so caches from earlier builds would keep serving the old translation. 17, 18 and 19 are taken by open PRs (#456, #535, #489), so please use 20 (and re-bump if another lands first).
  2. Tests. movrel_tests.rs checks explicit expected values on the host GPU, which covers non-power-of-two counts and out-of-range M0 well. Please also compare against the reference interpreter (AGENTS.md rule 4), cover v_movrelsd_b32's source path, and add a count of at least 129 registers so the 8-level tree and the 1 << levels bound are exercised. Also confirm the test runs on WARP rather than skipping when there is no GPU.
<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7231212789 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571#issuecomment-7231212789). Originally posted by [LooperSalty](https://github.com/LooperSalty) on 2026-11-28T00:49:04Z. --- Nice speedup, and the tree keeps the old semantics (0 when M0 matches no register, including above the tree). Two points: 1. **`TRANSLATOR_VERSION`.** `indexed_read` in `lower.rs` changes the emitted DXIL of every shader that uses `v_movrels_b32` / `v_movrelsd_b32`, but `victory-shader/src/lib.rs:29` stays at 16, so caches from earlier builds would keep serving the old translation. 17, 18 and 19 are taken by open PRs (#456, #535, #489), so please use 20 (and re-bump if another lands first). 2. **Tests.** `movrel_tests.rs` checks explicit expected values on the host GPU, which covers non-power-of-two counts and out-of-range M0 well. Please also compare against the reference interpreter (AGENTS.md rule 4), cover `v_movrelsd_b32`'s source path, and add a count of at least 129 registers so the 8-level tree and the `1 << levels` bound are exercised. Also confirm the test runs on WARP rather than skipping when there is no GPU.
KainanP commented 2026-11-28 00:59:01 +00:00 (Migrated from github.com)

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


Rebased onto the rebased #570 (a37407f1). The change is now b9adbab8, and the head is 446fb7a6, which adds test cases. It shows MERGEABLE. cargo test -p victory-shader --lib on the RTX 4060 Laptop: 369 passed, 0 failed, 21 ignored.

@akimfomin, thanks for the cross-check. an_indexed_read_takes_the_register_m0_names_and_0_past_them now runs programs whose M0 may name 9, 19 and 129 registers. None is a power of two, and at 129 the tree is 8 levels deep. Each run tries:

  • every M0 from 0 to one past the last register, so the exact upper bound and the first index past it;
  • the values just below, at and past the next power of two;
  • 1000, 0x80000000 and 0xFFFFFFFF.

Past the program's registers, the test expects 0. That is the translation's choice; the ISA does not say what the console reads there.

It passes on the RTX 4060 Laptop, Intel UHD and WARP, and also against the old compare chain, so it is a correctness guard. The shape test an_indexed_read_compares_m0_bits_not_every_register still fails on the chain and passes on the tree.

Marvisak/VIctory#3630 works independently toward the same select tree; its nine-register case matches the kind of cross-check above. No code was taken from it.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7231333067 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571#issuecomment-7231333067). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T00:59:01Z. --- Rebased onto the rebased #570 (`a37407f1`). The change is now `b9adbab8`, and the head is `446fb7a6`, which adds test cases. It shows MERGEABLE. `cargo test -p victory-shader --lib` on the RTX 4060 Laptop: 369 passed, 0 failed, 21 ignored. @akimfomin, thanks for the cross-check. `an_indexed_read_takes_the_register_m0_names_and_0_past_them` now runs programs whose M0 may name 9, 19 and 129 registers. None is a power of two, and at 129 the tree is 8 levels deep. Each run tries: - every M0 from 0 to one past the last register, so the exact upper bound and the first index past it; - the values just below, at and past the next power of two; - 1000, `0x80000000` and `0xFFFFFFFF`. Past the program's registers, the test expects 0. That is the translation's choice; the ISA does not say what the console reads there. It passes on the RTX 4060 Laptop, Intel UHD and WARP, and also against the old compare chain, so it is a correctness guard. The shape test `an_indexed_read_compares_m0_bits_not_every_register` still fails on the chain and passes on the tree. Marvisak/VIctory#3630 works independently toward the same select tree; its nine-register case matches the kind of cross-check above. No code was taken from it.
KainanP commented 2026-11-28 12:53:40 +00:00 (Migrated from github.com)

Imported from LooperSalty/AnyXboxX. Originally posted by KainanP on 2026-11-28T12:53:40Z.


@LooperSalty, les deux points de ta revue sont traités dans a1df4861 (nouvelle tête ; b9adbab8, le changement lui-même, ne bouge pas).

  1. TRANSLATOR_VERSION = 20. Si une des PR #456, #535 ou #489 passe avant, je remonte le numéro.
  2. Tests. an_indexed_read_takes_the_register_m0_names_and_0_past_them :
    • tourne sur l'adaptateur matériel et sur WARP. Un adaptateur absent est sauté, pas les deux, et le test échoue s'il n'y en a aucun ;
    • couvre aussi la source de v_movrelsd_b32 (19 et 129 registres). v_movrelsd_b32 vh, v1 écrit v[h+M0] = v[1+M0], puis v_movrels_b32 relit, pour chaque M0 dont l'écriture reste dans les registres du programme ;
    • garde 129 registres, soit un arbre de 8 niveaux, M0 jusqu'à la borne exacte, la puissance de deux suivante, 1 << levels et au-delà ;
    • compare avec l'interpréteur de référence (crates/anyxboxx-rdna2-ref, règle 4) pour chaque M0 dans les registres du programme. Le binaire est pris dans ANYXBOXX_RDNA2_REF ou dans le target du dépôt, et le test dit quand il ne le trouve pas. Résultat : 232 programmes par adaptateur, identiques à l'interpréteur sur la RTX 4060 Laptop, sur l'Intel UHD et sur WARP. Au-delà des 256 registres que modélise l'interpréteur, celui-ci refuse ; le test garde alors l'attente explicite (0).

Par ailleurs, la mesure sur la forme de la dispatch lente du titre (#3505 : 80 groupes, deux vagues de 32, donc limitée par la latence) donne, chaîne → arbre : 0,280 → 0,106 ms (20 VGPR), 0,437 → 0,177 (34), 0,83-1,00 → 0,212 (66), 2,932 → 0,243 (130), soit 2,6 à 12×. Le census de YUYING234 compte indexed_select=561 dans cette dispatch (7b2c3c1d, 50 ms). Je lui ai demandé un A/B en jeu.

Assisté par IA (Claude Code) ; tests exécutés sur RTX 4060 Laptop, Intel UHD et WARP.

<!-- greyfinch-sync:LooperSalty/AnyXboxX:comment:7239541445 --> Imported from [LooperSalty/AnyXboxX](https://github.com/LooperSalty/AnyXboxX/pull/571#issuecomment-7239541445). Originally posted by [KainanP](https://github.com/KainanP) on 2026-11-28T12:53:40Z. --- @LooperSalty, les deux points de ta revue sont traités dans `a1df4861` (nouvelle tête ; `b9adbab8`, le changement lui-même, ne bouge pas). 1. **`TRANSLATOR_VERSION` = 20.** Si une des PR #456, #535 ou #489 passe avant, je remonte le numéro. 2. **Tests.** `an_indexed_read_takes_the_register_m0_names_and_0_past_them` : - tourne sur l'adaptateur matériel **et** sur WARP. Un adaptateur absent est sauté, pas les deux, et le test échoue s'il n'y en a aucun ; - couvre aussi la source de `v_movrelsd_b32` (19 et 129 registres). `v_movrelsd_b32 vh, v1` écrit `v[h+M0] = v[1+M0]`, puis `v_movrels_b32` relit, pour chaque M0 dont l'écriture reste dans les registres du programme ; - garde 129 registres, soit un arbre de 8 niveaux, M0 jusqu'à la borne exacte, la puissance de deux suivante, `1 << levels` et au-delà ; - **compare avec l'interpréteur de référence** (`crates/anyxboxx-rdna2-ref`, règle 4) pour chaque M0 dans les registres du programme. Le binaire est pris dans `ANYXBOXX_RDNA2_REF` ou dans le `target` du dépôt, et le test dit quand il ne le trouve pas. Résultat : 232 programmes par adaptateur, identiques à l'interpréteur sur la RTX 4060 Laptop, sur l'Intel UHD et sur WARP. Au-delà des 256 registres que modélise l'interpréteur, celui-ci refuse ; le test garde alors l'attente explicite (0). Par ailleurs, la mesure sur la forme de la dispatch lente du titre (#3505 : 80 groupes, deux vagues de 32, donc limitée par la latence) donne, chaîne → arbre : 0,280 → 0,106 ms (20 VGPR), 0,437 → 0,177 (34), 0,83-1,00 → 0,212 (66), 2,932 → 0,243 (130), soit **2,6 à 12×**. Le census de YUYING234 compte `indexed_select=561` dans cette dispatch (`7b2c3c1d`, 50 ms). Je lui ai demandé un A/B en jeu. _Assisté par IA (Claude Code) ; tests exécutés sur RTX 4060 Laptop, Intel UHD et WARP._
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/571:upstream-pr/571'
git switch 'upstream-pr/571'

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/571'
git switch 'upstream-pr/571'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/571'
git switch 'upstream-pr/571'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/571'
git switch 'main'
git merge --squash 'upstream-pr/571'
git switch 'main'
git merge --ff-only 'upstream-pr/571'
git switch 'main'
git merge 'upstream-pr/571'
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!571
No description provided.