Skip to content

Add runtime CPU-feature dispatch for the hot batch loops - #1

Merged
alexey-milovidov merged 1 commit into
mainfrom
clickhouse-runtime-cpu-dispatch
Jul 15, 2026
Merged

alexey-milovidov merged 1 commit into
mainfrom
clickhouse-runtime-cpu-dispatch

Conversation

@alexey-milovidov

Copy link
Copy Markdown
Member

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 the build.rs warning). 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 (new cpu_dispatch module).

Dispatched loops: decode read_offsets; encode write_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 aarch64 force_export! specialization trick is preserved).

Verified at baseline -C target-cpu=x86-64: read_offsets v3 emits AVX2 (ymm), v4 emits AVX-512 (zmm); set_offsets likewise. All 128 crate tests + 5 doctests pass unchanged.

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
alexey-milovidov merged commit 619f4cb into main Jul 15, 2026
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant