llvm-learning
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseLLVM Learning Skill
LLVM 学习技能
This skill provides curated learning paths and resources for mastering LLVM, Clang, and compiler development.
本技能为掌握LLVM、Clang及编译器开发提供了精心整理的学习路径与资源。
Learning Paths
学习路径
Beginner Path
入门路径
- Start with Kaleidoscope: Official LLVM tutorial building a simple language
- Understand LLVM IR: Learn the intermediate representation
- Write Simple Passes: Transform IR with custom passes
- Explore Clang: Understand C/C++ frontend
- 从Kaleidoscope开始:官方LLVM教程,构建一门简单语言
- 理解LLVM IR:学习中间表示
- 编写简单Pass:用自定义Pass转换IR
- 探索Clang:理解C/C++前端
Intermediate Path
进阶路径
- Deep Dive into Optimization: Study built-in optimization passes
- Backend Development: Target code generation
- Analysis Frameworks: Pointer analysis, dataflow
- Clang Tooling: LibTooling, AST matchers
- 深入优化技术:研究内置优化Pass
- 后端开发:目标代码生成
- 分析框架:指针分析、数据流分析
- Clang工具链:LibTooling、AST匹配器
Advanced Path
高级路径
- MLIR: Multi-level IR for domain-specific compilation
- JIT Compilation: ORC JIT framework
- Security Features: Sanitizers, CFI implementation
- Contribute to LLVM: Patches, reviews, community
- MLIR:面向领域特定编译的多层IR
- JIT编译:ORC JIT框架
- 安全特性:Sanitizers、CFI实现
- 贡献LLVM:补丁、代码评审、社区参与
Official Resources
官方资源
Documentation
文档
- LLVM Docs: https://llvm.org/docs
- Clang Docs: https://clang.llvm.org/docs
- LLVM Language Reference: Complete IR specification
- Programmer's Manual: Core APIs and usage
- LLVM Docs:https://llvm.org/docs
- Clang Docs:https://clang.llvm.org/docs
- LLVM Language Reference:完整的IR规范
- Programmer's Manual:核心API及使用指南
Tutorials
教程
- Kaleidoscope: https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html
- Writing an LLVM Pass: https://llvm.org/docs/WritingAnLLVMPass.html
- Building LLVM: https://llvm.org/docs/GettingStarted.html
- Kaleidoscope:https://llvm.org/docs/tutorial/MyFirstLanguageFrontend/index.html
- Writing an LLVM Pass:https://llvm.org/docs/WritingAnLLVMPass.html
- Building LLVM:https://llvm.org/docs/GettingStarted.html
Community Resources
社区资源
Books and Guides
书籍与指南
- Learn LLVM 12 by Kai Nacke: Comprehensive practical guide
- LLVM Techniques, Tips, and Best Practices: Advanced patterns
- Getting Started with LLVM Core Libraries: Foundation concepts
- LLVM Essentials: Quick start guide
- Learn LLVM 12 by Kai Nacke:综合性实用指南
- LLVM Techniques, Tips, and Best Practices:高级模式
- Getting Started with LLVM Core Libraries:基础概念
- LLVM Essentials:快速入门指南
Video Resources
视频资源
- LLVM Developer Meetings: Recorded talks on YouTube
- LLVM Weekly: Newsletter with latest developments
- EuroLLVM/US LLVM: Conference recordings
- LLVM Developer Meetings:YouTube上的录制演讲
- LLVM Weekly:最新进展通讯
- EuroLLVM/US LLVM:会议录制内容
GitHub Learning Projects
GitHub学习项目
undefinedundefinedHighly recommended starter projects
Highly recommended starter projects
banach-space/llvm-tutor # Comprehensive pass examples
hunterzju/llvm-tutorial # Step-by-step tutorials
jauhien/iron-kaleidoscope # Kaleidoscope in Rust
bigconvience/llvm-ir-in-action # IR examples
undefinedbanach-space/llvm-tutor # Comprehensive pass examples
hunterzju/llvm-tutorial # Step-by-step tutorials
jauhien/iron-kaleidoscope # Kaleidoscope in Rust
bigconvience/llvm-ir-in-action # IR examples
undefinedLLVM IR Essentials
LLVM IR 核心知识
Basic Structure
基本结构
llvm
; Module level
@global_var = global i32 42
; Function definition
define i32 @add(i32 %a, i32 %b) {
entry:
%sum = add i32 %a, %b
ret i32 %sum
}
; External declaration
declare i32 @printf(i8*, ...)llvm
; Module level
@global_var = global i32 42
; Function definition
define i32 @add(i32 %a, i32 %b) {
entry:
%sum = add i32 %a, %b
ret i32 %sum
}
; External declaration
declare i32 @printf(i8*, ...)Common Instructions
常见指令
llvm
; Arithmetic
%result = add i32 %a, %b
%result = sub i32 %a, %b
%result = mul i32 %a, %b
; Memory
%ptr = alloca i32
store i32 %value, i32* %ptr
%loaded = load i32, i32* %ptr
; Control flow
br i1 %cond, label %then, label %else
br label %next
; Function calls
%retval = call i32 @function(i32 %arg)llvm
; Arithmetic
%result = add i32 %a, %b
%result = sub i32 %a, %b
%result = mul i32 %a, %b
; Memory
%ptr = alloca i32
store i32 %value, i32* %ptr
%loaded = load i32, i32* %ptr
; Control flow
br i1 %cond, label %then, label %else
br label %next
; Function calls
%retval = call i32 @function(i32 %arg)Type System
类型系统
llvm
; Integer types: i1, i8, i16, i32, i64, i128
; Floating point: half, float, double
; Pointers: i32*, [10 x i32]*, { i32, i64 }*
; Arrays: [10 x i32]
; Structs: { i32, i64, i8* }
; Vectors: <4 x i32>llvm
; Integer types: i1, i8, i16, i32, i64, i128
; Floating point: half, float, double
; Pointers: i32*, [10 x i32]*, { i32, i64 }*
; Arrays: [10 x i32]
; Structs: { i32, i64, i8* }
; Vectors: <4 x i32>Writing LLVM Passes
编写LLVM Pass
New Pass Manager (Recommended)
新Pass管理器(推荐)
cpp
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/PassBuilder.h"
struct MyPass : public llvm::PassInfoMixin<MyPass> {
llvm::PreservedAnalyses run(llvm::Function &F,
llvm::FunctionAnalysisManager &FAM) {
for (auto &BB : F) {
for (auto &I : BB) {
llvm::errs() << I << "\n";
}
}
return llvm::PreservedAnalyses::all();
}
};
// Plugin registration
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "MyPass", LLVM_VERSION_STRING,
[](llvm::PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](llvm::StringRef Name, llvm::FunctionPassManager &FPM,
llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (Name == "my-pass") {
FPM.addPass(MyPass());
return true;
}
return false;
});
}};
}cpp
#include "llvm/Passes/PassPlugin.h"
#include "llvm/Passes/PassBuilder.h"
struct MyPass : public llvm::PassInfoMixin<MyPass> {
llvm::PreservedAnalyses run(llvm::Function &F,
llvm::FunctionAnalysisManager &FAM) {
for (auto &BB : F) {
for (auto &I : BB) {
llvm::errs() << I << "\n";
}
}
return llvm::PreservedAnalyses::all();
}
};
// Plugin registration
extern "C" LLVM_ATTRIBUTE_WEAK ::llvm::PassPluginLibraryInfo
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, "MyPass", LLVM_VERSION_STRING,
[](llvm::PassBuilder &PB) {
PB.registerPipelineParsingCallback(
[](llvm::StringRef Name, llvm::FunctionPassManager &FPM,
llvm::ArrayRef<llvm::PassBuilder::PipelineElement>) {
if (Name == "my-pass") {
FPM.addPass(MyPass());
return true;
}
return false;
});
}};
}Running Custom Passes
运行自定义Pass
bash
undefinedbash
undefinedBuild as shared library
Build as shared library
clang++ -shared -fPIC -o MyPass.so MyPass.cpp
llvm-config --cxxflags --ldflagsclang++ -shared -fPIC -o MyPass.so MyPass.cpp
llvm-config --cxxflags --ldflagsRun with opt
Run with opt
opt -load-pass-plugin=./MyPass.so -passes="my-pass" input.ll -o output.ll
undefinedopt -load-pass-plugin=./MyPass.so -passes="my-pass" input.ll -o output.ll
undefinedClang Development
Clang开发
AST Exploration
AST探索
bash
undefinedbash
undefinedDump AST
Dump AST
clang -Xclang -ast-dump -fsyntax-only source.cpp
clang -Xclang -ast-dump -fsyntax-only source.cpp
AST as JSON
AST as JSON
clang -Xclang -ast-dump=json -fsyntax-only source.cpp
undefinedclang -Xclang -ast-dump=json -fsyntax-only source.cpp
undefinedLibTooling Basics
LibTooling基础
cpp
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
// Match all function declarations
auto matcher = functionDecl().bind("func");
class Callback : public MatchFinder::MatchCallback {
void run(const MatchFinder::MatchResult &Result) override {
if (auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func")) {
llvm::outs() << "Found: " << FD->getName() << "\n";
}
}
};cpp
#include "clang/Tooling/Tooling.h"
#include "clang/ASTMatchers/ASTMatchers.h"
#include "clang/ASTMatchers/ASTMatchFinder.h"
using namespace clang::ast_matchers;
// Match all function declarations
auto matcher = functionDecl().bind("func");
class Callback : public MatchFinder::MatchCallback {
void run(const MatchFinder::MatchResult &Result) override {
if (auto *FD = Result.Nodes.getNodeAs<FunctionDecl>("func")) {
llvm::outs() << "Found: " << FD->getName() << "\n";
}
}
};AST Matchers Reference
AST匹配器参考
cpp
// Common matchers
functionDecl() // Match function declarations
varDecl() // Match variable declarations
binaryOperator() // Match binary operators
callExpr() // Match function calls
ifStmt() // Match if statements
// Narrowing matchers
hasName("foo") // Match by name
hasType(asString("int")) // Match by type
isPrivate() // Match private members
// Traversal matchers
hasDescendant(...) // Match in subtree
hasAncestor(...) // Match in parent tree
hasBody(...) // Match function bodycpp
// Common matchers
functionDecl() // Match function declarations
varDecl() // Match variable declarations
binaryOperator() // Match binary operators
callExpr() // Match function calls
ifStmt() // Match if statements
// Narrowing matchers
hasName("foo") // Match by name
hasType(asString("int")) // Match by type
isPrivate() // Match private members
// Traversal matchers
hasDescendant(...) // Match in subtree
hasAncestor(...) // Match in parent tree
hasBody(...) // Match function bodyDevelopment Tools
开发工具
Essential Commands
核心命令
bash
undefinedbash
undefinedCompile to LLVM IR
Compile to LLVM IR
clang -S -emit-llvm source.c -o source.ll
clang -S -emit-llvm source.c -o source.ll
Optimize IR
Optimize IR
opt -O2 source.ll -o optimized.ll
opt -O2 source.ll -o optimized.ll
Disassemble bitcode
Disassemble bitcode
llvm-dis source.bc -o source.ll
llvm-dis source.bc -o source.ll
Assemble to bitcode
Assemble to bitcode
llvm-as source.ll -o source.bc
llvm-as source.ll -o source.bc
Generate native code
Generate native code
llc source.ll -o source.s
llc source.ll -o source.s
View CFG
View CFG
opt -passes=view-cfg source.ll
undefinedopt -passes=view-cfg source.ll
undefinedDebugging LLVM IR
调试LLVM IR
bash
undefinedbash
undefinedWith debug info
With debug info
clang -g -S -emit-llvm source.c
clang -g -S -emit-llvm source.c
Debug IR transformation
Debug IR transformation
opt -debug-pass=Structure -O2 source.ll
opt -debug-pass=Structure -O2 source.ll
Verify IR validity
Verify IR validity
opt -passes=verify source.ll
undefinedopt -passes=verify source.ll
undefinedCommon Analysis APIs
常用分析API
cpp
// Dominator Tree
#include "llvm/Analysis/Dominators.h"
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
if (DT.dominates(BB1, BB2)) { /* BB1 dominates BB2 */ }
// Loop Analysis
#include "llvm/Analysis/LoopInfo.h"
LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
for (Loop *L : LI) { /* process loops */ }
// Alias Analysis
#include "llvm/Analysis/AliasAnalysis.h"
AAResults &AA = FAM.getResult<AAManager>(F);
AliasResult R = AA.alias(Ptr1, Ptr2);
// Call Graph
#include "llvm/Analysis/CallGraph.h"
CallGraph &CG = MAM.getResult<CallGraphAnalysis>(M);cpp
// Dominator Tree
#include "llvm/Analysis/Dominators.h"
DominatorTree &DT = FAM.getResult<DominatorTreeAnalysis>(F);
if (DT.dominates(BB1, BB2)) { /* BB1 dominates BB2 */ }
// Loop Analysis
#include "llvm/Analysis/LoopInfo.h"
LoopInfo &LI = FAM.getResult<LoopAnalysis>(F);
for (Loop *L : LI) { /* process loops */ }
// Alias Analysis
#include "llvm/Analysis/AliasAnalysis.h"
AAResults &AA = FAM.getResult<AAManager>(F);
AliasResult R = AA.alias(Ptr1, Ptr2);
// Call Graph
#include "llvm/Analysis/CallGraph.h"
CallGraph &CG = MAM.getResult<CallGraphAnalysis>(M);Practice Projects
实践项目
Beginner
入门级
- Instruction counter pass
- Function call logger
- Dead code finder
- 指令计数器Pass
- 函数调用日志器
- 死代码查找工具
Intermediate
进阶级
- Simple constant propagation
- Branch probability estimator
- Memory access pattern analyzer
- 简单常量传播
- 分支概率估算器
- 内存访问模式分析器
Advanced
高级
- Custom optimization pass
- Security vulnerability detector
- Domain-specific language frontend
- 自定义优化Pass
- 安全漏洞检测器
- 领域特定语言前端
Community
社区
Getting Help
获取帮助
- LLVM Discourse: https://discourse.llvm.org
- LLVM Discord: Real-time chat
- LLVM Mailing Lists: llvm-dev, cfe-dev
- LLVM Discourse:https://discourse.llvm.org
- LLVM Discord:实时聊天
- LLVM邮件列表:llvm-dev, cfe-dev
Contributing
贡献代码
- Phabricator: Code reviews (being migrated to GitHub)
- GitHub: https://github.com/llvm/llvm-project
- Bug Tracker: https://github.com/llvm/llvm-project/issues
- Phabricator:代码评审(正在迁移至GitHub)
- GitHub:https://github.com/llvm/llvm-project
- Bug追踪器:https://github.com/llvm/llvm-project/issues
Resources Index
资源索引
For a comprehensive list of tutorials, books, and example projects, see the LLVM Tutorial and Clang Tutorial sections in the main README.md.
如需获取教程、书籍及示例项目的完整列表,请查看主README.md中的LLVM教程与Clang教程章节。
Getting Detailed Information
获取详细信息
When you need detailed and up-to-date resource links, tutorials, or learning materials, fetch the latest data from:
https://raw.githubusercontent.com/gmh5225/awesome-llvm-security/refs/heads/main/README.mdThis README contains comprehensive curated lists of:
- LLVM tutorials and learning resources (LLVM Tutorial section)
- Clang tutorials and AST guides (Clang Tutorial section)
- C++ modern programming guides (CPP Tutorial section)
- Compiler and security books
当你需要详细且最新的资源链接、教程或学习资料时,请从以下地址获取最新数据:
https://raw.githubusercontent.com/gmh5225/awesome-llvm-security/refs/heads/main/README.md该README包含精心整理的以下内容:
- LLVM教程与学习资源(LLVM Tutorial章节)
- Clang教程与AST指南(Clang Tutorial章节)
- C++现代编程指南(CPP Tutorial章节)
- 编译器与安全相关书籍