mohitmishra786/low-level-dev-skills

llvm

LLVM IR and pass pipeline skill.

Quelltext ansehen
Originales Skill-Dokument

Aus dem Quell-Repository gerendert; Überschriften, Beispiele, Code, Tabellen, Links und Bilder bleiben erhalten.

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 text
llvm-dis src.bc -o src.ll

2. Run optimisation passes with opt

bash
# 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

3. Lower IR to assembly with llc

bash
# 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

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 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

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
aus demselben Repository

Weitere Skills

Alle Skills
mohitmishra786
Community

clang

Clang/LLVM compiler skill for C/C++ projects. Use when working with clang or clang++ for diagnostics, sanitizer instrumentation, optimization remarks, static analysis with clang-tidy, LTO via lld, or when migrating from GCC to Clang. Activates on queries about clang flags, clang-tidy, clang-format, better error messages, Apple/FreeBSD toolchains, or LLVM-specific optimizations. Covers flag selection, diagnostic tuning, and integration with LLVM tooling.

Installationen
1
GitHub Stars
198
Aktualisiert
27. Juni
mohitmishra786
Community

static-analysis

Static analysis skill for C/C++ codebases. Use when hardening code quality, triaging noisy builds, running clang-tidy, cppcheck, or scan-build, interpreting check categories, suppressing false positives, or integrating static analysis into CI. Activates on queries about clang-tidy checks, cppcheck, scan-build, compilecommands.json, code hardening, or static analysis warnings.

Installationen
1
GitHub Stars
198
Aktualisiert
27. Juni
mohitmishra786
Community

linux-perf

Linux perf profiler skill for CPU performance analysis. Use when collecting sampling profiles with perf record, generating perf report, measuring hardware counters (cache misses, branch mispredicts, IPC), identifying hot functions, or feeding perf data into flamegraph tools. Activates on queries about perf, Linux performance counters, PMU events, off-CPU profiling, perf stat, perf annotate, or sampling-based profiling on Linux.

Installationen
1
GitHub Stars
193
Aktualisiert
27. Juni
mohitmishra786
Community

sanitizers

Compiler sanitizer skill for runtime bug detection in C/C++. Use when enabling and interpreting AddressSanitizer (ASan), UndefinedBehaviorSanitizer (UBSan), ThreadSanitizer (TSan), MemorySanitizer (MSan), or LeakSanitizer (LSan) with GCC or Clang. Activates on queries about sanitizer flags, sanitizer reports, ASANOPTIONS, memory errors, data races, undefined behaviour, uninitialised reads, or choosing which sanitizer to use for a given bug class.

Installationen
1
GitHub Stars
193
Aktualisiert
27. Juni