Skip to content

Let the compiler vectorize the filter and permutation helpers - #121403

Merged
Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:bytes64-mask-generic-vector
Sep 23, 2026
Merged

Algunenano merged 8 commits into
ClickHouse:masterfrom
Algunenano:bytes64-mask-generic-vector

Conversation

@Algunenano

@Algunenano Algunenano commented Sep 21, 2026 •

Copy link
Copy Markdown
Member

Several hot helpers were written with SSE2 intrinsics, which pinned them to 16 bytes per step even though the default build targets x86-64-v3, and left AArch64 on scalar code. Written in plain C++ or with generic vector types, the compiler picks AVX2 or AVX-512 and vectorizes the ARM paths too.

  • bytes64MaskToBits64Mask: one generic body replaces the AVX2 and AVX-512 branches and compiles to the same instructions at both levels. NEON keeps its intrinsics, and big-endian keeps the scalar loop because a bitcast of a vector of bits to an integer is endian dependent.
  • countBytesInFilter and countBytesInFilterWithNull: toBits64, a duplicate SSE2-only copy of the mask helper, is removed, and both now count with per-lane byte counters like the NEON path already did. The x86 loop goes from 13 instructions per 64 bytes to under 6.
  • isIdentityPermutation: comparing each element with the previous one carried a dependency across iterations, so it never vectorized. Comparing each element with its own index vectorizes on both architectures.
  • Six files included an x86 intrinsic header without using a single intrinsic.
before after
count filter bytes, 64 KB 69.6 GB/s 83.7 GB/s
count filter bytes, 1 MB 68.9 GB/s 86.5 GB/s
identity check, 64K elements 8485 ns 5699 ns
identity check, AArch64 (emulated) 0.33 s 0.25 s
ORDER BY over 20M sorted rows 0.096 s 0.048 s

Changelog category (leave one):

  • Performance Improvement

Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):

Speed up filtering and sorting: the helpers that build filter masks, count filter bytes and check for an identity permutation now use AVX2 or AVX-512 on x86 instead of SSE2, and are vectorized on ARM where they were not.


Workflow [PR]
Sync PR [sync-upstream/pr/121403]

Version info

  • Merged into: 26.10.1.514-master (included in 26.10 and later)

Algunenano and others added 4 commits September 21, 2026 17:00
A generic vector comparison converted to a vector of bits gives exactly the instructions the
movemask intrinsics produced, `vpcmpeqb` plus `vpmovmskb` at `x86-64-v3` and a single
`vptestmb` plus `kmovq` at `x86-64-v4`, so both branches are gone. It also replaces the scalar
loop on the little-endian architectures that matched neither branch before. NEON keeps its
intrinsics, because the generic form reduces each 16-byte group separately and costs 29
instructions against 19; big-endian keeps the scalar loop, because a bitcast of a vector of
bits to an integer is endian dependent and would reverse the mask.

Removing `immintrin.h` from the header exposed four `_blsr_u64` uses that relied on it leaking.
Every one of them already carried `mask & (mask - 1)` as the non-BMI branch, and Clang compiles
that to `blsr` where the target has BMI, so only the portable expression remains.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`toBits64` was a second, SSE2-only copy of `bytes64MaskToBits64Mask`, and both of its callers
only took the `popcount` of the result. Counting through a bitmask is the weaker algorithm
anyway, so `countBytesInFilter` and `countBytesInFilterWithNull` now use the one the NEON path
already used: a counter per byte lane, widened every 255 blocks. Written with generic vectors it
needs no intrinsics on either architecture, so the hand-written NEON goes away as well.

The x86 loop drops from 13 instructions per 64 bytes to under 6, worth 20 to 37% on the kernel
depending on working set. NEON keeps the exact loop the intrinsics produced. The architectures
that matched neither branch before, and so counted a byte at a time, now get a vector path too,
and there is no endianness concern because nothing is bitcast to an integer.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
Six files pulled in an x86 intrinsic header without using a single intrinsic. Two of them,
`distanceTransposed.cpp` and `MergeTreeIndexVectorSimilarity.h`, carried the include as a
workaround for `simsimd` failing on `__bfloat16`; both translation units are still compiled
with `USEARCH_USE_SIMSIMD` and `USEARCH_CAN_COMPILE_BF16` and build without it.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
The hand-written loop pinned the check to 16 bytes at a time, so an `x86-64-v3` build never used
AVX2, and AArch64 had no vector path at all because comparing each element with the previous one
carries a dependency across iterations. Stating the property directly, that every element equals
its own index, and folding a block into a single value gives one branch per block and lets the
compiler vectorize it on both.

About 1.4x on x86 for a cache-resident permutation, 0.27 s against 0.33 s for AArch64 under
emulation. Sorting 20M already-sorted rows, the worst case since the whole permutation is
scanned, drops from 0.096 s to 0.048 s.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f32e409]

Summary: ✅


AI Review

Summary

This PR replaces several x86-specific intrinsics in the filter-mask/counting helpers and isIdentityPermutation with generic vectorizable code, and adjusts the SimSIMD/usearch bf16 build defines so the include cleanups still compile. I did not find a remaining correctness, compatibility, or metadata issue in the current diff after checking the live code against the existing review threads.

Missing context / blind spots
  • ⚠️ Build (arm_tidy) was still in progress while I reviewed, so I could not verify that target's final compiler output from CI.
Final Verdict

✅ No new findings.

LLVM Coverage Report

Measured on commit f32e409.

Metric Baseline Current Δ
Lines 88.10% 88.10% +0.00%
Functions 91.70% 91.70% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 84/84 (100.00%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Sep 21, 2026
The comments explained the choices by comparing against implementations that were removed in
this branch, which means nothing to a reader of the result.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

✅ AI verdict: no_change — no significant changes across 37 queries analysed

no significant changes detected. K_source=6 K_base=30 flagged=0/65

clickbench

🟢 No significant changes

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: a391287e-9c74-406f-8c03-778d3332eef0
  • MIRAI run: b680d041-0fef-4ab2-ba74-fd66a6808b46
  • PR check IDs:
    • clickbench_1486818_1790164129
    • clickbench_1486824_1790164128
    • clickbench_1486830_1790164128
    • tpch_adapted_1_official_1486841_1790164128
    • tpch_adapted_1_official_1486852_1790164128
    • tpch_adapted_1_official_1486889_1790164129

@clickhouse-gh

clickhouse-gh Bot commented Sep 21, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f32e4091a with master e96f004b9 (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Compile time of recompiled translation units

3661 translation units recompiled, 16936 s compile time in total, 3660 of them have a recent master baseline.

Job report

Algunenano and others added 2 commits September 22, 2026 10:06
On PowerPC the result type of a vector comparison depends on
`-faltivec-src-compat`: the current default warns, and the announced
default returns a scalar, which does not compile. A conversion to a
vector of `bool` is the same `!= 0` per lane and has a fixed type.
The generated code is unchanged on x86-64 and AArch64.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
`__bfloat16` needs `<immintrin.h>`, which SimSIMD includes only from
`SIMSIMD_TARGET_HASWELL` up, so `X86_ARCH_LEVEL` below 3 failed to
compile. Under `SIMSIMD_DYNAMIC_DISPATCH` the bf16 entry points live in
`c/lib.c`, which already sets `SIMSIMD_NATIVE_BF16` to 0, so this only
picks a typedef and emits the same code.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Sep 22, 2026

@Algunenano Algunenano left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a trivial PR removing intrinsics. There are no reported changes in the performance tests, only on local benchmarks, but the "refactor" alone is worth it

Comment thread src/Columns/ColumnsCommon.h Resolved
Comment thread src/Columns/ColumnsCommon.cpp Resolved
Comment thread src/Interpreters/sortBlock.cpp Resolved
Comment thread src/Columns/ColumnsCommon.cpp Resolved
Comment thread src/Columns/ColumnsCommon.cpp Resolved
@Ergus Ergus self-assigned this Sep 22, 2026
Pin the size the mask copy assumes, avoid forming a pointer past the end of the filter,
keep the vector helpers inlined, and restore the early exit on the first element.

Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added the comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. label Sep 22, 2026
@Algunenano
Algunenano added this pull request to the merge queue Sep 23, 2026
Merged via the queue into ClickHouse:master with commit 728fcc7 Sep 23, 2026
176 checks passed
@Algunenano
Algunenano deleted the bytes64-mask-generic-vector branch September 23, 2026 13:49
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Sep 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp-query-execution Runtime execution pipeline, processors, vectorized operators, resource management during execution. pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants