llvm by mohitmishra786/low-level-dev-skills
npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill llvm引导智能体完成 LLVM IR 的完整流程:生成 IR、使用 opt 运行优化过程、使用 llc 降级为汇编,以及检查 IR 以进行调试或性能分析。
# 生成文本格式 IR (.ll)
clang -O0 -emit-llvm -S src.c -o src.ll
# 生成位码 (.bc)
clang -O2 -emit-llvm -c src.c -o src.bc
# 将位码反汇编为文本
llvm-dis src.bc -o src.ll
opt 运行优化过程# 应用特定过程
opt -passes='mem2reg,instcombine,simplifycfg' src.ll -S -o out.ll
# 标准优化流水线
opt -passes='default<O2>' src.ll -S -o out.ll
opt -passes='default<O3>' src.ll -S -o out.ll
# 列出可用过程
opt --print-passes 2>&1 | less
# 打印过程前后的 IR
opt -passes='instcombine' --print-before=instcombine --print-after=instcombine src.ll -S -o out.ll 2>&1 | less
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
llc 将 IR 降级为汇编# 将 IR 编译为目标文件
llc -filetype=obj src.ll -o src.o
# 编译为汇编
llc -filetype=asm -masm-syntax=intel src.ll -o src.s
# 针对特定 CPU
llc -mcpu=skylake -mattr=+avx2 src.ll -o src.s
# 显示可用目标
llc --version
需要理解的关键 IR 结构:
| 结构 | 含义 |
|---|---|
alloca | 栈分配(SSA 之前;mem2reg 会将其提升到寄存器) |
load/store | 内存访问 |
getelementptr (GEP) | 指针运算 / 字段访问 |
phi | SSA φ-节点:合并来自前驱基本块的值 |
call/invoke | 函数调用(invoke 包含异常边) |
icmp/fcmp | 整数/浮点数比较 |
br | 分支(条件或无条件的) |
ret | 返回 |
bitcast | 重新解释位(在代码生成中是无操作) |
ptrtoint/inttoptr | 指针↔整数转换(尽可能避免) |
| 过程 | 效果 |
|---|---|
mem2reg | 将 alloca 提升为 SSA 寄存器 |
instcombine | 指令合并 / 窥孔优化 |
simplifycfg | 控制流图清理,移除死代码块 |
loop-vectorize | 自动向量化 |
slp-vectorize | 超字级并行(直线代码向量化) |
inline | 函数内联 |
gvn | 全局值编号(公共子表达式消除) |
licm | 循环不变代码外提 |
loop-unroll | 循环展开 |
argpromotion | 将指针参数提升为值 |
sroa | 聚合体的标量替换 |
# 为什么循环没有被向量化?
clang -O2 -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize src.c
# 转储过程流水线
clang -O2 -mllvm -debug-pass=Structure src.c -o /dev/null 2>&1 | less
# 打印每个过程后的 IR(非常详细)
opt -passes='default<O2>' -print-after-all src.ll -S 2>&1 | less
| 工具 | 用途 |
|---|---|
llvm-dis | 位码 → 文本 IR |
llvm-as | 文本 IR → 位码 |
llvm-link | 链接多个位码文件 |
llvm-lto | 独立链接时优化 |
llvm-nm | 位码/目标文件中的符号 |
llvm-objdump | 反汇编目标文件 |
llvm-profdata | 合并/显示配置文件引导优化数据 |
llvm-cov | 覆盖率报告 |
llvm-mca | 机器代码分析器(吞吐量/延迟) |
关于 binutils 的等效工具,请参见 skills/binaries/binutils。
skills/compilers/clang 获取源码级别的 Clang 标志skills/binaries/linkers-lto 进行链接时优化skills/profilers/linux-perf 和 llvm-mca 进行微架构分析每周安装数
267
代码仓库
GitHub 星标数
32
首次出现
2026年2月20日
安全审计
安装于
opencode266
gemini-cli266
github-copilot266
codex266
amp266
kimi-cli266
Guide agents through the LLVM IR pipeline: generating IR, running optimisation passes with opt, lowering to assembly with llc, and inspecting IR for debugging or performance work.
# Emit textual IR (.ll)
clang -O0 -emit-llvm -S src.c -o src.ll
# Emit bitcode (.bc)
clang -O2 -emit-llvm -c src.c -o src.bc
# Disassemble bitcode to text
llvm-dis src.bc -o src.ll
opt# Apply a specific pass
opt -passes='mem2reg,instcombine,simplifycfg' src.ll -S -o out.ll
# Standard optimisation pipelines
opt -passes='default<O2>' src.ll -S -o out.ll
opt -passes='default<O3>' src.ll -S -o out.ll
# List available passes
opt --print-passes 2>&1 | less
# Print IR before and after a pass
opt -passes='instcombine' --print-before=instcombine --print-after=instcombine src.ll -S -o out.ll 2>&1 | less
llc# Compile IR to object file
llc -filetype=obj src.ll -o src.o
# Compile to assembly
llc -filetype=asm -masm-syntax=intel src.ll -o src.s
# Target a specific CPU
llc -mcpu=skylake -mattr=+avx2 src.ll -o src.s
# Show available targets
llc --version
Key IR constructs to understand:
| Construct | Meaning |
|---|---|
alloca | Stack allocation (pre-SSA; mem2reg promotes to registers) |
load/store | Memory access |
getelementptr (GEP) | Pointer arithmetic / field access |
phi | SSA φ-node: merges values from predecessor blocks |
call/ |
| Pass | Effect |
|---|---|
mem2reg | Promote alloca to SSA registers |
instcombine | Instruction combining / peephole |
simplifycfg | CFG cleanup, dead block removal |
loop-vectorize | Auto-vectorisation |
slp-vectorize | Superword-level parallelism (straight-line vectorisation) |
inline | Function inlining |
# Why was a loop not vectorised?
clang -O2 -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize src.c
# Dump pass pipeline
clang -O2 -mllvm -debug-pass=Structure src.c -o /dev/null 2>&1 | less
# Print IR after each pass (very verbose)
opt -passes='default<O2>' -print-after-all src.ll -S 2>&1 | less
| Tool | Purpose |
|---|---|
llvm-dis | Bitcode → textual IR |
llvm-as | Textual IR → bitcode |
llvm-link | Link multiple bitcode files |
llvm-lto | Standalone LTO |
llvm-nm | Symbols in bitcode/object |
llvm-objdump | Disassemble objects |
For binutils equivalents, see skills/binaries/binutils.
skills/compilers/clang for source-level Clang flagsskills/binaries/linkers-lto for LTO at link timeskills/profilers/linux-perf combined with llvm-mca for micro-architectural analysisWeekly Installs
267
Repository
GitHub Stars
32
First Seen
Feb 20, 2026
Security Audits
Gen Agent Trust HubPassSocketPassSnykPass
Installed on
opencode266
gemini-cli266
github-copilot266
codex266
amp266
kimi-cli266
React 组合模式指南:Vercel 组件架构最佳实践,提升代码可维护性
106,200 周安装
invokeFunction call (invoke has exception edges) |
icmp/fcmp | Integer/float comparison |
br | Branch (conditional or unconditional) |
ret | Return |
bitcast | Reinterpret bits (no-op in codegen) |
ptrtoint/inttoptr | Pointer↔integer (avoid where possible) |
gvn | Global value numbering (common subexpression elimination) |
licm | Loop-invariant code motion |
loop-unroll | Loop unrolling |
argpromotion | Promote pointer args to values |
sroa | Scalar Replacement of Aggregates |
llvm-profdata| Merge/show PGO profiles |
llvm-cov | Coverage reporting |
llvm-mca | Machine code analyser (throughput/latency) |