bcpack: decode the lanes of one stream side by side (fused scalar loop and AVX2) #2105
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/2112"
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 R0Y00/Xbox2Windows. Originally posted by NiiRoZz on 2026-11-25T00:37:47Z.
Change
The startup storage stand-in decodes BCPack DirectStorage streams with
decode_lanes(
src/startup_stubs/bcpack_decoder.h). This PR makes that decode faster without changing a singleoutput byte or accept/reject decision.
for a byte depends on its rANS state, so on every symbol decoded so far. A first pass that only finds
the byte positions costs as much as a full decode. Within one cycle the lanes are independent apart
from the prefix count of byte requests, so this PR runs them side by side.
decode_lanes_scalarruns both phases of each cycle in one pass over the lanes, keeps f and cf fromthe decoding step, and counts the model segment without branches. It is used when AVX2 is missing.
decode_lanes_avx2holds each per-lane field of all 16 lanes in two 8-lane registers. The bytearbitration is a mask computation: a clear-lowest-bit loop rather than pdep, which is microcoded on
AMD before Zen 3, plus a vpermd rank table. Header bytes, fragment ends and flushes go through the
original
phase1for that lane.decode_lanes_referenceand is used only by tests. Thedecode_lanessignature is unchanged.tests/bcpack_encoder.his a test-only encoder: per-lane reverse rANS plus the reference cycle loop tolearn the stream order. It makes synthetic BC1, BC7 and other lane layouts of any size: short and long
headers, narrowing, trimmed flushed fragments and unflushed fragments.
Overlapping work:
decode_lanesand edits its body. Thisbranch renames that body to
decode_lanes_reference, so the two conflict inbcpack_decoder.h. Toreconcile: put the report on
decode_lanes_reference, and haveinspect_bcpackcall the referenceloop. It is a diagnostic path, so it does not need the fast path, and the reports stay exact. The fast
paths give the same decision on every input tested.
12-lane tables are among the lane tables this branch tests.
Verification
Build and machine: commit
a0ff8ead, Windows 11 26300, Ryzen 7 9800X3D (Zen 5), MSVC, Ninja.Result: 34 passed, 1 skipped, 0 failed out of 35. The bcpack tests among them:
bcpack_encoder_roundtrip,bcpack_encoder_window_control,bcpack_lanes_{scalar,avx2},bcpack_lanes_python_{scalar,avx2}, their*_drain_controlcontrols,bcpack_prefix_and_mip_tail,bcpack_rans_symbolsandbcpack_tables.Capability skips:
bc7_mode6_multiblock_warpskips becauseBCPACK_REFERENCE_TOOLSis not set. It is unrelated tothis change.
_hardwaretests were not run.bcpack_lanes_avx2and its variants return 77 on a CPU or OS withoutAVX2, and in a clang-cl build, where the SIMD code is not compiled; the skip message names which. A
machine without AVX2 therefore passes the scalar tests and reports the AVX2 tests as skipped.
--fullruns every truncation and every single-bit flip of each stream: 423,874 streams, 0mismatches. The reference accepts 48,004 of the damaged streams, and both fast paths match it on each
one, destination bytes included.
--bench --largedecodes 4 MiB destinations (524,288 BC1 or 262,144 BC7 blocks). The paths areinterleaved, the warm-up round is dropped, the best of 7 is kept and the output checksums must match.
Times are
decode_bcpackms, with lane-decode ns per symbol in parentheses:Measured on one CPU (Zen 5); CPUs with slow gathers are unmeasured.
Evidence and follow-up
repository), plus synthetic streams generated at test time by
tests/bcpack_encoder.h. Its reverserANS equation comes from the vendored bcpack-rs code (MIT OR Apache-2.0); the credit is kept.
streams. The check covers the decision, the symbols (also on rejection) and every
decode_bcpackdestination byte. Inputs end at a no-access page and the destination sits between a canary and a
no-access page.
data it did not produce:
synthetic_11.jsonandsynthetic_12.json;tests/bcpack_mode6_vector.pyandtests/startup_stub_consumer.cpp;tests/bcpack_lanes_python_check.py).bcpack_encoder_window_controlinterleaves with a 7-byte cycle budget.*_drain_controlbuilds take a wrong DRAIN step at the home state. Each control passes only onits expected output: the C++ control must fail on both the vector symbols and the reference sweep,
and the Python control must report that Python accepts what C++ rejects.
fragment (v12 averages about 72), so the benchmark uses at most 150 and at most 4096.
docs/experiments.md, section "BCPack lane decoder paths", has the reproduction commandsand results.
prtest preset does not run the new tests; the full dev and release CTest jobs do.decode_bc7_blocks) is unchanged and now takes about half of the BC7time.
Checklist
This change was AI-assisted (Claude Code). An independent review agent checked it before this PR.
🤖 Generated with Claude Code
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:05:56Z.
Review: COMMENTED
Review of head
a0ff8ead6003cbf8b3ea744e8484b6231588277b(comment only, no approval).Scope:
decode_lanesfused scalar loop + AVX2 path, keptdecode_lanes_reference, test-only encoder, lane/python checks, CMake wiring, docs/experiments.md. Diff: 7 files, +1497/-4.Run locally (Linux):
git diff --check origin/main...HEAD: clean.python3 tests/docs_links_check.py: pass (18 files, 393 links).python3 tests/bidi_controls_check.py: pass (1417 files, no bidi controls).bcpack_encoder_checkbuilt with g++ -std=c++17 -O2 plus a localintrin.hshim (only_BitScanReverse/_BitScanForward):240 streams, 419030 bytes, 535960 symbols, 0 failed.--mutate windowcontrol exits non-zero as the CMake test expects.decode_lanes_scalar(served < 8->served < 7): encoder round-trip fails 240/240. Killed.decode_lanes_scalar(will = p0 < 0x1000000->p0 <= 0x1000000): encoder round-trip still0 failed. It survives this check. It is probably only caught by the reference-vs-path sweep inbcpack_lanes_check, or it is an equivalent mutant. I could not tell on Linux (see below).git checkout -- .).Not runnable on Linux:
bcpack_lanes_check.cppincludeswindows.h, and the AVX2 path is gated on_MSC_VER && _M_X64, so the scalar-vs-reference sweep, the python anchor check and the AVX2 path were not run here. CI showsBuild and test on Windowspass,Assemble and translate gfx1013 fixturespass,Run platform-independent Python checks on Linuxpass.Build and test on Windows at /O2was skipped. No separate Windows run was done by me.Findings:
bcpack_lanes_scalarkills ap0 <= 0x1000000change in thewillcondition (bcpack_decoder.hindecode_lanes_scalar). If it does not, add a stream that hitsp0 == 0x1000000exactly, or say why it cannot occur./O2Windows job was skipped. The fast paths depend on invariants that optimization can expose, so a /O2 run on the final head would be useful.AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by tedin7 on 2026-11-25T02:48:24Z.
Review: COMMENTED
Re-review of head
20f9814328e6f49b89f4eece8a620b675c341adc(comment only, no approval). Previous review was ona0ff8ead6003cbf8b3ea744e8484b6231588277b.What changed since the last review: only merges of main.
git diff a0ff8ead HEADon the PR's own files shows no change tosrc/startup_stubs/bcpack_decoder.h,tests/bcpack_encoder.h,tests/bcpack_encoder_check.cpp,tests/bcpack_lanes_check.cpportests/bcpack_lanes_python_check.py. TheCMakeLists.txtanddocs/experiments.mddifferences come from main (BCPack registrations are preserved). Author comment: local Windows Release passed all ten focused BCPack CTests (scalar and AVX2 vs reference and Python, short-drain controls).Run locally (Linux, on this head):
git diff --check origin/main...HEAD: clean.python3 tests/docs_links_check.py: pass (18 files, 401 links).python3 tests/bidi_controls_check.py: pass (1457 files, no bidi controls).Build and test on Windowspass,Assemble and translate gfx1013 fixturespass,Run platform-independent Python checks on Linuxpass.Build and test on Windows at /O2is still skipped. Mergeable: yes.served < 8killed 240/240; mutation 2p0 <= 0x1000000survived the encoder round-trip).Status of previous findings:
willboundaryp0 == 0x1000000indecode_lanes_scalar,bcpack_decoder.h:188): NOT addressed. No new test and no explanation. The author's Windows run does not say whether the lanes sweep kills this mutant. Please either add a stream hittingp0 == 0x1000000within_rem > 0andib_full, or say why it is unreachable or equivalent.No new findings. Neither open item blocks the change.
AI-assisted; commands listed were executed locally.
Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-25T02:43:46Z.
I kept the storage reuse and all BCPack test registrations. Local Windows Release passed all ten focused BCPack CTests, including the scalar and AVX2 comparisons against the reference and Python, and the short-drain controls. No capability skips.