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?

5

u/AlexReinkingYale May 11 '20

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

As a compiler writer myself, I agree with the author for the most part. If you need absolute maximum performance, you either shouldn't expect your compiler to give it to you or you shouldn't be writing in C/C++.

Here's what good enough means: the compiler's output won't bottleneck your optimization efforts. And this is mostly true. No matter what sort of application you're writing, the quality of LLVM's output is so high that the most important optimizations come from large-scale reorganizations of computation and resource loading, full stop.

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.

So I don't think this would help very much. Inlining isn't the only transformation that might make a program go faster, and it certainly isn't enough to improve your program's cache locality, data access and layout patterns, etc. For inlining specifically, LLVM is doing some really cool work replacing hand-rolled heuristics with ML-based ones. Other transformations (especially data layout) aren't up to the compiler thanks to ABI constraints.

The alternatives are to either painstakingly write assembly, or encode the particulars of the problem you're trying to solve in a domain-specific language. In a well-designed DSL, the space of allowable optimizations is much more targeted and you can push your performance much farther than you can in C/C++ alone.

1

u/MrWhite26 May 17 '20

or encode the particulars of the problem you're trying to solve in a domain-specific language.

The combination of SIMD intrinsics and template functions allows defining something like a DSL within C++.

It keeps all the compiler optimizations, but allows explicit instructions for a specific set of particulars.

1

u/AlexReinkingYale May 17 '20

Eh, not really. The closest thing to a success story I can think of along these lines is Eigen and it struggles to get good performance once your processing pipeline gets more than a level or two deep. The issue is that it isn't able to find good fusions or tilings of operations, automatically parallelize, or introduce redundant work that would enable parallelism. A proper DSL can make these global reorganizations much more easily.