Skip to content

Compile with -fPIE instead of -fPIC - #125488

Merged
Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:runtime-libs-fpie
Oct 15, 2026
Merged

Algunenano merged 2 commits into
ClickHouse:masterfrom
groeneai:runtime-libs-fpie

Conversation

@groeneai

@groeneai groeneai commented Oct 12, 2026 •

Copy link
Copy Markdown
Collaborator

Changelog category (leave one):

  • Build/Testing/Packaging Improvement

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

Compile all code with -fPIE instead of -fPIC. Everything is linked into the position-independent clickhouse executable, so the GOT loads and TLS descriptor sequences that -fPIC generates for symbol interposition were never needed.

Description

Related: #125094, #124818

Everything 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. #125094 moved the global -fPIC before cmake/linux/default_libs.cmake, which also put libcxx, libcxxabi and libunwind on -fPIC and made iceberg_suite_{local,s3}_tpch #7, #8 and #12 about 25% slower on arm. The first version of this PR put -fPIE back on those three targets (its arm perf run: 0.78x, 0.87x, 0.90x); this version fixes the root instead:

  • the global flag is -fPIE, where Set -fPIC before the C++ runtime libraries from contrib are added #125094 put it;
  • removed the PIC that targets forced on themselves: POSITION_INDEPENDENT_CODE on _cld2, the cuDF fragments and the GPU island targets, -Xcompiler -fPIC in CMAKE_CUDA_FLAGS (now -fPIE), -fPIC on xgboost and dmlc-core, and MSan's -fPIC -fpie (now -fPIE);
  • -fPIC stays in compiler-rt (the builtins, and the sanitizer and XRay runtimes through SANITIZER_COMMON_FLAGS, as upstream), in glibc-compatibility and the shared crypto/ssl with ENABLE_OPENSSL_DYNAMIC, and in musl's rcrt1.c.

Validation, locally:

  • compile_commands.json, last PIC/PIE flag per TU, for amd64 debug and release, aarch64 binary and release, ASan, MSan, TSan: every C/C++ TU is -fPIE except the compiler-rt targets above (with ENABLE_OPENSSL_DYNAMIC, also crypto, ssl, glibc-compatibility).
  • amd64 clickhouse: DYN with FLAGS_1: NOW PIE; aarch64, ASan and MSan test executables too, and the sanitizer ones run.
  • With a clang that defaults to -fno-pic, executables still link as PIE.
  • Not built: the GPU island (ENABLE_GPU): no CI job enables it and I have no CUDA toolkit.

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

Version info

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

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]>
@groeneai groeneai added can be tested Allows running workflows for external contributors groeneai-origin-request PR origin: a maintainer pinged or directed groeneai labels Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author
Internal second-model review: adjudication log (click to expand)

Pre-publication review by an independent model (engine: codex; 1 finding) plus my own review (0 blocker, 0 major).

# Sev Finding Verdict Evidence / action
1 💡 Related: #125094, #124818 in the description is not in the template's one-URL-per-line form DISAGREE The linkage block at the top has both full URLs, one per line; the description line only makes them visible.

Checked in my own review: cxx, cxxabi and unwind exist on every OS that sets ENABLE_POSITION_INDEPENDENT_BINARY (linux, darwin, freebsd, sunos); libunwind's .S sources are excluded by the genex; on Darwin clang forces PIC, so nothing changes there; no shared library on amd64/aarch64 pulls code from these archives.

Session id: cron:clickhouse-review-slot-48:20261012-192400

@groeneai

Copy link
Copy Markdown
Collaborator Author
Pre-PR validation gate (click to expand)
# Question Answer
a Deterministic repro? For the codegen change: the aarch64 objects of cxx/cxxabi/unwind differ between master and master with #125094 reverted (31 of 83 objects, 81 of 83 ThinLTO bitcode files). The perf effect needs arm hardware; this PR's arm performance run is the A/B.
b Root cause explained? #125094 put -fPIC into the flags these three runtime libraries are created with; the CI clang defaults to -fPIE, so 122 of their functions changed from PIE to PIC code (TLS descriptors, GOT loads), and the arm Iceberg TPC-H Q8/Q9/Q13 runs are ~25% slower since. How these functions slow the hash join on Graviton is not identified.
c Fix matches root cause? Yes. The three libraries get -fPIE last, which restores their pre-#125094 objects byte for byte; #125094 itself stays.
d Test intent preserved / new tests added? Build flags only, no functional test can observe a relocation model; the existing iceberg_suite_*_tpch perf queries are the oracle.
e Both directions demonstrated? Objects: master differs from the #125094-reverted tree, this change equals it (83 of 83, objects and bitcode). Link with a clang that defaults to -fno-pic: fails with #125094 reverted, links with this change.
f Fix is general across code paths? glibc-compatibility and harmful (also created in default_libs.cmake) have identical code in the slow and fast binaries, so they keep -fPIC. All OS default_libs.cmake create the three targets; FreeBSD configure and build checked.
g Fix generalizes across inputs (params/datatypes/wrappers)? Checked configurations: arm_release, arm_binary, amd_debug, amd_msan, a non-PIE-default compiler, FreeBSD. Assembly sources are untouched.
h Backward compatible? (maintainer-approved exception only) Yes, build flags only.
i Invariants and contracts preserved? The runtime objects stay relocatable (the PIE link works with a non-PIE-default compiler), and every other compile command is identical to master (compile_commands.json diff: 81 TUs, one -fPIE each).

Session id: cron:clickhouse-impl-slot-7:20261012-184800

@clickhouse-gh

clickhouse-gh Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Workflow [PR], commit [9415dcb]

Summary: ❌

job_name test_name status info comment
Performance Comparison (amd_release, master_head, 2/6) FAIL Performance dashboard
array_enumerate_fixed_string #1 FAIL dashboard IGNORED
iceberg_suite_s3_rw_features #9 unstable query history IGNORED

AI Review

Summary

PR #125488 replaces the global -fPIC flags with -fPIE and removes conflicting target overrides, while retaining the documented PIC exceptions. Reviewed head 9415dcba9bbd03ef49efb034b277f249112a6178, the surrounding build configuration, and all prior discussion. No actionable code defect was found, so no inline comments were posted. ⚠️ Request changes for incomplete validation of the expanded change.

Missing context / blind spots
  • ⚠️ The changes to cmake/cuda.cmake and contrib/cudf-cmake/CMakeLists.txt have not been built: the author has no CUDA toolkit, and CI does not exercise ENABLE_GPU. A CUDA 13 build and an existing GPU smoke query would close this gap. See the author's validation report.
Tests
  • ⚠️ All twelve current-head performance shards are pending. The earlier ARM measurements cover the superseded change to three runtime libraries; AMD comparisons on that revision were skipped. Complete the current AMD and ARM comparisons to establish the claimed benefit and check for regressions from changing the remaining libraries.
  • ⚠️ Current-head ASan, MSan, and TSan builds are running, and both packaged-binary compatibility checks are pending. The reported local helper-executable checks do not establish that the packaged release binaries retain DYN and DF_1_PIE. Results are available through the current-head Praktika report.
Final Verdict

⚠️ Request changes. Before merge, complete and assess the current-head AMD/ARM performance comparisons, sanitizer builds, and packaged-PIE checks. No source correction is requested based on the code review.

@clickhouse-gh clickhouse-gh Bot added the pr-not-for-changelog This PR should not be mentioned in the changelog label Oct 12, 2026
@clickhouse-gh clickhouse-gh Bot added the comp-build-system Build system (CMake, compilation, platform builds). label Oct 12, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - b9fbfb1

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Integration tests (arm_binary, 1/4) / test_storage_iceberg_concurrent/test_concurrent_reads.py::test_concurrent_reads not from this PR (it only changes the compile flags of the C++ runtime libraries): Code: 107, the Iceberg metadata file does not exist. test_storage_delta/test_writes_interop.py also ran in this job, and a Spark JVM reused across the two modules keeps its cached s3a filesystem bound to the other module's MinIO; the fix merged after the tree of this run was pinned #125448 (mine, merged)
Mergeable Check, PR praktika rollups of the row above #125448 (mine, merged)

CIDB has no other FAIL or ERROR row on this head.

Session id: cron:our-pr-ci-monitor:20261013-070008

@Algunenano Algunenano self-assigned this Oct 14, 2026
@Algunenano

Copy link
Copy Markdown
Member

🤖 Instead of patching -fPIE back onto three targets, let's fix the root: we should never compile with -fPIC. Everything we build goes into a single (mostly static) executable, so -fPIE is the correct model everywhere; -fPIC only adds interposition semantics (GOT loads, TLS descriptors) that we pay for and never use. That is also what #125094 should have done.

Please rework this PR in that direction:

  • CMakeLists.txt: replace the global -fPIC in CMAKE_C_FLAGS/CMAKE_CXX_FLAGS with -fPIE (keeping it above the default_libs include, as Set -fPIC before the C++ runtime libraries from contrib are added #125094 placed it), and drop the per-target override added here. Update the comment block above it accordingly.
  • cmake/sanitize.cmake: the MSan flags -fPIC -fpie become just -fPIE.
  • Targets that force PIC on themselves and would otherwise override the global flag: POSITION_INDEPENDENT_CODE ON in contrib/cld2-cmake and contrib/cudf-cmake, and -Xcompiler -fPIC / POSITION_INDEPENDENT_CODE ON in cmake/cuda.cmake.
  • Keep -fPIC where it is justified: musl's rcrt1.c (matches upstream, runs before relocation), glibc-compatibility with ENABLE_OPENSSL_DYNAMIC, and compiler-rt's builtins (mirror upstream, -fvisibility=hidden makes it moot).

Validation: compile_commands.json should have no -fPIC left outside those exceptions, the binary must stay PIE (DYN + DF_1_PIE, as checked by compatibility_check.py), plus a pr-performance run on both arm and amd and the sanitizer builds.

Also, the release binary must stay PIE, so #125278 is not a direction we want to take.

@groeneai

Copy link
Copy Markdown
Collaborator Author

Thanks, I'm reworking it in that direction. One thing I need: the amd Performance Comparison jobs are skipped unless pr-performance is on the PR when the workflow starts (filter_job.py reads the labels from the triggering event), and I can't set labels. Could you add it? I'll push the rework within a few hours.

@Algunenano Algunenano added the pr-performance Pull request with some performance improvements label Oct 14, 2026
@Algunenano

Copy link
Copy Markdown
Member

Added pr-performance for evaluation

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]>
@groeneai groeneai changed the title Compile libcxx, libcxxabi and libunwind with -fPIE again Compile with -fPIE instead of -fPIC Oct 14, 2026
@clickhouse-gh clickhouse-gh Bot added the manual approve Manual approve required to run CI label Oct 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

Done in 9415dcb:

  • CMakeLists.txt: the global flag is -fPIE, still above the default_libs include; the per-target override is gone and the comment is updated.
  • cmake/sanitize.cmake: MSan uses just -fPIE.
  • Dropped POSITION_INDEPENDENT_CODE ON in contrib/cld2-cmake, contrib/cudf-cmake and the GPU island in cmake/cuda.cmake, where -Xcompiler -fPIC is now -Xcompiler -fPIE. I also removed the two -fPIC in contrib/xgboost-cmake (_xgboost, _dmlc_core): not on your list, but they override the global flag the same way.
  • Your exceptions are kept. One more falls under the compiler-rt one: the sanitizer and XRay runtimes get -fPIC from SANITIZER_COMMON_FLAGS, as upstream, and XRay is linked into the non-sanitizer builds. I left them; I can switch them to -fPIE if you prefer. With ENABLE_OPENSSL_DYNAMIC, CMake compiles the shared crypto/ssl as PIC by itself, so that option keeps working.

Checked locally: in compile_commands.json for amd64 debug and release, aarch64 binary and release, ASan, MSan and TSan, the effective flag of every C/C++ TU is -fPIE except those compiler-rt targets. The amd64 clickhouse is DYN with FLAGS_1: NOW PIE, and so are aarch64, ASan and MSan test executables (the sanitizer ones run). A clang that defaults to -fno-pic still links PIE. Not built: the GPU island, since no CI job enables ENABLE_GPU and I have no CUDA toolkit here.

@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. and removed pr-not-for-changelog This PR should not be mentioned in the changelog labels Oct 14, 2026
@groeneai

Copy link
Copy Markdown
Collaborator Author

CI finish ledger - 9415dcb

Every failure below has an owner: a fixing PR (mine or external), or a full-effort fix task
whose fixing-PR link will be posted here when it opens. Only CH Inc sync is exempt.

Check / test Reason Owner / fixing PR
Performance Comparison (amd_release, master_head, 2/6) / array_enumerate_fixed_string #1 +18%, the same hot loop at another offset in the -fPIE binary owned by this PR (#125488): the resolution is a review decision on this layout effect, see below
Mergeable Check, PR praktika rollups of the row above owned by this PR (#125488), as above

Perf evaluation of this head:

Session id: cron:clickhouse-maint-slot-14:20261014-233400

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

LGTM. The performance changes are irrelevant and this is the correct way forward

@Algunenano
Algunenano added this pull request to the merge queue Oct 15, 2026
Merged via the queue into ClickHouse:master with commit 17a0ca3 Oct 15, 2026
176 of 178 checks passed
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). groeneai-origin-request PR origin: a maintainer pinged or directed groeneai manual approve Manual approve required to run CI pr-build Pull request with build/testing/packaging improvement 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