blob: ab41c494cfb2e409296d5987ef83f6b4720bfda4 [file] [log] [blame] [view]
Erik Staabfd0792a2024-07-12 13:02:211# Clang and C++ Modules in Chromium
2
3## Overview
4
5Modules in C++ have the potential to significantly speed up building Chromium.
6With textual inclusion, many headers are re-parsed over
7[35000 times](https://commondatastorage.googleapis.com/chromium-browser-clang/include-analysis.html)
8and with modules could be reduced to the order of 10 times.
9
10Clang supports two types of modules:
11[Clang header modules](https://clang.llvm.org/docs/Modules.html) and
12[C++20 modules](https://clang.llvm.org/docs/StandardCPlusPlusModules.html) (also
13called Clang Modules and Standard C++ Modules). We're currently exploring Clang
14header modules because:
151. Other projects (e.g. internally at Google) have had success deploying them to
16their code bases with large performance wins.
172. They can be experimented with without rewrites to the code base or changes to
18the build system.
19
20We're currently experimenting with modules for libc++ and they can be enabled
Takuto Ikuta7d1b2b0b2025-02-05 06:53:2221with the GN arg `use_libcxx_modules` and `use_implicit_libcxx_modules`. Using
22this arg is not currently recommended, due to the limitations mentioned below.
23It is only interesting to people working on the feature.
Erik Staabfd0792a2024-07-12 13:02:2124
25## Current limitations
26
27### Implicit vs explicit modules
28
Takuto Ikuta7d1b2b0b2025-02-05 06:53:2229`use_implicit_libcxx_modules` is using implicit modules, which are created
30on-the-fly when Clang doesn't see them in the module cache. This doesn't work
31with remote execution since the cached modules aren't known to the build system.
Erik Staabfd0792a2024-07-12 13:02:2132
33The module cache is set to `<outdir>/gen/libcxx/module_cache`. Since the modules
34aren't known to ninja they aren't cleaned with `ninja -t clean` and need to be
35manually deleted for a clean build.
36
37We will eventually switch to explicit modules to address these issues, which
38will require support in GN and has been partially implemented
39([CL1](https://gn-review.googlesource.com/c/gn/+/9601),
40[CL2](https://gn-review.googlesource.com/c/gn/+/9602),
41[CL3](https://gn-review.googlesource.com/c/gn/+/9680), and
42[crbug.com/gn/373](https://crbug.com/gn/373)).
43
Takuto Ikuta7d1b2b0b2025-02-05 06:53:2244`use_libcxx_modules` enables explicit modules using existing features.
45
Erik Staabfd0792a2024-07-12 13:02:2146### Duplicate modules
47
48Multiple pcm files are created per module. For correctness, Clang header modules
49default to using a hash of command line args in the module path. For compiling
50`base`, we have 19 different flag combinations and ~700 pcm files are created
51per flag combination for 13483 total pcms.
52
53Some flag combinations produce incompatible modules, like whether RTTI is turned
54on or off. For most others, we expect that the resulting modules from slight
55flag variations (e.g. setting include preprocessor defines unrelated to libc++)
56are compatible with each other and can be reused.
57
58### Performance
59
60With Chrome's Clang plugins turned on, modules perform worse than without
61modules even if fully cached ([crbug.com/351909443](https://crbug.com/351909443)).
62
63Building with modules and a cold module cache is much slower than without
64modules. This seems unexpected since Clang should still be doing less work.
65
66### Correctness
67
68When the module cache is cold, there are occasional build failures when ninja
69parallelism is high enough. I don't see it with 20 jobs but see it with 130 jobs
70([crbug.com/351865290](https://crbug.com/351865290)).