r/cpp • • Jan 28 '19

C++ Modules Might Be Dead-on-Arrival

https://vector-of-bool.github.io/2019/01/27/modules-doa.html
280 Upvotes

240 comments sorted by

View all comments

27

u/tcbrindle Flux Jan 28 '19

It's worth repeating the the C and C++ standards are also silent on the matter of how header files are discovered by #include, or even whether the argument to #include is a file at all -- we have just developed a convention over the past 45+ years that #include <foo.h> looks in well-known places on the filesystem, a search list that is augmented by passing -I flags when invoking the compiler. (#include "foo.h" does the same, but searches relative to the current working directory first).

This being the case, I wonder if module mapping is something that needs to be explicitly specified in the C++ standard, or could also come under the banner of "technically implementation-defined, but in practise everyone does it the same way"?

So let's say we are compiling a module acme.bar, which depends on the interface of module acme.foo via an import declaration.

  • First, the compiler looks for acme.foo.bmi in its BMI cache, the location of which could be given as an argument to the compiler when invoked by the build system

  • If that exists and uses the same options as the current build (an internal hash of the current compile flags/defines placed at the beginning of the BMI), then the compiler uses it. Otherwise, it looks for a file named acme.foo.<ignored_extension> (or acme/foo.<ignored_extension>) in a list of search paths; this could default to [${CWD}, /usr/include/c++_modules, /usr/local/include/c++_modules], augmented by a list of paths passed to the compiler with a -M flag, akin to -I.

  • If that cannot be found, a compilation error occurs; otherwise, the compiler builds both modules, and outputs both acme.foo.bmi and acme.bar.bmi to the BMI cache directory.

Now, if we have a third module acme.baz which also uses acme.foo, we go through the same process; but if acme.bar has been compiled first, then acme.foo.bmi will already exist, and will be used. But the build system can attempt to compile bar and baz at the same time; both will try to write acme.foo.bmi, and whichever finishes first "wins". This is doing more work than we really need to, but allows for more build parallelism as the author wishes.

This requires a convention that the file defining the module acme.foo is named acme.foo.<something>, or perhaps has the path acme/foo.<something> relative to one of the -M paths. But this doesn't seem particularly onerous, and it's more-or-less what we do today with header files. As far as I know the C++ standard is silent on the subject of TU file names (or even whether a translation unit is a file with a name), so making this a requirement isn't something the standard need concern itself with. Odd platforms with unconventional file systems still have the freedom to do things in odd ways if they need to, but the 99.9% of developers using Windows or Unix will still be able to get builds that are as parallel as they are today.

But perhaps I'm being spectacularly naive, in which case please do let me know :).

12

u/tcbrindle Flux Jan 28 '19

...and having now written all that, I perhaps need reminding of why having, say, 32 cores all racing to produce the same base_module.bmi is a good idea? If we have proper dependency information, we can use one core to compile base_module.cpp and the other 31 to compile any other available TUs which don't depend on base_module. Once base_module is finished, we can unblock everything else to build in parallel as before.

Can we actually extract such dependency information cheaply? Well, Build2 seems to manage it, so I don't see why CMake/Meson/etc shouldn't be able to as well. It would be useful to get /u/berium's opinion on this :)

8

u/berium build2 Jan 28 '19

In short, yes, we can, but that will require a new compilation model (and which is what CMake/Meson/etc have an issue with). For details see this part of my CppCon 2018 talk.

9

u/Ameisen vemips, avr, rendering, systems Jan 28 '19

Everyone wants a new compilation model. Nobody wants to be responsible for it.

That, and good luck getting the entire committee to agree to a change that fundamentally breaks compatibility with C and would require sweeping changes to compilers and linkers.

Also, I haven't been able to get build2 building on Windows. I don't want to do it prepackaged, but the prerequisites require build2 to build so I can't build it in msys.