Llvm

mohitmishra786/low-level-dev-skills/skills/compilers/llvm

作者 mohitmishra786bdc58472fa9f無授權條款253 個星標收錄於 2026年10月9日更新於 2026年10月9日儲存庫3 個月前更新

LLVM IR and pass pipeline skill. Use when working directly with LLVM Intermediate Representation (IR), running opt passes, generating IR with llc, inspecting or writing LLVM IR for custom passes, or understanding how the LLVM backend lowers IR to assembly. Activates on queries about LLVM IR, opt, llc, llvm-dis, LLVM passes, IR transformations, or building LLVM-based tools.

AI 產生的概覽

指導代理使用 LLVM 工具產生、檢視、最佳化與降轉 LLVM IR。

功能
這個技能說明如何以使用者身分操作 LLVM 中介表示:用 clang 產生文字 IR 或位元碼,用 opt 執行最佳化 pass,用 llc 將 IR 降轉為組譯或目的檔,並解讀常見的 IR 結構。它也說明如何診斷未發生的最佳化,並列出 llvm-dis、llvm-as、llvm-link、llvm-mca 等常用工具。產出的是指令指引與參考說明,而非檔案或程式碼。
適用情境
當你需要產生或閱讀 LLVM IR、執行或除錯 opt pass 流程、用 llc 降轉 IR,或想了解自動向量化等最佳化為何沒有發生時使用。它著重於 LLVM 工具的使用,而非撰寫新的 LLVM pass。
執行需求
需要環境中具備 LLVM 工具鏈(clang、opt、llc 及相關 llvm-* 工具)。僅為說明文件,不附帶指令碼。

LLVM IR and Tooling

Purpose

Guide agents through LLVM as a user: generating and inspecting IR, running existing optimisation passes with opt, lowering to assembly with llc, and diagnosing missed optimisations. For writing new LLVM passes (PassPlugin, llvm-lit testing), use skills/compiler-internals/llvm-passes instead.

Triggers

  • "Show me the LLVM IR for this function"
  • "How do I run an LLVM optimisation pass?"
  • "What does this LLVM IR instruction mean?"
  • "How do I write a custom LLVM pass?"
  • "Why isn't auto-vectorisation happening in LLVM?"

Workflow

1. Generate LLVM IR

bash
# 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 textllvm-dis src.bc -o src.ll

2. Run optimisation passes with opt

bash
# Apply a specific passopt -passes='mem2reg,instcombine,simplifycfg' src.ll -S -o out.ll
# Standard optimisation pipelinesopt -passes='default<O2>' src.ll -S -o out.llopt -passes='default<O3>' src.ll -S -o out.ll
# List available passesopt --print-passes 2>&1 | less
# Print IR before and after a passopt -passes='instcombine' --print-before=instcombine --print-after=instcombine src.ll -S -o out.ll 2>&1 | less

3. Lower IR to assembly with llc

bash
# Compile IR to object filellc -filetype=obj src.ll -o src.o
# Compile to assemblyllc -filetype=asm -masm-syntax=intel src.ll -o src.s
# Target a specific CPUllc -mcpu=skylake -mattr=+avx2 src.ll -o src.s
# Show available targetsllc --version

4. Inspect IR

Key IR constructs to understand:

ConstructMeaning
allocaStack allocation (pre-SSA; mem2reg promotes to registers)
load/storeMemory access
getelementptr (GEP)Pointer arithmetic / field access
phiSSA φ-node: merges values from predecessor blocks
call/invokeFunction call (invoke has exception edges)
icmp/fcmpInteger/float comparison
brBranch (conditional or unconditional)
retReturn
bitcastReinterpret bits (no-op in codegen)
ptrtoint/inttoptrPointer↔integer (avoid where possible)

5. Key passes

PassEffect
mem2regPromote alloca to SSA registers
instcombineInstruction combining / peephole
simplifycfgCFG cleanup, dead block removal
loop-vectorizeAuto-vectorisation
slp-vectorizeSuperword-level parallelism (straight-line vectorisation)
inlineFunction inlining
gvnGlobal value numbering (common subexpression elimination)
licmLoop-invariant code motion
loop-unrollLoop unrolling
argpromotionPromote pointer args to values
sroaScalar Replacement of Aggregates

6. Debugging missed optimisations

bash
# Why was a loop not vectorised?clang -O2 -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize src.c
# Dump pass pipelineclang -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

7. Useful llvm tools

ToolPurpose
llvm-disBitcode → textual IR
llvm-asTextual IR → bitcode
llvm-linkLink multiple bitcode files
llvm-ltoStandalone LTO
llvm-nmSymbols in bitcode/object
llvm-objdumpDisassemble objects
llvm-profdataMerge/show PGO profiles
llvm-covCoverage reporting
llvm-mcaMachine code analyser (throughput/latency)

For binutils equivalents, see skills/binaries/binutils.

Related skills

  • Use skills/compiler-internals/llvm-passes for writing and testing custom LLVM passes
  • Use skills/compiler-internals/compiler-frontend for generating LLVM IR from an AST
  • Use skills/compiler-internals/jit-compilation for ORC JIT execution of LLVM IR
  • Use skills/compilers/clang for source-level Clang flags
  • Use skills/binaries/linkers-lto for LTO at link time
  • Use skills/profilers/linux-perf combined with llvm-mca for micro-architectural analysis

來源與署名

來源:mohitmishra786/low-level-dev-skills位於skills/compilers/llvm提交bdc5847

授權條款: 無授權條款

內容歸原作者所有。SourceWeft 從公開儲存庫中收錄這些內容。

檢舉或申請下架