Repository navigation
Conversation
Clang zero-fills every automatic variable that has no initializer, so a read before the first write yields zero instead of stale stack contents (a stale pointer reads as null). The flag goes into COMPILER_FLAGS next to -fstack-protector-strong, so it reaches src/, programs/, base/ and contrib, including libc++, libc++abi, libunwind and the glibc compatibility layer; contrib/compiler-rt-cmake resets inherited flags to an allowlist and stays out, as it does for the stack protector. Sanitizer and fuzzer builds do not get it: MemorySanitizer reports exactly the reads it would turn into zeros, UndefinedBehaviorSanitizer reports the invalid bool and enum values such reads load, and a fuzzing build exists to find bugs. In the musl build, the objects that already carry -fno-stack-protector (the CRT start file and musl's early-init and dynamic linker objects) also get -ftrivial-auto-var-init=uninitialized: with the flag, __dls2, load_library and map_library in ldso/dynlink.c gained calls to memset, and stage 2 of the dynamic linker must not call extern symbols. With the opt-out, all 51 functions of those objects compile to the same code as without the flag. Measured on a local amd64 build with the amd_release PR configuration: 17,285 of 17,643 compile commands carry the flag (the rest are compiler-rt and isa-l's NASM sources), the MSan, ASan+UBSan and fuzzer configurations carry it nowhere, our_syscall, remapToHugeStep2/3 and the memcpy/memmove/memset/memcmp/bcmp implementations disassemble identically, and .text grows by 3.1 MB (+0.58%), the stripped binary by 3.2 MB (+0.43%), the compressed one by 0.4 MB (+0.18%). Closes ClickHouse#127891 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; 0 findings) and my own review (2 nits, not blocking).
Severity: ❌ blocker / Session id: cron:clickhouse-review-slot-9:20261027-022000 |
Pre-PR validation gate (click to expand)
Session id: cron:clickhouse-impl-slot-5:20261026-223400 |
|
cc @pamarcos @thevar1able, could you review this and add the |
|
Workflow [PR], commit [93458ba] Summary: ✅
AI ReviewSummaryThe PR enables Findings
Tests
Final Verdict
|
…init=zero` Performance Comparison on the flag-only head (632fd7f) reported 7 slower queries. Three of them come from a per-row zeroing of a stack buffer in our own code, found with `-Rpass-missed=annotation-remarks` and the disassembly: - `ImplBLAKE3::apply` `hasher`: a 1776-byte `memset` per row (the BLAKE3 CV stack). cryptographic_hashes ClickHouse#23 (BLAKE3) was 31% slower on amd_release. Local amd64 release build, 10M rows, one thread: base 0.680 s, flag 0.893 s, annotated 0.682 s. - `formatReadable` (`src/Common/formatReadable.cpp`) `buffer`: 163 bytes per row. format_readable #0/ClickHouse#1 (formatReadableSize/Quantity) were 17% slower on arm_release. Local amd64: formatReadableSize base 0.230 s, flag 0.248 s, annotated 0.242 s; the rest is `decimal_rep` in double-conversion's `ToFixed`, which is upstream library code and is not annotated here. - `FunctionFormatReadableTimeDelta::getFractionalString` `buffer`: 163 bytes per row. format_readable ClickHouse#2 (formatReadableTimeDelta) was 27% slower on arm_release (7% faster on amd_release). Each buffer is read only after it is written: the converters write the first `builder.position()` bytes, and `llvm_blake3_hasher_init` writes every field of the hasher except the CV stack, whose entries are pushed before they are read. Not annotated, because no ClickHouse stack buffer explains them: arithmetic ClickHouse#58 (amd: the `intDivOrZero` Float64 kernels disassemble identically with and without the flag, and the query is not slower locally), ip_trie ClickHouse#2 (arm: the lookup path zeroes at most 16 bytes per row), and collations ClickHouse#1 (arm: the zeroing is in ICU's `RuleBasedCollator::doCompare` iterators, ~1 KB per comparison, inside `contrib/icu`). Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
The description now has the Performance Comparison results before and after the annotations. The functional tests have not run yet, because |
|
The label change did not start a new workflow run, so the functional tests have still not run on 420eac3. Could someone re-run the workflow? The description now has the second Performance Comparison run of that head. Neither of its new slower queries on arm comes from a zeroed buffer: |
…init=zero` The remaining arm_release slowdowns on this PR come from contrib code: `codecs_float_select` ClickHouse#11 (+24%) and `ip_trie` ClickHouse#2 (+18%) decode `ZSTD`, and `collations` ClickHouse#1 (+16%) compares strings with ICU. - contrib/zstd -> ClickHouse/zstd#6 (0c4a7b5): `seqState`, the `sequences` ring and the decoded `seq_t` temporaries of the sequence decoders. `ZSTD_decompressSequences`, `...SplitLitBuffer` and `...Long` then compile to the same code as without the flag on aarch64 and x86-64. - contrib/icu -> ClickHouse/icu#3 (e16b61b): the two collation iterators of the UTF-8 `RuleBasedCollator::doCompare`, 400 or 496 bytes each, zero-filled on every comparison that leaves the Latin fast path. Local amd64 release builds, flag only vs with these annotations (max_threads=1, 3 x 7 runs): ORDER BY ... COLLATE 'tr' on 2M Cyrillic strings 0.131 s -> 0.113-0.119 s; the ZSTD Float64 scan returns to 0.183 s, as without the flag (0.164 s with it). Query results are identical (8 collations, zstd levels 3 and 19 with a 128 MiB window, a ZSTD MergeTree column). Co-Authored-By: Claude Opus 5.5 <[email protected]>
With `-ftrivial-auto-var-init=zero`, `Field::destroyContainerIteratively` starts with a 904-byte `memset` of the inline buffer of `to_destroy`, and it runs whenever an `Array`, `Tuple`, `Map` or `Object` `Field` is destroyed. Primary key analysis over a `Tuple` key destroys such a `Field` per range, which is what `promql_selector_pk_range` ClickHouse#2 measures (+14% to +16% on arm_release in each run of this PR that sampled it). Local amd64, the CI binaries of this PR (c51a35f) and of the master reference, `EXPLAIN indexes = 1` over the 80K marks of that test's samples table with a continuous `id` range, one thread: reference 0.090 s, c51a35f 0.103 s. With local release builds: 0.108 s without this change, 0.098 s with it; the memset's share of the profile goes from 4.5% to 0.6% (0.3% in the reference). Query results are identical (9 stateless tests on tuples, maps and deeply nested values, a 100000-level array literal, the PromQL queries). Co-Authored-By: Claude Opus 5.5 <[email protected]>
| # Sanitizer reports exactly such reads, and Undefined Behavior Sanitizer the invalid `bool` and enum values | ||
| # they load. contrib/compiler-rt-cmake resets it, like the stack protector above. | ||
| if (NOT SANITIZE AND NOT ENABLE_FUZZING) | ||
| set (COMPILER_FLAGS "${COMPILER_FLAGS} -ftrivial-auto-var-init=zero") |
There was a problem hiding this comment.
[P1] Close the reproduced primary-key slowdown before global enablement
The remaining AMD long_primary_key slowdown needs final-head validation before enabling this globally. At c51a35f, query 0, time increases from 0.1490 s to 0.2011 s (+34.9%); query 1 increases from 0.1492 s to 0.2000 s (+34.0%). Query 0 also slowed by 11.9% and 11.35% in the two earlier PR runs. Its latest seven-sample comparison estimates a 32.9–36.6% slowdown, and recent master p95 is about 0.173 s. The description independently reproduces slower index analysis locally.
The code-layout diagnosis still leaves this workload cost unresolved. The current Field exemption addresses container teardown, while these queries filter scalar DateTime/String keys; performance results for 93458ba are pending, so whether that revision also removes this slowdown remains unverified. Please compare these queries on the final AMD release binaries and mitigate the slowdown if it persists before unconditional rollout.
CI finish ledger - 93458baEvery failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
All 12 Session id: cron:our-pr-ci-monitor:20261028-113007 |
|
🕵 @snelheidai, please independently challenge the final-head performance evidence for this PR; no implementation change is requested yet. Scope:
Do not repeat identical CI shards unless the existing artifacts are insufficient, and do not propose another exemption without a concrete hot-path profile showing which zeroing causes the cost. |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
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. The official master build is compiled with Object file sizes12362 object files changed (+3.13 MiB total), 0 added.
719 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 units16494 translation units recompiled, 33018 s compile time in total, 15953 of them have a recent master baseline.
Slowest changed entities of
Translation units without a recent master baseline:
|
Closes: #127891
Related: ClickHouse/zstd#6
Related: ClickHouse/icu#3
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Harden official builds by compiling with
-ftrivial-auto-var-init=zero, so that an automatic variable that is read before it is written yields zero instead of stale stack contents.Description
-ftrivial-auto-var-init=zerogoes intoCOMPILER_FLAGSnext to-fstack-protector-strong, so it reachessrc/,programs/,base/andcontrib(exceptcontrib/compiler-rt-cmake, which resets flags). Sanitizer and fuzzer builds do not get it: MemorySanitizer and UndefinedBehaviorSanitizer report exactly the reads it would hide. The musl startup and dynamic linker objects, which already get-fno-stack-protector, keep their locals uninitialized: the flag addedmemsetcalls to stage 2 ofldso/dynlink.c, which must not call extern symbols.These locals made queries slower when zeroed, so they are marked uninitialized; each is written before it is read:
hasherinImplBLAKE3::apply(1776 bytes per row)bufferinformatReadableand inFunctionFormatReadableTimeDelta::getFractionalString(163 bytes each)to_destroyinField::destroyContainerIteratively(904 bytes per destroyedArray,Tuple,MaporObjectField)RuleBasedCollator::doCompare, Keep the UTF-8 comparison iterators uninitialized under -ftrivial-auto-var-init icu#3Geomean of new/old time over all queries, and slower queries:
cryptographic_hashes#23 +31%,arithmetic#58 +23%format_readable#0 +17%, #1 +17%, #2 +27%,collations#1 +16%,ip_trie#2 +16%codecs_float_select#11 +24%,ip_trie#2 +18%,decimal_casts#7 +17%,collations#1 +16%long_primary_key#0, #1, #2 +35%,arithmetic#58 +20%promql_selector_pk_range#2 +16%Field(93458ba)long_primary_key#1, #2 +15%Binary size of the compressed
clickhousefrom CI, compared with master: amd +0.22%, arm +0.27%.In a 5% slice of the stateless tests, no test fails with the flag and passes without it. No test is added: the flag only changes the result of reading an uninitialized variable, which is undefined behaviour.
Local measurements and the slower queries that are not annotated
COLLATE 'tr'sort of 2M Cyrillic strings 0.131 s with the flag, 0.113-0.119 s with the ICU change; index analysis of theTuplekey ofpromql_selector_pk_range(80K marks) 0.108 s with the flag, 0.098 s with theFieldchange, 0.090 s with the master binary from CI.codecs_float_selectUDF support #11,ip_trieRepair subtree2 #2 (defaultZSTDcodec): zeroing the per-block decoder state changed the code of zstd's sequence decoders; with the zstd change they compile as without the flag.long_primary_key(amd): index analysis runs +1.2-1.6% instructions but +18-56% cycles in the CI binaries, all front-end resteers (BACLEARS0.05-0.06M per query in 6 builds without the flag, 3-19M in 8 builds with it). The flag adds ~320 KiB of code betweenRange.cppandKeyCondition.cpp, which puts their hot branches 64 MiB apart, where they evict each other in the branch target buffer (master: 64 MiB - 329 KiB). ShiftingKeyCondition.cppby 128 KiB in a local build with the flag removes the resteers (BACLEARS0.05M, index analysis 0.135 s -> 0.113 s, CI master 0.109 s). This is code layout, not zeroing, so nothing is annotated for it.arithmeticLEFT JOIN ....ON SUPPORT #58: theintDivOrZerokernels compile identically, and the query is not slower locally with the CI binaries.decimal_castsError fetching data from materialized view #7: identical conversion loops in the two arm binaries, also reported slower on other PRs.format_readableRepair subtree2 #2: +27% -> +7% on arm with the annotation; on amd the annotated runs show +8-10% with identical code in the hot loop.🤖 Generated with Claude Code