flâneur — a map of the web's best reading

Compiler Optimizations Are Hard Because They Forget - Faultlore

faultlore.com · 2,324 words · saved by 1 readers

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

Explore this link on the map →

saved by

related reading