by deterministic · 3y ago · 0 comments
I mostly disable the optimisations provided by LLVM. Because it makes code generation much slower with almost no performance gains. Writing high level optimisation passes before converting the AST to LLVM is what made the generated code super fast.
LLVM has no knowledge of the semantics of the language. So it can only optimise low level details that the higher level optimisations will get rid of anyway.
It seems to me that the LLVM optimisations are only of benefit to you if you generate really bad LLVM code in the first place.
That said:
> It seems to me that the LLVM optimisations are only of benefit to you if you generate really bad LLVM code in the first place.
Much llvm development is sponsored by large corporations for whom it really is worth it to squeeze that last 1%.
That said, I don't think your statement that LLVM optimizations only benefit you if you generate bad input IR is correct. It just sounds like they are not useful for your specific problem domain.
LLVM optimizations are only really effective for simplifying integer expressions and loop patterns seen in code generated from imperative languages.
Most compilers don't have to, and then you just inline whatever you can until some estimated register pressure is reached at the call site.
The idea is that, you know best what optimizations work for your domain.
But a compiler needs a large amount of engineering for things which are not optimizations.
MLIR makes it possible to get this infra (developed utilizing lessons from LLVM and other compilers) for free and share improvements among multiple compilers without pulling your hair out trying to understand misleading academic papers.
My (partial/incomplete/buggy/experimental) Ruby compiler generates awful code, and still by far the biggest performance bottleneck is creation and garbage collection of objects that improving the low level code generation will have only marginal effects on.
E.g. finally adding type tagging for integers (instead of creating objects on the heap) sped up compiling itself by tens of times (taking it from unusually slow to comparable to MRI on that specific task) and there's nothing a low level optimizer will do to figure out transformations like that.
Maybe one day I'll get far enough on fixing the high level issues that it'll be worth even trying to do more complex low level optimizations, but that's a long time away.
LLVM is largely (at least originally) tuned for clang's output which tends towards simple IR that LLVM will clean up later, with a fair bias towards making numerical benchmarks run faster.