No description
AI Reading Assistant
Whole-book reading guide from stratified index samples; jump to passages in the text
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.
Page 17
o output1.ll 3.查看输出,看看 instcombine优化是如何进行的: $ cat output1.ll ; ModuleID = 'testfile.ll' define i32 @test1(i32 %A) { ret i32 %A } define internal i...
View in text
Excerpt 2
含函数参数信息。 为表达式生成 IR 代码 本节将展示如何使用编译器前端来为表达式生成 IR 代码。 详细步骤 为了实现 TOY 语言的 LLVM IR 代码生成器,需要执行以下步骤。 【49】 LLVM Cookbook 中文版 1.在编译的时候,这些代码需要链接到 LLVM 库中,因此 llvm-...
View in text
Excerpt 3
k 中文版 } for(j = 0; j < 20; j++) { t++; } } return t; } 2.用 Clang 将其转换成.ll文件: $ clang –O0 –S –emit-llvm sample.c –o sample.ll 3.在样例代码上运行新的 Pass:
View in text
Excerpt 4
Ops.end(), ValueEntry(getRank(V), V)); e += 1; } IR 向量化 向量化(Vectorization)是编译器的一个重要优化,它可以向量化代码,在多个数据 集上同时执行一条指令。如果后端架构支持向量寄存器,那么一个很宽范围的数据就能存 储于这些向量寄存器中,而...
View in text
Excerpt 5
经被优化了。但指令依旧是平台无关 的,需要映射到平台相关的指令。而指令选择阶段将采用目标无关的 DAG 节点作为输入, 匹配特定的模式,将其映射到特定平台的 DAG 输出节点。 TableGen DAG 指令选择器从.td文件读入指令模式,自动化构建部分的模式匹配代 码。 详细步骤 SelectionDAG...
View in text
Excerpt 6
Fn); void calculateCallsInformation(MachineFunction &Fn); void calculateCalleeSavedRegisters(MachineFunction &Fn); 【215】 LLVM Cookbook 中文版 相关的,并且需要将跳转表、常...
View in text
Excerpt 7
*TOYTargetMachine::createPassConfig(PassManagerBase &PM) { return new TOYPassConfig(this, PM); } bool TOYPassConfig::addPreISel() { return false; } b...
View in text
Excerpt 8
准备工作 你需要构建安装 LLVM。 详细步骤 执行以下步骤。 1.编写 bugpoint 工具的测试用例: $ cat crash-narrowfunctiontest.ll define i32 @foo() { ret i32 1 } define i32 @test() { ca...
View in text
Tags
AI categories
ProgrammingcompilerBackend
Text Preview (First 20 pages)
Registered users can read the full content for free
Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.
Generating text preview…
Loading comments...
Reply to Comment
Edit Comment