Repository navigation
Add runtime CPU-feature dispatch for the hot batch loops - #1
Merged
Merged
Conversation
Upstream pco has no explicit SIMD: it relies on the compiler autovectorizing its per-batch loops, which only happens when the crate is compiled with the relevant instruction sets enabled (the `build.rs` warning asks for `-C target-feature=+avx2,+bmi1,+bmi2`). That does not work for a portable binary that is compiled once at a fixed baseline micro-architecture level but must run on a range of CPUs: it would never use the wider vector units present at runtime. This adds the same mechanism a portable C/C++ codebase uses (e.g. ClickHouse's `TargetSpecific.h`): the vectorizable loop bodies are compiled several times — once at the crate baseline and once per higher x86-64 level (v3 = AVX2 + BMI1/2 + FMA, v4 = AVX-512 F/BW/CD/DQ/VL) — via `#[target_feature]` trampolines that force-inline a shared `#[inline(always)]` body, and the widest variant the running CPU supports is selected once and cached (`cpu_dispatch`). Dispatched loops: decode `read_offsets` (offset bit-unpacking) and encode `write_short_uints` / `write_uints` / `set_offsets`. On aarch64 the crate baseline already includes NEON, so the baseline body is used directly and the module compiles to nothing; the aarch64 `force_export!` specialization trick is preserved (repointed at the baseline `read_offsets_impl`). Verified at baseline `-C target-cpu=x86-64`: the v3 trampoline of `read_offsets` emits AVX2 (`ymm`) and v4 emits AVX-512 (`zmm`); `set_offsets` likewise. All 128 crate tests + doctests pass unchanged.
alexey-milovidov
added a commit
to ClickHouse/ClickHouse
that referenced
this pull request
Jul 15, 2026
Point contrib/pcodec at https://github.com/ClickHouse/pcodec (the fork of pcodec/pcodec in the ClickHouse organization) instead of a personal fork. The runtime CPU-dispatch patch is merged there via ClickHouse/pcodec#1; the pinned commit is identical in content, so nothing else changes. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
1 task done
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.
Patch carried in the ClickHouse fork so pco can be linked into ClickHouse's portable binary.
Upstream pco has no explicit SIMD — it relies on the compiler autovectorizing its per-batch loops, which only happens when the crate is built with
-C target-feature=+avx2,+bmi1,+bmi2(see thebuild.rswarning). ClickHouse compiles all Rust once at a fixed baseline micro-architecture level for a binary that must run on a range of CPUs, so those wins would be lost and the wider vector units never used at runtime.This adds the same runtime-dispatch mechanism a portable C/C++ codebase uses (cf. ClickHouse's
TargetSpecific.h): each vectorizable loop body is compiled at the crate baseline and again for x86-64-v3 (AVX2 + BMI1/2 + FMA) and x86-64-v4 (AVX-512 F/BW/CD/DQ/VL) via#[target_feature]trampolines that force-inline a shared#[inline(always)]body; the widest variant supported by the running CPU is detected once and cached (newcpu_dispatchmodule).Dispatched loops: decode
read_offsets; encodewrite_short_uints/write_uints/set_offsets. On aarch64 the baseline already includes NEON, so the baseline body is used directly and the module compiles to nothing (the aarch64force_export!specialization trick is preserved).Verified at baseline
-C target-cpu=x86-64:read_offsetsv3 emits AVX2 (ymm), v4 emits AVX-512 (zmm);set_offsetslikewise. All 128 crate tests + 5 doctests pass unchanged.