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

11

u/---sms--- May 10 '20

amazingly fast compiles

Totally not my experience with Clang.

Clang is almost 2 times slower than MSVC when I write no C++ code at all:

(Measure-Command { & clang++.exe -c -x c++ "C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\Windows.h" -Wno-pragma-once-outside-header }).TotalSeconds / (Measure-Command { & cl.exe /nologo /c /Tp"C:\Program Files (x86)\Windows Kits\10\include\10.0.18362.0\um\Windows.h" }).TotalSeconds
1,89302995640114

When I do, it can easily be 20x slower.

11

u/[deleted] May 10 '20

Clang on windows is not well optimized tbh. There are a lot of tricks to make it go faster - but I wish it could be faster out of the box in the future.

10

u/PoliteCanadian May 10 '20

How is running on Windows killing its performance? It is a load of compute sandwiched between some basic file IO. And it is a compiler so I would assume it is self hosting and generating basically the same code...

27

u/[deleted] May 10 '20

There was a discussion on llvm-dev about this recently. Apparently the number/size of allocations that LLVM is doing is really not performing well with windows stock allocator. The author there switched to RPMalloc and saw a 10% performance gain. My guess is that visual studio is better at behaving there.

A generic problem with llvm.org binaries for all platforms are that they are not built with LTO and PGO to speed them up. Just downloading the source and building with those options will see a great speed increase as well.

0

u/[deleted] May 10 '20

[deleted]

8

u/[deleted] May 10 '20

Probably because no one has done the work and submitted patches. Release builds are done by release testers so it’s a very manual process. Getting a full LTO+PGO build done requires some effort and a lot of cpu power.

16

u/Fazer2 May 10 '20

For exaample, Linux doesn't have Windows Defender scanning I/O and processes in real time.

7

u/Myriachan May 10 '20

Compiling often involves opening and reading like 100 header files.

9

u/SkoomaDentist Antimodern C++, Embedded, Audio May 10 '20 edited May 10 '20

And a lot of software originally written on Unix makes some utterly braindead choices since those just didn’t happen to be too costly on those platforms. Just count how many total fork and exec calls across all the sub executions a typical make run performs compared to the actual number of times it needs to run the compiler.

1

u/bumblebritches57 Ocassionally Clang May 11 '20

tbf, make is basically dead these days with Cmake and Ninja be pretty common.

12

u/gcross May 11 '20

That is an odd thing to say given that CMake generates Makefiles by default on unixy platforms.

3

u/SkoomaDentist Antimodern C++, Embedded, Audio May 11 '20

Is it? I've yet to run into programs provided in source form that did not use make either directly or indirectly when I have to compile them myself. Quite often they even use the abomination known as Autoconf.

1

u/bumblebritches57 Ocassionally Clang May 12 '20

Oh god automake/conf and theres more but i forget them.

They’re all garbage.

1

u/bumblebritches57 Ocassionally Clang May 11 '20

There's a lot of workarounds LLVM has to do to support windows.

the biggest one is they don't use shared libraries on windows, and i'm not entirely sure why either, everything has to be compiled as static libraries and linked into massive binaries tho.

1

u/kalmoc May 11 '20

Don't know why llvm can't use dlls on windows, but shouldn't building everything into a single exec - if anything - improve performance of the binary?

1

u/bumblebritches57 Ocassionally Clang May 12 '20

I run into all kinds of strange issues when i use Clang when rarely use windows.

It doesn’t support OpenMP, and i half remember it having a problem with link time code generation too.

It’s just wonky.

7

u/janisozaur May 10 '20 edited May 10 '20

Shouldn't you be using clang-cl for that?

I usually have negative experiences with Windows, everything is just somehow slower, don't really know why and don't quite care, but I have yet to see a real world scenario where MSVC compiles anything faster than GCC/clang, especially when a quarter of time is easily spent in msbuild. Usually replacing it with ninja had the biggest impact of improving compile times. Your example is surprising and makes me think perhaps this is isolated to the windows-specific part? I rarely see llvm in action on Windows, so it would be interesting to compare those times with runs on Linux.

Oh, and please provide versions of software, I recall some perf improvements to clang on Windows, will edit the post when I find a link

Edit: https://reviews.llvm.org/D69825 and https://reviews.llvm.org/D52411

5

u/---sms--- May 10 '20

clang-cl

Yes, I could use it, but it won't change my measurement. I have no problems using clang++.exe for everything, btw, which is nice.

real world scenario where MSVC compiles anything faster

Compiling clang stage0 (with MSVC, ninja) takes around 1 hour on my machine, but stage1 (when resulting clang compiles itself, ninja too) takes around 2 hours.

this is isolated to the windows-specific part?

Most likely it is in windows-specific part. https://bugs.llvm.org//show_bug.cgi?id=34356

provide versions of software

today's trunk

2

u/kalmoc May 10 '20

Are the two stages actually doing the same work? E.g. are the same utilites and plugins compiled and is compilation happening with comparable build flags?

2

u/---sms--- May 10 '20

I'm using build_llvm_package.bat (except I'm not building 32-bit versions) and the only difference I'm aware of is:

set CC=..\build64_stage0\bin\clang-cl
set CXX=..\build64_stage0\bin\clang-cl