Skip to content

Use the linker and LLVM tools from the compiler's own toolchain - #130659

Merged
murphy-4o merged 1 commit into
ClickHouse:masterfrom
groeneai:ci-link-with-compiler-toolchain-lld
Nov 11, 2026
Merged

murphy-4o merged 1 commit into
ClickHouse:masterfrom
groeneai:ci-link-with-compiler-toolchain-lld

Conversation

@groeneai

@groeneai groeneai commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Related: #130064
Related: #130239

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

CMake now looks for ld.lld, llvm-ar, llvm-objcopy and the other LLVM tools in the compiler's directory before searching PATH, so a build with a self-contained LLVM toolchain uses that toolchain's linker. CI release builds now link with the PGO-optimized lld of the CI toolchain, which makes the ThinLTO link of clickhouse about 18% faster.

Description

CI compiles with its own PGO-optimized LLVM 22.1.8 toolchain in /usr/local/bin, but links with apt's lld-22: master Build (amd_release) and Build (arm_release) logs show Using linker: /usr/bin/ld.lld-22, and the other LLVM tools come from /usr/bin. ch_find_program passes the versioned name first (ld.lld-22, then ld.lld), find_program tries each name over all of PATH before the next one, and the toolchain ships only unversioned names. In a ThinLTO build the optimizer and code generator run inside lld, so release links ran on apt's dynamically linked lld, built without PGO.

ch_find_program now searches the compiler's directory first and falls back to the previous search. The Rust partial links in contrib/{prql,polyglot,pcodec,vortex}-cmake now use ch_find_program too. Both linkers are LLVM 22.1.8 from the same commit.

Measured with the amd_release PR configuration (no debug info, ThinLTO, 16 pinned cores, same objects, 4 runs each): the clickhouse link takes 830 s instead of 1015 s (-18%), peak RSS unchanged at 19 GiB. On ARM an arm_release-like link was reported 22% faster (#130064). The binaries differ only in .comment and the build ID, so PIE, RELRO, BIND_NOW and the non-executable stack are unchanged.

Configured before and after: an apt compiler from /usr/bin and a compiler in a directory without tools keep their tools, -DLINKER_NAME still selects the linker, and the aarch64 cross build and the clang-tidy configuration switch to the toolchain's tools. Tools in the compiler's directory now win over tools elsewhere in PATH, even another LLVM version or GNU objcopy/nm/strip. CMAKE_OBJCOPY, CMake's own lookup that the Rust symbol scripts take, is left to #126478.

🤖 Generated with Claude Code


Workflow [PR]
Sync PR [sync-upstream/pr/130659]

Version info

  • Merged into: 26.11.1.1923-master (included in 26.11 and later)

`ch_find_program` was a plain `find_program` with the versioned name first
(`ld.lld-22`, then `ld.lld`). `find_program` tries each name over all of PATH
before the next one, so a versioned tool anywhere in PATH wins over an
unversioned one in the compiler's own directory.

The CI build image compiles with its own PGO-optimized LLVM toolchain in
/usr/local/bin, which ships unversioned tool names only, while apt's lld-22
and llvm-22 put versioned names into /usr/bin. So CI compiled with the
toolchain's clang but linked, archived and stripped with the apt tools
("Using linker: /usr/bin/ld.lld-22" in the amd_release and arm_release
logs). In a ThinLTO build the optimizer and code generator run inside the
linker, so the release links ran on an lld built without PGO.

Search the compiler's directory first, then fall back to the previous search.
The Rust partial links in contrib/{prql,polyglot,pcodec,vortex}-cmake, which
had their own copy of the ld.lld lookup, use the same macro. Both are LLVM
22.1.8 from the same commit. Measured with the amd_release PR configuration
(-g0, ThinLTO, 16 pinned cores, the same objects, 4 runs each): the
`clickhouse` link takes 830 s instead of 1015 s (-18%), user CPU -16%, peak
RSS unchanged at 19 GiB, and the output is identical except `.comment` and the
build ID.

A build with an apt compiler from /usr/bin is unchanged: the versioned tools
are found in that directory first. `-DLINKER_NAME` still takes precedence. The
header of utils/download-ci-toolchain.sh, which keeps only the compiler, no
longer claims that CI uses the installed tools.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review (click to expand)
Finding Verdict
❌ The Rust partial links (_LLD_FOR_STRIP, now in contrib/{prql,polyglot,pcodec,vortex}-cmake) had their own versioned-first ld.lld lookup, so CI still ran apt's ld.lld-22 there AGREE, fixed: the same ch_find_program
💡 utils/download-ci-toolchain.sh said "As in CI, the build keeps using the installed lld" AGREE, fixed: the header no longer compares with CI
⚠️ An existing build directory keeps its cached tool paths on reconfigure DISAGREE: unchanged find_program cache semantics (the macro cached before too, and -D<VAR>= overrides depend on it); CI configures fresh build directories
⚠️ The illumos toolchain file looks up ld.lld on its own DISAGREE: it uses only the unversioned name, and CI's PATH has /usr/local/bin first, so amd_illumos already gets the toolchain's ld.lld (no ThinLTO there)
⚠️ No test checks which directory the tools come from DISAGREE: build-system change, no self-test (#119766); configured before and after for the CI x86 and aarch64 cross builds, apt compilers, -DLINKER_NAME, a tool-less compiler directory and clang-tidy
⚠️ The -18% link time and identical output need evidence DISAGREE: measured, 4 runs per linker on the same objects (1015 s -> 830 s median, every pair -17..-19%); only .comment and the build ID differ, PIE/RELRO/BIND_NOW/stack flags identical
⚠️ CMAKE_OBJCOPY, used by the Rust symbol scripts, still comes from CMake's own lookup Not changed here: #126478 sets it to OBJCOPY_PATH; both are LLVM 22.1.8 in CI
💡 A compiler directory with GNU objcopy/nm/strip and no LLVM tool now wins over LLVM tools elsewhere in PATH Noted in the description; no CI image has this layout

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? Yes. Configuring master with the CI toolchain first in PATH prints Using linker: /usr/bin/ld.lld-22 (and /usr/bin/llvm-*-22) every time, as in the master Build (amd_release) / Build (arm_release) logs.
b Root cause explained? find_program tries each name over all of PATH before the next; ld.lld-22 exists only in apt's /usr/bin, the toolchain has only ld.lld, so the apt tools win and the ThinLTO backends run in a non-PGO lld.
c Fix matches root cause? Yes: the lookup prefers the compiler's own directory, then falls back. No image change, no hard-coded path, no flag change.
d Test intent preserved / new tests added? Build-system change: no self-test (test-infrastructure rule, #119766); validated locally by configure matrix and link A/B.
e Both directions demonstrated? Base: /usr/bin/*-22 (x86_64 and aarch64 cross); patched: toolchain tools. clickhouse ThinLTO link 1015 s -> 830 s median, 4 runs each, 16 pinned cores.
f Fix is general across code paths? Every tool lookup goes through ch_find_program, including the Rust partial links' ld.lld; CMAKE_OBJCOPY is CMake's own lookup and is left to #126478.
g Fix generalizes across inputs (params/datatypes/wrappers)? Compiler by name, by absolute path, via a symlink in a tool-less directory, apt vs toolchain, -DLINKER_NAME absolute: all configured before and after; only the toolchain case changes.
h Backward compatible? (maintainer-approved exception only) Yes. No setting or format. Apt-compiler builds with their llvm-NN package keep the same tools; existing build dirs keep cached tool paths. Disclosed: tools in the compiler's directory (another LLVM version, or GNU objcopy/nm/strip) now win over tools elsewhere in PATH.
i Invariants and contracts preserved? Tools come from the compiler's toolchain when it has them, else from the old search. Output identical except .comment and the build ID; PIE, RELRO, BIND_NOW, non-executable stack identical.

Session id: cron:clickhouse-impl-slot-6:20261109-141000

@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [ece1001]

Summary: ✅


AI Review

Summary

The PR prefers tools beside the C++ compiler and applies the same lookup to the four Rust partial-link steps, allowing CI to use its PGO-optimized LLVM linker. No actionable findings at ece10018a175558bfddbca2b5395f0a604c559b1. All 11 focused CMake probes passed, including compiler-directory preference, fallback, explicit and cached overrides, dummy launchers, cross-build paths, and compiler symlinks. The supplied measurements support the reported link-time improvement.

Missing context / blind spots
  • ⚠️ Release and cross-platform CI builds were still pending or running when reviewed. Completing those jobs will close the remaining platform-validation gap; a full local ClickHouse build and link benchmark were not run.
Final Verdict

Status: ✅ Approve. No code changes requested; remaining CI results should be checked before merging.

@clickhouse-gh clickhouse-gh Bot added pr-build Pull request with build/testing/packaging improvement submodule changed At least one submodule changed in this PR. can be tested Allows running workflows for external contributors comp-build-system Build system (CMake, compilation, platform builds). labels Nov 11, 2026
@clickhouse-gh

clickhouse-gh Bot commented Nov 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing ece10018a with master 87837bf9b (stripped binary size, per-symbol sizes and ThinLTO time; compile times per translation unit against the most recent warmup build that recompiled it).

✅ No significant changes.

Binary sizes

programs/clickhouse-stripped: smaller than the master baseline by the known offset between the two builds, so the difference is not shown. A delta that differs from the offset by more than 50% of it is shown, in either direction.

The official master build is compiled with -g and a pull request build is not, and XRay counts debug instructions towards its instrumentation threshold, so master instruments thousands of functions more and its binary is ~0.4% larger no matter what the pull request does.

Slowest function optimization changes (ThinLTO)

Median per-function time ratio to the master baseline: programs/clickhouse ×0.53, programs/clickhouse-keeper ×0.81 (each binary is linked separately, on a different machine and with different build flags); deltas below are relative to it.

Binary Function Master PR Δ vs median
programs/clickhouse _ZN6detail10static_forITk7is_enumN2DB23AttributeUnderlyingTypeEZNS1_29callOnDictionaryAttributeType… 3.9 s 7.1 s +5.1 s (+251%)
Compile time of recompiled translation units

6 translation units recompiled, 4 s compile time in total, 6 of them have a recent master baseline.

Job report

@murphy-4o
murphy-4o added this pull request to the merge queue Nov 11, 2026
Merged via the queue into ClickHouse:master with commit 48f9d3b Nov 11, 2026
369 of 370 checks passed
@robot-clickhouse robot-clickhouse added the pr-synced-to-cloud The PR is synced to the cloud repo label Nov 11, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

can be tested Allows running workflows for external contributors comp-build-system Build system (CMake, compilation, platform builds). pr-build Pull request with build/testing/packaging improvement 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