Erik Staab | fd0792a | 2024-07-12 13:02:21 | [diff] [blame] | 1 | # Clang and C++ Modules in Chromium |
| 2 | |
| 3 | ## Overview |
| 4 | |
| 5 | Modules in C++ have the potential to significantly speed up building Chromium. |
| 6 | With textual inclusion, many headers are re-parsed over |
| 7 | [35000 times](https://commondatastorage.googleapis.com/chromium-browser-clang/include-analysis.html) |
| 8 | and with modules could be reduced to the order of 10 times. |
| 9 | |
| 10 | Clang 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 |
| 13 | called Clang Modules and Standard C++ Modules). We're currently exploring Clang |
| 14 | header modules because: |
| 15 | 1. Other projects (e.g. internally at Google) have had success deploying them to |
| 16 | their code bases with large performance wins. |
| 17 | 2. They can be experimented with without rewrites to the code base or changes to |
| 18 | the build system. |
| 19 | |
| 20 | We're currently experimenting with modules for libc++ and they can be enabled |
Takuto Ikuta | 7d1b2b0b | 2025-02-05 06:53:22 | [diff] [blame] | 21 | with the GN arg `use_libcxx_modules` and `use_implicit_libcxx_modules`. Using |
| 22 | this arg is not currently recommended, due to the limitations mentioned below. |
| 23 | It is only interesting to people working on the feature. |
Erik Staab | fd0792a | 2024-07-12 13:02:21 | [diff] [blame] | 24 | |
| 25 | ## Current limitations |
| 26 | |
| 27 | ### Implicit vs explicit modules |
| 28 | |
Takuto Ikuta | 7d1b2b0b | 2025-02-05 06:53:22 | [diff] [blame] | 29 | `use_implicit_libcxx_modules` is using implicit modules, which are created |
| 30 | on-the-fly when Clang doesn't see them in the module cache. This doesn't work |
| 31 | with remote execution since the cached modules aren't known to the build system. |
Erik Staab | fd0792a | 2024-07-12 13:02:21 | [diff] [blame] | 32 | |
| 33 | The module cache is set to `<outdir>/gen/libcxx/module_cache`. Since the modules |
| 34 | aren't known to ninja they aren't cleaned with `ninja -t clean` and need to be |
| 35 | manually deleted for a clean build. |
| 36 | |
| 37 | We will eventually switch to explicit modules to address these issues, which |
| 38 | will 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 Ikuta | 7d1b2b0b | 2025-02-05 06:53:22 | [diff] [blame] | 44 | `use_libcxx_modules` enables explicit modules using existing features. |
| 45 | |
Erik Staab | fd0792a | 2024-07-12 13:02:21 | [diff] [blame] | 46 | ### Duplicate modules |
| 47 | |
| 48 | Multiple pcm files are created per module. For correctness, Clang header modules |
| 49 | default 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 |
| 51 | per flag combination for 13483 total pcms. |
| 52 | |
| 53 | Some flag combinations produce incompatible modules, like whether RTTI is turned |
| 54 | on or off. For most others, we expect that the resulting modules from slight |
| 55 | flag variations (e.g. setting include preprocessor defines unrelated to libc++) |
| 56 | are compatible with each other and can be reused. |
| 57 | |
| 58 | ### Performance |
| 59 | |
| 60 | With Chrome's Clang plugins turned on, modules perform worse than without |
| 61 | modules even if fully cached ([crbug.com/351909443](https://crbug.com/351909443)). |
| 62 | |
| 63 | Building with modules and a cold module cache is much slower than without |
| 64 | modules. This seems unexpected since Clang should still be doing less work. |
| 65 | |
| 66 | ### Correctness |
| 67 | |
| 68 | When the module cache is cold, there are occasional build failures when ninja |
| 69 | parallelism 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)). |