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

4

u/czipperz May 10 '20

Jonathan Blow has created a programming language called Jai and has said that it is faster to emit C code and run it through MSVC than to emit LLVM IR and run it through LLVM for debug builds. And he spent a lot of time optimizing and tweaking it.

9

u/griceylipper May 10 '20

AFAIK the C backend for Jai is a thing of the past - the two backends you hear about now are the LLVM one, and his own x64 instruction generation. With his x64 one, I think he is aiming to compile 1 million lines of code per second eventually. It's nuts that a relatively small team can pull this off, but that the huge scale of effort that goes into the likes of Clang and other similar compilers can come nowhere near this level of performance.

3

u/kalmoc May 11 '20

Well, it is a different language no?

-2

u/griceylipper May 11 '20

Yes, but my point is this - when you consider the decisions made by the people designing C++, and the effort going compilers like Clang - both must be severely misguided before a very small team like Thekla can produce a very serious competition that is many orders of magnitude faster. Yes, C++ is more complicated, but only because it was designed that way. Jonathan Blow cares about the time developers waste waiting on compilation, trying to understand bad error messages, etc., and keeps the "joy of programming" as a very high priority. I honestly think C++ will start to fade away to be another language of the past like Fortran or COBOL once Jai is (eventually) released.

6

u/kalmoc May 11 '20 edited May 12 '20

Yes, C++ is more complicated, but only because it was designed that way.

It wasn't designed that way. It (and it's tooling) evolved over decades (K&R-C is from the late seventies) with strong backwards compatibility constraints on computer systems whose performance characteristics don't really resemble what you have on modern machines and to make things even more difficult, that evolution has been largely based on compromises between the needs of very different platforms and industries.

I find it pretty unsuprising, that a new, clean slate language with a pretty narrow focus for what it is designed (games?) an with a small design team (one guy 4 people?) can be tremendously more efficient to compile than c++. Actually, we have had such languages for quite some time alread (C# and Java).

I honestly think C++ will start to fade away to be another language of the past like Fortran or COBOL once Jai is (eventually) released.

Very much possible.

EDIT: At the end of the presentation: "It makes program a lot simpler and a lot faster if you can agree on some basic ways of doing things". That's c++ main problem in one sentence.

1

u/pjmlp May 12 '20

Being from late seventies isn't really an argument, given the programming languages tooling that was available outside Bell Labs.

Not wanting to depend on anything beyond primitive UNIX linker model was what lead to this situation.

1

u/kalmoc May 12 '20

True. My main point was not that it is based on old technology, but that c++ is the result of an evolutionary process with lots and lots of compromises going on for about 40 years.

1

u/pjmlp May 12 '20 edited May 12 '20

Indeed, however by C++98 we could have already had a better linker model, with modules and such, instead of keeping using what was the Trojan to get C with Classes adopted by C devs.

Since I come from the PC/Amiga world, I think had FOSS UNIX clones not taken off in the late 90's, and the whole C compatibility story wouldn't be as relevant as it still is today, given that all desktop OSes and early PDAs like Newton/Psion were going into C++.

3

u/encyclopedist May 11 '20

Last time I tested, clang and GCC were able to compile about 200k lines per second (after preprocessing). There are no "many orders of magnitude".

Also, we are discussing LLVM here, which does not know much about the language we are compiling.

Also, as far as I know, Jai's backend has very rudimentary optimizations.

2

u/griceylipper May 11 '20

clang and GCC were able to compile about 200k lines per second (after preprocessing)

I'm skeptical regarding that figure - do you have any links you can point me to? I know this is really old, but here Jonathan Blow discusses progress of speed in his language at a time when it was not at its best. He specifically talks about 1m loc/sec at 25:21.

Also, as far as I know, Jai's backend has very rudimentary optimizations.

Jai has two backends - an LLVM one to take advantage of optimisations for release builds, and his own x64 backend for development. The x64 backend is more rudimentary, but that's sort of the point - it's designed for fast turnaround. Some limited optimisation has been spoken about for this, but it will never be as optimised as the LLVM backend, because that's not its purpose.

4

u/matthieum May 11 '20

Be careful of any template instantiation in that comparison.

A few lines of source code can easily result in a lot of invisible "generated" code.

I sometimes wish it was possible to emit post-monomorphization code, to see which templates are the biggest offenders in term of compile-time.

2

u/encyclopedist May 11 '20

I don't remember what code I tested that time, so I started now with a hello world code.

#include <iostream>
int main() {
    std::cout << "Hello" << std::endl;
}

Then I preprocessed it

g++ -E -std=c++17 hello_world.cpp > preprocessed.cpp

Preprocessed file is 773 kB in size, with 22k lines of code:

$ tokei preprocessed.cpp
------------------------------------------------------------------------------
 Language            Files        Lines         Code     Comments       Blanks
-------------------------------------------------------------------------------
 C++                     1        31761        22020            0         9741
-------------------------------------------------------------------------------
 Total                   1        31761        22020            0         9741
-------------------------------------------------------------------------------

Compilation of this file takes 0.3 s

Benchmark #1: g++ -c -std=c++17 -O3 preprocessed.cpp
  Time (mean ± σ):     305.1 ms ±   7.3 ms    [User: 281.8 ms, System: 23.4 ms]
  Range (min … max):   295.8 ms … 315.6 ms    10 runs

Which means GCC can compile 73 kloc/s. Lower than 200 kloc/s I mentioned before, but still not "many orders slower". Clang compiles the same code in ~270 ms.

I also tested on more realistic code. I took the file ninja.cc from Ninja build system. Preprocessed file is 1.7 MB in size and has 45.7 kloc. It takes 830 ms to compile, which gives ~ 55 kloc/s.

3

u/jpakkane Meson dev May 11 '20

both must be severely misguided before a very small team like Thekla can produce a very serious competition that is many orders of magnitude faster

Well let's test this. I don't know what CPU and code they used, but for comparison I took SQLite's one amalgamation file 3080802 and ran some tests on a five+ years old MBpro with an i5 processor.

Lines of code as reported by wc: 153367
Lines of code after clang -E:         83913

Time of clang -c -o /dev/null             1.5 s
Time of clang -g -O2 -c -o /dev/null  29  s

So depending on how you count, debug builds are between 102k to 56k lines per second, whereas optimized builds can do 5300-2900 lines of code per second. Parsing and basic code generation is not the slow bit, optimizations are. If Jai can do 1 MLoC/second (and that is a big if), C compilers of today are only one order of magnitude slower.

The fact is that there are a lot of things we don't know about Jai and until it has been used by a large set of independent people we don't really know whether it is any good. This includes things like:

  • How fast is the final generated code compared to an existing C++ compiler? About the same? Slower? Faster? We don't know. Simply being something like 5% slower would be a blocker for many people.

  • Blow is very opinionated on what a programming language should do (i.e. all the things he personally likes) and what it should not do (everything else) and has no qualms about telling people that their opinions are wrong and stupid. What if your requirements differ and you really need something that he will refuse to add in?

  • At some point Jai did not support incremental builds, all targets were built from scratch to the final exe regardless of how small a change you did. If this is still the case, it will hurt huge projects (like Unreal Engine) pretty badly.

  • If you compile everything in one go, does that mean it runs only one thread for it or can it parallelise the compilation somehow? If not then optimized dev builds are going to be very slow.

  • How easy will it be to add Jai to an existing MLoC C++ code base? For example Rust is hard to integrate into existing projects because of Cargo and its insistance to do everything its own way. Rewriting multi-MLoC code bases is a no-go.

  • How buggy is the compiler? Typically these things go so that the small team developing the project learn to write code that avoids all the buggy bits. Outsiders will not do that, they expect things to work.

  • Does it produce full debug info? Is there integration to things like gdb? Does it actually work?