Skip to content

cmake: add WITH_PCH option to build with precompiled headers - #71901

Open
Doracly wants to merge 4 commits into
ceph:mainfrom
Doracly:PCH
Open

Doracly wants to merge 4 commits into
ceph:mainfrom
Doracly:PCH

Conversation

@Doracly

@Doracly Doracly commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Adds WITH_PCH, an opt-in precompiled header (PCH) for the targets measured
to benefit from one. A core payload of std, fmt, boost and Ceph
infrastructure headers goes to all of them; rgw, osd, mon, mds,
librbd, rbd-mirror and the latter two's unit tests add a few of their own
entry headers, such as mon/Monitor.h. The header lists, the per-target
figures and the rule for adding an entry are at the end of src/CMakeLists.txt.

In a Debug build it takes about 30% off the compile CPU of a whole build with
Clang and about 15% with GCC, with or without tests; the listed targets lose a
quarter to two thirds with Clang and a tenth to over a third with GCC. With
Clang, ASAN and TSAN builds gain about the same. The PCHs add about 2GB to the
build tree with Clang and 9GB with GCC.

The option defaults to OFF, so CI and ordinary builds still compile without a
PCH and keep catching translation units that stopped including what they use.
With WITH_PCH=OFF nothing changes.

Compilers and caches

WITH_PCH supports Clang and GCC; cmake refuses it with any other compiler.

ccache caches objects built against a PCH only with pch_defines and
time_macros in its sloppiness. With WITH_CCACHE, the PCH targets compile
through a small generated script that appends both to CCACHE_SLOPPINESS
without touching ccache.conf; all other targets and external projects keep
plain ccache. Clang also gets -Xclang -fno-pch-timestamp with WITH_CCACHE,
or every rebuilt PCH would make the objects built against it miss.

GCC needs two more fixes (gcc 15.2, ccache 4.12.3, sccache 0.13.0):

  • After a change to a macro only, ccache and sccache could return the old PCH:
    CMake builds it from a stub source, both caches can key on its preprocessed
    text, and that has no #defines. ccache guards against this for Clang only.
    -dD on the PCH targets puts the #defines into that text; it doesn't
    change objects.
  • ccache hashes the old .gch as an input of the compile replacing it, and
    GCC's PCH differs on every build, so every PCH rebuild in an existing tree
    made all objects of the target miss. The script deletes the old .gch first.

sccache needs neither the script nor -fno-pch-timestamp.

CMake marks its PCH header as a system header, which hides the warnings of
every header in the payload. WITH_PCH empties CMAKE_PCH_PROLOGUE, so they
are reported as without a PCH.

A PCH is built with its target's compile settings, and the compiler can use it
for a source file whose own settings differ, without a warning. Such files are
therefore compiled without the PCH: rgw/rgw_iam_policy.cc (an include
directory), common/version.cc (a definition) and, with GCC,
common/config.cc and common/options.cc (-fno-var-tracking-assignments).

The first two commits fix what a PCH exposes: mon/ConfigMap.cc relied on
including common/dout.h after #undef dout_prefix to restore the default,
but the PCH has already included it; MergeDiff.cc redefined
_LARGEFILE64_SOURCE after the PCH's <features.h> had set it, which warns.

Testing

  • Clang: compiled IR of all ~1,200 translation units built against a PCH,
    Debug and Release, compared with the build without it: no difference changes
    behaviour. Unit tests give the same results with and without PCH, also in
    ASAN and TSAN builds. With ccache, a rebuild is served entirely from the
    cache, and a changed macro in a payload rebuilds the PCH and its users.
  • GCC: with a macro defined only in a payload header and checked by a
    static_assert, macro-only changes, switching back, touch and real
    changes all build correctly with ccache and with sccache; without -dD,
    both return the stale PCH. A fresh tree is served entirely from ccache,
    byte-identical to the cold build.

No CI job exercises this code, deliberately: some build has to compile without
a PCH.

Measured on one machine (AMD Ryzen AI 9 HX 370, 24 threads, 64GB) in October
2026; read the figures as ordinal, not exact.

Documentation

The developer guide documents the option under a new "Speeding up local
builds" section, next to ccache. The grouping commit is identical to the one in
#72620, so the two PRs merge cleanly in either order.

Contribution Guidelines

  • To sign and title your commits, please refer to Submitting Patches to Ceph.

  • If you are submitting a fix for a stable branch (e.g. "quincy"), please refer to Submitting Patches to Ceph - Backports for the proper workflow.

  • When filling out the below checklist, you may click boxes directly in the GitHub web UI. When entering or editing the entire PR message in the GitHub web UI editor, you may also select a checklist item by adding an x between the brackets: [x]. Spaces and capitalization matter when checking off items this way.

Checklist

  • Tracker (select at least one)
    • References tracker ticket
    • Very recent bug; references commit where it was introduced
    • New feature (ticket optional)
    • Doc update (no ticket needed)
    • Code cleanup (no ticket needed)
  • Component impact
    • Affects Dashboard, opened tracker ticket
    • Affects Orchestrator, opened tracker ticket
    • No impact that needs to be tracked
  • Documentation (select at least one)
    • Updates relevant documentation
    • No doc update is appropriate
  • Tests (select at least one)
Show available Jenkins commands

You must only issue one Jenkins command per-comment. Jenkins does not understand
comments with more than one command.

@Doracly
Doracly requested a review from a team as a code owner September 18, 2026 15:09
@github-actions

Copy link
Copy Markdown

Thank you for your contribution. Since you are not yet a member of the Ceph organization with write permissions on ceph/ceph.git, our CI will not automatically run. Any member of the Ceph organization may label this PR ci-approved to allow Jenkins CI jobs to run.

@Doracly
Doracly force-pushed the PCH branch 3 times, most recently from d6fa609 to 3240685 Compare October 6, 2026 08:19
@Doracly
Doracly marked this pull request as draft October 7, 2026 12:58
@Doracly
Doracly force-pushed the PCH branch 4 times, most recently from 2674af8 to 2a9f568 Compare October 8, 2026 08:38
@Doracly
Doracly marked this pull request as ready for review October 8, 2026 08:39
@Doracly
Doracly requested a review from a team as a code owner October 8, 2026 08:39
@Doracly

Doracly commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Moved the WITH_PCH docs out of the ccache section into their own section; the text is unchanged.

@Doracly

Doracly commented Oct 13, 2026

Copy link
Copy Markdown
Contributor Author

Split the doc commit: the build speed-ups now share a "Speeding up local builds" section; the grouping commit is identical to the one in #72620, so the two PRs merge cleanly in either order.

@Doracly

Doracly commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

1 similar comment
@Doracly

Doracly commented Oct 15, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test windows

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@Doracly

Doracly commented Oct 26, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. The WITH_PCH guard now uses MATCHES "Clang" like src/CMakeLists.txt, so AppleClang passes too (untested on macOS).

@Doracly

Doracly commented Oct 27, 2026

Copy link
Copy Markdown
Contributor Author

jenkins test make check.

@Doracly

Doracly commented Oct 28, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. librbd_plugin_pwl_cache, rbd_test and rbd_mirror_test now get the librbd PCH too: their compile CPU drops from 229 s to 110 s (Debug, Clang, PCH build included). The docs no longer claim figures in src/CMakeLists.txt; it only lists the targets.

@github-actions

github-actions Bot commented Nov 2, 2026

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

@Doracly

Doracly commented Nov 2, 2026

Copy link
Copy Markdown
Contributor Author

Rebased onto main. WITH_PCH now also works with GCC: -dD keeps ccache and
sccache from returning a stale PCH after a macro-only change, and with
WITH_CCACHE the PCH targets compile through a small script that sets the
sloppiness and deletes GCC's old .gch first, so ccache still hits after a PCH
rebuild; other targets keep plain ccache. Warnings in payload headers are no
longer hidden. New commit for a _LARGEFILE64_SOURCE warning in
MergeDiff.cc; ConfigMap.cc now includes dout.h with its other headers.
The figures in src/CMakeLists.txt are Debug only, with a GCC column. Updated
the description.

@Doracly

Doracly commented Nov 9, 2026

Copy link
Copy Markdown
Contributor Author

@idryomov could you ack ea2b0b8 for rbd? It only defines _LARGEFILE64_SOURCE in MergeDiff.cc if it isn't already defined.

@Doracly

Doracly commented Nov 19, 2026

Copy link
Copy Markdown
Contributor Author

@tchaikov could you review this when you have time? CI is green, and the doc grouping commit is the same as in #72620, so either PR can merge first.

@idryomov gentle ping for the rbd ack on ea2b0b8: it only wraps the _LARGEFILE64_SOURCE define in MergeDiff.cc in #ifndef.

@Doracly
Doracly requested a review from tchaikov November 19, 2026 08:15
@Doracly

Doracly commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

@ajarr could you ack ea2b0b8 for rbd? It only defines _LARGEFILE64_SOURCE in src/tools/rbd/action/MergeDiff.cc if it isn't already defined.

@github-actions

Copy link
Copy Markdown

This pull request can no longer be automatically merged: a rebase is needed and changes have to be manually resolved

Including dout.h after #undef dout_prefix restores the default prefix
only if dout.h was not included before, which a precompiled header does.
No other header ConfigMap.cc includes defines dout_prefix, so include
dout.h with the others and drop the #undef.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
With -D_GNU_SOURCE, a precompiled header including <features.h> defines
it first, and the plain #define then warns about a redefinition.
librbd/migration/FileStream.cc guards it the same way.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
Precompiles core std, fmt, boost and Ceph headers per target for the
targets measured to benefit; rgw, osd, mon, mds, librbd, rbd-mirror and
the latter two's unit tests add a few of their own. A whole Debug build
takes about 30% less compile CPU with Clang, 15% with GCC. Unlike with
CMake's default, warnings in the precompiled headers are reported.

With GCC, -dD stops ccache and sccache from returning a stale PCH after
a macro change, and deleting the old PCH before rebuilding it lets ccache
hit.

Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
Assisted-by: claude-opus-5-5
Signed-off-by: Dominik Raddatz <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants