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
Explore this link on the map →saved by
related reading
- [1912.05036] RVSDG: An Intermediate Representation for Optimizing Compilersarxiv.org
- A Gentle Introduction to LLVM IR · mcyoungmcyoung.xyz
- Algorithms for Modern Hardware - Algorithmicaen.algorithmica.org
- Compilers - What Every Programmer Should Know About Compiler Optimizations | Microsoft Learnlearn.microsoft.com
- abseil / Performance Hintsabseil.io
- The compiler will optimize that away | RoyalSlothblog.royalsloth.eu
- Paul Buchheit: Optimizing everything: some details matter a lot, most don'tpaulbuchheit.blogspot.com
- What Every C Programmer Should Know About Undefined Behavior #2/3 - The LLVM Project Blogblog.llvm.org
- Building a Performant Compiler from Scratch — Bowen Chengbowencheng.dev
- What Every C Programmer Should Know About Undefined Behavior #3/3 - The LLVM Project Blogblog.llvm.org
- Optimization of Computer Programs in Cicps.u-strasbg.fr
- Understanding static single assignment formsblog.yossarian.net