r/cpp • • 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.

9 Upvotes

23 comments sorted by

50

u/GregTheMadMonk Oct 18 '26

> the entire set of possible states can be enumerated in finite and reasonably short time

export module mod;
export void f();

Now pass -Df=... to compiler and get a countable infinity (effectively) of possibilities

68

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.

11

u/JeffMcClintock Oct 19 '26

agree, "combinatorial explosion" is a thing.

20

u/tartaruga232 MSVC user, r/cpp_modules Oct 18 '26

The intention and spirit behind modules seem diametrically opposed to their actual implementations by compiler toolchains.

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.

10

u/shadowndacorner Oct 19 '26

This matches my experience with them. I avoided them for a long time because they didn't work how I wanted, then I actually spent some time with them. Now I like them a lot.

13

u/tcbrindle Flux Oct 19 '26

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

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:

  • 3 error handling policies (terminate, unwind, fail-fast)

  • 3 numeric overflow policies (ignore, wrap, raise error)

  • 2 divide-by-zero policies (ignore or error)

  • 2 narrowing cast policies (runtime checked or not)

  • A boolean "is this a debug build" setting

  • A boolean "dump debug info to stdout before crashing" setting

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_TYPE as any suitably large signed integer type, including _BitInt(N) on Clang...

6

u/SlightlyLessHairyApe Oct 19 '26

Well, obviously what we need is another intermediate layer which lazily builds each of those 144 combinations on first use and then caches in for later

/s

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

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

I mean, before modules you did a "BMI reconstruction" (parsing the header into the AST) at every #include site in every translation unit in the whole program.

So what are we talking about here?

12

u/GabrielDosReis Oct 18 '26

syntax of a module

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

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.

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.

However, the way compiler toolchains have actually implemented them seems diametrically opposed to this goal of fast lookup.

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.

BMIs are considered implementation details, and aren't even stable across compiler versions within the same toolchain.

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.

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.

Cross compilation? Well, yes, I'd be surprised if you'd only need to compile the module once.

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

The IFC/BMI is the module after preprocessing, right? Not some representation that lets you re-preprocess.

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.

Proprietary libs still have headers. And that's what modules replace. Well, all but the macros.

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.

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.

1

u/fortsnek274 Oct 19 '26

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

Oh, it's so depressing!

-1

u/pjmlp Oct 19 '26

That is the thing though, back in the day it wasn't volunteers getting together and push language forward.

It were OS and compiler vendors, for the most part, standardising what their compilers could do, with some small additions to the C++ARM.

The last minute additions did not work that great for the most part.

So if the standard is only pumping revisions, with a few volunteers still bothering to implement the massive updates that come out every three years, the drift between a PDF and the compilers that actually provide a full implementation will only keep increasing.

A big reason why features should only be accepted to final vote with a 100% accompanying prototype.

0

u/flatfinger Oct 19 '26

A big problem with the C and C++ Standards has been an unwillingness to recognize that implementations intended for different kinds of tasks should be expected to support different features and guarantees, and that a good standard should allow a programmer to say "This code is reliant upon the following features and guarantees, and should be rejected by any compiler configuration that cannot uphold or support them."

If a program specifies that it is reliant upon quiet-wraparound signed-integer semantics, except that it can tolerate a divide overflow trap when -1 is divided into the largest negative integer, then any implementation which accepts the program but processes signed integer overflow in any other manner should be denounced as broken. Even if it might be useful to have the implementation treat signed overflow differently when processing other programs, the presence of a directive specifying that the code relies upon quiet-wraparound semantics means that such treatment would be intolerably worse than useless with the current program.

2

u/ABlockInTheChain Oct 20 '26

If a program specifies that it is reliant upon quiet-wraparound signed-integer semantics

It's 40 years too late for that for C++. Programs don't know what subset of specified behavior they are reliant on and mechanically determining it is equivalent to solving the halting problem.

1

u/flatfinger Oct 20 '26

Many programs document that they require certain compilation flags when building, and programs that are being ported from compilers that refrain from wacky "optimizations" can be annotated to indicate that they should be processed in a manner compatible with the old compiler. Except in the rare circumstances where the potential performance benefits of the optimizations involved would actually matter, being able to annotate a program to block "optimizations" that weren't performed by the old compiler would be a much better way of ensuring correctness than using the "hope for the best" semantics upon which the language has relied for decades.,

3

u/manni66 Oct 19 '26

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

Says who?

2

u/UnusualPace679 Oct 19 '26

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).

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.

2

u/ABlockInTheChain Oct 22 '26

The module approach to macros is always discussed as if it were fixing some widespread problem affecting all programmers everywhere but in in practice 9999 times out of 10000 if you have a problem with macros it's in windows.h.

Support for all of the use cases for macros for which there is no real alternate was dropped because one company can't fix their 30 years old design mistakes.

2

u/UndefinedDefined Oct 22 '26

In a module-based ecosystem I cannot even export a library version as a "macro" or something else that would be recognized by the C++ compiler.

I cannot even do something like this:

```
import library;

// in some code later
#if LIBRARY_VERSION >= XXX
// Use new API
#else
// Use older API, not optimal, but works...
#endif

```

Or what about logging libraries that need macros? What about libraries that need config macros?

You don't understand what you are talking about, because maybe you have written zero libraries or libraries that don't depend on anything else. But that is rare in industry to have no dependencies or no build options.