Shaders: track GFX10 instruction starts with a bitmap (order-independent decode) #2796

Open
github-bot wants to merge 1 commit from github-bot/gh-2796 into main AGit
Owner

@⁠brainbudt on GitHub · 2026-11-25 20:46 UTC · original
brainbudt:perf/gfx10-decode → main · +272 −11 · 1 files · 1 commits · open

In gfx10::decode, an if/else discovers its else arm after the join. The previous Decoded::Ascending fast path drained into a BTreeMap on the first out-of-order insert and paid tree costs for the rest of the shader.

What changed

  • Replace the BTreeMap and Decoded enum with a byte-start bitmap + Vec<Instruction>, sorting once at the end only when discovery was out of order (crates/victory-shader/src/gfx10/mod.rs).
  • Preserves unaligned far jumps and exact overlap-error precedence and byte offsets.
  • Pure safe Rust, no new dependencies, public API unchanged.

Benchmark on realistic branching CFG (457 instructions, 32 nested if/else diamond loops, first backward insert at 11%; median of 5 alternating release runs)

Host Decode before → after Through SSA (incl. decode) Through DXIL (incl. decode) Decode share of DXIL
macOS ARM64 29.05 → 22.18 µs (-23.6%) 197.73 → 190.88 µs 1,038.25 → 1,119.43 µs 2.80% → 1.98%
Linux x86-64 60.65 → 32.73 µs (-46.0%) 293.93 → 248.83 µs 1,629.08 → 1,545.89 µs 3.72% → 2.12%

Verification

  • All 30 focused GFX10 and IR tests passed on macOS ARM64 and Linux x86-64; 46,867 differential inputs (plus 50,000 randomized/far-jump cases during review) matched the original BTreeMap traversal and downstream SSA output byte for byte.
  • Windows MSVC cargo check -p victory-shader --target x86_64-pc-windows-msvc --tests, all-target Clippy -D warnings, and rustfmt --check passed.

Reviewed with mistral-loop: 1 round with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved.

Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).

**@⁠brainbudt** on GitHub · 2026-11-25 20:46 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796) `brainbudt:perf/gfx10-decode` → `main` · +272 −11 · 1 files · 1 commits · open In `gfx10::decode`, an `if/else` discovers its else arm after the join. The previous `Decoded::Ascending` fast path drained into a `BTreeMap` on the first out-of-order insert and paid tree costs for the rest of the shader. ### What changed - Replace the `BTreeMap` and `Decoded` enum with a byte-start bitmap + `Vec<Instruction>`, sorting once at the end only when discovery was out of order (`crates/victory-shader/src/gfx10/mod.rs`). - Preserves unaligned far jumps and exact overlap-error precedence and byte offsets. - Pure safe Rust, no new dependencies, public API unchanged. ### Benchmark on realistic branching CFG (457 instructions, 32 nested `if/else` diamond loops, first backward insert at 11%; median of 5 alternating release runs) | Host | Decode before → after | Through SSA (incl. decode) | Through DXIL (incl. decode) | Decode share of DXIL | |---|---:|---:|---:|---:| | macOS ARM64 | 29.05 → 22.18 µs (-23.6%) | 197.73 → 190.88 µs | 1,038.25 → 1,119.43 µs | 2.80% → 1.98% | | Linux x86-64 | 60.65 → 32.73 µs (-46.0%) | 293.93 → 248.83 µs | 1,629.08 → 1,545.89 µs | 3.72% → 2.12% | ### Verification - All 30 focused GFX10 and IR tests passed on macOS ARM64 and Linux x86-64; 46,867 differential inputs (plus 50,000 randomized/far-jump cases during review) matched the original `BTreeMap` traversal and downstream SSA output byte for byte. - Windows MSVC `cargo check -p victory-shader --target x86_64-pc-windows-msvc --tests`, all-target Clippy `-D warnings`, and `rustfmt --check` passed. Reviewed with mistral-loop: 1 round with Mistral Large 4 and DeepSeek V4.1 Flash on high. Verdict: approved. Created with gpt-6.2-astra in Codex (orchestrated with Gemini 4 Neon in T3 Code).
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 05:14:27 +00:00
github-bot changed title from Shaders: speed up GFX10 decoding by 57-111% in ascending address order to Shaders: track GFX10 instruction starts with a bitmap (order-independent decode) 2026-11-26 06:30:02 +00:00
github-bot force-pushed github-bot/gh-2796 from c3b74a838f to 57172f6456 2026-11-26 06:30:03 +00:00 Compare
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-25 21:10 UTC · original

Reviewed and benchmarked c3b74a83 on Windows 11 against main (13f91ca).

Code review

  • Decoded::Ascending(Vec<Instruction>) maintains strictly ascending offsets cleanly: contains O(1)-short-circuits above last.offset and binary-searches otherwise; before(at) via partition_point(|i| i.offset < at) matches BTreeMap::range(..at).next_back(); any non-ascending insert drains seamlessly into Unordered(BTreeMap), preserving exact overlap checks and visit semantics.
  • Merge-tree: clean against 13f91ca, #2761, #2765, #2771, #2789, #2793, and #2798; conflicts in gfx10/mod.rs with #2686 (decode_with_flows).

Verification & same-machine --release benchmark (rustc 1.98.1 MSVC)

  • cargo test --locked -p victory-shader --lib -- gfx10::: 19 passed, 0 failed, 1 ignored (including differential checks vs reference_decode); ir::: 9 passed, 0 failed.
  • cargo clippy --locked -p victory-shader --all-targets -- -D warnings (default & --features host): 0 warnings; cargo xtask policy: 184 files ok.
  • gfx10_decode_throughput (--release, same machine vs 13f91ca): 13.018 M inst/s (86.73 MB/s) -> 19.131 M inst/s (127.45 MB/s) (+47.0% / 1.47x).
**@⁠fuzeofficlal** on GitHub · 2026-11-25 21:10 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#issuecomment-7174717746) Reviewed and benchmarked `c3b74a83` on Windows 11 against `main` (`13f91ca`). ### Code review - `Decoded::Ascending(Vec<Instruction>)` maintains strictly ascending offsets cleanly: `contains` O(1)-short-circuits above `last.offset` and binary-searches otherwise; `before(at)` via `partition_point(|i| i.offset < at)` matches `BTreeMap::range(..at).next_back()`; any non-ascending insert drains seamlessly into `Unordered(BTreeMap)`, preserving exact overlap checks and visit semantics. - Merge-tree: clean against `13f91ca`, `#2761`, `#2765`, `#2771`, `#2789`, `#2793`, and `#2798`; conflicts in `gfx10/mod.rs` with `#2686` (`decode_with_flows`). ### Verification & same-machine `--release` benchmark (`rustc 1.98.1` MSVC) - `cargo test --locked -p victory-shader --lib -- gfx10::`: **19 passed, 0 failed, 1 ignored** (including differential checks vs `reference_decode`); `ir::`: **9 passed, 0 failed**. - `cargo clippy --locked -p victory-shader --all-targets -- -D warnings` (default & `--features host`): **0 warnings**; `cargo xtask policy`: **184 files ok**. - `gfx10_decode_throughput` (`--release`, same machine vs `13f91ca`): `13.018 M inst/s (86.73 MB/s)` -> **`19.131 M inst/s (127.45 MB/s)`** (**+47.0% / 1.47x**).
Author
Owner

@⁠rezioliver-collab on GitHub · 2026-11-25 21:15 UTC · original

On an Apple M1, switching to BTreeMap after 10%, 50%, or 90% of the instructions retained 4.9%, 26.9%, or 59.9%, respectively, of the throughput gain measured on the ascending-order control.

First out-of-order insertion main µs/decode PR µs/decode Throughput gain vs. main Share of control gain retained
None (ascending control) 164.81 64.21 +156.2% 100%
After 100 of 1,000 instructions 164.08 152.95 +7.7% 4.9%
After 500 of 1,000 instructions 164.76 115.99 +42.1% 26.9%
After 900 of 1,000 instructions 165.27 85.08 +93.5% 59.9%

I compared native release builds of main (13f91ca) and this PR (c3b74a8) on macOS 14.7 with Rust 1.98.1. All four synthetic GFX10 shaders had the same 1,000-instruction mix and byte length. Only the branch displacements changed, making the decoder visit two adjacent instructions in reverse order at the selected point. A separate instrumented build confirmed the exact switch points.

Each case had 40 paired runs, alternating which build went first, with 256 warm-up decodes and 1,024 timed decodes per sample. Times are medians. Throughput gains are the median of the per-pair ratios (main time / PR time − 1).

The full ordered instruction lists matched in all four cases. I compared the complete dumps byte for byte, including every instruction’s offset, encoding, opcode, raw words, and resolved name.

**@⁠rezioliver-collab** on GitHub · 2026-11-25 21:15 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#issuecomment-7174834511) On an Apple M1, switching to `BTreeMap` after 10%, 50%, or 90% of the instructions retained **4.9%, 26.9%, or 59.9%**, respectively, of the throughput gain measured on the ascending-order control. | First out-of-order insertion | `main` µs/decode | PR µs/decode | Throughput gain vs. `main` | Share of control gain retained | |---|---:|---:|---:|---:| | None (ascending control) | 164.81 | 64.21 | +156.2% | 100% | | After 100 of 1,000 instructions | 164.08 | 152.95 | +7.7% | 4.9% | | After 500 of 1,000 instructions | 164.76 | 115.99 | +42.1% | 26.9% | | After 900 of 1,000 instructions | 165.27 | 85.08 | +93.5% | 59.9% | I compared native release builds of `main` (`13f91ca`) and this PR (`c3b74a8`) on macOS 14.7 with Rust 1.98.1. All four synthetic GFX10 shaders had the same 1,000-instruction mix and byte length. Only the branch displacements changed, making the decoder visit two adjacent instructions in reverse order at the selected point. A separate instrumented build confirmed the exact switch points. Each case had 40 paired runs, alternating which build went first, with 256 warm-up decodes and 1,024 timed decodes per sample. Times are medians. Throughput gains are the median of the per-pair ratios (`main` time / PR time − 1). **The full ordered instruction lists matched in all four cases.** I compared the complete dumps byte for byte, including every instruction’s offset, encoding, opcode, raw words, and resolved name.
Author
Owner

@⁠Marvisak on GitHub · 2026-11-25 23:22 UTC · original · edited 2026-11-26 07:18 UTC

Automated triage for the maintainers.

Merge candidate. Re-reviewed at 57172f6. This is the order-independent approach suggested last time. gfx10::decode (crates/victory-shader/src/gfx10/mod.rs) now records instruction starts in a byte bitmap and pushes instructions into a Vec, sorting once at the end only if the list is out of order. The BTreeMap and the switch to it are gone.

before for far jumps scans back at most max_bytes (the longest instruction seen so far) and re-decodes the nearest start. Any instruction ending at at must start in that window, so far_jump's prev.offset + len == at check gives the same results. control is now generic over Borrow<Instruction>, and ir.rs still compiles against it. The new tests compare against the old tree decoder kept as a reference: opcode ranges, truncations, branch targets, random streams, overlap error precedence, unaligned far targets. The benchmark is #[ignore].

Reply to the author: thanks. The gain is modest (decode is about 2-4% of translation), but the core change is now small and well covered.

CI: check passing.


Generated by Claude Code

**@⁠Marvisak** on GitHub · 2026-11-25 23:22 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#issuecomment-7177481123) · edited 2026-11-26 07:18 UTC <!-- victory-triage v1 head=57172f6456e74bf6270a83e92080116d3a6e394d verdict=merge-candidate at=2026-11-26T07:18:47Z --> Automated triage for the maintainers. **Merge candidate.** Re-reviewed at `57172f6`. This is the order-independent approach suggested last time. `gfx10::decode` (`crates/victory-shader/src/gfx10/mod.rs`) now records instruction starts in a byte bitmap and pushes instructions into a `Vec`, sorting once at the end only if the list is out of order. The `BTreeMap` and the switch to it are gone. `before` for far jumps scans back at most `max_bytes` (the longest instruction seen so far) and re-decodes the nearest start. Any instruction ending at `at` must start in that window, so `far_jump`'s `prev.offset + len == at` check gives the same results. `control` is now generic over `Borrow<Instruction>`, and `ir.rs` still compiles against it. The new tests compare against the old tree decoder kept as a reference: opcode ranges, truncations, branch targets, random streams, overlap error precedence, unaligned far targets. The benchmark is `#[ignore]`. **Reply to the author:** thanks. The gain is modest (decode is about 2-4% of translation), but the core change is now small and well covered. CI: `check` passing. --- _Generated by [Claude Code](https://claude.ai/code)_
Author
Owner

@⁠brainbudt on GitHub · 2026-11-26 05:29 UTC · original

Note

🤖 Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt

Replaced the BTreeMap fallback with an order-independent byte-start bitmap + Vec<Instruction> (sorting once at the end only if out of order), and re-benchmarked on a 457-instruction shader with 32 nested if/else diamond loops (first backward insert at 51/457, recurring in every loop):

  • Decode time: 29.05 → 22.18 µs (-23.6%) on ARM64; 60.65 → 32.73 µs (-46.0%) on x86-64.
  • Share of full DXIL translation: 2.80% → 1.98% (ARM64) and 3.72% → 2.12% (x86-64); total through-DXIL time moved -5.1% on x86-64 and was noisy on ARM64, so the claim is scoped strictly to decoding.
  • Verified byte-identical decode and SSA output across 46,867 differential inputs (ascending, diamonds, loops, overlaps in both discovery orders, unaligned far jumps, truncated/malformed inputs).
**@⁠brainbudt** on GitHub · 2026-11-26 05:29 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#issuecomment-7183536912) > [!NOTE] > 🤖 **Gemini 4 Neon (with GPT-6.2 Astra, Mistral Large 4, and DeepSeek V4.1 Flash) responding on behalf of brainbudt** Replaced the `BTreeMap` fallback with an order-independent byte-start bitmap + `Vec<Instruction>` (sorting once at the end only if out of order), and re-benchmarked on a 457-instruction shader with 32 nested `if/else` diamond loops (first backward insert at 51/457, recurring in every loop): - **Decode time:** `29.05 → 22.18 µs` (**-23.6%**) on ARM64; `60.65 → 32.73 µs` (**-46.0%**) on x86-64. - **Share of full DXIL translation:** `2.80% → 1.98%` (ARM64) and `3.72% → 2.12%` (x86-64); total through-DXIL time moved `-5.1%` on x86-64 and was noisy on ARM64, so the claim is scoped strictly to decoding. - Verified byte-identical decode and SSA output across 46,867 differential inputs (ascending, diamonds, loops, overlaps in both discovery orders, unaligned far jumps, truncated/malformed inputs).
Author
Owner

@⁠kotekompotek on GitHub · 2026-11-25 21:17 UTC · original · reviewed

Verified order-equivalent: Decoded::before (partition_point + last) returns exactly the greatest-offset-below-at instruction the range(..at).next_back() did; contains on Ascending is last-offset-or-below && binary_search, matching contains_key on a set built the same way; insert dedups via the preceding contains guard, so the equal-offset case can't reach the push arm. Final into_values ordering is identical to the old BTreeMap output. The ascending fast path pays off whenever discovery is mostly in-order — which is the common case for linear code with forward branches. LGTM.

**@⁠kotekompotek** on GitHub · 2026-11-25 21:17 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#pullrequestreview-6065108495) · **reviewed** Verified order-equivalent: `Decoded::before` (partition_point + last) returns exactly the greatest-offset-below-`at` instruction the `range(..at).next_back()` did; `contains` on Ascending is `last-offset-or-below && binary_search`, matching `contains_key` on a set built the same way; `insert` dedups via the preceding `contains` guard, so the equal-offset case can't reach the push arm. Final `into_values` ordering is identical to the old BTreeMap output. The ascending fast path pays off whenever discovery is mostly in-order — which is the common case for linear code with forward branches. LGTM.
Author
Owner

@⁠futurepro62 on GitHub · 2026-11-26 09:54 UTC · original

NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head 57172f6 (on its base 63bcca4).

Tested

  • NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), dxil.dll from Windows SDK 10.0.26100 (the loader's default), debug.
  • cargo build --workspace --locked, then cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware: 871 passed, 0 failed, 11 ignored.
  • The new tests ran and passed: matches_tree_decoder_across_encodings_and_truncations, matches_tree_decoder_across_opcode_ranges, matches_tree_decoder_across_branch_targets, matches_tree_on_nested_loops_and_random_streams, preserves_overlap_error_precedence_and_unaligned_far_targets.
  • --skip leaves out tests that fail on main 63bcca4 on this card too, not because of this PR: heap_direct_texture_reads_mark_their_heap_index ends the victory-shader test process with 0xc0000005 (#2571), so without the skip the rest of that crate never reports; dynamic_slots_are_held_until_released, lists_share_a_table_and_copies_keep_their_views and the two stride_tests hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); gathers_at_a_level_match_the_hardware fails on lane 0 (#1744).

Not tested

  • AMD (the tests take the default adapter), a release build, the title (test run only).

AI use

  • Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
**@⁠futurepro62** on GitHub · 2026-11-26 09:54 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#issuecomment-7188642124) NVIDIA full-suite run, to go with the reports above by fuzeofficlal, since none of them names a GPU run. Tested exact head `57172f6` (on its base `63bcca4`). **Tested** - NVIDIA GeForce RTX 5070 Ti 16 GB (Blackwell, 32-lane waves only, mesh shaders), driver 621.40, Ryzen 7 9800X3D, Windows 11 Home 26300, Rust 1.98.1 MSVC (pinned), `dxil.dll` from Windows SDK 10.0.26100 (the loader's default), debug. - `cargo build --workspace --locked`, then `cargo test --workspace --locked --no-fail-fast -- --test-threads=4 --skip emit::memory_tests::heap_direct_texture_reads_mark_their_heap_index --skip descriptor::host::tests::dynamic_slots_are_held_until_released --skip memory::tests::lists_share_a_table_and_copies_keep_their_views --skip emit::stride_tests::a_heap_buffer_read_by_element_takes_the_views_stride --skip emit::stride_tests::a_heap_buffer_read_where_no_buffer_view_is_reads_zeros --skip emit::synthetic_tests::gathers_at_a_level_match_the_hardware`: **871 passed, 0 failed, 11 ignored**. - The new tests ran and passed: `matches_tree_decoder_across_encodings_and_truncations`, `matches_tree_decoder_across_opcode_ranges`, `matches_tree_decoder_across_branch_targets`, `matches_tree_on_nested_loops_and_random_streams`, `preserves_overlap_error_precedence_and_unaligned_far_targets`. - `--skip` leaves out tests that fail on main `63bcca4` on this card too, not because of this PR: `heap_direct_texture_reads_mark_their_heap_index` ends the `victory-shader` test process with `0xc0000005` (#2571), so without the skip the rest of that crate never reports; `dynamic_slots_are_held_until_released`, `lists_share_a_table_and_copies_keep_their_views` and the two `stride_tests` hit the 1,000,000-view heap cap of NVIDIA drivers (#1835, #2317); `gathers_at_a_level_match_the_hardware` fails on lane 0 (#1744). **Not tested** - AMD (the tests take the default adapter), a release build, the title (test run only). **AI use** - Claude Code (Claude Opus 5.5) ran the tests and wrote this report. I checked it before posting.
Author
Owner

@⁠fuzeofficlal on GitHub · 2026-11-26 11:01 UTC · original · approved these changes

Reviewed 57172f64 and verified locally: replacing the BTreeMap in gfx10::decode with a byte-start bitmap, an unsorted Vec<Instruction> sorted once at completion, and a bounded backward bitmap scan up to max_bytes preserves unaligned far_jump targets, instruction uniqueness, and overlap/decode error precedence. The five differential test suites cover unaligned jumps, out-of-order overlaps, and error precedence thoroughly.

Verified locally on Windows 11 x64 (57172f64):

  • rustfmt --edition 2024 --check crates/victory-shader/src/gfx10/mod.rs: clean.
  • cargo run --locked -p xtask -- policy (191 files ok) and cargo clippy --workspace --all-targets -- -D warnings (default and --all-features): clean.
  • gfx10::tests (21 passed, 1 ignored), ir::tests (9 passed), structure::tests (13 passed, 1 ignored), emit::lower::tests (33 passed): all passed.
**@⁠fuzeofficlal** on GitHub · 2026-11-26 11:01 UTC · [original](https://github.com/Marvisak/VIctory/pull/2796#pullrequestreview-6072968499) · **approved these changes** Reviewed `57172f64` and verified locally: replacing the `BTreeMap` in `gfx10::decode` with a byte-start bitmap, an unsorted `Vec<Instruction>` sorted once at completion, and a bounded backward bitmap scan up to `max_bytes` preserves unaligned `far_jump` targets, instruction uniqueness, and overlap/decode error precedence. The five differential test suites cover unaligned jumps, out-of-order overlaps, and error precedence thoroughly. Verified locally on Windows 11 x64 (`57172f64`): - `rustfmt --edition 2024 --check crates/victory-shader/src/gfx10/mod.rs`: clean. - `cargo run --locked -p xtask -- policy` (`191 files ok`) and `cargo clippy --workspace --all-targets -- -D warnings` (default and `--all-features`): clean. - `gfx10::tests` (21 passed, 1 ignored), `ir::tests` (9 passed), `structure::tests` (13 passed, 1 ignored), `emit::lower::tests` (33 passed): all passed.
This pull request can be merged automatically.
You are not authorized to merge this pull request.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin '+refs/pull/2796/head:github-bot/gh-2796'
git switch 'github-bot/gh-2796'

Merge

Merge the changes and update on Forgejo.
git switch 'main'
git merge --no-ff 'github-bot/gh-2796'
git switch 'github-bot/gh-2796'
git rebase 'main'
git switch 'main'
git merge --ff-only 'github-bot/gh-2796'
git switch 'github-bot/gh-2796'
git rebase 'main'
git switch 'main'
git merge --no-ff 'github-bot/gh-2796'
git switch 'main'
git merge --squash 'github-bot/gh-2796'
git switch 'main'
git merge --ff-only 'github-bot/gh-2796'
git switch 'main'
git merge 'github-bot/gh-2796'
git push origin 'main'
This discussion has been locked. Commenting is limited to contributors.
No reviewers
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.

Dependencies

No dependencies set

Reference
github/Marvisak--VIctory!2796
No description provided.