Skip to content

Add compiler and linker flags for section-based optimizations - #99474

Merged
alexey-milovidov merged 13 commits into
masterfrom
add-linker-optimizations
Mar 26, 2026
Merged

alexey-milovidov merged 13 commits into
masterfrom
add-linker-optimizations

Conversation

@alexey-milovidov

@alexey-milovidov alexey-milovidov commented Mar 13, 2026 •

Copy link
Copy Markdown
Member

Add -ffunction-sections and -fdata-sections to compiler flags for the main ClickHouse code (previously only set for contrib libraries). Together with the already-present -Wl,--gc-sections on Linux and newly added -Wl,-dead_strip on Darwin, this lets the linker strip unused functions and data.

Add -faddrsig (Linux only) to emit address-significance tables, enabling the linker to safely identify which symbols can be folded.

Add -Wl,--icf=all (lld-only, Linux) to fold identical code and data sections, reducing binary size.

Changelog category (leave one):

  • Performance Improvement

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

Enable linker section-based optimizations (-ffunction-sections, -fdata-sections, --icf=all) to reduce binary size and improve instruction cache utilization.

Documentation entry for user-facing changes

  • Documentation is written (mandatory for new features)

Version info

  • Merged into: 26.4.1.301

…-icf=all`

These were previously only set for contrib libraries but not for the main
ClickHouse code. Together with the already-present `-Wl,--gc-sections`, this
enables the linker to remove unused functions/data and fold identical sections,
reducing binary size.

- `-ffunction-sections` / `-fdata-sections`: place each function and data item
  into its own ELF section.
- `-faddrsig`: emit an address-significance table so the linker can safely
  distinguish address-taken symbols during ICF.
- `-Wl,--icf=all` (lld only): fold identical code and data sections.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

It's unclear what the effect will be. Please check and compare the resulting binary size.

@clickhouse-gh

clickhouse-gh Bot commented Mar 14, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [1ceaf35]

Summary: ✅


AI Review

Summary

This PR adds section-based linker/compiler optimizations (-ffunction-sections, -fdata-sections, -faddrsig, --icf=all) and Darwin -dead_strip to reduce binary size. High-level verdict: there is a serious compilation-time risk on Darwin from enabling -dead_strip globally; based on the PR discussion data, this should be gated or disabled until validated.

Missing context
  • ⚠️ No authoritative Praktika performance/build report was provided in this review context to quantify linker-time regression beyond discussion comments.
Findings
  • ⚠️ Majors
    • [CMakeLists.txt:165] -Wl,-dead_strip is now enabled for all Darwin builds, and PR discussion already reports large Darwin build-time regressions (amd_darwin ~12 -> 37 min, arm_darwin ~10 -> 27 min). This is a ClickHouse compilation-time risk and can significantly slow CI/developer feedback loops.
    • Suggested fix: gate Darwin dead-stripping behind a setting (default OFF) or temporarily restrict it to Linux-only until measured acceptable in CI.
ClickHouse Rules
Item Status Notes
Deletion logging ➖
Serialization versioning ➖
Core-area scrutiny ➖
No test removal ✅
Experimental gate ➖
No magic constants ✅
Backward compatibility ✅
SettingsChangesHistory.cpp ➖
PR metadata quality ✅
Safe rollout ⚠️ Darwin rollout currently shows large build-time regression in discussion data
Compilation time ⚠️ -Wl,-dead_strip on Darwin appears to significantly increase build time
Performance & Safety
  • Darwin linker-time performance is a concern: enabling -Wl,-dead_strip globally appears to create a substantial CI build-time increase, which is a rollout risk even if runtime behavior is unchanged.
Final Verdict
  • Status: ⚠️ Request changes
  • Minimum required actions:
    • Gate or disable Darwin -dead_strip path until CI confirms acceptable build-time overhead.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Mar 14, 2026
Comment thread CMakeLists.txt Outdated
# remove unused ones (--gc-sections) and fold identical ones (--icf).
# -faddrsig emits an address-significance table that lets the linker's ICF
# distinguish address-taken symbols from non-address-taken ones.
set(COMPILER_FLAGS "${COMPILER_FLAGS} -ffunction-sections -fdata-sections -faddrsig")

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.

-faddrsig is added unconditionally to COMPILER_FLAGS, but GCC does not support this flag (g++: error: unrecognized command-line option -faddrsig\). This will break non-Clang builds. Please gate it behind a compiler check (e.g. check_cxx_compiler_flag("-faddrsig" HAS_FADDRSIG) and append only when supported), or restrict it to the Clang/lld path.

@clickhouse-gh clickhouse-gh Bot added the submodule changed At least one submodule changed in this PR. label Mar 16, 2026
Comment thread contrib/CMakeLists.txt Resolved
alexey-milovidov and others added 2 commits March 16, 2026 08:01
Sanitizer builds do not benefit from `-ffunction-sections`, `-fdata-sections`,
`-faddrsig`, and `--icf=all` (binary size is irrelevant), and the increased
number of ELF sections hurts instruction-cache locality. This caused
`ConfigProcessorManyElements` to exceed its 300 s timeout under UBSan (444 s)
and `ResolvePoolTest.StillBannedAfterSuccess` to blow its 5 ms timing budget.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
The `amd_llvm_coverage` unit tests fail with a timeout in
`CoordinationTest_TestStateMachineAndLogStore_Test` because the extra
ELF sections from `-ffunction-sections`/`-fdata-sections` hurt
instruction-cache locality in instrumented builds, same as sanitizers.
Also exclude coverage from `--icf=all` since ICF could merge functions
with distinct coverage counters.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
Comment thread CMakeLists.txt

if (LINKER_NAME MATCHES "lld" AND NOT SANITIZE AND NOT SANITIZE_COVERAGE AND NOT WITH_COVERAGE)
# Fold identical code and data sections to reduce binary size.
set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -Wl,--icf=all")

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.

Using --icf=all here can silently change pointer-identity semantics unless all object files provide reliable address-significance metadata. In this PR, -faddrsig is currently unsupported on GCC (already noted separately), so once that is gated, a GCC+lld build could still enable --icf=all without addrsig coverage and risk incorrect function/object address comparisons.

Please either:

  1. downgrade to -Wl,--icf=safe, or
  2. gate --icf=all on confirmed -faddrsig support for all compiled objects in this target.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We don't use gcc.

@alexey-milovidov

Copy link
Copy Markdown
Member Author

Binary Size Impact

This PR adds -ffunction-sections, -fdata-sections, -faddrsig, and -Wl,--icf=all to reduce binary size through section-based GC and identical code folding.

Stripped binary comparison (clickhouse-common-static .deb)

Architecture Master PR Delta
amd64 226,743,608 (216.2 MB) 212,480,540 (202.6 MB) -14.3 MB (-6.3%)
aarch64 208,993,618 (199.3 MB) 196,217,900 (187.1 MB) -12.8 MB (-6.1%)

Debug symbols (clickhouse-common-static-dbg .deb)

Reported as ~87% smaller, though this dramatic reduction may partly reflect changes in how debug info is structured when using -ffunction-sections + ICF, rather than pure size savings.

Bottom line: The PR achieves a meaningful ~6% reduction (~13-14 MB) in the stripped release binary on both architectures. The raw binary sizes between master and PR are not directly comparable due to differences in how debug info is split/packaged.

@Algunenano Algunenano self-assigned this Mar 18, 2026
@Algunenano

Copy link
Copy Markdown
Member

Reported as ~87% smaller, though this dramatic reduction may partly reflect changes in how debug info is structured when using -ffunction-sections + ICF, rather than pure size savings.

What were you comparing against? PRs build without symbols because linking with LTO without it is much faster, and we only build them in master.

Comment thread CMakeLists.txt
# -faddrsig emits an address-significance table that lets the linker's ICF
# distinguish address-taken symbols from non-address-taken ones.
# Sanitizer and coverage builds do not benefit from these size optimizations
# and the extra sections hurt instruction-cache locality in hot loops,

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.

These three flags are applied without an OS_LINUX guard, so they also affect the macOS Darwin cross-compilation builds (which use Apple's ld64 via cctools-port, not lld).

On Darwin:

  • --gc-sections is not added (correctly guarded inside if (OS_LINUX))
  • --icf=all is not added (correctly guarded inside if (OS_LINUX) + LINKER_NAME MATCHES "lld")
  • But -ffunction-sections / -fdata-sections have no effect without -dead_strip, and -faddrsig emits __LLVM,__addrsig sections that ld64 does not understand and cannot strip

Empirically verified by comparing Build (amd_darwin) binary sizes between this PR and PR #99662 as a same-master baseline:

  • amd_darwin/clickhouse: +166 KB (+0.10%) (larger, not smaller)
  • arm_darwin/clickhouse: +114 KB (+0.07%) (larger, not smaller)

The ideal fix would be to also add -Wl,-dead_strip for Darwin (ld64 supports it and it enables dead stripping at the function-atom level using -ffunction-sections), which would unlock the same GC benefit on macOS. Short of that, these three flags should be guarded with OS_LINUX to avoid the small regression on Darwin builds.

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

Comparing against PR #99662 as a baseline (same master, no flag changes):                                                                
                                                                                                                                         
┌──────────────────────────────────────────────────────┬────────────────────────┬──────────────────────┬──────────────────┐              
│                       Artifact                       │ PR #99474 (with flags) │ PR #99662 (baseline) │      Delta       │              
├──────────────────────────────────────────────────────┼────────────────────────┼──────────────────────┼──────────────────┤              
│ amd_release/clickhouse (raw)                         │ 233,800,568 B          │ 240,752,320 B        │ −6.95 MB (−2.9%) │
├──────────────────────────────────────────────────────┼────────────────────────┼──────────────────────┼──────────────────┤
│ amd_release/clickhouse-common-static (stripped deb)  │ 212,480,540 B          │ 226,474,182 B        │ −14.0 MB (−6.2%) │              
├──────────────────────────────────────────────────────┼────────────────────────┼──────────────────────┼──────────────────┤              
│ amd_release/clickhouse-common-static-dbg (debug deb) │ 111,531,442 B          │ 113,502,242 B        │ −1.97 MB (−1.7%) │              
├──────────────────────────────────────────────────────┼────────────────────────┼──────────────────────┼──────────────────┤              
│ arm_release/clickhouse (raw)                         │ 223,469,514 B          │ 229,951,524 B        │ −6.48 MB (−2.8%) │
├──────────────────────────────────────────────────────┼────────────────────────┼──────────────────────┼──────────────────┤              
│ arm_release/clickhouse-common-static-dbg (debug deb) │ 112,243,752 B          │ 114,562,822 B        │ −2.32 MB (−2.0%) │
└──────────────────────────────────────────────────────┴────────────────────────┴──────────────────────┴──────────────────┘              

The stripped package shrinks ~6% (14 MB) while the raw binary shrinks ~3%. The larger reduction in the stripped binary confirms the      
effect is mainly code elimination/folding, not debug info. Real reduction.
                            
  Build Time Impact (independently verified)
                                                                                                                                           
  ┌─────────────────────┬───────────┬──────────────────────┬────────────────┐
  │      Build job      │ PR #99474 │ PR #99662 (baseline) │     Delta      │                                                              
  ├─────────────────────┼───────────┼──────────────────────┼────────────────┤
  │ Build (amd_binary)  │ 707.6s    │ 595.5s               │ +112s (+18.8%) │
  ├─────────────────────┼───────────┼──────────────────────┼────────────────┤
  │ Build (arm_binary)  │ 672.7s    │ 422.1s               │ +250s (+59.3%) │                                                              
  ├─────────────────────┼───────────┼──────────────────────┼────────────────┤                                                              
  │ Build (amd_release) │ 1923.4s   │ 1688.2s              │ +235s (+13.9%) │                                                              
  ├─────────────────────┼───────────┼──────────────────────┼────────────────┤                                                              
  │ Build (arm_release) │ 1703.4s   │ 1661.5s              │ +42s (+2.5%)   │
  └─────────────────────┴───────────┴──────────────────────┴────────────────┘                                                              
                              
  The arm_binary build time is 59% longer. The amd_release build is 14% longer. ARM has a much larger absolute delta in the non-ThinLTO    
  binary configuration because --icf=all runs single-threaded during the link step there. While the ThinLTO release builds absorb the cost
  better, the non-release configurations used for integration tests are materially slower to link.                                         

Since the build times are highly depending on hitting cache or not, and this introduced new flags (which are obviously not cached) I don't think this is critical. We can check again a new run to confirm.

  Performance Impact (from CI)
                              
  Only ARM performance tests ran (AMD perf comparisons were skipped):
  - 18 queries faster, 3 slower (2,103 total) — no statistically significant regression                                                    
  - The 3 slower queries (apply_patch_parts_join, 4–7ms range) are at timescales where ±50% variance is normal noise                       
  - Several queries improved 10–64%: rand, general_purpose_hashes, aggregation_external, array_fill — consistent with smaller icache       
  footprint from ICF                                                                                                                       
                                                                                                                                           
  No AMD performance data — the PR claims a binary size/performance win but only ARM data is present to support it.                        

Let's tag this as a performance PR and make some change so it reruns and does both AMD and ARM performance comparison to confirm the impact it might have.

So my recommendation: Change the PR to a Performance improvement, push the change to improve Darwin too, and we can check performance and build time in the new commit

- Add `-Wl,-dead_strip` for Darwin builds (equivalent of `--gc-sections` on
  Linux) so that `-ffunction-sections`/`-fdata-sections` actually enable dead
  code elimination on macOS.
- Guard `-faddrsig` with `OS_LINUX` in both the main build and contrib, since
  the address-significance tables are only consumed by lld's `--icf=all` and
  Darwin's ld64 does not understand them (causing a small binary size increase).

#99474 (review)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@clickhouse-gh clickhouse-gh Bot added pr-performance Pull request with some performance improvements and removed pr-build Pull request with build/testing/packaging improvement labels Mar 18, 2026
@Algunenano

Copy link
Copy Markdown
Member
---                                                                                                                                    
  Binary Size (independently verified)                                                                                                   
                                                                                                                                         
  ┌──────────────────────────┬───────────┬──────────┬────────────────────┐                                                               
  │          Build           │ PR #99474 │ Baseline │       Delta        │                                                               
  ├──────────────────────────┼───────────┼──────────┼────────────────────┤                                                               
  │ amd_release/clickhouse   │ 222.8 MB  │ 229.6 MB │ −7.1 MB (−2.95%)   │                                                               
  ├──────────────────────────┼───────────┼──────────┼────────────────────┤                                                               
  │ arm_release/clickhouse   │ 213.0 MB  │ 219.3 MB │ −6.6 MB (−2.87%)   │                                                               
  ├──────────────────────────┼───────────┼──────────┼────────────────────┤                                                               
  │ amd_release stripped deb │ 202.7 MB  │ 216.0 MB │ −14.0 MB (−6.17%)  │                                                               
  ├──────────────────────────┼───────────┼──────────┼────────────────────┤                                                               
  │ amd_darwin/clickhouse    │ 138.7 MB  │ 156.5 MB │ −18.7 MB (−11.38%) │                                                               
  ├──────────────────────────┼───────────┼──────────┼────────────────────┤                                                               
  │ arm_darwin/clickhouse    │ 130.3 MB  │ 147.5 MB │ −18.0 MB (−11.64%) │                                                               
  └──────────────────────────┴───────────┴──────────┴────────────────────┘                                                               
                                                                                                                                       
  Linux results are consistent with the first revision (~3% raw, ~6% stripped). The macOS Darwin builds now show massive reductions of   
  11–12% thanks to -dead_strip — this is expected because ThinLTO is disabled for Darwin builds, so there was more dead code that hadn't
  been removed before.                                                                                                                   
                                                                                                                                       
  ---
  Build Time

  ┌─────────────┬───────────┬──────────┬────────────────┐
  │    Build    │ PR #99474 │ Baseline │     Delta      │
  ├─────────────┼───────────┼──────────┼────────────────┤
  │ amd_release │ 28.9m     │ 28.1m    │ +48s (+2.9%)   │
  ├─────────────┼───────────┼──────────┼────────────────┤
  │ arm_release │ 28.2m     │ 27.7m    │ +29s (+1.7%)   │                                                                                
  ├─────────────┼───────────┼──────────┼────────────────┤
  │ amd_binary  │ 10.4m     │ 9.9m     │ +31s (+5.1%)   │                                                                                
  ├─────────────┼───────────┼──────────┼────────────────┤                                                                                
  │ arm_binary  │ 9.5m      │ 7.0m     │ +150s (+35.4%) │
  ├─────────────┼───────────┼──────────┼────────────────┤                                                                                
  │ amd_darwin  │ 36.5m     │ 11.9m    │ +1478s (+207%) │                                                                              
  ├─────────────┼───────────┼──────────┼────────────────┤                                                                                
  │ arm_darwin  │ 27.4m     │ 10.3m    │ +1028s (+167%) │                                                                              
  └─────────────┴───────────┴──────────┴────────────────┘                   
  Linux release builds: +2–3% — acceptable tradeoff for 3–6% binary reduction.                                                           
                                                                                                                                       
  arm_binary: +35% — this is the non-ThinLTO build used for integration tests. Still a significant regression (down from +59% in the     
  previous revision, but still adds 2.5 minutes).                                                                                      
                                                                                                                                         
  macOS Darwin builds: +167–207% — the builds tripled in time. amd_darwin went from 12 minutes to 37 minutes, arm_darwin from 10 to 27   
  minutes. Apple's ld64 (via cctools-port) is not optimized for dead-stripping large binaries with many per-function sections. This is a
  severe CI regression.                                                                                                                  

As expected, the CI builds normalized once ccache had any effect. I expect Darwin would do the same

Performance


  PR #99474 Performance Report

  AMD — Slower

  ┌───────────┬───────────────────────────────┬───────────────────────────┬────────────────────────┐
  │ Magnitude │             Test              │ Master slower/total (30d) │        Verdict         │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.98x     │ apply_patch_parts_join #5     │ 8/687 + 104 unstable      │ Flaky                  │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.69x     │ destroy_aggregate_states #0   │ 4/685 + 7 unstable        │ Occasionally flaky     │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.41x     │ remove_unused_columns #0      │ 0/685 (10 unstable)       │ Never slower on master │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.22x     │ re2_regex_caching #6          │ 4/686 + 15 unstable       │ Occasionally flaky     │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.22x     │ re2_regex_caching #5          │ 6/686 + 12 unstable       │ Occasionally flaky     │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.22x     │ re2_regex_caching #3          │ 5/686 + 14 unstable       │ Occasionally flaky     │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.20x     │ group_by_multiple_strings #14 │ 0/243                     │ Never on master        │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.18x     │ window_functions #2           │ 0/632 (1 unstable)        │ Never on master        │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.17x     │ ascii #1                      │ 38/496 (7.7%)             │ Frequently flaky       │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.15x     │ ascii #8                      │ 8/489 (1.6%)              │ Flaky                  │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.13x     │ string_bytes_functions #0     │ 0/687 (9 unstable)        │ Never on master        │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.13x     │ string_bytes_functions #1     │ 1/687                     │ Essentially never      │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.12x     │ join_squashing_blocks #1      │ 2/683                     │ Rare on master         │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.11x     │ isValidASCII #2               │ 0/685                     │ Never on master        │
  ├───────────┼───────────────────────────────┼───────────────────────────┼────────────────────────┤
  │ 1.10x     │ ascii #10                     │ 12/478 (2.5%)             │ Flaky                  │
  └───────────┴───────────────────────────────┴───────────────────────────┴────────────────────────┘

  AMD — Faster

  ┌───────────┬───────────────────────────────┬──────────────────────────────────────┬─────────────────────────────────────────┐
  │ Magnitude │             Test              │         Master faster/total          │                 Verdict                 │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.40x     │ array_fill #2                 │ 0 faster, 1 slower, 8 unstable / 685 │ Likely real improvement                 │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.35x     │ compact_part_subcolumns #0    │ 4 faster, 51 unstable / 685          │ Unstable on master                      │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.19x     │ array_fill #3                 │ 0 faster, 8 slower / 685             │ Real improvement (was slower on master) │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.16x     │ function_tokens #0            │ 0 faster, 6 slower / 685             │ Real improvement                        │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.15x     │ group_by_multiple_strings #17 │ 0/217                                │ Never on master — real improvement      │
  ├───────────┼───────────────────────────────┼──────────────────────────────────────┼─────────────────────────────────────────┤
  │ 1.13x     │ join_runtime_filter #1        │ 0/551                                │ Never on master — real improvement      │
  └───────────┴───────────────────────────────┴──────────────────────────────────────┴─────────────────────────────────────────┘

  ARM — Slower

  ┌───────────┬─────────────────────────────┬────────────────────────┬────────────────────┐
  │ Magnitude │            Test             │  Master slower/total   │      Verdict       │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 3.37x     │ sort_patterns #7            │ 23/1352 + 79 unstable  │ Flaky              │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 2.69x     │ optimized_select_final #0   │ 59/1355 (4.4%)         │ Frequently flaky   │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.30x     │ classification #4           │ 4/1352 + 71 unstable   │ Flaky/unstable     │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.23x     │ reading_from_file #0        │ 18/1351 + 699 unstable │ Extremely unstable │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.19x     │ formats_columns_sampling #9 │ 12/1221 (1%)           │ Occasionally flaky │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.18x     │ window_fns_downstream_mt #1 │ 7/1350 + 12 unstable   │ Occasionally flaky │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.17x     │ parse_engine_file #9        │ 0/799                  │ Never on master    │
  ├───────────┼─────────────────────────────┼────────────────────────┼────────────────────┤
  │ 1.11x     │ join_reserve #0             │ 66/1354 (4.9%)         │ Frequently flaky   │
  └───────────┴─────────────────────────────┴────────────────────────┴────────────────────┘

  ARM — Faster

  ┌───────────┬──────────────────────────────────────┬───────────────────────────────────┬────────────────────────────────────┐
  │ Magnitude │                 Test                 │        Master faster/total        │              Verdict               │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 2.75x     │ sort_patterns #3                     │ 0 faster, 31 slower + 96 unstable │ Flaky (usually slower!)            │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 2.74x     │ random_fixed_string #3               │ 0 faster, 39 unstable             │ Likely real improvement            │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.47x     │ apply_patch_parts_join #4            │ 0 faster, 8 slower + 369 unstable │ Extremely unstable                 │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.46x     │ apply_patch_parts_join #6            │ 0 faster, 8 slower + 374 unstable │ Extremely unstable                 │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.21x     │ index_bulk_filtering #2              │ 0 faster, 10 slower + 40 unstable │ Unstable                           │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.13x     │ general_purpose_hashes #16           │ 0/154                             │ Never on master — real improvement │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.13x     │ join_convert_outer_to_inner #3       │ 0 faster, 13 slower               │ Real improvement (was slower!)     │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.12x     │ vectorize_aggregation_combinators #0 │ 0 faster, 107 slower              │ Real improvement (was slower!)     │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.12x     │ apply_patch_parts_join_int #1        │ 0 faster, 103 slower              │ Real improvement (was slower!)     │
  ├───────────┼──────────────────────────────────────┼───────────────────────────────────┼────────────────────────────────────┤
  │ 1.12x     │ function_tokens #0                   │ 0 faster, 26 slower               │ Real improvement (was slower!)     │
  └───────────┴──────────────────────────────────────┴───────────────────────────────────┴────────────────────────────────────┘

  ---
  Summary

  Genuine regressions to investigate (never/rarely on master):

  AMD:
  - remove_unused_columns #0 — 1.41x, never on master
  - group_by_multiple_strings #14 — 1.20x, never on master
  - window_functions #2 — 1.18x, never on master
  - string_bytes_functions #0,#1 — 1.13x, never/rarely on master
  - isValidASCII #2 — 1.11x, never on master

  ARM:
  - parse_engine_file #9 — 1.17x, never on master

  Genuine improvements (never faster on master before):

  AMD: array_fill #2 (1.40x), group_by_multiple_strings #17 (1.15x), join_runtime_filter #1 (1.13x)

  ARM: general_purpose_hashes #16 (1.13x), vectorize_aggregation_combinators #0 (1.12x), join_convert_outer_to_inner #3 (1.13x),
  apply_patch_parts_join_intersecting #1 (1.12x), function_tokens #0 (1.12x) — several of these were previously slower on master, so this PR
   genuinely fixes them.

In general it looks good. We might want to look into some of the regressions just in case.

alexey-milovidov added a commit that referenced this pull request Mar 21, 2026
Prefer `ld64.lld` over Apple's `ld` (cctools-port ld64) for macOS
builds. This should significantly reduce Darwin link times, as
cctools-port's ld64 is very slow with `-ffunction-sections` and
`-dead_strip`. Falls back to `ld` if `ld64.lld` is not available.

Works for both native macOS builds and cross-compiled builds from Linux.

#99474 (comment)

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
@alexey-milovidov

Copy link
Copy Markdown
Member Author

macOS Darwin builds: +167–207% — the builds tripled in time. amd_darwin went from 12 minutes to 37 minutes, arm_darwin from 10 to 27 minutes. Apple's ld64 (via cctools-port) is not optimized for dead-stripping large binaries with many per-function sections. This is a severe CI regression.

Switched to LLD.

Genuine regressions to investigate

The AArch64 build has only some random regression. The AMD64 build has more random regressions - just in case, I checked all these regressions on an AArch64 server, and none of them were confirmed.

Comment thread CMakeLists.txt
endif ()

if (OS_DARWIN)
# Remove unreachable functions/data (Darwin equivalent of --gc-sections).

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.

⚠️ This change enables Darwin dead-stripping (-Wl,-dead_strip) together with global -ffunction-sections/-fdata-sections, and in this PR discussion it already showed a large Darwin build-time regression (amd_darwin about 12 -> 37 min, arm_darwin about 10 -> 27 min).

For ClickHouse, compilation time regressions are a required review check. Please gate this optimization off on Darwin for now (or behind an opt-in setting) until we have clear CI evidence that linker-time impact is acceptable.

@alexey-milovidov
alexey-milovidov merged commit 4eae2d8 into master Mar 26, 2026
149 checks passed
@alexey-milovidov
alexey-milovidov deleted the add-linker-optimizations branch March 26, 2026 22:54
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Mar 26, 2026
pull Bot pushed a commit to Lobinson1/ClickHouse that referenced this pull request Sep 28, 2026
`-Wl,--icf=all` (ClickHouse#99474) lets lld fold byte-identical functions even
when their address is taken, which breaks the guarantee that different
functions have different addresses. In ThinLTO builds `gpr_free` is
inlined into gRPC's zlib free callback `zfree_gpr`, which becomes
`mov %rsi,%rdi; jmp free`, byte-identical to zlib-ng's default `zcfree`,
so both get one address. Measured with llvm-nm: the official 26.8.10.6
image and a local ThinLTO build of master fold them (12 functions share
that address locally); 26.3.33.24 and the no-LTO amd_binary do not.

zlib-ng 2.1.7 (`zutil.c`) chooses the buffer layout by comparing the
stream's callbacks with its defaults. `alloc_aligned` sees
`zalloc_gpr != zcalloc`, allocates 8 + n + 64 bytes and returns the
64-byte aligned pointer with the original one stored below it.
`free_aligned` sees `zfree_gpr == zcfree` and frees the aligned pointer
itself. jemalloc places a large allocation at a random cache line of the
extent's first page (JEMALLOC_CACHE_OBLIVIOUS); when it is the last line
(1 in 64), the freed pointer is the extent's second page, which the emap
does not register, and `ifree` -> `prof_free` reads `e_prof_tctx` from a
NULL `edata_t`: `Address: 0x50`, as in the CI crashes. Otherwise an
interior pointer is freed.

Every gRPC call with transport compression goes through this path:
`deflateEnd` for the result and `inflateEnd` for a compressed request.
In CI only the WeeklyCFI integration jobs showed it, because amd_cfi is
the only integration build with ThinLTO.

In safe mode lld does not fold functions whose address is significant
according to the `-faddrsig` tables the build already emits. Virtual
methods, constructors and destructors (`unnamed_addr`) and functions
whose address is never taken are still folded; objects without an
address-significance table (Rust) are not folded.

Measured on a local ThinLTO build of master (clang-22, lld-22, -g0):
- `zfree_gpr` and `zcfree`: one address before, two after.
- `lea F; cmp` sites where F is shared by different functions: 87
  before (20 are zlib-ng's `zfree == zcfree` test inlined into rdkafka,
  rocksdb, arrow, orc, libarchive, minizip, gRPC and zlib-ng itself), 3
  after: the source alias `AES_encrypt`/`asm_AES_encrypt`, and twice a
  destructor passed to `__cxa_throw`, none an identity test.
- gRPC loop with gzip/deflate transport compression: before, the server
  crashed in 3/3 runs within 6 queries (the official 26.8.10.6 image in
  7/7 within 22, 26.3.33.24 ran 2000/2000), the request side alone in
  4/4, and 1 KB requests also gave a crash at a wild address in
  `grpc_core::Arena::~Arena()` and a spurious decompression error.
  After: 34000 queries without a failure.
- Size: `.text` +2.21 MiB (+0.46%), stripped binary +2.97 MiB (+0.45%),
  zstd -3 of the stripped binary +0.55 MiB (+0.28%). Folded code:
  52.0 MiB before, 50.6 MiB after.

The new test sends compressed gRPC requests and results. It fails on
the ThinLTO build without this change and guards the MasterCI release
stateless jobs; PR CI has no ThinLTO build.

An alternative with no size cost is to drop the identity shortcuts from
our zlib-ng fork (upstream 2.2.0 no longer has `alloc_aligned` /
`free_aligned`, zlib-ng#1713), but that leaves the other comparison
sites.

Closes ClickHouse#115121.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr-performance Pull request with some performance improvements pr-synced-to-cloud The PR is synced to the cloud repo submodule changed At least one submodule changed in this PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants