Skip to content

Implement support for C++20 modules - #1523

Draft
silvanshade wants to merge 45 commits into
ccache:masterfrom
silvanshade:cpp-modules
Draft

silvanshade wants to merge 45 commits into
ccache:masterfrom
silvanshade:cpp-modules

Conversation

@silvanshade

@silvanshade silvanshade commented Oct 11, 2024 •

Copy link
Copy Markdown

This is a PR intended to implement support for C++20 modules. The relevant issue is #1252.

I've submitted this early as a draft PR in case there is important feedback that might influence how this should be designed.

Implemented:

  • basic compiler abstraction; related: Compiler abstraction #956
  • unified compiler terminal output processing (reduces 3 possible passes into one)
  • parse module options for clang, gcc, and cl
  • parse p1689r5 ddi files
  • parse GCC enhanced depfiles when -fmodules is used without -fdeps-format
  • parse MSVC /scanDependencies output
  • parse MSVC /sourceDependencies output
  • use p1689 and related data to determine dependencies and infer some settings
  • use p1689 and related data to update result key (alongside depfile)
  • serialize/deserialize DDI and BMI files

Remaining:

  • implement driver for clang-scan-deps
  • implement alternate heuristics for clang and MSVC (not all compilers expose the same p1689 data)

This is currently enough to get GCC to be able to cache and restore module artifacts for some simple compilations.

Some additional work remains to enable support for Clang and MSVC.

@silvanshade
silvanshade force-pushed the cpp-modules branch 7 times, most recently from 0f17eae to 898bb40 Compare October 17, 2024 16:23
@silvanshade
silvanshade force-pushed the cpp-modules branch 2 times, most recently from a402abe to f868826 Compare October 19, 2024 02:02
@jrosdahl jrosdahl added the issue: feature New feature label Nov 2, 2024
@jrosdahl jrosdahl linked an issue Nov 2, 2024 that may be closed by this pull request
@jrosdahl

jrosdahl commented Nov 3, 2024

Copy link
Copy Markdown
Member

Thanks for working on this.

Comment thread src/ccache/ccache.cpp Fixed
Comment thread src/ccache/ccache.cpp Fixed
Comment thread src/ccache/ccache.cpp Fixed
@silvanshade
silvanshade force-pushed the cpp-modules branch 2 times, most recently from 04b3c64 to d26b8d6 Compare November 22, 2024 03:49
@silvanshade
silvanshade force-pushed the cpp-modules branch 4 times, most recently from d0e58f3 to 0971a23 Compare February 18, 2025 03:40
@koplas

koplas commented Apr 8, 2025

Copy link
Copy Markdown

I know this WIP, but how can this be tested? I compiled this branch with the feature enabled, but touching a file results in a cascade of recompilations. I tried it with GCC 14.2.1 and clang 21.

@silvanshade

silvanshade commented Apr 9, 2025 •

Copy link
Copy Markdown
Author

@koplas the best way to understand what is currently working is to keep an eye on the cxx_modules test. That test will continue to be updated as the rest of the support is built out.

Currently only GCC 14 is supported with regard to the caching of module artifacts.

Support for MSVC is also almost ready. Most of the pieces are in place for that, I just need to implement some additional processing for the /sourceDependencies output.

Clang support will arrive after that. It's not particularly difficult but does require some extra steps since the need to use clang-scan-deps requires processing in two phases. (I believe eventually clang-scan-deps functionality will be merged into the main frontend making this unnecessary in the long run).

As for how to test with GCC 14, you can take a look at the example here:

CCACHE_DEPEND=1 $CCACHE_COMPILE -std=c++20 -fmodules-ts -fdeps-format=p1689r5 -fdeps-file=foo.ddi -MD -MF foo.d -o foo.o -x c++ -c foo.cppm
CCACHE_DEPEND=1 $CCACHE_COMPILE -std=c++20 -fmodules-ts -fdeps-format=p1689r5 -fdeps-file=bar.ddi -MD -MF bar.d -o bar.o -x c++ -c bar.cppm
CCACHE_DEPEND=1 $CCACHE_COMPILE -std=c++20 -fmodules-ts -fdeps-format=p1689r5 -fdeps-file=main.ddi -MD -MF main.d -c main.cpp

Replace $CCACHE_COMPILE with ccache g++ or equivalent.

Direct mode and depend mode need to be enabled for ccache and most of those module-related arguments you see in the example need to be detected for the compiler invocation by ccache.

You can also set the environment variable CCACHE_DEBUG=1 while compiling and check the logs that ccache creates for more detail about what is happening and whether the module artifacts were cached/restored.

@koplas

koplas commented Apr 11, 2025

Copy link
Copy Markdown

Thank you for your response! Enabling depend mode solved the issue, and I no longer experience recompilation cascades.

This change modifies the parsing of Makefile depend files to properly
handle the additional output GCC >= 14 produces when compiling with the
-fmodules[-ts] flag.
This commit combines multiple processing phases into a single phase:

- path rewriting for `base_dir`
- collecting includes from MSVC /showIncludes

The motivation for combining these is because for MSVC C++20 module
support, we also need to be able to process /sourceDependencies which
prints a JSON object to stdout.

Without this change, adding support for /sourceDependencies as a
separate phase would mean potentially processing stdout up to three
times per compilation.
This change is to allow modifying the context on deserialization of
certain special file types related to C++20 modules
(such as DDI files or similar).

We sometimes need to use the data from these files to reconstruct
information about the compilation context which cannot be obtained
solely through the command-line arguments and which would otherwise
require running the compiler and observing the output.

Specifically this is a problem when trying to determine the correct
output name for the BMI file.

GCC does not allow to specify the BMI file-name, and the compiler
chosen BMI name depends on the name (identifier) of the C++20 module in
the translation unit.

Clang and MSVC allow the output BMI file-name to be optionally
specified but if it is not specified there is no guarantee or
fixed schema for how the name will be chosen.
@avikivity

Copy link
Copy Markdown

Was the work here abandoned? I was looking forward to using modules (although, without the distcc equivalent, it will still be difficult).

@silvanshade

Copy link
Copy Markdown
Author

Was the work here abandoned?

It hasn't been abandoned but I don't know when it will be completed.

What isn't visible in this PR is additional work I've done on preparing patches for both Clang and GCC which fill in most of the missing information for the p1689 files, in order to avoid having to rely on messy and error-prone heuristics to try and reconstruct or infer that information.

The Clang work in particular is somewhat substantial because it replaces the need for clang-scan-deps entirely.

I tried to avoid needing to go this route because obviously getting PRs upstream to facilitate this has its own challenges. But the reality is that the current state of module support and p1689 support is not sufficient for a good implementation of this feature.

I am hoping I will have the chance to revisit this sometime early next year, and get those PRs submitted upstream, but I can't promise anything.

@mathstuf

Copy link
Copy Markdown

Do you have anything written up on what needs to change in P1689 support? (My interest is that I authored P1689.)

As for having clang do it instead of clang-scan-deps, that was my preferred direction as well, but I got lost in the layers of abstraction without much progress before an upstream developer carried that torch. See this issue for upstream awareness of that: llvm/llvm-project#76726

@silvanshade

Copy link
Copy Markdown
Author

Do you have anything written up on what needs to change in P1689 support? (My interest is that I authored P1689.)

I don't have anything concrete written up but that would be helpful.

From what I remember of the last time I worked on this, it wasn't so much that the p1689 format itself was the issue as it was that Clang and GCC just didn't tend to fill in many of the fields that could have been used when constructing the manifests for caching.

I don't remember the specifics, but things like knowing which input source file was compiled, what the corresponding object file was, what the other inputs/headers were, etc., basically all of the same kind of info that is reconstructed from the depfiles.

As for having clang do it instead of clang-scan-deps, that was my preferred direction as well, but I got lost in the layers of abstraction without much progress before an upstream developer carried that torch. See this issue for upstream awareness of that: llvm/llvm-project#76726

I see.

I did in fact manage to get a single-pass front-end implementation to work, which emits the p1689 data during compilation (like with Makefile depfiles), but the implementation purposely lacks a lot of the functionality of clang-scan-deps which is beyond the scope of the p1689 data, like the stuff related to scanning for compile_commands.json.

That, and the fact that clang-scan-deps was structured as a separate tool, with much of the logic not implemented through the existing module analysis libraries, meant that I had to re-implement a fair amount of the p1689 analysis in order to make it available to the frontend, rather than being able to re-use what was already there from that tool.

It wasn't clear to me how the project would react to that, although my impression was that clang-scan-deps should probably continue to exist as a separate tool, including its more advanced functionality.

Both the PRs for Clang and for GCC were actually in a usable state though, populating all of the fields specified in the p1689 spec (with the option for extra vendor specific data), but they needed some refactoring and commentary and justification before submission.

Since people seem to be interested, I'll see if I can make those implementations available and link them here.

@ChuanqiXu9

Copy link
Copy Markdown

Just out of curiosity, I noticed mozilla/sccache#2516 I think sccache are more complex than ccache, but that PR is 10x smaller than this. Would you like to tell the difference? Sorry if you feel uncomfortable, I am just interested.

@silvanshade

silvanshade commented Dec 23, 2025 •

Copy link
Copy Markdown
Author

Just out of curiosity, I noticed mozilla/sccache#2516 I think sccache are more complex than ccache, but that PR is 10x smaller than this. Would you like to tell the difference? Sorry if you feel uncomfortable, I am just interested.

At a glance, that PR is doing far less than what this PR is doing, feature-wise.

That PR appears to only partially support Clang (not GCC nor MSVC) based solely on specific command-line arguments. They don't parse the P1689 JSON files at all, nor the MSVC custom JSON format, nor process the Make depfiles for module artifacts (in GCC's case).

It's a starting point but I wouldn't expect that approach to be a complete solution as far as C++20 module support goes.

EDIT: Another difference, which isn't directly related to the feature support, but does make this PR larger, is that in some cases I've included updated documentation, bug-fixes, refactorings, substantial tests, and introduced some new abstractions (e.g., for the compiler) in order to make the code more maintainable and to help offset the additional complexity of module support.

@jrosdahl jrosdahl added type: feature PR adds a new feature and removed issue: feature New feature labels Jan 30, 2026
@TroyKomodo

Copy link
Copy Markdown

Just out of curiosity, I noticed mozilla/sccache#2516 I think sccache are more complex than ccache, but that PR is 10x smaller than this. Would you like to tell the difference? Sorry if you feel uncomfortable, I am just interested.

At a glance, that PR is doing far less than what this PR is doing, feature-wise.

That PR appears to only partially support Clang (not GCC nor MSVC) based solely on specific command-line arguments. They don't parse the P1689 JSON files at all, nor the MSVC custom JSON format, nor process the Make depfiles for module artifacts (in GCC's case).

It's a starting point but I wouldn't expect that approach to be a complete solution as far as C++20 module support goes.

EDIT: Another difference, which isn't directly related to the feature support, but does make this PR larger, is that in some cases I've included updated documentation, bug-fixes, refactorings, substantial tests, and introduced some new abstractions (e.g., for the compiler) in order to make the code more maintainable and to help offset the additional complexity of module support.

I'm the author of the other PR, maybe im missing something but in the context of sccache I dont think we would ever need to parse the P1689 output at all since we are not a build system so we dont need to decide how & what order to compile the modules, we rely on the build system (cmake or any other c++20 modules supported build system) to generate the correct compile commands and then we use those to correctly cache. This approach should work for both MSVC and Clang. The gcc approach is a bit annoying since it requires a bit more work, however cmake seems to use clang's dep scanner and then generates similar compile commands to clang.

Maybe im missing something but why do you need to do all the extra work specifically for ccache?

@silvanshade

Copy link
Copy Markdown
Author

Maybe im missing something but why do you need to do all the extra work specifically for ccache?

It's been a while since I've thought about thought deeply about this so I probably don't remember all of the exact details.

The problem though is that if you don't parse the P1689 files, which I really think should be the closest thing to the source of truth from the perspective of external tooling, then you require on some other tool making all of the exact dependency details explicit through a big collection of command line flags.

Currently this does work if you're compiling using a build system that provides this (like CMake) and everything is fully explicit on the command line. But it doesn't work if you're compiling with GCC where some of this information is implicit or potentially contained in non-standard entries in the Make depfiles (which I either was parsing or ignoring here depending on the exact context).

It's also reasonable to assume I think that at some point the compiler frontends, particularly Clang's, might move more in the direction of providing a very lightweight interface for compiling a project with modules where almost all of the necessary information is implicit. The patches I worked on for the Clang frontend (which merged the clang-scan-deps functionality into the main frontend) worked more like this.

There's also the fact that ccache already parses Make depfiles for computing some dependency information when constructing the manifests. This is described to some extent here.

Theoretically, the P1689 provide a much more precise format for communicating the same information and more, so as long as ccache continues to function the way it does, it makes sense to also use the information from there. The problem is that (without the patches I mentioned working on), Clang and GCC leave a lot of the optional fields unpopulated.

From a design perspective, I think it's also better in the long run for maintenance if the caching tools could rely more on the P1689 format, and less on parsing the compiler flags. The latter is a lot messier, because flags are different between compilers and between versions, and also just due to the nature of having to parse and normalize all of that into something usable.

I don't know much about how sccache works so hard to say much beyond that. I would be curious to see how well the approach your considering seems to work in practice. I suspect getting a nice solution for GCC may be a bit involved. I'm not sure that relying on clang-scan-deps for that (if that's what CMake is doing) is a great solution but maybe it would be enough for most users at least given how things are right now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: feature PR adds a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add support for C++20 modules

8 participants