Repository navigation
Optimize countBytesInFilter and countBytesInFilterWithNull for AArch64 NEON - #119555
alexey-milovidov merged 3 commits into
Conversation
…4 NEON Accelerate countBytesInFilter and countBytesInFilterWithNull on AArch64 using direct NEON vector register accumulation: - 64-byte unrolled processing loop using uint8x16_t registers. - Non-zero normalization with vsubq_u8(acc, vcgtq_u8(v, zero)) exploiting modulo-256 arithmetic where - 0xFF == + 1. - For countBytesInFilterWithNull, form boolean masks prior to combining: vandq_u8(vcgtq_u8(filt, zero), vceqzq_u8(null_map)) ensuring strict equivalence to (filt != 0) && (null == 0) for arbitrary non-boolean byte values. - Periodic 16-bit widening via vpadalq_u8 every 255 iterations (16,320 bytes) preventing uint8 lane overflow, flushed via vaddlvq_u16 to 64-bit count. - Fix scalar tail and fallback for countBytesInFilterWithNull to check (*pos != 0 && *pos2 == 0). - Leaves x86 (SSE2) and generic fallback paths unaffected. Performance on Apple Silicon (64 KiB blocks): - countBytesInFilter: ~125 GB/s (latency ~485 ns, ~15x faster than scalar) - countBytesInFilterWithNull: ~62 GB/s filter BW / ~124 GB/s total memory BW Closes: ClickHouse#116073
|
Workflow [PR], commit [24e2cd0] Summary: ✅
AI ReviewSummaryThis PR replaces the AArch64 implementations of Missing context / blind spots
Final Verdict✅ No remaining review findings in the current diff. Merge should still wait for the pending AArch64 CI results because the changed hot path is LLVM Coverage ReportMeasured on commit 24e2cd0.
Changed lines: Changed C/C++ lines covered: 16/17 (94.12%) · Uncovered code |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units7 translation units recompiled, 7 s compile time in total, 7 of them have a recent master baseline. |
…ion test for countIf tail
|
Addressed review feedback in
|
|
🕵 Status pass (2026-09-12), no public commits were needed.
Merge is a human's call. |
|
📊 Cloud Performance Report ✅ AI verdict: 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
|
|
@JoshyDo @alexey-milovidov — changelog category: Why: Ignore if the current category is intentional. |
|
@JoshyDo @alexey-milovidov — title/description suggestions: Description (currently
Ignore if intentional. |
|
@JoshyDo @alexey-milovidov — ClickGap found the following in this PR:
Close anything that's wrong or already addressed. |
|
I just stumbled into this PR when working on removing intrinsics and noticed 2 things:
Having a look |
|
PR with this change (among others): #121403 |
|
Thanks @Algunenano ! Really cool to see the accumulator pattern translate so well to x86, and I love how clean the generic vector implementation turned out in #121403. Awesome follow-up! |
…ull-map-byte-is-a-predicate Conflicts in `ColumnNullable.cpp`, `ColumnVariant.cpp` and `ColumnsCommon.cpp`, all resolved to master's side. Master now reads those null maps as predicates itself: both `applyNullMapImpl` negations were fixed in 990aba5 (ClickHouse#119785), and `countBytesInFilterWithNull` was rewritten with a predicate-correct vector path and scalar tail (ClickHouse#119555 and the follow-up rework), so this branch no longer changes those three sites or their comments. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Optimize
countBytesInFilterandcountBytesInFilterWithNullfor AArch64 NEON.Closes: #116073
Related: #116173
Target branch:
masterSource branch:
aarch64-neon-count-bytes-in-filterChangelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Accelerate
countBytesInFilterandcountBytesInFilterWithNullon AArch64 using direct NEON vector register accumulation, improving filter counting throughput by up to 15x.Detailed Description
Prior to this PR,
countBytesInFilteron AArch64 fell back to scalar byte-by-byte iteration, running at ~8 GB/s. A previous attempt in PR #116173 converted 64 bytes into a 64-bit mask via pairwise additions before popcounting, reaching ~58 GB/s.This PR implements direct NEON in-register vector accumulation:
Processes four 16-byte registers per iteration with
vld1q_u8.vcgtq_u8(v, zero)produces0xFFfor non-zero bytes and0x00for zero bytes. Using uint8 wrapping arithmetic,vsubq_u8(acc, vcgtq_u8(v, zero))adds 1 for every matching byte (0 - 0xFF == +1), completely eliminating expensive bit-packing or popcount instructions.Every 255 iterations (16,320 bytes), lanes are accumulated pairwise into 16-bit registers using
vpadalq_u8, preventing 8-bit lane overflow (max lane count:4 * 510 = 2040 << 65535).Horizontal reduction
vaddlvq_u16flushes each chunk to the 64-bitcount.countBytesInFilterWithNull:Computes
(filt != 0) && (null == 0)by pre-normalizing boolean masks before combining viavandq_u8(vcgtq_u8(filt, zero), vceqzq_u8(null_map)). This avoids incorrect matches on non-boolean filter bytes (such as 2, 42, 255) that raw byte-wisevbicq_u8produced.An empty range (
start == end) returns 0 without touching the pointers; any other invalid precondition (start > end, or a nullfilt/null_mapfor a non-empty range) is a programming error and is caught loudly bychassertinstead of being silently absorbed by a fallback. Loop conditions use pointer distance checksstatic_cast<size_t>(end_pos - pos) >= chunk_size.The scalar tail of
countBytesInFilterWithNullused(*pos & ~*pos2) != 0, which overcounts whenever both bytes are non-zero but their bitwise combination is (for example2 & ~1 == 2). It now reads(*pos != 0 && *pos2 == 0), matching the vectorized paths. This is a genuine correctness fix and affects x86-64 as well; it is pinned by the new stateless test05189_count_bytes_in_filter_with_null.Added
#include <arm_neon.h>under#if defined(__aarch64__) && defined(__ARM_NEON)directly inColumnsCommon.cppfor IWYU compliance.Benchmark Results (Apple Silicon M-series, 64 KiB Blocks)
For
countBytesInFilterWithNull(dual-stream 64 KiB filt + 64 KiB null_map):Verification
(filt_byte, null_byte)combinations across sizes {1, 63, 64, 65, 128}.nullptrinputs (both zero and non-zero ranges), inverted ranges (start > end),start == end, and!null_mappaths under ASan and UBSan without error.Workflow [PR]
Sync PR [sync-upstream/pr/119555]
Version info
26.9.1.1404(included in26.9and later)