Skip to content

cmake : add PCH and unity build to improve build times - #28091

Merged
danbev merged 26 commits into
ggml-org:masterfrom
danbev:build-profiling
Sep 11, 2026
Merged

danbev merged 26 commits into
ggml-org:masterfrom
danbev:build-profiling

Conversation

@danbev

@danbev danbev commented Aug 31, 2026 •

Copy link
Copy Markdown
Member

Overview

This pull requests updates cmake configurations to add precompiled headers (PCH) for headers that were identified to be the most expensive in the Parsing/Frontend compiler tool chain stage. This PR only effects this stage and not the Codegen/Backend stage.

Two scripts have been added, one that builds a bare minimum (without Server, Tools, and Tests) and one full which includes Server, Tools, and Tests.

Additional information

The original ask was to look into unity builds. I tried that first, but it only shrank build time a little, so I switched to clang -ftime-trace + ClangBuildAnalyzer to actually see where compile time was going before picking a fix and tried using PCH along the way and wanted to open this PR for visibility.

I've looked into unity builds for the models in llama.cpp, and added 317be76 which produces the following results on linux (Ubuntu 24.04)

+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Full,    master        | 396 |   811.0 s  |   692.2 s  | 1,503.2 s  |
| Full,    with PCH      | 405 |   380.0 s  |   664.7 s  | 1,044.7 s  |
| Full,    with PCH + UB | 264 |   357.7 s  |   635.7 s  |   993.4 s  |
+------------------------+-----+------------+------------+------------+

PCH  = precompiled header.
Full = includes building Server, Tools, and Tests.
UB   = unity build for models

This information can also be found in the docs/build-profiling.md.

While there are some improvements to be made regarding header parsing they are spread out in multiple translation units so the actual improvements might be small. I think there is still work to be done to improve the compile time but they are in the codegen/backend stage and it would probably be easier to do that in a new PR to keep this one isolated to PCH and the unity build and make reviews easier.

And I've only focused on Linux/Mac here and it might be worth looking at windows and see if we can improve the build time there as well.

Requirements

@github-actions github-actions Bot added documentation Improvements or additions to documentation server ggml changes relating to the ggml tensor library for machine learning mtmd Related to multimodal functionality (video/image/audio) labels Aug 31, 2026
danbev added 8 commits August 31, 2026 15:17
…nce_size

Add a version check for GCC 12 to conditionally apply the `-Winterference-size`
pragma.
This directory will not be included in the merge later and this commit
can be ignore at that point. Just fixing to keep CI happy.
There are 7 peg-parser tests that can share one PCH instead of then each
parsing the full tests.h.
Just updating after a number of PCH additions. These are not exact
figures and will vary a bit from run to run, but they give a general idea
of the performance impact of PCH.
@github-actions github-actions Bot added the testing Everything test related label Sep 1, 2026
This commit introduces a unity build for the models to improve
compilation time.

The improvements were roughly the following:
```console
+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Full,    master        | 396 |   811.0 s  |   692.2 s  | 1,503.2 s  |
| Full,    with PCH      | 405 |   380.0 s  |   664.7 s  | 1,044.7 s  |
| Full,    with PCH + UB | 264 |   357.7 s  |   635.7 s  |   993.4 s  |
+------------------------+-----+------------+------------+------------+

TU   = Translation Unit.
Full = includes Server, Tools, and Tests.
PCH  = precompiled headers.
UB   = unity build for models.
```
@github-actions github-actions Bot added the model Model specific label Sep 1, 2026
@danbev danbev changed the title cmake : add precompile headers to improve build times (wip) cmake : add PCH and unitity builds to improve build times (wip) Sep 1, 2026
@Green-Sky

Copy link
Copy Markdown
Collaborator

typo in title, unitity -> unity

@danbev danbev changed the title cmake : add PCH and unitity builds to improve build times (wip) cmake : add PCH and unity build to improve build times (wip) Sep 1, 2026
@BrewTestBot BrewTestBot mentioned this pull request Sep 14, 2026
1 task done
danbev added a commit that referenced this pull request Sep 14, 2026
This commit removes the precompiled headers that I added in Commit
3bcfeb7  ("cmake : add PCH and unity build to improve build times
(#28091)").

The motivation for this is that this looked good when developing this
but has caused multiple issues that I had taken into consideration and
we have decided to remove it and only keep the unity builds from the
above commit.

Refs: #28882 (comment)
danbev added a commit to danbev/llama.cpp that referenced this pull request Sep 14, 2026
This commit updates the PCH cmake function to use
PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR for the PCH paths.

The motivation for this is that when using add_subdirectory,
CMAKE_SOURCE_DIR is fixed to the top-level projects source directory,
that is the caller of add_subdirectory and not the llama.cpp root
which means that common/common.h header will not be resolved.

Refs: ggml-org#28091 (comment)
danbev added a commit that referenced this pull request Sep 15, 2026
This commit updates cmake to use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR for paths in function calls.

The motivation for this is that when using add_subdirectory,
CMAKE_SOURCE_DIR is fixed to the top-level projects source directory,
that is the caller of add_subdirectory and not the llama.cpp root
which means that common/common.h header will not be resolved.

Refs: #28091 (comment)
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
* scripts : add initial profiling script (wip)

* src : add precompile headers (PCH) for models.h

* common : add common.h as PCH

* ggml : add PCH for ggml-impl.h

* mtmd : use PCH for models.h

* scripts : add script to build with Server/Tools/Tests

* server : add PCH for common.h

* docs: add profiling progress notes (wip)

* ggml : add exclude for GCC + SVE on ARM

Refs: https://github.com/ggml-org/llama.cpp/actions/runs/33393906061/job/99493756214?pr=28091

* ggml : attempt to fix use of std::hardware_destructive_inference_size

Refs: https://github.com/ggml-org/llama.cpp/actions/runs/33396221677/job/99501265689?pr=28091

* squash! ggml : attempt to fix use of std::hardware_destructive_inference_size

Add a version check for GCC 12 to conditionally apply the `-Winterference-size`
pragma.

* editorconfig : exclude profiling reports dir

This directory will not be included in the merge later and this commit
can be ignore at that point. Just fixing to keep CI happy.

* ggml : skip PCH for gcc on non-x86 architectures

* tests : add PCH for peg-parser/tests.h

There are 7 peg-parser tests that can share one PCH instead of then each
parsing the full tests.h.

* common : add PCH for chat.h

* docs : update linux build profiling full results

Just updating after a number of PCH additions. These are not exact
figures and will vary a bit from run to run, but they give a general idea
of the performance impact of PCH.

* cmake : introduce unity build for models

This commit introduces a unity build for the models to improve
compilation time.

The improvements were roughly the following:
```console
+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Full,    master        | 396 |   811.0 s  |   692.2 s  | 1,503.2 s  |
| Full,    with PCH      | 405 |   380.0 s  |   664.7 s  | 1,044.7 s  |
| Full,    with PCH + UB | 264 |   357.7 s  |   635.7 s  |   993.4 s  |
+------------------------+-----+------------+------------+------------+

TU   = Translation Unit.
Full = includes Server, Tools, and Tests.
PCH  = precompiled headers.
UB   = unity build for models.
```

* docs : update linux profiling table with unitiy build results

* docs : update mac profiling results to include unity build [no ci]

* docs: remove profiling reports

* scripts : merge build profile scripts into one script

I was lazy before and just copied the first script to enable Tests,
Server, and Tools. This now merges them into a single script.

* Revert "editorconfig : exclude profiling reports dir" [no ci]

This reverts commit 2922a12.

* src : rename ggml_view_2d_slice to gemma3n_view_2d_slice

This is to be consistent with the rename in gemma4.cpp which was
required to avoid a name clash.

* cmake : add build profile script for windows [no ci]

This commit adds a port of the scripts/build-profile.sh script to
windows powershell.

This was developed on Windows on ARM but should work on X64 as well but
needs to be tested there as well.
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
This commit fixes an issue that I introduced when adding PCH
(precompiled headers) in Commit 3bcfeb7
("cmake : add PCH and unity build to improve build times (ggml-org#28091)".

See linked issue for details.

Co-authored-by: mjungnickel18
Co-authored-by: Pascal <admin@serveurperso.com>

Resolves: ggml-org#28758
Refs: https://github.com/ggml-org/llama.cpp/actions/runs/34592933983/job/103262608990#step:9:1284
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
This commit removes the precompiled headers that I added in Commit
3bcfeb7  ("cmake : add PCH and unity build to improve build times
(ggml-org#28091)").

The motivation for this is that this looked good when developing this
but has caused multiple issues that I had taken into consideration and
we have decided to remove it and only keep the unity builds from the
above commit.

Refs: ggml-org#28882 (comment)
quimmedes pushed a commit to quimmedes/cafe-llama.cpp that referenced this pull request Sep 16, 2026
…28771)

This commit updates cmake to use PROJECT_SOURCE_DIR instead of CMAKE_SOURCE_DIR for paths in function calls.

The motivation for this is that when using add_subdirectory,
CMAKE_SOURCE_DIR is fixed to the top-level projects source directory,
that is the caller of add_subdirectory and not the llama.cpp root
which means that common/common.h header will not be resolved.

Refs: ggml-org#28091 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

documentation Improvements or additions to documentation ggml changes relating to the ggml tensor library for machine learning model Model specific mtmd Related to multimodal functionality (video/image/audio) server testing Everything test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants