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