Skip to content

Search long integer slices in 512-byte chunks on AArch64 in hasAll and hasAny - #122871

Merged
alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
chiranmoyf:dev-sliceHasImplAnyAll-sve
Nov 7, 2026
Merged

alexey-milovidov merged 3 commits into
ClickHouse:masterfrom
chiranmoyf:dev-sliceHasImplAnyAll-sve

Conversation

@chiranmoyf

@chiranmoyf chiranmoyf commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

hasAll(first, second) and hasAny(first, second) on integer arrays look up each value of second in first with a linear search, sliceContains.

The search compares 128 bytes of first with the value (i.e., 32 Int32 or 16 Int64 elements), then checks whether any of them matched, so it can stop as soon as the value is found. On AArch64, that check is relatively expensive. NEON has no single instruction that tests a vector for any set lane, like vptest on x86, so the comparison results have to be reduced to one flag first. With one check per 128 bytes, a large part of the time goes into these checks rather than into the comparisons.

For long arrays, it is cheaper to compare more elements before each check. This change checks once per 512 bytes.

Change

On AArch64, an array of at least 136 4-byte or 68 8-byte integers is searched in chunks of 512 bytes by a plain loop, which Clang vectorizes to 8 or 16 NEON comparisons per iteration, with one check per chunk.

  • The first 32 bytes are checked on their own, so a value near the front is still found quickly.
  • The last chunk overlaps the previous one, so there is no scalar tail.
  • Larger chunks were not faster and do more work when the value is found early.

05493_has_all_has_any_chunked_search places a value at every position of arrays with lengths around the chunk boundaries. It includes a last chunk that overlaps the previous one, a value that is missing, and a value at a null. It covers Int32, UInt32, Int64 and UInt64, with and without nulls.

Shorter arrays of 1 and 2-byte types, non-integer types, and x86 are unchanged. On x86, the chunk loop was slower than the current code.

Performance

hasAll and hasAny are up to 2x faster on AArch64 for long arrays of 32 and 64-bit integers.
tests/performance/has_all.xml and tests/performance/has_any_has_all_nullable.xml on Graviton 4 (c8g.4xlarge): pastila

Changelog category (leave one):

  • Performance Improvement

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

hasAll and hasAny are up to 2x faster on AArch64 for long arrays of 32 and 64-bit integers.

This PR is a joint contribution by:

Abhishek Jain (@abhijain1204fujitsu)


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

Version info

  • Merged into: 26.11.1.1623-master (included in 26.11 and later)

@rienath rienath self-assigned this Sep 29, 2026
@rienath rienath added the can be tested Allows running workflows for external contributors label Sep 29, 2026
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [cf3b328]

Summary: ✅


AI Review

Summary

This PR searches long 32- and 64-bit integer arrays in 512-byte chunks on AArch64 for hasAll and hasAny, with regression coverage for chunk boundaries, overlapping tails, missing values, and nulls. No actionable findings in cf3b328404abb419b37f77ebf7186de982b74053. Earlier findings concern implementations that have been removed.

Missing context / blind spots
  • ⚠️ No local ClickHouse build or binary was available. The head-specific CI reports expose no per-test results yet; ARM execution of 05493_has_all_has_any_chunked_search remains unverified. Performance evidence comes from the author's Graviton4 before/after measurements, which cover the replacement implementation; the dashboard runs cover earlier implementations.
Final Verdict

Status: ✅ Approve. No new inline findings or changes to existing threads.

@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... labels Sep 29, 2026
Comment thread src/Common/TargetSpecific.h Resolved
@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026 •

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing cf3b32840 with master 66b092b52 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of 7a516a1ab; 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.

Object file sizes

5 object files changed (+172.81 KiB total), 0 added.

Object file Master PR Δ
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/has_all.cpp.o 5.38 MiB 5.49 MiB +108.46 KiB (+1.97%)
src/Functions/GatherUtils/CMakeFiles/clickhouse_functions_gatherutils.dir/has_any.cpp.o 4.74 MiB 4.80 MiB +64.34 KiB (+1.33%)

719 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only clickhouse-bundle) and not compared.

Compile time of recompiled translation units

30 translation units recompiled, 149 s compile time in total, 30 of them have a recent master baseline.

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 29, 2026

Copy link
Copy Markdown
Contributor

📊 Cloud Performance Report

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

The PR adds ARM SVE runtime detection and SVE-dispatched variants of findExtreme (min/max over numeric columns) and the hasAll integer array search. On x86 builds the new ARM guard is 0, so the only change there is a regrouping of preprocessor blocks that keeps the same behavior. The only flagged query, clickbench Q11 (-15.9%), runs neither code path. Its CPU time is essentially unchanged (1453.6 vs 1462.6 ms), and the PR median of 164 ms sits at the edge of master's current range of 165-212 ms, so it is downgraded to not_sure as run-to-run variance. This run shows no measurable performance effect from the PR.

clickbench

⚠️ 1 inconclusive

Flagged queries (1 of 43)
Query Verdict Baseline median (ms) PR median (ms) Change q-value Hint
⚠️ 11 not_sure 195 164 -15.9% <0.0001 Q11 is a COUNT(DISTINCT) GROUP BY with no min/max or hasAll, so it never reaches the SVE paths this PR adds. CPU time is flat, and the PR median of 164 ms sits at the edge of master's normal range.

Change = percent below ×2; the ratio of medians (×N faster/slower) beyond, where percent understates the scale. q-value = BH-FDR adjusted p; smaller is stronger evidence. MIRAI flags a query when q < fdr_q (default 0.10) — the value the verdict is based on.

tpch_adapted_1_official

🟢 No significant changes

Debug info
  • StressHouse run: c7e85fda-1150-4b87-8342-c622a690df89
  • MIRAI run: 0ea9b31c-0313-45b6-abac-cfd591d595b9
  • PR check IDs:
    • clickbench_2410129_1790696115
    • clickbench_2410135_1790696115
    • clickbench_2410141_1790696115
    • tpch_adapted_1_official_2410151_1790696115
    • tpch_adapted_1_official_2410163_1790696115
    • tpch_adapted_1_official_2410199_1790696115

@Algunenano

Copy link
Copy Markdown
Member

Note that the current goal is to remove intrinsics, not add them. See #123420 (comment)

Let's wait until the code is simplified (and sped up) to decide if we really need dispatching please

@Algunenano

Copy link
Copy Markdown
Member

Extra note: After #123420, it's trivial to dispatch to SVE2 (SVE isn't enough) and speed up things. No intrinsics, just dynamic dispatch and letting the compiler do it

@rienath

rienath commented Oct 2, 2026

Copy link
Copy Markdown
Member

It's not clear if SVE(2) would be faster than Neon. Let's check once we merge auto-vectorisation PR you linked and SVE infra #122860

@chiranmoyf

Copy link
Copy Markdown
Contributor Author

Converting this to a draft until #122860 is merged.

After that, I will benchmark this against #123420. We plan to add SVE2 dispatchers incrementally once #122860 is merged.

For AArch64, Clang vector extensions compile to NEON by default, which may not be sufficient for some operations such as filtering (compaction). NEON has no direct equivalent of the SVE svcompact intrinsic, so retaining SVE dispatchers for such operations could still make sense.

Non-intrinsics performance improvements are also in our internal pipeline and will be released after appropriate testing.

cc @Algunenano @rienath

@clickhouse-gh

clickhouse-gh Bot commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Dear @rienath, you haven't been active on this PR for 30 days. You will be unassigned. Will you continue working on it? If so, please feel free to reassign yourself.

@alexey-milovidov alexey-milovidov self-assigned this Nov 2, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master into the branch (03b803a). #122860 is merged, and #123420 replaced the hasAll intrinsics kernels with portable vector code, which also runs on AArch64. So I resolved the conflict like this: take master's sliceHasImplAnyAll.h and re-add the SVE kernel on top of it as sliceHasAllSVE<has_first_null_map>. It is dispatched from sliceHasIntegral for ArraySearchType::All when isArchSupported(TargetArch::ARM_SVE). The 1-byte sliceHasAllBytes kernel is still used for arrays with at least 16 elements. The kernel's own null pre-check is gone, because sliceHasImplAnyAll already does it. A randomized comparison with a scalar reference (1M cases, with and without null maps, Int8/UInt8/Int16/UInt32/Int64) found no mismatches.

@chiranmoyf you planned to benchmark against #123420, so here are numbers. I ran a microbenchmark of the two kernels on a Neoverse-V2 (Graviton4, 128-bit SVE, the same CPU as c8g). It calls master's portable sliceHasIntegral and sliceHasAllSVE directly, takes the best of 5 runs, and shows the time ratio portable/SVE (above 1 means SVE is faster):

first / second sizes all values found Int8 Int16 Int32 Int64
15 / 3 yes 1.68 0.44 0.71 0.93
100 / 10 yes 1.02 0.63 0.95 0.86
5000 / 500 yes 0.73 0.53 0.67 0.61
50000 / 5000 yes 0.72 0.47 0.54 0.49
15 / 3 no 0.95 0.62 0.55 0.66
100 / 10 no 0.42 0.39 0.39 0.39
5000 / 500 no 0.74 0.20 0.24 0.23
50000 / 5000 no 0.72 0.24 0.23 0.22

On this CPU the SVE kernel now loses to the portable code almost everywhere: it is 1.1x to 5x slower. The only win is very short Int8 arrays. The 1.18x-5.5x gains in the description were measured against the old scalar fallback, which no longer exists on AArch64. So in its current form this PR would make hasAll slower on Graviton4. That matches the concern raised by @Algunenano and @rienath. Whether to keep it (for example, if 256-bit SVE on Graviton3 behaves differently, or with a redesigned kernel) or to close it is up to you and the reviewers. The ARM performance comparison in the new CI run should confirm this.

@Algunenano

Copy link
Copy Markdown
Member

We should test SVE2 with dynamic dispatch and no custom code. When I tested it the compiler was good enough to produce code that was vectorized and faster by itself, no need to declare things manually

@chiranmoyf

chiranmoyf commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov Yes, I tested internally, and the current SVE is slower than #123420. I am working on a more portable version and will push in a day.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Pushed 6738d9e:

  • Fixed the arm_tidy build failure in gtest_has_all.cpp. SliceHasSVETraits was specialized for exact types, but Int8 is signed _BitInt(8), so NumericArraySlice<int8_t> hit an undefined template. The traits are now selected by size and signedness. gtest_has_all.cpp.o and hasAll.cpp.o build on aarch64.
  • Merged master.

The open P1 review point still stands: the current SVE kernel is slower than the portable hasAll code on master (see the Graviton4 table above). @chiranmoyf, please push the reworked kernel with numbers against current master. Another option, as @Algunenano suggested, is to compile the portable code for SVE/SVE2 through dynamic dispatch with no hand-written intrinsics. This PR should not be merged with the current dispatch.

@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 CI on 6738d9e confirms the open P1 review point. The only failure is Performance Comparison (arm_release, master_head, 2/6): has_all query #2 is +23% slower and query #3 is +56% slower than master, both confirmed after a server restart (report). Master has no relevant changes since the last merge, so I did not push another merge. @chiranmoyf, the PR stays blocked until the reworked kernel (or a multitarget build of master's portable code, as @Algunenano suggested) beats master on has_all.

@abhijain1204fujitsu
abhijain1204fujitsu force-pushed the dev-sliceHasImplAnyAll-sve branch from 6738d9e to a9e96df Compare November 6, 2026 04:36
@chiranmoyf chiranmoyf changed the title Improve hasAll performance for integer arrays on AArch64 using SVE Speed up hasAll and hasAny over long integer arrays using sets Nov 6, 2026
…and `hasAny`

`sliceContains` searches a slice in blocks of 128 bytes, testing the combined
mask of each block. On AArch64, for 4 and 8-byte integers, a slice of at least
136 (`Int32`) or 68 (`Int64`) elements is now searched in chunks of 512 bytes by
a plain loop, which clang vectorizes to 8 or 16 NEON comparisons per iteration with
one test per chunk. The first vector is still checked separately, so a value
near the front is found as quickly as before. The last chunk overlaps the
previous one. Shorter slices are unchanged.

New test `05493_has_all_has_any_chunked_search` places a value at every
position of arrays around the chunk boundaries. It covers 4 and 8-byte signed
and unsigned types, with and without nulls, and compares the results with `has`.
@abhijain1204fujitsu
abhijain1204fujitsu force-pushed the dev-sliceHasImplAnyAll-sve branch from a9e96df to 3fe3891 Compare November 6, 2026 18:02
@chiranmoyf chiranmoyf changed the title Speed up hasAll and hasAny over long integer arrays using sets Search long integer slices in 512-byte chunks on AArch64 in hasAll and hasAny Nov 6, 2026
@chiranmoyf

chiranmoyf commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov Apologies for the delay. We have pushed the portable AArch64 code. In our testing, this improved the performance of 14 of the 46 queries in has_all.xml and has_any_has_all_nullable.xml. Data: Graviton4-c8g.4xlarge

We accidentally force-pushed another branch on top of this one, which is why you see extra commits in the history. We’ll be careful not to force-push from now on.

@chiranmoyf

chiranmoyf commented Nov 6, 2026 •

Copy link
Copy Markdown
Contributor Author

@alexey-milovidov @Algunenano We tried compiler-generated SVE/SVE2 code with dynamic dispatch (no hand-written intrinsics), but performance was worse than on master. SVE/SVE2 portable code data: Graviton3-m7g.8xlarge, Graviton4 c8g.4xlarge. I'll check the code generated.

@chiranmoyf

Copy link
Copy Markdown
Contributor Author

Repurposing this PR for the portable AArch64 code, which extends the current implementation. We’re also working on a platform-independent approach that switches to a set-based algorithm for longer arrays.

The `arm_asan_ubsan` build failed with `loop not vectorized: the optimizer
was unable to perform the requested transformation` (`-Wpass-failed`) for the
loop in `chunk_contains`: the instrumented loads cannot be vectorized, and the
`#pragma clang loop vectorize(enable)` turns that into an error.

https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=122871&sha=3fe3891446e7d57fbeb5ab69b33500455af06836&name_0=PR&name_1=Build%20%28arm_asan_ubsan%29
ClickHouse#122871

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 The Build (arm_asan_ubsan) failure on 3fe3891 was caused by this PR: under ASan the instrumented loads in chunk_contains cannot be vectorized, and #pragma clang loop vectorize(enable) turns that into a -Wpass-failed error. d0c8bdb applies the pragma only in non-sanitizer builds (reproduced and verified with a standalone AArch64 compile with and without -fsanitize=address,undefined). Release builds are unchanged. The performance comparison on ARM still has to confirm the gain against master.

@chiranmoyf

Copy link
Copy Markdown
Contributor Author

All tests have passed, and the gains are visible in the dashboard. Please review for any gaps, @alexey-milovidov.
I’ll explore the regression with portable compiler-generated SVE/SVE2 code and the set-based approach in a separate PR.

@alexey-milovidov alexey-milovidov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

All good.

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Nov 7, 2026
Merged via the queue into ClickHouse:master with commit 9dfd669 Nov 7, 2026
187 checks passed
@robot-ch-test-poll3 robot-ch-test-poll3 added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 7, 2026
eoger-ant pushed a commit to eoger-ant/ClickHouse that referenced this pull request Nov 10, 2026
The `chunk_contains` loop in `sliceContains` requests
`vectorize(enable) vectorize_width(16) interleave_count(2)`. A forced
transformation that the loop vectorizer cannot perform is reported as
`-Wpass-failed=transform-warning`, which `-Werror` makes fatal.

Debug builds compile with `-Og`, which clang runs as its O1 pipeline.
There the vectorizer does not recognize `found |= ...` as a reduction
("value that could not be identified as reduction is used outside the
loop"), so `Build (arm_debug)` fails on `has_any.cpp` and `has_all.cpp`
since ClickHouse#122871. PR CI has no arm_debug build, so only master caught it.

Guard the pragma with `DEBUG_OR_SANITIZER_BUILD` instead of `SANITIZER`.
It is defined in every build without `NDEBUG` and in every sanitizer
build, so sanitizer builds still skip the pragma and release builds keep
it, including the loud failure if a compiler stops honouring it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-regular-function Regular scalar functions: string processing, data conversion, arithmetic, math, comparison, condi... external Author is not a member of the ClickHouse organization pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants