❔ Optimize Compiler Code Generation
I am writing a compiler and I am almost done with all the base syntax/functionality, and am up to optimization.
Right now, the compiler isn’t optimized at all. No constant folding, no function inlining, and dead code is still emitted
Where can I learn about the optimization steps above and other optimizations for my compiler’s code generation? How helpful is the Dragon Book? (I already have the book, but am not up to a point where it talked about optimization yet). Any good articles/videos/resources?
8 Replies
optimization is a deep topic, better use a library
What libraries would help me with this? (LLVM?) even if I end up using a library, I still would like to learn about optimization further, so do you have any recommendations?
idk, from what I heard people who write actual compilers for non toy programming languages just use llvm as a black box
from theory standpoint, I don't know, I haven't learned that
@T = (Q, Σ, Γ, q₀, *, δ)
The Dragon Book covers many-many things, but as everything Dragon Book, it's really dense and super formal
What I'd do is take a list of optimizations (there's a wiki page listing quite a few), have some casual reading on the ones you are interested in, and once you have a basic understanding, then dive into the Dragon Book
Some essentials to look up:
- Intermediate representation (IR) of code, aka. on what form do we optimize? Relevant terms: three-address code, register machine, stack machine, SSA
- Structure of low-level IR codes, basic blocks, control flow graph
- Essential block-local optimization techniques: available expression, copy propagation, dead code elimination
- Various beginner-friendly optimizations: unreachable code cutting in the CFG, jump threading, jump inlining, and TCO if you feel adventurous
- Adapting block-local optimizations to procedure level (here I really recommend looking into the topic of data-flow analysis)
Sounds like a great idea, thanks!
We are writing an optimizing compiler currently and we are planning to do automatic vectorization, so we are extremely deep in the rabbit-hole 😄
Was this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.