Whole-book reading guide from stratified index samples; jump to passages in the text
Tip the Site
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat Pay
Alipay
Open WeChat or Alipay and scan. No login required.
AI guide
【One-Line Pitch】
A hands-on, recipe-style guide for compiler developers who want to master LLVM's modular architecture, from building a toy language frontend to writing custom optimization passes and understanding code generation.
【Book Arc】
- **Opening (~0%–9%)**: Introduces LLVM's modular design philosophy and walks through installation, basic usage of Clang and opt, converting C to LLVM IR, linking bitcode files, and generating assembly for different targets.
- **Early (~9%–23%)**: Builds a complete compiler frontend for a toy language "TOY", covering lexer, parser, AST construction, and IR code generation, then extends it with control flow (if/then/else) and loops using PHI nodes.
- **Early (~23%–32%)**: Adds advanced language features like user-defined operators (unary/binary) and JIT support, showing how to dynamically compile and execute code.
- **Middle (~32%–45%)**: Dives into writing custom LLVM passes—function-level analysis passes, loop structure analysis, opcode counting, and alias analysis—plus modifying existing passes like InstCombine and Reassociate.
- **Middle (~45%–55%)**: Covers platform-independent code generation topics: vectorization (SLP and loop vectorization), exception handling pruning, register allocation via greedy algorithm, and code emission through MC framework.
- **Late (~55%–end)**: Explores backend internals including MachineInstr, MachineBasicBlock, SelectionDAG nodes, and visualization of control flow and dependency graphs with GraphViz.
【Key Takeaways】
- **LLVM's modularity is its core strength** (Opening): Each compilation stage—frontend, optimizer, backend—is an independent library with clean C++ APIs, making it the most approachable compiler framework for learning and extension.
- **Clang and opt are the primary workhorses** (Opening): Clang converts C/C++ to LLVM IR (`-emit-llvm -S`), while opt applies individual optimizations like `-instcombine` and `-deadargelim`, letting you inspect each transformation step.
- **Building a toy language frontend teaches the full pipeline** (Early): The TOY language example walks through lexer → parser → AST → Codegen(), demonstrating how source code maps to LLVM IR with minimal complexity.
- **PHI nodes are essential for SSA-form control flow** (Early): Loops and conditionals require PHI nodes to select values from different basic blocks, as shown in the `for` loop example where `%i` comes from either `%entry` or `%loop`.
- **Custom passes are straightforward to write and register** (Middle): Inheriting from `FunctionPass`, implementing `runOnFunction`, and using `RegisterPass` lets you build analysis and transformation passes loadable via `opt -load`.
- **Pattern matching enables powerful IR transformations** (Middle): The InstCombine example shows how to match complex expression patterns like `(A|(B^C)) ^ ((A^C)^B)` and reduce them to simpler forms like `A&(B^C)`.
- **Register allocation uses greedy heuristics over graph coloring** (Middle): LLVM prioritizes variables with longer live ranges for registers, using interference graphs and spill weights to manage limited register resources.
- **Code emission supports both JIT and static compilation** (Middle): LLVM can emit code directly to memory for JIT execution or use the MC framework to produce assembly and object files for any target platform.
【Reading Tips】
- **Skim the early installation and basic usage chapters** (~0%–9%) if you're already familiar with compilers; focus instead on the TOY language example which is the book's pedagogical core.
- **Deep-read the TOY compiler chapters** (~9%–32%)—they're the most valuable part, showing a complete frontend implementation you can extend and experiment with.
- **Pay special attention to the PHI node explanation** in the loop section (Early); it's a common stumbling block for SSA newcomers, and the example makes it concrete.
- **For pass-writing chapters** (Middle), focus on the structure: class definition, `runOnFunction` implementation, and `RegisterPass` registration—the pattern repeats across all examples.
- **The backend chapters** (Late) are more reference-like; skim for concepts like MachineInstr and SelectionDAG rather than trying to memorize APIs.
【Coverage Limits】
Excerpts cover roughly the first half to two-thirds of the book (through platform-independent code generation); later chapters on specific backend targets and advanced JIT techniques are not represented in this guide.
经被优化了。但指令依旧是平台无关 的,需要映射到平台相关的指令。而指令选择阶段将采用目标无关的 DAG 节点作为输入, 匹配特定的模式,将其映射到特定平台的 DAG 输出节点。 TableGen DAG 指令选择器从.td文件读入指令模式,自动化构建部分的模式匹配代 码。 详细步骤 SelectionDAG...
Support this siteYour recognition and a small knowledge-service contribution help keep this technical work open source.
Scan the WeChat Pay or Alipay code below. Logged-in and guest visitors can both tip.
WeChat PayAlipay
Open WeChat or Alipay and scan. No login required.
Loading comments...
Reply to Comment
Edit Comment