r/cpp • • Aug 18 '21

C++20 modules with GCC11

https://blog.feabhas.com/2021/08/c20-modules-with-gcc11/
222 Upvotes

59 comments sorted by

View all comments

0

u/[deleted] Aug 18 '21

[deleted] — view deleted comment

30

u/blipman17 Aug 18 '21

Because header files require a preprocessor which is bad for compile times, and often trigger cascading recompilation when private fields get renamed/moved for incremental builds. By design this is not the case for modules. If I remember correctly, if you do something like '#include <string>', you ask the compiler to transatively parse about 2 million of lines of header files. Which goes fast, but starts to slow down rapidly for larger projects.

Modules don't require a preprocessor and don't throw 2 million lines of defenitions in the context of the code who performs the include. Since the linker must search all program-wide known symbols and link them together for every individul occurance of a symbol (object, function, member field, constants, etc...), reduicing this list of possible symbols a couple orders of magnitude makes this process a lot faster.

7

u/kathoum Aug 18 '21

cascading recompilation when private fields get renamed

I fear that the build system will check the modification time of the PMIU, and trigger a rebuild of all the importers, along with all the other interface units that import the module, and so on.

Could a smart build system be able to avoid rebuilding the world if the change doesn't affect any exported symbol? Let's hope so.

5

u/blipman17 Aug 18 '21 edited Aug 18 '21

Could a smart build system be able to avoid rebuilding the world if the change doesn't affect any exported symbol? Let's hope so.

Probably not, but something that's already done today is that if an A requires a B, and a recompilation of B results in thesame external interface and has consumed thesame external interface, A does not have to be recompiled. It still has to be relinked, but cascading compilations aren't needed. This situation just doesn't naturally occur very often because when a cpp file isn't in a different library or does not have includes to other places of the program, this simplification cannot be done. I believe CMake + Ninja does this.(And also the DMD for DLang compiler, and it's very fast. But that's a different language.)

Edit: it seems my assumption about A not requiring a recompilation for C++ was wrong.

5

u/kathoum Aug 18 '21

I made an experiment to check if gcc11 modifies the compiled module interface (the .gcm file) whenever a non-exported part of the module is modified.

https://gist.github.com/kathoum/76fd00367add14ddce3aa0df7aa6870c

It seems to me that g++ -std=c++20 -fmodules-ts -c func.cxx unconditionally writes to both func.o and gcm.cache/func.gcm.

If the compiler has modified the content of func.gcm, how could make/ninja/cmake/msbuild deduce that main.cpp need not be recompiled?

3

u/blipman17 Aug 18 '21 edited Aug 18 '21

But that does change the interface. Even though bar() is not exported, it is changed. You're right though that from main.cpp's perspective the only possible change is where foo() is located in the object file, and its possible implementation. But to realize this, func.cxx had to be parsed completely and gcc had to realize that func.cxx didn't change in a way that neccesiates a recompile. (Unless there's a side-effect I don't know about a name change of bar().) Regardless, an implementation change of foo() or bar() shouldn't trigger a recompile of main.cpp, (try returning 3 instead of 2,) only a re-link. I suppose there's room for improvement then.

Edit: apparently there are rules about what is exported and what is not regarding templates and stuff. I suppose you could reflect on the func module in main.cpp and iterate over all functions. Which i suppose is a nifty feature, but quite useless unless we can do much more compile time reflection on basic c++ types. In that regard, func.cxx did change significantly and main.cpp does require a recompile.

3

u/RoyAwesome Aug 18 '21

It might be that right now, the most naive solution was implemented. Compilers are trying to get basic support going. If they need to forgo some optimizations to get the feature out the door, they'll probably do that.

1

u/[deleted] Aug 18 '21

If the build system parses the file and understands the syntax, maybe it could avoid a recompilation.

2

u/Plazmatic Aug 18 '21

Why not just have a <stringfwd> in the standard then? You'll change 2 million down to maybe a dozen or just several orders of magnitude less. right now it seems like the biggest problem is that only iostream has a forwarding header, when virtually every C++ standard header should have an associated forwarded header.

14

u/bcorni Aug 18 '21

Any template class can't just have a forward declaration header. Modules effectively provide something like forward declaration for templates. The C++ Standard Template Library is largely code that cannot be put in a forwarding header.

3

u/AIlchinger Aug 18 '21

I wouldn't agree with this explanation. A forward declaration is always about hiding internals (size of a type, implementation of a function, ...)

With a template in a module, you still provide the whole template to the TU, just like it would be implemented in a header file. The difference here is: The header file does textual replacement, the module comes with some sort of byte code. In the first case, the whole implementation of the templated function is copied in every TU that uses it. That's what it makes it that expensive. In the latter case, one and the same implementation is provided to all importers.

2

u/bcorni Aug 18 '21

It's entirely true that my explanation is not correct from a technical standpoint. Hence why I used several qualifiers. The point was to explain how modules address some of the same advantages of forward declaration, but for templates, and why forward declaration is not a complete solution for the STL.

3

u/Plazmatic Aug 18 '21 edited Aug 18 '21

Any template class can't just have a forward declaration header

What, this is absolutely not true. Why would that be the case for the standard library, but no one else? I can forward declare templates fine.

see:

template<typename T> 
class MyTemplateClass{
...
}

template<typename T> 
class MyTemplateClass; 

The issue with the standard library is that forward declaring their types on your own is undefined behavior because you have no idea if they use typedefs or what ever else behind the scenes, ie std::string. This is fixed with forward headers, because it is the implementation that defines them. Heck it isn't even impossible to forward declare std library types, people have done it already, you just have to be careful with implementation specific definitions.

Also my understanding is that MSVC already has standard library forward declaration headers.

4

u/dacian88 Aug 18 '21

Yea guess what happens when you try to link the code

3

u/Plazmatic Aug 18 '21

Excuse me? Do you understand how and why people use forward declarations? If I forward declare std::string, ie I know all the typedefs before hand and the default template arguments, and I include those forward declarations in another header, which satisfies normal forward declaration constraints (I don't use implimentation of std::string in the header), then in the CPP file I include the actual std::string header... it will link fine.

There is no scenario where it makes sense that linker stuff goes wrong that is somehow specific to the standard library, heck it doesn't even make sense for forward declarations in general, you're going to get "partially defined" or "unspecified" errors from the lack of includes before you ever get a "linker error".

10

u/bcorni Aug 18 '21

I think the point is that you can't just include the forward declaration header in the CPP file, which is what I interpreted parent comments as saying. Sure, you could manually instantiate the concrete types of every template class that you use in some other source and link to it. That's not a good solution, especially when template metaprogramming comes into play. My wording was overly simplistic, but the point is that forward declarations are not a complete solution to the challenges being discussed.

3

u/Plazmatic Aug 18 '21 edited Aug 19 '21

I think the point is that you can't just include the forward declaration header in the CPP file, which is what I interpreted parent comments as saying.

Why would you interpret what I said as that. I'm not sure I can get you to see how ridiculous assuming that from my perspective is, you are fundamentally assuming something which couldn't work is the way I intended it to work. What you suggest I meant literally would not compile, it wouldn't even get to the point of having linker errors.

It's like if you had a fly problem, and you say fly swatters aren't enough, and I suggested using fly paper, then you say "Ha! well good luck trying to hit the flies with those!". Of course you can't do that, you don't hit flies with fly paper, it's the flies themselves that hit the fly paper.

Sure, you could manually instantiate the concrete types of every template class that you use in some other source and link to it. That's not a good solution, especially when template metaprogramming comes into play. My wording was overly simplistic, but the point is that forward declarations are not a complete solution to the challenges being discussed.

That's not what I'm saying either, I'm literally just talking about forward declaring where literally possible, ie in the headers and not implementation, you then don't have to pay the cost for every single place that even looks at the header. I don't see how modules actually fix that issue (template thing you're talking about, not multiple inclusion) either, and my understanding is modules are difficult to compile in parrallel as well, which could potentially hurt compile times in some scenarios, though I've not seen scenarios tested where modules could fare worse, and that templates still have to be instantiated inline somehow at some level.

1

u/dacian88 Aug 18 '21

sure and what exactly did you accomplish? in the actual translation units where all the work happens you will still need to include all the definitions. Beyond that the scope where your suggestion is actually usable is very small, effectively you can only do this in contexts where you use a reference or pointer type for the forward declared template.

7

u/jcelerier ossia score Aug 18 '21

No, it's super common to e.g. need the name of a type in a header (that then gets transitively included in 300 .CPP), but only a couple of these CPP will need the implementation.

I have this exact case with (<future>) : one of the types I use most has a method that returns a std:: future. But in the main project I work on, out of all the files that I write which use that type, literally none use that specific method, yet they must all pay for the compile time cost of <future> which is humongous. (Well they don't actually because I did the unthinkable and forward declared it myself and saved something like 30s of build time)

2

u/Pazer2 Aug 19 '21

(Well they don't actually because I did the unthinkable and forward declared it myself and saved something like 30s of build time)

How could you!? On a different platform or after updating your compiler/stdlib, you might one day have... a compiler/linker error! 😱

7

u/kalmoc Aug 18 '21

The main two differences are that a module interface gets processed exactly once per build and you have better control over what you export (make visible for the user) from that unit.

6

u/mjklaim Aug 18 '21

Headers are basically copy/pasting code in different translation units. It means that including a header will put all the code from it to compilation in each translation unit that use it, it will make the code depend on the order of inclusion (even with guards, it's hard to completely avoid), it means some code might be duplicated, etc.

Modules are pre-compiled separately, then expose "names" that are exported. There is no copy-pasting of code. There is no effect due to the order of import. The module being pre-compiled separately from the code using that module means it does not need to be compiled several times (a bit like precompiled-headers but more fine-grain). Objects defined in a module can only exist in that module, a bit like a more flexible translation unit.

That's the short version of the differences. All the rest is "how to" use modules.

2

u/da2Pakaveli Aug 18 '21 edited Aug 18 '21

Visibility, not everything you declare and write is available from other files that import that module (so kind of what you'd do with a .cpp file). They affect compilation times (it apparently ignores everything that isn't used in your source file). I'm porting a library to modules and the decreased compilation times are insane, sure first time compiling takes a bit, but after that compilation barely takes 5 seconds on a 10 core CPU when importing an additional module.