RFC [CMake]: Adding an option to enable precompiled headers for llvm-libraries

I’d like to propose an option to enable precompiled headers for commonly included headers in the llvm-libraries.

This would be most significant for MSVC compilation times, but other compilers are likely to benefit as well.
The one drawback that I am unable to confirm is that CMake’s PCH infrastructure doesn’t play nicely with ccache.

I opened a pull request with this change in the main repo here: [CMAKE][llvm-libraries] Add Precompiled Headers option to improve build times by ameerj · Pull Request #91755 · llvm/llvm-project · GitHub

This PR is a work-in-progress, and is mainly a proof of concept for how the addition may look like.

The aim is to be non-intrusive with the addition, but the nature of globally including headers across many files exposes namespace clashes and ODR violations. It would also be more ideal to specify per-target PCH.

I would appreciate any feedback or suggestions!

Is this strictly for MSVC? If not, why wouldn’t you just build with -DCLANG_ENABLE_MODULES=On?

The motivation for this change is for MSVC. I’m involved in a project that imports LLVM as a submodule and builds from source, and most of the devs and CI workflows use MSVC.

I wasn’t aware of the state of modules in LLVM. i know that technically MSVC has some kind of support for C++20 modules. So I will look into this.

MSVC also support header unit modules that work very similar to Clang’s original module system. There was very recent video by a MS office dev about using them internally and there trade-offs vs PCH Progress Report: Adopting Header Units in Microsoft Word

Bumping up the thread for extra visibility.

To summarize the GitHub discussion. Modules with MSVC and CMake aren’t really ready yet, so that’s not an option. The first step is to provide an opt-in way to use the precompiled headers. It’s not required for the rest of the community to use the precompiled headers or to maintain them. When landing your change you don’t need to test it with the precompiled headers. There is a plan to make the precompiled headers useful for more people and in more contexts, so more work is expected in this area. But it’s not mandatory for anyone to use the precompiled headers.

What kind of comments I’m looking for? If the opt-in precompiled headers break your workflow, that’s the most important feedback. If the precompiled headers affect you down the road, that is also important to know but I don’t expect it to be a blocking issue. If you are interested in how exactly the precompiled headers are implemented, please comment in the GitHub discussion.

@asb can you please add a link to this RFC in the next LLVM Weekly? I don’t think it was ever mentioned and I’d like to make sure enough people have seen the proposal and won’t be surprised by it.

2 Likes