char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Daniel Beveniuscmake : add PCH and unity build to improve build times (#28091)3bcfeb700

char/aliceai-t5-moe
4.4 KiB122 linesraw

Build profiling

This page is a working document for analyzing the current build and try to identify ways to improve the build time.

Requirements

The profiling script requires clang to be used as the compiler tool chain and also requires that ClangBuildAnalyzer is installed.

Mac:

brew install clang-build-analyzer

Linux:

git clone https://github.com/aras-p/ClangBuildAnalyzer.git
cd ClangBuildAnalyzer
cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build -j$(nproc)
sudo cp build/ClangBuildAnalyzer /usr/local/bin/

Windows: install LLVM/clang and Ninja (e.g. via the LLVM releases page and winget install Ninja-build.Ninja), then build ClangBuildAnalyzer the same way as on Linux:

git clone https://github.com/aras-p/ClangBuildAnalyzer.git
cd ClangBuildAnalyzer
cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release

Then add ClangBuildAnalyzer\build to PATH.

Usage

Mac/Linux:

$ ./scripts/build-profile.sh

Windows:

> .\scripts\build-profile.ps1

Both accept --full/-Full (include Server, Tools, and Tests) and a jobs override (-jN / -Jobs N).

Note: on Windows, cmake defaults to the Visual Studio generator, which ignores CMAKE_C_COMPILER/CMAKE_CXX_COMPILER and silently falls back to MSVC. build-profile.ps1 passes -G Ninja so clang is actually used, this is required on ARM64.

Linux (Ubuntu 24.04)

Environment:

  • Clang: 18.1.3 (Ubuntu clang version 18.1.3 (1ubuntu1))
  • libstdc++: GCC 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04.1)
  • Target: x86_64-pc-linux-gnu
+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Minimal, master        | 249 |   468.2 s  |   270.3 s  |   738.5 s  |
| Minimal, with PCH      | 253 |   177.1 s  |   265.8 s  |   442.9 s  |
| 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

Note that the number of translation units (TUs) increases when using precompiled headers — each PCH target adds one extra TU for the precompilation step itself.

Mac (Apple M3)

Environment:

  • Clang: Apple clang version 17.0.0 (clang-1700.3.19.1)
  • libc++: ships with Apple clang 17.0.0 (Xcode toolchain)
  • Target: arm64-apple-macosx15.6
+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Minimal, master        | 256 |   154.5 s  |    94.8 s  |   249.3 s  |
| Minimal, with PCH      | 261 |    65.9 s  |    90.0 s  |   155.9 s  |
| Full,    master        | 407 |   265.7 s  |   209.7 s  |   475.4 s  |
| Full,    with PCH      | 414 |   154.6 s  |   197.5 s  |   352.1 s  |
| Full,    with PCH + UB | 274 |   143.0 s  |   192.2 s  |   335.2 s  |
+------------------------+-----+------------+------------+------------+

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

Windows (ARM64)

Environment:

  • Clang: clang version 22.1.8 (LLVM, C:\Program Files\LLVM)
  • STL: MSVC STL (Visual Studio 2022 Build Tools 14.44.35207)
  • Target: aarch64-pc-windows-msvc
+------------------------+-----+------------+------------+------------+
| Build                  | TUs | Frontend   | Backend    | Total      |
+------------------------+-----+------------+------------+------------+
| Minimal, master        | 249 |   159.4 s  |    82.2 s  |   241.6 s  |
| Full,    master        | 373 |   337.2 s  |   167.4 s  |   504.6 s  |
| Minimal, with PCH + UB | 113 |    62.3 s  |    82.4 s  |   144.7 s  |
| Full,    with PCH + UB | 240 |   233.0 s  |   185.1 s  |   418.1 s  |
+------------------------+-----+------------+------------+------------+

PCH = precompiled header.
Full = includes building Server, Tools, and Tests.
UB   = unity build for models
1## Build profiling
2This page is a working document for analyzing the current build and try to
3identify ways to improve the build time.
4
5### Requirements
6The profiling script requires clang to be used as the compiler tool chain and
7also requires that ClangBuildAnalyzer is installed.
8
9Mac:
10```console
11brew install clang-build-analyzer
12```
13
14Linux:
15```console
16git clone https://github.com/aras-p/ClangBuildAnalyzer.git
17cd ClangBuildAnalyzer
18cmake -B build -DCMAKE_BUILD_TYPE=Release
19cmake --build build -j$(nproc)
20sudo cp build/ClangBuildAnalyzer /usr/local/bin/
21```
22
23Windows: install LLVM/clang and Ninja (e.g. via the
24[LLVM releases page](https://github.com/llvm/llvm-project/releases) and
25`winget install Ninja-build.Ninja`), then build ClangBuildAnalyzer the same
26way as on Linux:
27```console
28git clone https://github.com/aras-p/ClangBuildAnalyzer.git
29cd ClangBuildAnalyzer
30cmake -B build -G Ninja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ -DCMAKE_BUILD_TYPE=Release
31cmake --build build --config Release
32```
33Then add `ClangBuildAnalyzer\build` to `PATH`.
34
35### Usage
36Mac/Linux:
37```console
38$ ./scripts/build-profile.sh
39```
40
41Windows:
42```console
43> .\scripts\build-profile.ps1
44```
45
46Both accept `--full`/`-Full` (include Server, Tools, and Tests) and a jobs
47override (`-jN` / `-Jobs N`).
48
49Note: on Windows, `cmake` defaults to the Visual Studio generator, which
50ignores `CMAKE_C_COMPILER`/`CMAKE_CXX_COMPILER` and silently falls back to
51MSVC. `build-profile.ps1` passes `-G Ninja` so clang is actually used, this
52is required on ARM64.
53
54### Linux (Ubuntu 24.04)
55
56Environment:
57- Clang:     18.1.3 (Ubuntu clang version 18.1.3 (1ubuntu1))
58- libstdc++: GCC 13.3.0 (Ubuntu 13.3.0-6ubuntu2~24.04.1)
59- Target:    x86_64-pc-linux-gnu
60
61```console
62+------------------------+-----+------------+------------+------------+
63| Build                  | TUs | Frontend   | Backend    | Total      |
64+------------------------+-----+------------+------------+------------+
65| Minimal, master        | 249 |   468.2 s  |   270.3 s  |   738.5 s  |
66| Minimal, with PCH      | 253 |   177.1 s  |   265.8 s  |   442.9 s  |
67| Full,    master        | 396 |   811.0 s  |   692.2 s  | 1,503.2 s  |
68| Full,    with PCH      | 405 |   380.0 s  |   664.7 s  | 1,044.7 s  |
69| Full,    with PCH + UB | 264 |   357.7 s  |   635.7 s  |   993.4 s  |
70+------------------------+-----+------------+------------+------------+
71
72PCH  = precompiled header.
73Full = includes building Server, Tools, and Tests.
74UB   = unity build for models
75```
76Note that the number of translation units (TUs) increases when using precompiled
77headers — each PCH target adds one extra TU for the precompilation step itself.
78
79### Mac (Apple M3)
80
81Environment:
82- Clang:  Apple clang version 17.0.0 (clang-1700.3.19.1)
83- libc++: ships with Apple clang 17.0.0 (Xcode toolchain)
84- Target: arm64-apple-macosx15.6
85
86```console
87+------------------------+-----+------------+------------+------------+
88| Build                  | TUs | Frontend   | Backend    | Total      |
89+------------------------+-----+------------+------------+------------+
90| Minimal, master        | 256 |   154.5 s  |    94.8 s  |   249.3 s  |
91| Minimal, with PCH      | 261 |    65.9 s  |    90.0 s  |   155.9 s  |
92| Full,    master        | 407 |   265.7 s  |   209.7 s  |   475.4 s  |
93| Full,    with PCH      | 414 |   154.6 s  |   197.5 s  |   352.1 s  |
94| Full,    with PCH + UB | 274 |   143.0 s  |   192.2 s  |   335.2 s  |
95+------------------------+-----+------------+------------+------------+
96
97PCH = precompiled header.
98Full = includes building Server, Tools, and Tests.
99UB   = unity build for models
100```
101
102### Windows (ARM64)
103
104Environment:
105- Clang:  clang version 22.1.8 (LLVM, `C:\Program Files\LLVM`)
106- STL:    MSVC STL (Visual Studio 2022 Build Tools 14.44.35207)
107- Target: aarch64-pc-windows-msvc
108
109```console
110+------------------------+-----+------------+------------+------------+
111| Build                  | TUs | Frontend   | Backend    | Total      |
112+------------------------+-----+------------+------------+------------+
113| Minimal, master        | 249 |   159.4 s  |    82.2 s  |   241.6 s  |
114| Full,    master        | 373 |   337.2 s  |   167.4 s  |   504.6 s  |
115| Minimal, with PCH + UB | 113 |    62.3 s  |    82.4 s  |   144.7 s  |
116| Full,    with PCH + UB | 240 |   233.0 s  |   185.1 s  |   418.1 s  |
117+------------------------+-----+------------+------------+------------+
118
119PCH = precompiled header.
120Full = includes building Server, Tools, and Tests.
121UB   = unity build for models
122```