Comprehensive learning resources and tutorials for LLVM, Clang, and compiler development...
This skill provides curated learning paths and resources for mastering LLVM, Clang, and compiler development.
# 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
; 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*, ...)
; 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)
; 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>
#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;
});
}};
}
# Build as shared library
clang++ -shared -fPIC -o MyPass.so MyPass.cpp `llvm-config --cxxflags --ldflags`
# Run with opt
opt -load-pass-plugin=./MyPass.so -passes="my-pass" input.ll -o output.ll
# Dump AST
clang -Xclang -ast-dump -fsyntax-only source.cpp
# AST as JSON
clang -Xclang -ast-dump=json -fsyntax-only source.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";
}
}
};
// 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 body
# Compile to LLVM IR
clang -S -emit-llvm source.c -o source.ll
# Optimize IR
opt -O2 source.ll -o optimized.ll
# Disassemble bitcode
llvm-dis source.bc -o source.ll
# Assemble to bitcode
llvm-as source.ll -o source.bc
# Generate native code
llc source.ll -o source.s
# View CFG
opt -passes=view-cfg source.ll
# With debug info
clang -g -S -emit-llvm source.c
# Debug IR transformation
opt -debug-pass=Structure -O2 source.ll
# Verify IR validity
opt -passes=verify source.ll
// 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);
For a comprehensive list of tutorials, books, and example projects, see the LLVM Tutorial and Clang Tutorial sections in the main README.md.
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.md
This README contains comprehensive curated lists of: