Repository navigation
Fix out-of-range Float64 to Int32/UInt32 conversion depending on the row position on aarch64 - #133517
Conversation
Since ClickHouse#117346 (fa5cc17, "Check for NaN and Inf in a separate pass in the float to integer conversion") the non-accurate Float -> Int (>= 32 bit) conversion in convertNumericGeneral runs in a loop marked only `#pragma clang loop vectorize(disable)`. That pragma sets the vector width to 1 but does not disable interleaving. On aarch64 clang 23 interleaves the loop by 4 and the SLP vectorizer packs the copies into `fcvtzs v.2d` + `uzp1 v.4s`, which keeps the low 32 bits of the 64-bit conversion (wraps), while the n % 4 remainder rows use scalar `fcvtzs w, d`, which saturates. An out-of-range Float64 -> Int32/UInt32 therefore gave different values for the same input in one column, depending on block size and row position: SELECT groupArray(CAST(materialize(-81183017266.28046) AS Int32)) FROM numbers(5) -- [421361358,421361358,421361358,421361358,-2147483648] SQLancerPP (arm_release) NoREC found it as a WHERE vs SELECT-list count mismatch (12 vs 9). The first parent of ClickHouse#117346 saturated in every row. Add interleave(disable), the remedy already used in DataTypesDecimal.cpp, so the loop is scalar as its comment requires. The comment's old description of which instruction wraps was the x86 view and the opposite of aarch64. Validated on a local aarch64 cross build: the new test passes with the fix, fails with only the token removed, passes again with it restored; it fails on the CI aarch64 binary of 903285f. x86-64 is not affected: with CI's AMD_BINARY flags all 34 FunctionsConversion*.cpp.o objects disassemble identically with and without the change. Closes ClickHouse#133509 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Condense the comment above the check-free float to integer conversion loop to two lines. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?REF=master&sha=903285ff6155738893fabd51de289fb86a2ab084&name_0=NightlySQLancer&name_1=SQLancerPP%20%28arm_release%29 Co-Authored-By: Claude Opus 5.5 <[email protected]>
Internal second-model review: adjudication log (click to expand)Pre-publication review by an independent model (engine: codex; 1 finding) plus my own cold review (2 notes). No blocker or major findings.
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-8:20261122-091000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-4:20261122-080900 |
|
Workflow [PR], commit [75ea87f] Summary: ✅
AI ReviewSummaryThe PR makes finite Missing context / blind spots
Final VerdictStatus: ✅ Approve |
`interleave(disable)` made the check-free loop scalar on aarch64 for every float to integer pair, and `intDiv` ClickHouse#2 (`toUInt32` of `Float64`) became 24% slower in the arm performance comparison of this PR. Revert it. Only `Float64` to `Int32`/`UInt32` is inconsistent on aarch64: the vector conversion goes through 64-bit lanes and keeps the low 32 bits, while the scalar `fcvtzs w`/`fcvtzu w` saturates. That pair gets its own loop that clamps the value to the range of the result type before converting, which gives the scalar result for every finite value and lets the loop vectorizer use `fmaxnm`/`fminnm` + `fcvtz[su] v.2d` + `xtn`. The same-width pairs keep the code of master: their vector and scalar instructions saturate alike. On the aarch64 cross build, toInt32/toUInt32/CAST/Nullable of 1019 values (out of range both ways, the bounds, fractions, -0.0) give the same results as the scalar build of the previous commit and as the CI binary of 58726cc, the first parent of ClickHouse#117346. llvm-mca for neoverse-v2 (the m8g runners of the arm performance comparison): 0.75 cycles per row, as master; the scalar loop took 2.0. The x86-64 code is that of master. CI report: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=133517&sha=53c88db4bb8b09904e64fcd797dcf581fd9f5706&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%203%2F6%29 PR: ClickHouse#133517 Co-Authored-By: Claude Opus 5.5 <[email protected]>
The consistency checks pass for any clamp that returns one value per input, also a wrong one. A value at a bound that is in range once truncated converts to the same integer on every platform, so assert those exactly: with the upper bound of the clamp off by one, the aarch64 build returns 2147483646 and 4294967294 and the test fails. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units40 translation units recompiled, 211 s compile time in total, 40 of them have a recent master baseline. |
|
Measured the loop shape on arm64 (Apple clang 21, -O2) with a standalone harness: vector lane =
So the clamp gives one value per input in every row, and equals the saturated pre-#117346 aarch64 result. One note: that value is not the x86-64 value. A Measured with the exact-arithmetic tools at colony.technology (npx speedrunpapi). |
Closes: #133509
Related: #117346
Related: #128538
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Description
On aarch64 master, the same out-of-range
Float64converted toInt32/UInt32gives two values in one column:SELECT groupArray(CAST(materialize(-81183017266.28046) AS Int32)) FROM numbers(5)returns[421361358,421361358,421361358,421361358,-2147483648]. Found bySQLancerPP (arm_release)/ NoREC (report): WHERE counts 12 rows, the same predicate in the SELECT list 9.Cause: since #117346 (
fa5cc176) the check-free conversion loop inconvertNumericGeneralhas#pragma clang loop vectorize(disable), which still allows interleaving. On aarch64 clang 23 interleaves it by 4 and the SLP vectorizer packs the copies intofcvtzs v.2d+uzp1(wraps), while the remainder rows use scalarfcvtzs w(saturates). The first parent of #117346 saturated in every row.Change: on aarch64,
Float64toInt32/UInt32gets its own loop that clamps the value to the range of the result type before converting. That gives the saturated value of the scalar instruction in every row, and the loop stays vectorized. Disabling interleaving instead madeintDiv#2 (toUInt32ofFloat64) 24% slower in this PR's arm performance comparison. The other pairs keep the current code: their vector and scalar instructions saturate alike. Revertingfa5cc176would also fix it, but brings back the per-row check. The 26.11 branch, not released yet, has the same loop.Validation:
903285ff; on a local aarch64 cross build it passes, fails with the clamp disabled, and passes again. The original NoREC statements give 12/12.toInt32/toUInt32/CASTof 1019 values (out of range both ways, the bounds, fractions) give the same results as the CI binary of the first parent of Useclang-23#117346.🤖 Generated with Claude Code
Workflow [PR]
Sync PR [sync-upstream/pr/133517]
Version info
26.12.1.178-master(included in26.12and later)