Skip to content

Add ARM SVE infrastructure and SVE-optimized paths for unary arithmetic and findExtreme - #97473

Closed
chiranmoyf wants to merge 5 commits into
ClickHouse:masterfrom
MonakaResearch:sve-enablement-and-optimization
Closed

chiranmoyf wants to merge 5 commits into
ClickHouse:masterfrom
MonakaResearch:sve-enablement-and-optimization

Conversation

@chiranmoyf

@chiranmoyf chiranmoyf commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor
  1. Add ARM SVE detection and SVE dispatch infrastructure on AArch64.

  2. Add MULTITARGET_FUNCTION_ARM_SVE and extend MULTITARGET_FUNCTION_X86_V4_V3 to MULTITARGET_FUNCTION_X86_V4_V3_ARM_SVE, and MULTITARGET_FUNCTION_X86_V4 to MULTITARGET_FUNCTION_X86_V4_ARM_SVE.

  3. Add dispatchers for unary arithmetic and findExtreme functions. Performance improvements for several example functions on m7g.8xlarge - pastila.

Changelog category (leave one):

  • Performance Improvement

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

Improve performance of unary arithmetic functions and min/max aggregation on AArch64 CPUs with SVE support (e.g. AWS Graviton 3) via runtime-dispatched SVE implementations.


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

@clickhouse-gh

clickhouse-gh Bot commented Feb 20, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [f730450]

Summary: ❌

job_name test_name status info comment
Build (arm_darwin) FAIL
Build ClickHouse FAIL cidb
Upgrade check (amd_release) FAIL
Cannot start clickhouse-server FAIL cidb
Check failed FAIL cidb
AST fuzzer (amd_msan) FAIL
Logical error: Can't determine table for parallel replicas (STID: 3634-41e5) FAIL cidb
Docs examples FAIL
Documentation examples FAIL cidb
Fast test (arm_darwin) DROPPED

AI Review

Summary

This PR adds AArch64 SVE detection and runtime-dispatched SVE-specialized variants for unary arithmetic and findExtreme-based paths. The Linux/FreeBSD runtime dispatch pieces are close, but the current TargetSpecific macro split still breaks the non-Linux AArch64 contract in one place, and the new FreeBSD support re-opens the ENABLE_SVE_GLOBALLY native-host preflight gap on that platform.

Findings

❌ Blockers

  • [src/Common/TargetSpecific.h:404-447] The PR now says SVE codegen is enabled only on Linux/FreeBSD AArch64, but the function-level branch still matches every __aarch64__ build. That means FunctionUnaryArithmetic.h and findExtreme.cpp still instantiate _ARM_SVE clones on arm_darwin; because SVE_FUNCTION_SPECIFIC_ATTRIBUTE is only defined in the earlier Linux/FreeBSD-only block, those call sites are left with an undefined token and fail to compile. Suggested fix: apply the same defined(__linux__) || defined(__FreeBSD__) guard to the lower #elif ENABLE_MULTITARGET_CODE && defined(__GNUC__) && defined(__aarch64__) branch, or provide a no-op SVE_FUNCTION_SPECIFIC_ATTRIBUTE/fallback expansion for unsupported AArch64 OSes.

⚠️ Majors

  • [cmake/cpu_features.cmake:76-97] ENABLE_SVE_GLOBALLY is now documented and partially wired for FreeBSD via haveSVE, but the native-host preflight is still wrapped in if (OS_LINUX ...). On native AArch64 FreeBSD builders, -DENABLE_SVE_GLOBALLY=1 can therefore still configure successfully on a non-SVE host and only fail later when build-time tools compiled with +sve execute. Suggested fix: extend the native-host preflight to supported FreeBSD AArch64 hosts as well, using a FreeBSD-appropriate feature probe instead of the Linux-only /proc/cpuinfo path.
Missing context / blind spots
  • ⚠️ The current PR CI matrix exposes amd_freebsd but not an arm_freebsd build, so the new FreeBSD SVE path is not exercised there. An AArch64 FreeBSD build/configure check would close that gap.
Final Verdict

❌ Changes required before merge.

LLVM Coverage Report

Metric Baseline Current Δ
Lines 87.30% 87.30% +0.00%
Functions 91.90% 91.90% +0.00%
Branches 79.40% 79.40% +0.00%

Changed lines: Changed C/C++ lines covered: 22/29 (75.86%) · Uncovered code

Full report · Diff report

@clickhouse-gh clickhouse-gh Bot added the pr-performance Pull request with some performance improvements label Feb 20, 2026
@rienath rienath self-assigned this Feb 20, 2026
@Algunenano

Algunenano commented Feb 23, 2026 •

Copy link
Copy Markdown
Member

As I'm working on #90043 I'm wondering if it doesn't make more sense to make sve a requirement for the default build instead. We'd drop support for graviton 2, which is old and noticebly worse than 3 and 4. And we would not need dispatch, and have better code all around.

Testing it here: #97781

@rienath

rienath commented Mar 2, 2026 •

Copy link
Copy Markdown
Member

@chiranmoyf could you please take a look why the build in CI fails?

@ajeyabsf

Copy link
Copy Markdown

@rienath, the error is from clang-tidy. The error is related to MULTITARGET_FUNCTION_X86_V4_V3_SVE. clang-tidy is flagging that FUNCTION_BODY is expanded multiple times in MULTITARGET_FUNCTION_X86_V4_V3_SVE.

There are two solutions possible to this.

  1. Add '-bugprone-macro-repeated-side-effects' to clang-tidy.
  2. Add a NOLINT comment to each of the function call sites. There are 14 of them, and the comment would have to be added to each place. It might have to be added if there are new invocations in future.

What do you think of these solutions? Do you have any other solution?

@rienath

rienath commented Mar 12, 2026

Copy link
Copy Markdown
Member

@ajeyabsf thanks for taking a look. We expanded multiple times with x86 too, but looks like it didn't fire because our CI runner is on ARM.

Let's add // NOLINT(bugprone-macro-repeated-side-effects) at each call site to be as surgical as possible. The only downside is that future contributors would need to include it too. But in practice, people copy-paste existing invocations of the macro anyway, so the NOLINT will come along for the ride

@rienath rienath changed the title Adds SVE support for ColumnVector, aggregate sum, function unary arithmetic and functions comparison. Add SVE support for ColumnVector, aggregate sum, function unary arithmetic and functions comparison Mar 12, 2026
@rageshh-fj
rageshh-fj force-pushed the sve-enablement-and-optimization branch from 5c855c8 to dc6b2aa Compare March 19, 2026 16:57
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Mar 19, 2026
@chiranmoyf

Copy link
Copy Markdown
Contributor Author

@rienath /// NOLINT is used to suppress MULTITARGET_FUNCTION_BODY macro warnings in AggregateFunctionSum.h. Therefore, we have added the same instead of /// NOLINT(bugprone-macro-repeated-side-effects) in ColumnVector.cpp and other places.

Comment thread cmake/cpu_features.cmake Resolved

@rienath rienath 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.

I've partially reviewed the PR and will continue once these comments are addressed. Some of them are questions / calls for discussion, so feel free to answer and ping me

Comment thread cmake/cpu_features.cmake Resolved
Comment thread cmake/cpu_features.cmake Resolved
Comment thread src/Common/TargetSpecific.h Resolved
Comment thread src/Common/TargetSpecific.h Resolved
Comment thread src/Common/TargetSpecific.h Resolved
Comment thread src/Common/TargetSpecific.h Resolved
Comment thread src/Common/TargetSpecific.h Resolved
Comment thread src/Common/CPUID.h Resolved
@rageshh-fj
rageshh-fj force-pushed the sve-enablement-and-optimization branch from dba3fb9 to bd2e757 Compare April 4, 2026 14:22
@chiranmoyf
chiranmoyf requested review from rienath and rschu1ze April 6, 2026 06:08

@rienath rienath 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.

The PR looks great! I've left a few nits, but we are almost done.

Regarding the pastila links with performance tests that you left in PR description: could you please add them as .xml performance tests in tests/performance folder? Especially the ones benchmarking ColumnVector. As far as I can tell, we have no existing test that isolates this path. It'd be great to include multiple element widths (not only 4 bytes). This PR doesn't target them all, but it would give us broader coverage for future work

Comment thread cmake/cpu_features.cmake Resolved
Comment thread src/Common/TargetSpecific.cpp Resolved
Comment thread src/Common/findExtreme.cpp Outdated
/// We see no benefit from using AVX512BW or AVX512F (over AVX2), so we only declare SSE and AVX2
if (isArchSupported(TargetArch::x86_64_v3))
return findExtremeImpl_x86_64_v3<T, ComparatorClass, add_all_elements, add_if_cond_zero>(ptr, condition_map, start, end);
#elif USE_ARM_MULTITARGET_CODE

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.

Nice!
image

Comment thread src/Functions/FunctionUnaryArithmetic.h Outdated
using ArrayA = typename ColVecA::Container;
using ArrayC = typename ColVecC::Container;

MULTITARGET_FUNCTION_X86_V4_V3(

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.

Nice!

image

Comment thread src/Columns/ColumnVector.cpp Resolved
@rageshh-fj
rageshh-fj force-pushed the sve-enablement-and-optimization branch from bd2e757 to cdb82ba Compare April 23, 2026 05:04
@chiranmoyf

chiranmoyf commented Apr 23, 2026 •

Copy link
Copy Markdown
Contributor Author

As far as I can tell, we have no existing test that isolates this path ...

Added the queries in filter_on_column_vector.xml file. Datatypes starting 1 byte to 8 bytes are covered.
Other queries involving aggregation and unary arithmetic functions were taken from existing xml files.

It'd be great to include multiple element widths (not only 4 bytes)

We cannot use svcompact directly. Maybe a combination of other intrinsics might work. We can look into it once the macros infrastructure is in place.

Comment thread src/Functions/FunctionsComparison.h Resolved
@chiranmoyf
chiranmoyf requested a review from rienath April 23, 2026 08:56
@chiranmoyf chiranmoyf changed the title Add SVE support for ColumnVector, aggregate sum, function unary arithmetic and functions comparison Add SVE support for ColumnVector, aggregate and function unary arithmetic Apr 23, 2026
@chiranmoyf chiranmoyf changed the title Add SVE support for ColumnVector, aggregate and function unary arithmetic Add ARM SVE detection and SVE dispatch infrastructure and SVE-optimized paths for ColumnVector filter, unary arithmetic, and findExtreme Apr 25, 2026
@chiranmoyf

Copy link
Copy Markdown
Contributor Author

Hi @rienath, just following up, could you please take another look when you get a chance?

@rienath

rienath commented Aug 3, 2026

Copy link
Copy Markdown
Member

@chiranmoyf @abhijain1204fujitsu thanks for working on this! Before we merge, I'd like to resolve conflicts and make a few minor changes. Moreover, we cannot merge with red CI, so we'd need to wait for relevant fixes and merge master, perhaps multiple times. To avoid friction, could you please give me rights to commit to this branch (maintainer_can_modify set to true)?

@chiranmoyf

Copy link
Copy Markdown
Contributor Author

@rienath, we're working on getting you commit rights. In the meantime, please let us know if there are any specific changes you'd like us to make.

Comment thread src/Common/TargetSpecific.h Outdated
*/
#define DECLARE_ARM_SVE_SPECIFIC_CODE(...)

#elif ENABLE_MULTITARGET_CODE && defined(__GNUC__) && defined(__aarch64__)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

USE_ARM_MULTITARGET_CODE becomes 1 on every __aarch64__ build here, but haveSVE only reports TargetArch::ARM_SVE on Linux (src/Common/CPUID.h returns false on Darwin/FreeBSD). As a result, supported non-Linux AArch64 builds such as arm_darwin still compile all of the new *_ARM_SVE clones from FunctionUnaryArithmetic.h and findExtreme.cpp, but runtime dispatch can never select them.

In FunctionUnaryArithmetic.h alone that duplicates every unary-op instantiation across a long list of translation units for code that is unreachable on those platforms. Please either gate this AArch64 multitarget branch to OSes where haveSVE can return true, or add non-Linux SVE detection so codegen and runtime selection stay aligned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Added SVE runtime detection for FreeBSD. Darwin doesn't support SVE yet, so SVE is enabled only on Linux and FreeBSD.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

src/Common/TargetSpecific.h:404 is still #elif ENABLE_MULTITARGET_CODE && defined(__GNUC__) && defined(__aarch64__) without the Linux/FreeBSD guard you added above, and src/Common/TargetSpecific.h:418-447 still expands every MULTITARGET_FUNCTION_*_ARM_SVE call site into an _ARM_SVE clone.

So FunctionUnaryArithmetic.h and findExtreme.cpp still emit SVE-specific declarations on arm_darwin even though the earlier block leaves USE_ARM_MULTITARGET_CODE=0 there. Because SVE_FUNCTION_SPECIFIC_ATTRIBUTE is only defined in the Linux/FreeBSD branch, this is now a compile-time failure on Darwin rather than just unreachable code.

…and TargetSpecific::SVE to TargetSpecific::ARM_SVE

Renamed ENABLE_SVE to ENABLE_SVE_GLOBALLY in cpu_features.cmake.
Renamed TargetSpecific::SVE to TargetSpecific::ARM_SVE and
TargetArch::SVE to TargetArch::ARM_SVE to mirror the x86_64_*
arch-family prefix convention and match the _ARM_SVE
function-name suffix.
Updated comments in TargetSpecific.h.
@abhijain1204fujitsu
abhijain1204fujitsu force-pushed the sve-enablement-and-optimization branch from f35cdb8 to 5c58f4a Compare August 26, 2026 06:23
pull Bot pushed a commit to erickirt/ClickHouse that referenced this pull request Aug 29, 2026
`DB::iota` and `DB::iotaWithStep` are loop-carried fills that clang
auto-vectorises on every target, but on AArch64 it interleaves them 2x
where the shipped `-march=x86-64-v3` uses 4x. Reported by @ egor-click in
ClickHouse#116581 with the AArch64 measurements and this patch: 1.21x on
`SELECT count() FROM numbers(3000000000)` and 1.13x on `sum(number)` on
Graviton3. Those timings are his, so the Performance Comparison job
arbitrates.

The cause is not the absence of `-mcpu=` on AArch64 that the issue
identifies. Measured on the pinned clang-22, `-mcpu=neoverse-v1`,
`-mtune=neoverse-v1` and `-mcpu=neoverse-n1` all leave the interleave
factor at 2. The deciding property is the LLVM subtarget feature
`FeatureMaxInterleaveFactor4`
(`contrib/llvm-project/llvm/lib/Target/AArch64/AArch64Features.td:926`),
which raises `MaxInterleaveFactor` from its AArch64 default of 2 and
which processors opt into individually: Neoverse-V2, Apple M1 and
Cortex-A57 carry it, Neoverse-N1, Neoverse-V1 and Neoverse-N2 do not. No
flag available to this repository reaches it, so the general fix belongs
upstream in `AArch64Processors.td` and a per-loop hint is the only lever
here.

The hint is arch-guarded, which the patch in the issue is not. On this
translation unit it is byte-identical at `x86-64-v3` and `-v4`, but
unguarded it changes 367 instruction lines at `x86-64-v2` and 380 at
level 1, and level 1 is what `BuildTypes.AMD_COMPAT` ships. With the same
`-march`, `arm64-apple-macos11` already interleaves 4x, so the hint is
inert on Darwin. The guard therefore matches the two existing
positive-hint sites in `FunctionsConversion.h` exactly. No
`-Wpass-failed` suppression is needed: a bare `interleave_count` compiles
clean under this build's `-Weverything -Werror`, unlike the
`vectorize_width` those sites use.

Verified on the AArch64 object by execution rather than by argument. It
has no undefined symbols, so it links freestanding and runs under
`qemu-aarch64-static`: 13356900 element comparisons against an
independent scalar reference over counts 0 to 2100 for `UInt8`, `UInt32`
and `UInt64`, with wrapping first values and steps including 0 and the
maximum, and a sentinel region past `count`. Zero mismatches and zero
overruns, hash identical to the unmodified object, while the same harness
reddens for a `value += 2` mutant and for an `i <= count` mutant. All six
vectorised loops move from `interleaved count: 2` to `4` with no change
in vectorisation width or loop count, and each instantiation goes from
one `stp` to two, that is from two accumulators to four.

`NO_INLINE` is kept, and every hot loop still opens on a `.p2align 5`
boundary, matching the global `-falign-loops=32`. The two `UInt32`
instantiations gain one extra aligned block, which is the shape the
`UInt8` ones already had.

`src/Common/findExtreme.cpp` is deliberately not touched. The reporter
scoped that half as review input on ClickHouse#97473, which restructures that file
behind `MULTITARGET_FUNCTION` and does not touch `src/Common/iota.cpp`. A
preprocessor guard could not keep this hint out of its SVE clone in any
case, because `__ARM_FEATURE_SVE` is undefined inside a `target("+sve")`
clone even when that clone is SVE-vectorised.

Residuals not resolved here: small-`count` callers are unmeasured, and of
those `translate` and `arrayRandomSample` have no performance test at
all; `iotaWithStep` has no performance coverage of its own and is changed
for symmetry with the loop beside it; AArch64 cores other than Graviton3
are untested, so the direction should hold but not the magnitude.

Related: ClickHouse#116581

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

clickhouse-gh Bot commented Aug 31, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing f730450ad with master 90edfe017 (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
Binary Master PR Δ
programs/clickhouse-stripped 711.21 MiB 708.43 MiB -2.78 MiB (-0.39%)

Only the stripped binary is compared: the official master build keeps debug symbols while PR builds strip them, so the other binaries differ by construction.

Object file sizes

82 object files changed (+308.57 KiB total), 0 added.

Object file Master PR Δ
src/CMakeFiles/clickhouse_common_io.dir/Common/findExtreme.cpp.o 225.96 KiB 275.49 KiB +49.52 KiB (+21.92%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/roundDuration.cpp.o 282.23 KiB 305.15 KiB +22.91 KiB (+8.12%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/factorial.cpp.o 311.14 KiB 332.54 KiB +21.40 KiB (+6.88%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/sign.cpp.o 355.11 KiB 375.46 KiB +20.36 KiB (+5.73%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/negate.cpp.o 489.88 KiB 509.26 KiB +19.38 KiB (+3.96%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/abs.cpp.o 501.65 KiB 520.96 KiB +19.30 KiB (+3.85%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/roundAge.cpp.o 274.33 KiB 291.61 KiB +17.28 KiB (+6.30%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/byteSwap.cpp.o 392.52 KiB 408.98 KiB +16.46 KiB (+4.19%)
src/Functions/CMakeFiles/clickhouse_functions_obj.dir/intExp10.cpp.o 289.53 KiB 305.56 KiB +16.04 KiB (+5.54%)

737 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

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

Job report

@clickhouse-gh

clickhouse-gh Bot commented Sep 3, 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.

@rienath rienath self-assigned this Sep 3, 2026
@abashkeev abashkeev added the comp-build-system Build system (CMake, compilation, platform builds). label Sep 3, 2026
@chiranmoyf

Copy link
Copy Markdown
Contributor Author

This PR was opened from a GitHub Enterprise account, which doesn't allow maintainers to edit it. We have reopened it as
#122860 from a personal fork, where maintainers should have write access. cc @rienath @rschu1ze

@chiranmoyf chiranmoyf closed this Sep 29, 2026
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-build-system Build system (CMake, compilation, platform builds). manual approve Manual approve required to run CI pr-performance Pull request with some performance improvements

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants