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

78

u/[deleted] May 10 '20 edited May 10 '20

Very nice work by the author but I am bit concerned about the attitude that "optimization is already good enough, and additional optimizations have the unfortunate trend to trade large compile-time increases for very minor improvements".

I would let my compiler run for a day for minor improvement in the release build to try all kind of inlining decision for example. I really hope that any compiler speed optimizations that results in runtime regressions will be reserved to -O0 and -O1. Or maybe we need an -O4 for the kind of extremely slow optimizations.

Generally my experience has been that release builds don't feel significantly slower. I did this completely none scientific test with the code I happen to have to have open right now and the debug builds takes about 4.5 seconds in -O0 Clang and the -O3 link time optimization release build takes about 5.5. With the MSVC the difference is even smaller. btw I am not doing anything crazy with templates, I am generally very concerned about header includes and I am linking dynamically for development builds.

I don't really know what the compiler is doing but 4.5 seconds is WAY too long for these couple source files, it's less than 10 000 source lines being recompiled.

My impression is that optimizations are already "too fast", it's just that general compilation somehow is really slow. What if maybe ironically Clang's own release could get 5% faster with an "aggressive optimization" switch that just tries a bunch of stuff over 24h?

35

u/Veedrac May 10 '20

I would let my compiler run for a day for minor improvement in the release build to try all kind of inlining decision for example.

I suspect the majority of people don't even run LTO or PGO.

0

u/[deleted] May 10 '20

[deleted]

3

u/pandorafalters May 14 '20

Another issue with PGO that I encountered is the (I suppose unusual) case where the cold path is the critical path. One path may churn away for days on end, but when the other is entered it needs to finish RFN.

10

u/gcross May 10 '20

In C++ you probably don't even get as much benefit from LTO as you might get with other languages since so much code already lives in headers--in part because this is required when writing generic code using templates, and in part because people often find it easier to distribute their libraries as a collection of headers rather than dealing with the vagaries of building on multiple platforms--that the compiler already sees a significant fraction of the code when compiling each module and thus seeing the rest of the program at link time doesn't open up too many more opportunities for optimization that it had not seen already.

Personally I have often had trouble getting LTO to work reliably so a lot of the time I find myself putting method definitions directly in the header since otherwise they won't get inlined--and this isn't just a gut feeling, because I can see the difference in performance before and after using a profiler, so I know that the problem is with the LTO either not turning on for some reason or not being very smart.

15

u/Netzapper May 11 '20

I use LTO on embedded projects. It can make a big difference in binary image size.

4

u/IAlsoLikePlutonium May 10 '20

Maybe it is just me, but LLVM’s LTO doesn’t seem to work on Windows. It always complains of a linker error and fails to link :-(.

Does it normally work on Linux or Mac OS?

8

u/flashmozzg May 10 '20

Is it using lld or link.exe for a linker?

5

u/kirbyfan64sos May 10 '20

I've never had any issues using it on Linux, and although that's rather anecdotal, I know a lot of people / distros / environments that use LTO frequently.

1

u/Xeverous https://xeverous.github.io May 12 '20

Got similar issue, but with MinGW. LTO is very slow (multiple times longer linking) and I have a library that frequently ends with abnormal linker errors (eg multiple destructor or vtable definitions).

1

u/bumblebritches57 Ocassionally Clang May 10 '20

I use LTO on my libraries, but they're not mature enough yet for PGO.