Repository navigation
Conversation
On the !defined(__aarch64__) path of ZSTD_decodeSequence(), dynamic array indexing into seqState->prevOffset[ll0] and seqState->prevOffset[offset] inhibits LLVM SROA on the seqState_t alloca. As a result, modern Clang spills seqState.DStream.bitsConsumed to the stack after every BIT_readBitsFast() and spills/reloads prevOffset[0..2] on every sequence. Mirror the existing #if defined(__aarch64__) path by loading seqState->prevOffset[0..2] into local scalars (prevOffset0, prevOffset1, prevOffset2) and writing them back at function exit. This eliminates all dynamic indexing into seqState_t, allowing Clang to keep DStream.bitsConsumed and prevOffset[0..2] in registers (+11.7% to +17.9% on Clang trunk, +0.7% to +3.0% on Clang 19, neutral to +1.1% on GCC 11 and GCC 15 on AMD EPYC Genoa) while reducing Clang decoder .text size by 2.5 KB.
This was referenced Nov 26, 2026
Author
|
cc @danlark1 |
3 of 4 tasks
3 of 4 tasks
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
First split PR from #4751 (following the feedback in #4751 (review)).
Summary
On the
!defined(__aarch64__)path ofZSTD_decodeSequence()inlib/decompress/zstd_decompress_block.c, small-offset decoding dynamically indexesseqState->prevOffset[ll0]andseqState->prevOffset[offset]. BecauseprevOffset[3]lives insideseqState_talongsideDStream, those variable-offset accesses inhibit LLVM SROA on theseqState_tstack allocation, forcing modern Clang to storeseqState.DStream.bitsConsumedto the stack after everyBIT_readBitsFast()and spill/reloadprevOffset[0..2]on every sequence.This PR mirrors the existing
#if defined(__aarch64__)path (+23 / -10 lines): loadseqState->prevOffset[0..2]into local scalars (prevOffset0,prevOffset1,prevOffset2) at the start ofZSTD_decodeSequence(), update them with constant indices/conditionals, and write them back at function exit.ZSTD_execSequence*.-Wdeclaration-after-statement -Werror).Assembly Evidence (
ZSTD_decompressSequences_bmi2, Clang trunk)Before (
e50cdb0d4) — stack frame0x188bytes,209(%rsp)references,870instructions:After (
pr1-scalar-prevoffset) — stack frame0x118bytes (-112 B),134(%rsp)references (-75),776instructions:seqState.DStream.bitsConsumedstays purely in a register (add %edx,%r9d,add %esi,%r9d, ...) with zero stack stores per bit read.instructions:u(perf stat,-3/-19): GCC 11-1.85% / -1.85%, GCC 15-1.64% / -1.72%, Clang 19+0.39% / +0.43%, Clang trunk-11.04% / -11.47%.Benchmarks (AMD EPYC 9B14 Genoa)
Protocol:
e50cdb0d407f5db512ed377fbee6c9ba473fea8e-O3 -Wall -Wextra -Wdeclaration-after-statement -Werror(identical flags and linkage per pair,HAVE_ZLIB=0 HAVE_LZMA=0 HAVE_LZ4=0 HAVE_THREAD=0, ASLR disabled, isolated pinned core)gcc 11(Debian 11.5.0),gcc 15(Debian 15.2.0),clang 19(Debian 19.1.7),clang trunk(llvm/llvm-project@86801a03ccc8)silesia.tar(sha256: fc60dca8d229df75c4e8bc8ad4c60347ea7770823b97de31cea84352cf462b32) precompressed once by the baseline encoder at-1,-3,-9,-19, and-3 --long=27, plus anextDictworkload (50 × 16 KiB slices ofsilesia.tarcompressed at-3with a 64 KiB trained dictionary).-i0) then 5 interleaved ABBA pairs alternating run order viazstd -b -d -i3(-i2forextDict). Base-vs-base paired noise floor on this host: within[-0.26%, +0.56%]across all four compilers.Median Paired Throughput Delta vs.
e50cdb0d4(with[min, max]spread)-1[+0.25, +0.76][+0.60, +0.68][+0.58, +0.87][+11.55, +12.16]-3[+0.08, +0.50][+0.39, +0.93][+2.28, +2.81][+16.91, +17.52]-9[+0.38, +0.99][-0.01, +0.63][+1.72, +2.75][+17.07, +18.31]-19[+0.30, +1.88][-0.65, +0.80][+1.04, +2.38][+16.81, +18.12]-3 --long=27[-0.51, +0.98][+0.80, +1.06][+2.56, +3.12][+16.40, +17.03]extDict(50×16 KiB)[+0.90, +1.23][+0.01, +0.36][+0.88, +1.29][+7.23, +7.61]Absolute
-3medians (MB/s):gcc 111382.8 -> 1387.8,gcc 151436.6 -> 1445.8,clang 191322.6 -> 1358.1,clang trunk1202.8 -> 1410.6.Code Size Delta (
zstd_decompress_block.o.text).text.textVerification
lib/builds cleanly with-std=gnu90 -Wall -Wextra -Wdeclaration-after-statement -Werror(gcc 11,gcc 15) and-std=c90 -Wall -Wextra -Wdeclaration-after-statement -Werror(clang 19,clang trunk).silesia.taracross all 5 frames (-1,-3,-9,-19,-3 --long=27) and all 50extDictfiles across all four compilers.pointer-overflow(-fsanitize=address,undefined,pointer-overflow -fno-sanitize-recover=all):tests/fuzzer,tests/zstreamtest, andtests/decodecorpuspassed.-m32):tests/fuzzerandtests/zstreamtestpassed.make checkpassed.