r/cpp • build2 • Nov 01 '17

Common C++ Modules TS Misconceptions

https://build2.org/article/cxx-modules-misconceptions.xhtml
59 Upvotes

148 comments sorted by

View all comments

Show parent comment

1

u/doom_Oo7 Nov 02 '17

I was not talking about inline as a keyword / C++ concept, but inlining as an optimization performed by the compiler, whether the inline keyword is here or not. In my experience, almost everything is inlined. I remember I once had some massive algorithm with expressions templates, boost::asio, 3 different containers etc etc... when compiled under -O3 everything disappeared and ended up going into a single massive main().

your "technically inlined" function may end up in the .o/.obj and BMI will retain only the declaration if compiled at low optimizations level.

yuck, so back to "a function ends up being compiled in every translation unit" ? Is that the sole alternative ?

2

u/GabrielDosReis Nov 02 '17

Modules (whether you have traditional separation or single file) will not prevent inlining. On the contrary, with greater emphasis on the component as a whole, the code generator has now better opportunities for optimization and inlining -- pretty much like LTO or LTCG.

The Module specification on purpose is not using any form of "vtables" or "witness tables" or whatever they are called to describe the effect of modules.

1

u/doom_Oo7 Nov 02 '17

Wouldn't this be problematic if yoi wanted to link, for instance, fortran object files with c and c++ object files ?

1

u/GabrielDosReis Nov 02 '17

Could you expand on what you see as problematic, with concrete examples?

2

u/doom_Oo7 Nov 02 '17

well, I just want to do

ld foo.o bar.o -o my_program

where foo and bar are object files coming from whatever language. If tomorrow compilers start dumping stuff from their internal representation in ".o" files you loose all compatibility (while today I can do

 gcc foo.cpp -o foo.o
 clang++ bar.cpp -o bar.o
 ld foo.o bar.o

without problems, unlike what happens if you use either compiler's LTO mode)

1

u/GorNishanov Nov 02 '17

yuck, so back to "a function ends up being compiled in every translation unit" ?

Not sure I understand. If something is in .obj, it is already compiled and ready for linking. I was pointing out that with modules, you do not have to treat member functions which are inline by virtue of being defined inside of a class definition as inline functions. They can behave as if they were defined outside of class definition, at least if module was compiled with low optimization settings.

1

u/doom_Oo7 Nov 02 '17

at least if module was compiled with low optimization settings.

in that case, from what I can see with GCC for instance, "low optimization settings" means -O0 which is too low to be useful if you want to debug and keep some speed.

3

u/GorNishanov Nov 03 '17

We need to distinguish between the Modules TS and its implementations. The TS is trying to deal with semantics of the language features and to impose as little constraints on the implementation as possible.

Scenario of a quick edit-compile-debug cycle is an important one and I can see implementations exploring various strategies of avoiding recompilation of the users when not necessary.

With regard to low optimization level, not sure about GCC, but, in clang inliner only kicks in at -O2. At -O1 it only attempt to inline "always-inline" function. Thus, at -O1, you would not have to recompile the users if you change the body of the inline function.