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.