Repository navigation
Conversation
`sum` and `avg` of all these types accumulate into a `Decimal128`, and the batch loops of `AggregateFunctionSumData` added every row through `wide::integer<128>` (and, without LTO, an out-of-line `addOverflow` call), which never vectorizes. The new `sumIntoDecimal128` splits every value at bit 32 and sums the parts in 64-bit lanes written with clang vector extensions, so the same code compiles to SSE, AVX2, AVX-512 and NEON. A lane cannot overflow within 2^32 rows; the lanes are combined into the 128-bit result after each chunk. The result is bit-identical, including the wrap-around of `Decimal128`. It is used for aggregation without keys (`addMany`, `addManyConditional`, `addManyNotNull`), so it covers `sum`, `avg`, their `-If` forms, `Nullable` arguments and `avg` of `DateTime64` / `Time64`. Single-threaded `SELECT sum(x)` over 50 million rows in a `Memory` table: 2.8x to 8.3x faster depending on type and mode. The kernel is 15x to 120x faster than the previous loop in microbenchmarks on Zen 5, Alder Lake, Haswell and Zen 2.
Comparing byte vectors gives a vector of `char`, which is unsigned on ARM and s390x, so the masks were converted to 255 instead of all ones and the conditional paths (`-If`, `Nullable`) kept only the low byte of every value. Widen the flags to 64-bit lanes before comparing them.
Vector comparisons are deprecated for AltiVec vectors, so the PowerPC build failed with `-Wdeprecated-altivec-src-compat`. Derive the masks from the sign bit of `flag - 1` or `-flag` instead, which does not depend on the element type of comparison results either. LLVM still emits the same byte comparisons on x86.
Window frames and ranges of equal keys call the batch path with a few rows, for which zeroing, reducing and recombining the vector lanes cost more than the old scalar loop. The vector loop now only runs over whole steps, and the remaining rows are added to an `unsigned __int128` directly, which also replaces the three per-type scalar tails. The vector loop is unrolled four times explicitly, because the compiler does not do it reliably.
`sum`, `sumWithOverflow` and `avg` of `Int128` and `UInt128` accumulate in the argument type, and the batch loops added every row through `wide::integer<128>`. Modulo 2^128, signed and unsigned values are added the same way as `Decimal128`, so the vectorized kernel is reused: it is renamed to `sumIntoInt128`, returns the 128-bit result, and accepts `Int128` and `UInt128` values too.
Its steps are narrow (a sign extension and an addition), and with four steps in flight the sum over large ranges was about 20% slower than before the short-range change.
The tail has fewer rows than one step, but the compiler unrolled and vectorized it into code that never runs and a larger stack frame; disable that. Also drop casts through `__int128`: converting a signed value to `unsigned __int128` already sign-extends.
`sumWithOverflow` takes the same path as `sum` here, and the full-range values already wrap around.
clang-tidy (`cppcoreguidelines-init-variables`) rejected the variable assigned in two branches.
sum and avg of Decimal32, Decimal64 and Decimal128sum and avg of Decimal32, Decimal64, Decimal128, Int128 and UInt128
|
Workflow [PR], commit [bd1a570] Summary: ✅
AI ReviewSummaryPR #126134 vectorizes batch Missing context / blind spots
Final VerdictStatus: ✅ Approve. No required changes. |
…ryResolveAddress` ClickHouse#126039 made `StackTrace::tryResolveAddress` return `ResolvedAddress` instead of `std::optional<ResolvedAddress>`, while ClickHouse#126137 started using it as an optional in `gtest_functions_stress.cpp`, so master does not compile `unit_tests_dbms`. The same change as ClickHouse#126269, to unblock CI here. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=126134&sha=latest&name_0=PR&name_1=Build%20(arm_tidy%2C%203%2F4) PR: ClickHouse#126134
# Conflicts: # src/Functions/tests/gtest_functions_stress.cpp
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units11 translation units recompiled, 57 s compile time in total, 11 of them have a recent master baseline. Median compile-time ratio to the baselines is ×1.07 (machine-speed difference or a change affecting every TU); per-TU deltas below are relative to that ratio. |
sumandavgofDecimal32,Decimal64andDecimal128accumulate into aDecimal128, and ofInt128andUInt128into the argument type; the batch loops added every row throughwide::integer<128>, which never vectorizes. The newsumIntoInt128splits the values at bit 32 and sums the parts in 64-bit lanes written with clang vector extensions, so one portable implementation compiles to SSE, AVX2, AVX-512 and NEON. Results are bit-identical, including the wrap-around modulo 2^128. It applies to aggregation without keys, including-If,Nullablearguments,sumWithOverflowof 128-bit types andavgofDateTime64; ranges shorter than one vector step (e.g. from window frames) are added without the vector part.Single-threaded over 20 million rows (
Memorytable):sumofDecimal326.7x,Decimal645x,Decimal1283x,Int1288.1x faster,sumIf(Int128)5.8x;UInt128was already cheap and is unchanged end to end.Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Up to 8x faster
sumandavg(also with-Ifand overNullable) ofDecimal32,Decimal64,Decimal128,DateTime64andInt128values when aggregating withoutGROUP BY.Workflow [PR]
Sync PR [sync-upstream/pr/126134]