Compiler Optimizations Are Hard Because They Forget - Faultlore
How exactly would you design an optimizing compiler? Or more specifically, how do you design and implement the actual optimizations? Trying to do it all at once is really overwhelming and probably impossible, so a lot of compiler optimization is: Staple a hundred of those “optimization passes” together (hopefully reusing step 2 as much as possible) and suddenly your compiler starts doing really big and complicated changes as an emergent property of all the small ones! One of the first things you’ll learn about in compilers is “peephole optimization”, which is the simplest kind of optimization pass, requiring very little analysis. Like, some of them could probably be done with an actual regex! Here’s a couple simple peepholes: Ah, but, hmm. What if you have (x * 2) * 2? Then you could turn that into x << 2! Oh hey, and we don’t actually need to write a new peephole for that, because the existing ones will combine to compute that, right? Just do: Let’s try it out on our definitely real c
Compiler Optimizations Are Hard Because They Forget How exactly would you design an optimizing compiler? Or more specifically, how do you design and implement the actual optimizations? Trying to do it all at once is really overwhelming and probably impossible, so a lot of compiler optimization is: Identify a situation where a trick could be applied Build analysis that can find that situation (or a generalization of it) Apply the trick to all the places you can find Staple a hundred of those “optimization passes” together (hopefully reusing step 2 as much as possible) and suddenly your compiler
saved by
related reading
- A Gentle Introduction to LLVM IR · mcyoungmcyoung.xyz
- A friendly introduction to machine learning compilers and optimizershuyenchip.com
- Making Deep Learning go Brrrr From First Principleshorace.io
- What can we learn from how compilers are designed?tedinski.com
- The compiler will optimize that away | RoyalSlothblog.royalsloth.eu
- Compilers - What Every Programmer Should Know About Compiler Optimizations | Microsoft Learnlearn.microsoft.com
- What to learndanluu.com
- Writing a C compiler in 500 lines of Pythonvgel.me
- Many can write faster asm than the compiler, yet don't. Why? — LessWronglesswrong.com
- Destroy All Values: Designing Deinitialization in Programming Languages - Faultloregankra.github.io
- Understanding static single assignment formsblog.yossarian.net
- abseil / Performance Hintsabseil.io