3 ms·
Optimizer person here, who has implemented each of these at least 20 times each ;-) The tradeoffs and engineering are quite complex. The short answer is that
by DannyBee 3y ago
Optimizer person here, who has implemented each of these at least 20 times each ;-)
The tradeoffs and engineering are quite complex.
The short answer is that the algorithms themselves are not the hard part. Compilers do not get better by magic algorithms most of the time, they get better by careful and hard tuning and testing (where are optimizations missed, what performance is lost or gained somewhere, etc).
They get better 0.01% at a time over 20 years. There are no magic bullets, only really hard work.
Those that try to implement these without LLVM discover it themselves, and either put in the work, or give up and realize it's better to reuse the work.
I mean, don't get me wrong - we spend plenty of time reducing complexity of algorithms, etc.
The difference between a textbook algorithm and one implemented in a production compiler is often the difference between a fisher price cell phone and and iphone.
That isn't always true mind you (sparse constant prop is pretty simple in both cases), but it's often true.
But this is applied engineering.
For example, the SSA construction algorithm in LLVM is based on sreedhar and gao's linear time algorithm.
The paper describes a mechanism that requires construction of separate data structures, is somewhat complex, etc.
If you were to implement it straight out, it's pretty slow. Much slower than other mechanisms.
LLVM's version is simple, 200 lines of code, and faster than just about any other algorithm you will find on both small and large functions. Oh, it also handles liveness pruning and works to compute both forward and reverse iterated dominance frontiers.
See https://llvm.org/doxygen/GenericIteratedDominanceFrontier_8h_source.html https://llvm.org/doxygen/GenericIteratedDominanceFrontier_8h...
Could language authors spend their time understanding the theory well enough to do this, reducing complexity, and engineering something that works as well. Sure, it's just software.
Is it a good use of their time?
Probably not.
This did not come out of thin air like this either. It's based on 10+ years of people improving pieces of it, reducing complexity, reusing it elsewhere/etc. It's easy to look at it as having come this way fully formed, but it didn't ;) (in this particular case, even the in-llvm code history does not do it justice).
Someday, I hope LLVM is not really necessary, whether it's because we can run the sorts of complex/combined algorithms and not worry about it, or because AI is good enough at approximating optimizing pipelines or whatever.
But right now? if you want to compete on performance for real, you'd be hard pressed to do it.