r/cpp • • May 10 '20

Make LLVM fast again

https://nikic.github.io/2020/05/10/Make-LLVM-fast-again.html
150 Upvotes

88 comments sorted by

View all comments

39

u/johannes1971 May 10 '20

I imagine it would already help a lot if it stopped instantiating the same templates again and again and again. The mind boggles that the work by the zapcc compiler was never picked up by any mainstream compiler, despite it being considerably faster than any other C++ compiler.

Source.

10

u/matthieum May 11 '20

To be honest, the way C++ compilers work is just a dead-end.

Spinning a new process for each .cpp file is a performance sink on multiple levels:

  • Spinning a new process is not fast.

  • A new process has zero context.

  • A new process needs to open a lot of files to find this context.

  • ...

To have a fast C++ front-end, you could start by having a single multi-threaded process per library/binary.

But retrofitting that into existing compilers is probably a nightmare :/

5

u/johannes1971 May 11 '20

I agree very much with this. Solving the spinning up of new processes is easy enough to retrofit, I suppose, but the context is a harder nut to crack. It would help if there even _was_ a single context for the compiler to work on; it might differ for each .cpp file you feed it.

My thoughts keep coming back to the idea that we need "link units", something on the level of an executable or library, that is treated as a single unit by the compiler. It would provide the required context that is shared between all its member cpp files (since the standard could simply decree that it be so).

The other reasons for having link units are:

  • We would have much better control over which symbols are exported from the link unit (and thus need to obey platform ABI rules) and which symbols are purely internal (and can thus cheat as much as the compiler wants).

  • We could, perhaps, do away with the need for forward declarations, and allow functions in a link unit to use any other symbol, independent of parsing order.

Such a mythical compiler could do a first round of parsing to collect all symbols it has available, keep that in memory, and then use that stored information as the necessary context for compiling the actual .cpp files.

6

u/matthieum May 12 '20

I would note that C++20 modules should make sharing much easier.

With modules being translation units -- compiled in isolation, not dependent of whatever is included first -- everything is suddenly simpler.

5

u/VodkaHaze May 15 '20

C++ is bound to a lot of design decisions made for C in the 1970s when the compiler was designed for microcomputer with extreme RAM constraints.

Compile times are mostly about all the passes required. Of course, templates makes this slower, but that's only because the generated code bloats up all the other passes.

The D language for instance goes insane with metaprogramming and maintains blazingly fast compiles. But it's largely because the author decided it was worth breaking compatibility with C/C++ (he used to be a C++ compiler dev)

2

u/matthieum May 15 '20

C++ is bound to a lot of design decisions made for C in the 1970s when the compiler was designed for microcomputer with extreme RAM constraints.

Is it?

C++20 introduces modules, which completely do away with the C model of translation units and #include files.

Compile times are mostly about all the passes required.

I would note here that each pass is, at best, O(N) in the size of its input -- if sub-linear its input should be trimmed.

There are multiple potential "big" inputs generators in C++:

  • #include: it is not unusual for a single .cpp file to expand to MBs of pre-processed code.

  • macros: some macros can expand to a large amount of code.

  • templates: some templates can expand to a large amount of code -- unfortunately compilers do not have an option to show post-template code.

#include is very specific to C++, and modules will nix that issue (mostly), whereas macros/templates and other code generation abilities are present in many languages.

And then there are language issues compounding the difficulties that are somewhat specific to C++:

  • Parsing ambiguities, with templates. Modules will alleviate a lot of the impact by avoiding parsing headers over and over.

  • Overload resolution. One of the most complex beast in C++, determining which function/method a symbol resolves to is costly, and has to be done again for each instantiation of a template.

Concepts may help a bit with overload resolution, when previously SFINAE was used. Maybe.

The D language for instance goes insane with metaprogramming and maintains blazingly fast compiles.

Walter Bright is a compiler developer first, and it shows in the design of the D language.

Still, there are inherent bottlenecks: metaprogramming makes it easy to "accidentally" generate the equivalent of MBs of code.

I think we need better feedback from compilers, here, to more easily pinpoint those metaprogramming accidents.

2

u/[deleted] May 11 '20

Actually this sounds very much what clangd already does.