Repository navigation
Set -fPIC before the C++ runtime libraries from contrib are added - #125094
Conversation
`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.
|
Workflow [PR], commit [8618a09] Summary: ✅
AI ReviewSummaryThis PR moves the Missing context / blind spots
Final Verdict✅ No blocking or major code issues found in the current diff. |
|
🕵 Following up on the AI review's "blind spot" note: I configured the PR head (5df49d4) locally with clang and checked the generated
So the C++ runtime libraries now get |
|
🕵 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 The only red check was
@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 |
|
Acknowledged. I'm investigating it. No fix is in progress yet. Here is what CIDB shows for 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). |
|
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 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 |
Build profile diff (arm_release)Comparing ✅ No significant changes. Binary sizes
The official master build is compiled with Compile time of recompiled translation units12650 translation units recompiled, 32789 s compile time in total, 12192 of them have a recent master baseline.
|
|
Correction to my earlier comment here: the arm slowdown of |
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]>
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]>
Changelog category (leave one):
Changelog entry (a user-readable short description of the changes that goes into CHANGELOG.md):
Set
-fPICbefore the C++ runtime libraries fromcontribare added, so that builds with acompiler 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,libcxxabiandlibunwindtargets are added bycmake/linux/default_libs.cmake,which is included before the block that appends
-fPICtoCMAKE_CXX_FLAGS. CMake copies thecompile flags into a subdirectory when
add_subdirectoryis called, so those targets neverreceived
-fPICand were compiled as non-relocatable objects. This moves the positionindependent 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_LINUXdefaults toON: clang then compiles with-fPIE, and the objectsare relocatable anyway. Fedora's clang does not do that — measured with clang 21.1.8, its
default for
aarch64-linux-gnuandx86_64-linux-gnuis identical to-fno-pic— so theruntime libraries come out non-relocatable and linking any executable fails:
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=ONworkaround removed, thelibcxxabi,libcxxandlibunwindcompile commands contain-fPIC, and a rebuild recompiledthose 403 objects and linked
contrib/llvm-project-cmake/llvm-min-tblgen, which previouslyfailed, 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.ninjathat thecxx/cxxabi/unwindcompile edges carry
-fPICwould do it.Workflow [PR]
Sync PR [sync-upstream/pr/125094]
Version info
26.10.1.2148-master(included in26.10and later)