perf(formats): port packed BMI2 BC7 reconstruction with xmem rotation fix #565
No reviewers
Labels
No labels
accessibility
bug
documentation
duplicate
enhancement
good first issue
help wanted
invalid
question
wontfix
No milestone
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
github/lobomfz--VIctory-xmem!565
Loading…
Reference in a new issue
No description provided.
Delete branch "github-bot/gh-565"
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?
@tommiehansen on GitHub · 2026-12-05 09:34 UTC · original
tommiehansen:perf/packed-bc7-bmi2→main· +742 −3 · 2 files · 6 commits · openARM64 follow-up
Native macOS ARM64 fallback CI completed: 52 passed, 0 failed, 7 ignored in release at
5ae3c1bwith the x64 optimization switch requested. The scalar fallback is therefore verified on this ARM64 runner. The originally cancelled macOS job is superseded by this targeted run; x64 measurements below remain unchanged.Summary
Port the packed BMI2 BC7 reconstruction from SovietBearz0r's ReVIctory #55 (09478adbb0557afeee874a2a79c42b79e7805f42), identified in VGLALALA/ReVictory-RDNA3.
VICTORY_BCPACK_FAST_BC7=1for now; no launcher-default change. A CPU-feature check alone does not establish a performance win on all BMI2 CPUs.Verification
Head:
5ae3c1b26f715a039482bff5202e0c08c205bd9e. Pinned CI.The differential test visits all 285 valid shipped codes repeatedly: 9,120 records per dictionary mode, including history references. Existing rotation and synthetic stream tests are also executed with the opt-in path enabled. The original macOS job was cancelled; the subsequent native ARM64 fallback job passed (see above).
Synthetic reconstruction-only benchmark
72,960 records per invocation; 8 balanced ABBA/BAAB blocks (16 timings per arm), warmed tables, medians:
This measures block reconstruction, not rANS unpacking, complete streaming, GPU execution, or FPS. End-to-end AMD testing remains needed before making the path default. No RT or image-quality settings change.
Independent native review and follow-up
Kal0s-design's native Windows review reports zero mismatches or panics across millions of generated records, including dictionary references, all shipped controls, errors and synthetic controls. The original reconstruction benchmark reproduced 5.06×, 5.22× and 5.12× speedups (three release runs, 16 samples/arm). These are block reconstruction timings, not end-to-end decoding or FPS.
Follow-up
aa7ab3badds a direct dictionary-reference test (records at index zero with both references nonzero, both dictionary modes) and a runtime BMI2 fallback inside the safe packed entry point;9bbb2ddtidies formatting. Fresh pinned CI runs native, Linux and ARM64 tests plus paired benchmarks. Historical results refer to5ae3c1b; keep opt-in until streaming evidence exists.Rustfmt follow-up:
5e91341applies all six remaining hunks from the actual rustfmt CI diff, with exact resulting blobdeeb1938; formatting only. Final-head format/test recheck started.Final review follow-up (2026-12-05)
Native paired reconstruction,
9bbb2dd(16 samples per arm; 72,960 records): median 13.532 → 2.659 ms (5.09×, −80.35%); debug/release targeted correctness 3 passed each. Independent follow-up confirms the new dictionary test fails under three deliberately broken dictionary implementations, 55 passed / 0 failed / 8 ignored and zero differential mismatches. Final head5e91341is formatting-only relative to9bbb2dd; format audit passed. This measures BC7 CPU reconstruction, not gameplay.Current upstream SIMD reconciliation — 2026-12-05
Merged current main
8c4e663into the PR branch without rewriting history. Retains #363's AVX2 lane decoder and SIMD BC1–BC7 packing. The BMI2 alternative replaces only BC7 reconstruction whenVICTORY_BCPACK_FAST_BC7=1, the existing accelerated path is supported, and BMI2 is available. The forced scalar setting retains precedence.Final-head CI 47574629850 at
11197f4: 6/6 jobs passed, including native ARM64 fallback, format, public decode activation (default/packed/forced scalar), and 864 exact paired Quick/Standard timings. Current SIMD rather than the old scalar implementation is the performance baseline.All 144 Standard paired blocks faster. Reconstruction covers all 285 shipped control codes in both dictionary modes; full decode uses synthetic mode 6 with identical accelerated lanes. Prior 5× result was against the historical scalar baseline. Opt-in remains; no streaming/gameplay FPS claim. GitHub mergeable=true, draft=false.
@Kal0s-design on GitHub · 2026-12-05 10:13 UTC · original
Verified head
5ae3c1bon native Windows x86_64 with the MSVC toolchain, on an x86-64 desktop CPU with BMI2.Tests first. On the PR, cargo test -p victory-formats gives 54 passed, 0 failed, 8 ignored in debug with the default fallback, and the same 54/8 in release with VICTORY_BCPACK_FAST_BC7=1. origin/main (
8bf4878) gives 52 passed, 7 ignored in both profiles. The difference is exactly the three new x86_64-only tests (two run, the benchmark is ignored), and it also explains the 52/7 on the macOS ARM64 run, where those tests are compiled out. With the env var set, the existing decode(7, ..) tests go through the packed path, so the release run does exercise it beyond the new differential test.To check that the output is identical, I wrote a local differential harness (not pushed) that feeds the same 32-byte BCPack records to bc7() and packed_bc7() and compares the Result and every 16-byte output block. I ran it in release in both dictionary modes.
There were zero mismatches and no panics. The shipped table already spans the whole BC7 control space: modes 0 to 7, all 16 or 64 partitions where the mode has them, all four rotations for modes 4 and 5, and both index-selection values for mode 4.
One gap in the PR's own differential test is worth a note. packed_test_records zeroes the references in round 0 and from round 1 every record index is at least 285, while a reference is at most 255. So pick() never reaches the dictionary, and the "both dictionary modes" row only tests the history path. History::from_dictionary is covered by the harness above (and it matches), but a short stream with non-zero references from record 0 in the test would cover it in CI.
About the rotation fix. This PR does not change rotation() (bcpack.rs:790). It was already on main and only lets modes 4 and 5 read bits 3-4 of the control word. The packed path builds its Control from the same rotation() and bc7_precision(), so its output is identical to main's scalar decoder. The "fix" is a difference from ReVIctory PR 55, which reads ctrl >> 3 & 3 for every mode. In BC7 only modes 4 and 5 have a rotation field, so xmem's reading is the spec-correct one. In practice none of the 285 shipped control words sets bits 3-4 outside modes 4 and 5, so on the shipped table the two readings give the same output. Under xmem's reading the field widths of every shipped and synthetic control sum to 128 bits.
Dispatch looks right. fast_bc7_enabled() (bcpack.rs:814) needs is_x86_feature_detected!("bmi2") and VICTORY_BCPACK_FAST_BC7=1, and it is cached in a OnceLock. The scalar bc7() is unchanged and stays the default. The module and the Tables field are cfg(target_arch = "x86_64"). The unsafe blocks have SAFETY notes (bcpack.rs:837, bc7_fast.rs:179 and 262). A small non-blocking point is that packed_bc7 is a safe fn whose soundness depends on the caller having checked BMI2. Today every caller does (decode() through the gate, and the tests through their own check), but marking it unsafe fn or adding a debug_assert would make that harder to break later.
The benchmark claim reproduces. I ran the ignored packed_bc7_reconstruction_benchmark three times in release, 72,960 records, 16 timings per arm. Taking medians, packed was 5.06x, 5.22x and 5.12x faster than scalar (time change about -80%). That is in line with the 4.67x to 5.10x in the body. Like the body says, this covers only block reconstruction, not rANS, streaming, GPU or FPS. The record counts also check out (285 x 32 = 9,120 and 285 x 256 = 72,960). I also looked at the two linked CI runs. Both come from CI-only commits on the fork that add a workflow on top of main, but the workflows check out
5ae3c1band assert the SHA before running, so they do test this head. The Windows and Linux format jobs and the ARM64 fallback job passed, and the first macOS job shows as cancelled, as the body says.A small style thing is that cargo fmt --check reports 11 hunks in the new code (mostly the one-line test loops and closures), while main is clean. Clippy shows no warnings for the crate. Keeping it opt-in until there is end-to-end data seems right to me.
AI-assisted: yes
@tommiehansen on GitHub · 2026-12-05 12:28 UTC · original
Thanks for the extensive independent testing! The dictionary coverage gap and safe BMI2 entry point are addressed in
aa7ab3b, with formatting cleanup in9bbb2dd. Fresh CI is running. I've added your reconstruction results to the body, scoped to the tested revision.@Kal0s-design on GitHub · 2026-12-05 12:46 UTC · original
Rechecked
9bbb2ddnatively on Windows with MSVC, same setup as before. Compared with5ae3c1bthe diff is the BMI2 guard in packed_bc7, the new dictionary test and formatting. I didn't see any other logic change.The new packed_bc7_matches_actual_dictionary_references test does reach the dictionary now. To make sure, I broke History::from_dictionary locally in three ways (the whole entry zeroed, only the packed indices zeroed, only the endpoints zeroed). The new test failed every time, and the older history test still passed each time, which is the gap from my first comment. I reverted the change afterwards. One small limit is that the references are 1 and 255 with a zero delta, so the test only touches dictionary entries 0 and 254, with endpoints and indices from the same entry. That is enough as a regression guard, and the wider local harness covers the rest.
The safety change looks good to me. packed_bc7 now checks BMI2 itself and falls back to the scalar bc7 when it is missing, so the safe fn no longer depends on its callers. The check is std's cached feature lookup and runs once per call rather than once per block, so it costs nothing measurable. decode_block keeps its target_feature attribute and the SAFETY note is now true locally.
cargo test -p victory-formats gives 55 passed, 0 failed, 8 ignored in debug, and the same 55/8 in release with VICTORY_BCPACK_FAST_BC7=1. That is one more than before, which is the new test. I also re-ran a shorter version of the differential harness in release, with about 300k mixed records per dictionary mode plus the unused-code streams, the edge patterns for all 285 codes and the 2,200 synthetic controls. There were zero mismatches and no panics.
The formatting is settled too. On
9bbb2ddcargo fmt --check still reported 6 hunks in bcpack.rs, and5e91341, which only reformats, clears them. cargo fmt -p victory-formats --check is clean on5e91341with the pinned toolchain.AI-assisted: yes
@tommiehansen on GitHub · 2026-12-06 00:05 UTC · original
Thanks for the thorough dictionary tests and negative controls! :) The PR now also preserves upstream's new SIMD decoder from #363. All six final CI jobs passed at
11197f4, with about 52% lower BC7 reconstruction time and 10.45% lower full synthetic decode time on Windows compared with the current SIMD path.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.