r/cpp • u/delta_p_delta_x • Oct 18 '26
The intention and spirit behind modules seem diametrically opposed to their actual implementations by compiler toolchains.
The lynchpin of C++ modules is the built module interface (BMI). Each BMI is meant to be a lightweight data structure representing the syntax of a module unit (comprised of one primary module interface and potentially one or more partitions), with approximately O(1) complexity of symbol lookup. This massively accelerates repeated lookups into the same translation unit, and avoids paying the cost of a full preprocessor run and front-end lex + parse, as might otherwise happen with header files. In this way both the first clean build, and incremental builds are considerably accelerated and made much more efficient—build the BMI once, reuse it across future compiles, and we don't need large core counts and massively parallel jobs because the BMI is now available afterwards.
However, the way compiler toolchains have actually implemented them seems diametrically opposed to this goal of fast lookup. Today, BMIs are highly sensitive to any change in the command-line invocation of the compiler/dependency scanning tool, including the C++ version, warning and feature flags, and especially macro definitions, with -DMACRO or /DMACRO. This means that changing just one flag is likely to cause a BMI cache miss and a full BMI reconstruction, which can be fairly slow for larger modules (like module std).
BMIs are considered implementation details, and aren't even stable across compiler versions within the same toolchain. Compare to object files, where something compiled by really old versions can still be used on newer versions without too much fuss. This, from the user's perspective, entirely defeats that design ideal mentioned above: fast repeated lookups. Consider a simple example: a certain module uses a project-specific macro representing the platform, to change several function signatures. This module is now precompiled thrice, once each for -DFOO_WINDOWS, -DFOO_DARWIN, -DFOO_LINUX, instead of just once.
I believe that macros and the preprocessor are highly problematic. However, their very limitations are also an advantage here: their effect on code collapses to completely binary decisions. A macro is either defined or not, and a macro is either in the set of accepted values, or it isn't (e.g. #if __cplusplus >= 202603L). The preprocessor state might change throughout the source code of a translation unit, but the entire set of possible states can be enumerated in finite and reasonably short time at compile time—otherwise the front-end would never complete its work. To me, this seems like the preprocessor and parser could enumerate all possible preprocessor states of the source code, and stamp out both possibilities for each macro switch in the BMI; during lookup, the compiler can select the correct code path depending on what is provided at that particular invocation (not code definitions, but command-line invocations, as this would break the module assurance that macros do not leak into BMIs from consumers, nor do consumers see macros from a BMI).
This would make things like library feature configuration with macros really straightforward—specify the macros one wants for that feature at the command-line, which is enshrined in build system code rather than source code—the BMI can be reused, done.
Now, the compiler and C++ standard are a lot harder as these probably affect the internal state of the compiler rather than merely the value of __cplusplus; however, I genuinely feel like there really ought to be a better way forward, especially if C++ is to continue as a 'source may not be available' language. This discussion is moot if we always want to compile from source, but I want to say that is wasteful, and another driving force of the modules feature was to do away with wasteful and repeated recomputation of the same thing.
As mentioned above, build systems are also forced into this rather inefficient model to accommodate compiler behaviour. CMake in its latest iteration, for instance, has a notion of 'BMI compatibility', and probably does some sort of set manipulation on compile flags to determine if another BMI for the same module interface is needed or not, based on how it is imported. Why? This shouldn't be the case, a BMI really ought to be widely (if not universally) compatible, regardless of what flags go in the command-line.
I'm sure I don't have the entire picture as I am not a compiler front-end developer, and I don't mean to sound impertinent here, the effort put into delivering modules has been huge. However there are some decisions made which, as modules user and advocate, seem a little contradictory to the spirit of modules. I've written this after some thought on the situation and I'm interested in hearing from the community and compiler front-end developers who have worked on modules on my thoughts above.
Cheers.
50
u/GregTheMadMonk Oct 18 '26
> the entire set of possible states can be enumerated in finite and reasonably short time
Now pass
-Df=...to compiler and get a countable infinity (effectively) of possibilities68
u/STL MSVC STL Dev Oct 18 '26
Preprocessor settings are multiplicative. I'm sorry, but you have no idea what you're suggesting here.
20
u/tartaruga232 MSVC user, r/cpp_modules Oct 18 '26
That's just your expectation. If you actually start using modules, you will likely find that they do not work like your expectations are.
I think a lot of people have some wrong expectations about modules.
Don't read too much into the keyword "module". Just learn what the semantics are and start using it. Then you will discover that the feature is actually useful as it is.
I think people worry way too much about BMI's being rebuilt too often.
13
u/tcbrindle Flux Oct 19 '26
My pet project, Flux, is not a huge C++ library by most standards, but it does have a few configuration switches you can play with. Specifically, it has:
If I understand your proposal correctly, you're suggesting that the BMI should include 3 * 3 * 2 * 2 * 2 * 2 = 144 distinct copies of the library? That seems... less than optimal.
And that's before we get to the fact that you can define
FLUX_INT_TYPEas any suitably large signed integer type, including_BitInt(N)on Clang...12
u/sokka2d Oct 18 '26
Read up on what “combinatorial explosion” means.
Also, not recompiling object files when preprocessor flags change can be fine, or can be a recipe for ODR violations.
7
u/not_a_novel_account cmake dev Oct 19 '26
I mean, before modules you did a "BMI reconstruction" (parsing the header into the AST) at every
#includesite in every translation unit in the whole program.So what are we talking about here?
12
u/GabrielDosReis Oct 18 '26
I don't remember syntax ever drove my design or vision for C++ Modules, or the IFC that showed dramatic savings in both time and space of building C++.
Semantics should be front and center, for most things C++.
6
u/fortsnek274 Oct 19 '26
Well, yes, but isn't that what the compiler does with headers too? It's just that a different TU can reuse parsing results. Like a PCH.
This isn't some LLM output is it. Maybe you mean the compilation cascade, which clang docs show you how that can be reduced. Hash the interface.
Yes, I tend to think of modules as standardised precompiled headers where the build system should cache them to IFC/BMI, keyed on compilation flags. And maybe get the compiler to switch out dllexport for dllimport.
Cross compilation? Well, yes, I'd be surprised if you'd only need to compile the module once.
The IFC/BMI is the module after preprocessing, right? Not some representation that lets you re-preprocess.
Proprietary libs still have headers. And that's what modules replace. Well, all but the macros.
Yeah, build systems are having trouble. But a module compilation per set of compile flags is the proper way it seems. There is no way to compile a module once for everything.
But I would hope there is a way to reduce the number of compilations. Such as ignoring preprocessor defs that the module doesn't use. For, say, two projects with different macros that import the same DLL module. Or reusing an IFC where you only need to swap out dllexport for dllimport.
6
u/stick_figure Oct 19 '26
Having tangled with the clang frontend implementation, I would put the shoe on the other foot. People's expectations for what modules are is completely different than what the implementers involved in standardization set out to create. The scope drift has caused this huge mismatch in expectations and reality, and there's a massive funding gap between what the community wants and has been sold and what the industry is willing to sponsor in terms of frontend development.
People act like they've been waiting years for features in a product that they're paying into, but the reality is that implementing the next C++ standard version is something folks are cramming in between other management priorities, and fixing the modules ecosystem is not at the top of any corporate quarterly plan.
3
u/manni66 Oct 19 '26
Says who?
2
u/UnusualPace679 Oct 19 '26
Are you proposing a limited form of macros?
1
u/Conscious_Support176 Oct 21 '26
Macros are a preprocessor thing and can paste tokens together so a separate lex and parse would be needed after preprocessing if a module system supported all macros.
Macros were always a cheap substitute for actual capabilities, so just about everything you might want to use them for in a bmi can now be done better a different way.
Implementing a cut down macro system seems pointless. If the macros are a build option to support different targets and your build applies it to all bmis for that build, why not define a separate build folder for each combination of options so you never need to rebuild just because you switched target?
1
u/UndefinedDefined Oct 22 '26
I personally see the biggest drawback with modules specification that they don't account for any options of the compiled code. Options were historically a domain of the preprocessor, but every library I know provides these.
So where are modules? It's an unusable feature in practice, because C++ still has zero options (except using macros) to target multiple platforms in a portable way, to add support for features in libraries, and to customize dependencies.
I think this was the biggest mistake and this literally means that non-toy projects and libraries having a lot of dependencies cannot possibly embrace modules. I tried, no savings, if your project already uses PCH, there is literally no gain. The ecosystem is #include-based anyway, and I don't really know any module-only lib that I would need.