Skip to content

Set -fPIC before the C++ runtime libraries from contrib are added - #125094

Merged
alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
oleg-milanov:fix-pic-for-runtime-libs
Oct 11, 2026
Merged

alexey-milovidov merged 2 commits into
ClickHouse:masterfrom
oleg-milanov:fix-pic-for-runtime-libs

Conversation

@oleg-milanov

@oleg-milanov oleg-milanov commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

Set -fPIC before the C++ runtime libraries from contrib are added, so that builds with a
compiler that does not default to PIE link successfully. This fixes building with compilers that do not default to PIE, such as Fedora's clang on aarch64 and x86_64.

Description:

The libcxx, libcxxabi and libunwind targets are added by cmake/linux/default_libs.cmake,
which is included before the block that appends -fPIC to CMAKE_CXX_FLAGS. CMake copies the
compile flags into a subdirectory when add_subdirectory is called, so those targets never
received -fPIC and were compiled as non-relocatable objects. This moves the position
independent code setup above that include and documents why the placement matters; the musl,
FreeBSD and Darwin guards are unchanged.

This stays hidden with the compiler configuration used in CI, because LLVM's
CLANG_DEFAULT_PIE_ON_LINUX defaults to ON: clang then compiles with -fPIE, and the objects
are relocatable anyway. Fedora's clang does not do that — measured with clang 21.1.8, its
default for aarch64-linux-gnu and x86_64-linux-gnu is identical to -fno-pic — so the
runtime libraries come out non-relocatable and linking any executable fails:

ld.lld-21: error: relocation R_AARCH64_ABS64 cannot be used against symbol
'vtable for __cxxabiv1::__class_type_info'; recompile with -fPIC

Reproduced on Fedora Asahi Linux (aarch64), clang 21.1.8, hermetic toolchain,
-D CMAKE_BUILD_TYPE=Debug.

Verification: with the -D CMAKE_POSITION_INDEPENDENT_CODE=ON workaround removed, the
libcxxabi, libcxx and libunwind compile commands contain -fPIC, and a rebuild recompiled
those 403 objects and linked contrib/llvm-project-cmake/llvm-min-tblgen, which previously
failed, successfully.

Note for reviewers: CI cannot catch this today, since the toolchain clang defaults to PIE. If
you want a guard, asserting from the generated build.ninja that the cxx/cxxabi/unwind
compile edges carry -fPIC would do it.


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

Version info

  • Merged into: 26.10.1.2148-master (included in 26.10 and later)

`cmake/linux/default_libs.cmake` adds the `libcxx`, `libcxxabi` and `libunwind`
targets before the block that appends `-fPIC` to `CMAKE_CXX_FLAGS` runs. CMake
copies the compile flags into a subdirectory when `add_subdirectory` is called,
so those targets never received `-fPIC` and were compiled as non-relocatable
objects.

This goes unnoticed as long as the compiler defaults to PIE, which is the case
for the clang build used in CI (`CLANG_DEFAULT_PIE_ON_LINUX` is `ON` upstream).
With a compiler built with that option off - for example the clang shipped by
Fedora - the objects cannot be linked into the position independent executable:

    ld.lld-21: error: relocation R_AARCH64_ABS64 cannot be used against symbol
    'vtable for __cxxabiv1::__class_type_info'; recompile with -fPIC

Move the position independent code setup before the runtime libraries are
created and document why the placement matters.

Verified that the `libcxxabi`, `libcxx` and `libunwind` compile commands contain
`-fPIC` with no cache override, and that the previously failing link now succeeds.
@CLAassistant

CLAassistant commented Oct 10, 2026 •

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Oct 10, 2026
@alexey-milovidov alexey-milovidov self-assigned this Oct 10, 2026
@alexey-milovidov alexey-milovidov added the can be tested Allows running workflows for external contributors label Oct 10, 2026
@clickhouse-gh

clickhouse-gh Bot commented Oct 10, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [8618a09]

Summary: ✅


AI Review

Summary

This PR moves the -fPIC / -pie setup above cmake/linux/default_libs.cmake so the contrib runtime libraries inherit those flags when add_subdirectory snapshots the directory scope. I did not find a correctness issue in the resulting CMakeLists.txt change: the affected subdirectories only create libraries, and the new ordering is consistent with the existing build-system pattern elsewhere in this file.

Missing context / blind spots
  • The current 8618a09d CI run is still pending, so I could not confirm whether the earlier arm_release performance signal reported on 5df49d44 reproduces on the current head.
  • I could not run a local cmake configure in this environment because cmake is not installed in PATH.
Final Verdict

✅ No blocking or major code issues found in the current diff.

@clickhouse-gh clickhouse-gh Bot added the pr-build Pull request with build/testing/packaging improvement label Oct 10, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Following up on the AI review's "blind spot" note: I configured the PR head (5df49d4) locally with clang and checked the generated build.ninja:

  • libcxx (57 objects), libcxxabi (19) and libunwind (10): 81 of 83 compile edges carry -fPIC. The remaining two are UnwindRegistersRestore.S and UnwindRegistersSave.S. These are hand-written assembly and use the ASM flags, which neither the old nor the new placement of the block touches.
  • The executable link flags contain -pie -Wl,-pie.

So the C++ runtime libraries now get -fPIC no matter what the compiler's PIE default is. That was the gap the review pointed out.

@clickhouse-gh clickhouse-gh Bot added the external Author is not a member of the ClickHouse organization label Oct 11, 2026
@alexey-milovidov

Copy link
Copy Markdown
Member

🕵 Merged master (the branch was 7 days behind) and pushed 8618a09. The merged tree configures cleanly. The diff against master is still only the moved block in CMakeLists.txt.

The only red check was Performance Comparison (arm_release, master_head, 1/6), which reported iceberg_suite_local_tpch #7 (+33%) and iceberg_suite_s3_tpch #7/#8 as slower. In each case the change only just reached the instability threshold. I don't think this PR caused it:

@groeneai, investigate the failure: https://s3.amazonaws.com/clickhouse-test-reports/praktika.html?PR=125094&sha=5df49d443d930eb2af2fec8d9ce51182a0336c04&name_0=PR&name_1=Performance%20Comparison%20%28arm_release%2C%20master_head%2C%201%2F6%29 and provide a fix in a separate PR. These iceberg_suite_*_tpch queries are noisy on ARM. If the fix is already in progress, link it here.

@groeneai

Copy link
Copy Markdown
Collaborator

Acknowledged. I'm investigating it. No fix is in progress yet.

Here is what CIDB shows for iceberg_suite_{s3,local}_tpch #7/#8 on arm_release over the last 14 days. There were 2025 runs and 19 were reported slower. 18 of the 19 are on three PRs where every run was slower: #115970 (5/5), #115605 (10/10) and this PR (3/3, 1.21x to 1.33x). #119443 had one slower run out of 8 (4 heads). Master was 0/272, at most 1.07x. The master 9e081974 report is about query #6 in the release_base nightly comparison, not #7/#8.

So these queries are stable from run to run, but some binaries are consistently about 1.3x slower. This PR's binary is one of them, even though the PR does not touch the join code. Next I'll compare the arm binaries of this PR and of master, and I'll follow up here with a PR (or my findings).

@groeneai

Copy link
Copy Markdown
Collaborator

I compared the arm binaries. This PR's binary is consistently slower on these queries, but I can't tie that to the change, and the queries are not noisy from run to run.

These queries depend on that loop because the Iceberg suite plans them with lineitem (60.8M rows) as the hash join build side: join reordering gets no row estimate for ReadFromObjectStorageStep (estimateReadRowsCount). The same Q8/Q9 on MergeTree (tpch #7/#8) build 0.64M and 3.9M rows, run in 0.15 s and 0.32 s instead of 1.1 s and 1.45 s, and were at 0.99x with this PR's binary.

That is being fixed in #124320, which gives join reordering the data lake's row count. On its arm run, Q8 builds 0.8M rows instead of 60.8M (1.19 s to 0.44 s), and Q9 15.6M instead of 78.7M (1.65 s to 0.83 s). #123085 adds the same from Iceberg manifest files, behind use_iceberg_manifest_statistics (off by default). So I'm not opening a separate PR.

@clickhouse-gh

clickhouse-gh Bot commented Oct 11, 2026

Copy link
Copy Markdown
Contributor

Build profile diff (arm_release)

Comparing 8618a09db with master a2baaa0e4 (stripped binary size, per-symbol sizes and ThinLTO time; object sizes against the warmup build of ab3bc622c; 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.

Compile time of recompiled translation units

12650 translation units recompiled, 32789 s compile time in total, 12192 of them have a recent master baseline.

Translation unit Master PR Δ vs median
contrib/aws-cmake/__/aws/generated/src/aws-cpp-sdk-glue/source/GlueClient.cpp 5.6 s 14.6 s +8.9 s (+158%)
contrib/aws-cmake/__/aws/generated/src/aws-cpp-sdk-s3/source/S3Client.cpp 7.0 s 15.7 s +8.6 s (+123%)
contrib/aws-cmake/__/aws/generated/src/aws-cpp-sdk-glue/source/GlueClient1.cpp 5.4 s 13.6 s +8.2 s (+151%)
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/parsing.cc 6.0 s 12.6 s +6.6 s (+109%)
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/frame_data.cc 5.5 s 11.7 s +6.2 s (+112%)
contrib/grpc-cmake/__/grpc/src/core/load_balancing/xds/cds.cc 6.7 s 12.5 s +5.9 s (+88%)
contrib/grpc-cmake/__/grpc/src/core/load_balancing/rls/rls.cc 7.1 s 12.8 s +5.7 s (+80%)
contrib/grpc-cmake/__/grpc/src/core/server/xds_server_config_fetcher_legacy.cc 7.0 s 12.3 s +5.3 s (+76%)
contrib/grpc-cmake/__/grpc/src/core/ext/transport/chttp2/transport/http2_client_transport.cc 11.2 s 16.3 s +5.1 s (+45%)

Job report

@alexey-milovidov
alexey-milovidov added this pull request to the merge queue Oct 11, 2026
Merged via the queue into ClickHouse:master with commit 20db6d2 Oct 11, 2026
166 checks passed
@robot-ch-test-poll robot-ch-test-poll added the pr-synced-to-cloud The PR is synced to the cloud repo label Oct 11, 2026
@groeneai

Copy link
Copy Markdown
Collaborator

Correction to my earlier comment here: the arm slowdown of iceberg_suite_{local,s3}_tpch #7/#8 does come from this change. Master runs them about 25% slower since it landed, and only libcxx/libcxxabi/libunwind code differs between slow and fast binaries. #125488 compiles those three with -fPIE again and keeps the Fedora fix.

groeneai added a commit to groeneai/ClickHouse that referenced this pull request Oct 14, 2026
Everything we build is linked into the position-independent executable, so
-fPIE is the right code model: -fPIC only adds symbol interposition (GOT
loads, TLS descriptor sequences) that an executable never uses. Instead of
putting -fPIE back on the three C++ runtime targets, the global flag is now
-fPIE, set where ClickHouse#125094 put it, before the default_libs include. The
objects stay relocatable, so builds with compilers that do not default to
PIE keep working.

Also drop the per-target PIC that would override it: POSITION_INDEPENDENT_CODE
on _cld2, the cuDF fragments and the GPU island targets, -Xcompiler -fPIC in
CMAKE_CUDA_FLAGS (now -Xcompiler -fPIE), -fPIC on xgboost and dmlc-core, and
the MSan "-fPIC -fpie" (now -fPIE).

-fPIC stays where it is needed or mirrors upstream: compiler-rt (builtins,
and the sanitizer and XRay runtimes through SANITIZER_COMMON_FLAGS),
glibc-compatibility with ENABLE_OPENSSL_DYNAMIC, and the shared crypto/ssl
libraries of that option, which CMake compiles as PIC by itself. musl's
rcrt1.c keeps -fPIC on master.

Measured locally (compile_commands.json, effective last PIC/PIE flag per TU):
- amd64 debug, release; aarch64 binary, release; asan, msan, tsan: every
  C/C++ TU is -fPIE except clang_rt_builtins and the compiler-rt sanitizer /
  XRay runtimes; with ENABLE_OPENSSL_DYNAMIC also crypto, ssl and
  glibc-compatibility.
- amd64 debug clickhouse: ELF type DYN, FLAGS_1 NOW PIE; clickhouse local
  queries with threads and exceptions work.
- aarch64 flatc, msan and asan llvm-min-tblgen: DYN + PIE, the sanitizer
  builds run.
- With a clang wrapper defaulting to -fno-pic, llvm-min-tblgen still links
  as PIE.

The GPU island (ENABLE_GPU) is not built by CI and I have no CUDA toolkit,
so the cmake/cuda.cmake and contrib/cudf-cmake changes are not built.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
zvonand pushed a commit to zvonand/ClickHouse that referenced this pull request Oct 15, 2026
ClickHouse#125094 moved `-fPIC` into CMAKE_C/CXX_FLAGS before
cmake/linux/default_libs.cmake creates the `cxx`, `cxxabi` and `unwind`
targets, so they are compiled with `-fPIC` instead of the CI compiler's
default `-fPIE` (CLANG_DEFAULT_PIE_ON_LINUX=ON). That is the only codegen
change of ClickHouse#125094 in CI.

Since it reached master (2026-10-11 20:38Z), iceberg_suite_{local,s3}_tpch
ClickHouse#7, ClickHouse#8 and ClickHouse#12 (TPC-H Q8, Q9, Q13) are ~25% slower on arm: 251/251 master
runs and 220/225 PR builds are slow (p50 1.29x of the previous level),
against 616/735 fast runs before. All other arm perf queries are unchanged
(median ratio 1.00 over 5670). A slow ClickHouse#125094 arm binary and a fast
same-base binary differ in 122 functions, all in these three libraries:
TLSDESC sequences relaxed by lld instead of local-exec accesses, and GOT
loads instead of direct addresses (std::uncaught_exceptions, __cxa_*,
std::mutex, std::shared_mutex, ...). glibc-compatibility and harmful are
identical in both binaries. The hot loop of the slowed hash join
(RowRefList::insert) is byte-identical; how the runtime-library change
slows it on Graviton is not identified, and the arm perf run of this
change is the A/B.

The three targets now get `-fPIE` as a PRIVATE C/C++ compile option. It
comes after CMAKE_<LANG>_FLAGS, so it wins over `-fPIC`; with the CI
compiler `-fPIC ... -fPIE` is identical to the default. `-fPIE` objects
stay relocatable, so ClickHouse#125094's fix for compilers that do not default to
PIE is kept.

Measured locally:
- aarch64 arm_binary objects and arm_release ThinLTO bitcode of the three
  libraries: 83/83 identical to master with ClickHouse#125094 reverted (31/83 and
  81/83 differ from current master).
- compile_commands.json vs master (arm_release, arm_binary): only the 81
  C/C++ TUs of the three targets differ, each by one `-fPIE`; assembly
  sources and all other TUs are unchanged.
- With a clang wrapper defaulting to `-fno-pic`, llvm-min-tblgen fails to
  link with ClickHouse#125094 reverted (relocation R_X86_64_32 ... recompile with
  -fPIC) and links as a PIE with this change, as on master.
- amd64 debug build, MSan configure, FreeBSD configure and build of the
  three targets: OK.

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

can be tested Allows running workflows for external contributors comp-build-system Build system (CMake, compilation, platform builds). external Author is not a member of the ClickHouse organization pr-build Pull request with build/testing/packaging improvement pr-synced-to-cloud The PR is synced to the cloud repo

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants