Repository navigation
base: define wraparound in ignore-overflow helpers - #120808
alexey-milovidov merged 10 commits into
Conversation
The ignore-overflow helpers currently perform signed arithmetic with undefined behavior. NO_SANITIZE_UNDEFINED suppresses diagnostics but does not define wraparound, so an optimizer may use the overflow assumption. For integer results, perform each operation in the unsigned counterpart of its result type and convert the modulo result back. This preserves integer promotion for narrow operands and defines the intended wraparound for signed and wide integer types. Keep the existing operator path for non-integer result types such as Decimal. Signed-off-by: Yongqiang Tian <[email protected]>
|
Workflow [PR], commit [b31d724] Summary: ✅
AI ReviewSummaryThis PR replaces UB-prone signed arithmetic in Final Verdict
LLVM Coverage ReportMeasured on commit b31d724.
Changed lines: Changed C/C++ lines covered: 18/18 (100.00%) · Uncovered code |
Signed-off-by: Yongqiang Tian <[email protected]>
|
I’ve added a regression test and verified it locally with Clang 22. I’m not very familiar with ClickHouse’s test infrastructure, so please let me know if its placement or structure should be adjusted |
|
Thanks, fixed in eebd4b7. The boundary operands now go through a runtime-dependent runtimeValue helper, preventing constant propagation even under LTO. I also verified that the old implementation fails at -O3 with this test shape, while the unsigned-domain implementation passes. |
|
📊 Cloud Performance Report
This PR rewrites the integer overflow helpers (add/sub/mul/negateIgnoreOverflow) to use unsigned-cast wraparound instead of UB-based signed arithmetic. TPC-H Q1 is dominated by summed arithmetic over many rows, so it is the one benchmark query that plausibly exercises the changed hot path, and the deterministic math flagged it as not_sure (+26.0%) rather than a locked regression. The baseline for this query is intrinsically noisy and CPU time also rose, so the delta is a real candidate worth a rerun but not a confirmed regression. No cross-query pattern: only Q1 touches the modified code path. clickbench🟢 No significant changes tpch_adapted_1_officialFlagged queries (1 of 22)
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. Debug info
|
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Object file sizes68 object files changed (+212.73 KiB total), 0 added.
716 more object files are built by the master warmup baseline only (it builds every object-file target, a pull request build only Compile time of recompiled translation units3048 translation units recompiled, 15780 s compile time in total, 3048 of them have a recent master baseline. |
The existing cases only exercised operands whose `decltype(x op y)` is the
same type as the operands themselves, so they did not lock down the other
half of the contract: narrow operands undergo the usual integral promotions,
and the helpers must wrap in the promoted result type rather than at the
operand width.
Add `ArithmeticOverflow.IgnoreOverflowPromotedNarrowOperands`, covering
`Int16` and `UInt8` - for both, `decltype(x op y)` is `int`, which is pinned
with a `static_assert` - and asserting `int`-typed results that do not fit
back into the operand type.
A refactor that switched the helpers to `make_unsigned_t<T1>` and wrapped at
the operand width was control-proved to fail the new cases:
`subIgnoreOverflow(Int16{-32768}, Int16{1})` yields `32767` instead of
`-32769`, and `addIgnoreOverflow(UInt8{255}, UInt8{1})` yields `0` instead
of `256`.
`Int8` is `signed _BitInt(8)`, which does not promote, so it is covered by
`ArithmeticOverflow.IgnoreOverflowWraparound` instead - it is added to that
test's type list here.
Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
|
🕵 On the I compiled a representative hot loop ( So there is no mechanism by which this PR slows Q1 down, and the +26.0% is baseline noise. Note also that the |
|
🕵 The only red on the previous head was
This complements the earlier assembly check for the Cloud Performance Report Also merged the latest |
|
🕵 The only red on head
This is tracked in #121456 (" |
|
🕵 Merged The only red on the previous head was |
|
🕵 The only red on head |
|
Hi @alexey-milovidov, |
|
🕵 Merged |
|
🕵 @groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/json.html?PR=120808&sha=5b482b107d0718ed3072ac0cc43da01ea980dc2b&name_0=PR&name_1=Stateless%20tests%20%28arm_binary%2C%20parallel%29 and provide a fix in a separate PR. If the fix is already in progress, link it here.
|
|
Acknowledged. No fix for this is in progress yet; I'm investigating the |
|
Fix: #122751 |
|
🕵 Merged |
…y_log The query_log listing could print the test's own `SYSTEM FLUSH LOGS query_log, text_log` rows (`QueryStart 3 10` and `QueryFinish 3 10`). A statement's QueryStart row is logged after its interpreter executes, and for SYSTEM FLUSH LOGS the flush is the execution, so both rows of that statement are queued only after its own flush completed. They become visible when another flush (a concurrent test's SYSTEM FLUSH LOGS, or the periodic one) commits them before the listing reads the table. The server log of the failing job shows exactly that: a flush requested by another test during this test's slow query_log insert ran 14 ms after query 3 returned and committed its two rows into a new part, and the listing, which started 108 ms after query 3 returned, read that part. The reference pins only the rows of queries 1 and 2, so restrict the listing to `script_query_number IN (1, 2)`. The reference is unchanged. Related: ClickHouse#120808 Co-Authored-By: Claude Opus 5.5 <[email protected]>
…arter of their rows
No existing query number or text changes: 2 queries (20 numbered entries) are disabled and 4 (20
entries) appended, as 26-45. The two `{type}` queries get `disabled="1"`, which stops all ten of
their expansions; the 16 sub-second ones (master's 0-4, 7-14, 17-19) are appended verbatim through
a new `{narrow_type}` substitution, the four over Int128 and UInt256 (master's 5, 6, 15, 16: 1.49-2.43 s
on MasterCI amd, 1.18-1.50 s on arm) through `{wide_type}` on numbers(500000) instead of 2000000.
Each of the four sorts 2M arrays of 32 16- or 32-byte elements on one thread through the typed fast
path of ClickHouse#109832; user time is linear in rows. The copies keep the same 65409-row blocks (8 instead of
31) and the same per-block work. Every run also pays a fixed fault-in of its block after perf.py's
jemalloc purge, as on MasterCI.
Measured with perf.py on the official MasterCI amd builds, 8 pinned cores, two repetitions on
different NUMA nodes (per-row gates with the L3 capped to 30 MiB, 20 runs per server):
- per-row user time copy / original 0.969-1.006; user-time sort share within 0.016;
- kept fraction of a per-row change 0.897-0.950 (fixed part 21-59 ms per run);
- ClickHouse#117346 (clang 23; MasterCI 0.878 amd / 0.913 arm on the UInt256 sort): original 0.911 / 0.916,
copy 0.922 / 0.916; ClickHouse#120808 on the Int128 reverse sort: original 1.045 / 1.046, copy 1.051 / 1.044.
The Decimal128 query (entry 22) stays enabled: its copy kept under half of ClickHouse#120808's effect in one
of four pass measurements (0.42).
Projected copy medians 0.40-0.66 s amd, 0.31-0.41 s arm: about 21 s (amd) and 15 s (arm) less query
time per master perf job.
Related: ClickHouse#129258
Co-Authored-By: Claude Opus 5.5 <[email protected]>
Closes: #120351
The
addIgnoreOverflow,subIgnoreOverflow,mulIgnoreOverflow, andnegateIgnoreOverflowhelpers currently perform signed arithmetic underNO_SANITIZE_UNDEFINED. The attribute suppresses undefined-behaviordiagnostics, but does not give signed overflow wrapping semantics.
With the current header, a probe for
addIgnoreOverflow(INT_MAX, 1) < INT_MAXreturns true at-O0, but Clang 20and Clang 22 optimize the comparison to false at
-O1,-O2, and-O3.The source and helper names require the wrapped result, and the build does not
globally use
-fwrapvor-fno-strict-overflow.For integer results, this change performs each operation in the unsigned version
of the expression's result type and converts the result back. That defines the
intended modulo arithmetic without relying on a diagnostic-suppression
attribute. Using the expression's result type also preserves integer promotion
for narrow operands. Non-integer result types, including
Decimal, continue touse their existing overloaded operators.
Validation:
masterat 9622a4a;-O0,-O1,-O2, and-O3;the patched header passes at all four levels;
Int8/Int16, signedInt32/Int64/Int128/Int256, and unsignedUInt32/UInt64/UInt128/UInt256;clickhousetarget builds with the official binary-builder andClang 22, including existing
Decimal<Int128>andDecimal<Int256>callers;git diff --checkpasses.The linked issue contains the focused reproducer. I did not find an existing
direct unit-test target for
arithmeticOverflow.hor a stable SQL-level trigger,so this change relies on the focused compiler matrix and project CI.
Changelog category (leave one):
Workflow [PR]
Sync PR [sync-upstream/pr/120808]
Version info
26.10.1.1193-master(included in26.10and later)