$npx -y skills add mohitmishra786/low-level-dev-skills --skill llvmLLVM 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 ab
| 1 | # LLVM IR and Tooling |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | 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. |
| 6 | |
| 7 | ## Triggers |
| 8 | |
| 9 | - "Show me the LLVM IR for this function" |
| 10 | - "How do I run an LLVM optimisation pass?" |
| 11 | - "What does this LLVM IR instruction mean?" |
| 12 | - "How do I write a custom LLVM pass?" |
| 13 | - "Why isn't auto-vectorisation happening in LLVM?" |
| 14 | |
| 15 | ## Workflow |
| 16 | |
| 17 | ### 1. Generate LLVM IR |
| 18 | |
| 19 | ```bash |
| 20 | # Emit textual IR (.ll) |
| 21 | clang -O0 -emit-llvm -S src.c -o src.ll |
| 22 | |
| 23 | # Emit bitcode (.bc) |
| 24 | clang -O2 -emit-llvm -c src.c -o src.bc |
| 25 | |
| 26 | # Disassemble bitcode to text |
| 27 | llvm-dis src.bc -o src.ll |
| 28 | ``` |
| 29 | |
| 30 | ### 2. Run optimisation passes with `opt` |
| 31 | |
| 32 | ```bash |
| 33 | # Apply a specific pass |
| 34 | opt -passes='mem2reg,instcombine,simplifycfg' src.ll -S -o out.ll |
| 35 | |
| 36 | # Standard optimisation pipelines |
| 37 | opt -passes='default<O2>' src.ll -S -o out.ll |
| 38 | opt -passes='default<O3>' src.ll -S -o out.ll |
| 39 | |
| 40 | # List available passes |
| 41 | opt --print-passes 2>&1 | less |
| 42 | |
| 43 | # Print IR before and after a pass |
| 44 | opt -passes='instcombine' --print-before=instcombine --print-after=instcombine src.ll -S -o out.ll 2>&1 | less |
| 45 | ``` |
| 46 | |
| 47 | ### 3. Lower IR to assembly with `llc` |
| 48 | |
| 49 | ```bash |
| 50 | # Compile IR to object file |
| 51 | llc -filetype=obj src.ll -o src.o |
| 52 | |
| 53 | # Compile to assembly |
| 54 | llc -filetype=asm -masm-syntax=intel src.ll -o src.s |
| 55 | |
| 56 | # Target a specific CPU |
| 57 | llc -mcpu=skylake -mattr=+avx2 src.ll -o src.s |
| 58 | |
| 59 | # Show available targets |
| 60 | llc --version |
| 61 | ``` |
| 62 | |
| 63 | ### 4. Inspect IR |
| 64 | |
| 65 | Key IR constructs to understand: |
| 66 | |
| 67 | | Construct | Meaning | |
| 68 | |-----------|---------| |
| 69 | | `alloca` | Stack allocation (pre-SSA; `mem2reg` promotes to registers) | |
| 70 | | `load`/`store` | Memory access | |
| 71 | | `getelementptr` (GEP) | Pointer arithmetic / field access | |
| 72 | | `phi` | SSA φ-node: merges values from predecessor blocks | |
| 73 | | `call`/`invoke` | Function call (`invoke` has exception edges) | |
| 74 | | `icmp`/`fcmp` | Integer/float comparison | |
| 75 | | `br` | Branch (conditional or unconditional) | |
| 76 | | `ret` | Return | |
| 77 | | `bitcast` | Reinterpret bits (no-op in codegen) | |
| 78 | | `ptrtoint`/`inttoptr` | Pointer↔integer (avoid where possible) | |
| 79 | |
| 80 | ### 5. Key passes |
| 81 | |
| 82 | | Pass | Effect | |
| 83 | |------|--------| |
| 84 | | `mem2reg` | Promote alloca to SSA registers | |
| 85 | | `instcombine` | Instruction combining / peephole | |
| 86 | | `simplifycfg` | CFG cleanup, dead block removal | |
| 87 | | `loop-vectorize` | Auto-vectorisation | |
| 88 | | `slp-vectorize` | Superword-level parallelism (straight-line vectorisation) | |
| 89 | | `inline` | Function inlining | |
| 90 | | `gvn` | Global value numbering (common subexpression elimination) | |
| 91 | | `licm` | Loop-invariant code motion | |
| 92 | | `loop-unroll` | Loop unrolling | |
| 93 | | `argpromotion` | Promote pointer args to values | |
| 94 | | `sroa` | Scalar Replacement of Aggregates | |
| 95 | |
| 96 | ### 6. Debugging missed optimisations |
| 97 | |
| 98 | ```bash |
| 99 | # Why was a loop not vectorised? |
| 100 | clang -O2 -Rpass-missed=loop-vectorize -Rpass-analysis=loop-vectorize src.c |
| 101 | |
| 102 | # Dump pass pipeline |
| 103 | clang -O2 -mllvm -debug-pass=Structure src.c -o /dev/null 2>&1 | less |
| 104 | |
| 105 | # Print IR after each pass (very verbose) |
| 106 | opt -passes='default<O2>' -print-after-all src.ll -S 2>&1 | less |
| 107 | ``` |
| 108 | |
| 109 | ### 7. Useful llvm tools |
| 110 | |
| 111 | | Tool | Purpose | |
| 112 | |------|---------| |
| 113 | | `llvm-dis` | Bitcode → textual IR | |
| 114 | | `llvm-as` | Textual IR → bitcode | |
| 115 | | `llvm-link` | Link multiple bitcode files | |
| 116 | | `llvm-lto` | Standalone LTO | |
| 117 | | `llvm-nm` | Symbols in bitcode/object | |
| 118 | | `llvm-objdump` | Disassemble objects | |
| 119 | | `llvm-profdata` | Merge/show PGO profiles | |
| 120 | | `llvm-cov` | Coverage reporting | |
| 121 | | `llvm-mca` | Machine code analyser (throughput/latency) | |
| 122 | |
| 123 | For binutils equivalents, see `skills/binaries/binutils`. |
| 124 | |
| 125 | ## Related skills |
| 126 | |
| 127 | - Use `skills/compiler-internals/llvm-passes` for writing and testing custom LLVM passes |
| 128 | - Use `skills/compiler-internals/compiler-frontend` for generating LLVM IR from an AST |
| 129 | - Use `skills/compiler-internals/jit-compilation` for ORC JIT execution of LLVM IR |
| 130 | - Use `skills/compilers/clang` for source-level Clang flags |
| 131 | - Use `skills/binaries/linkers-lto` for LTO at link time |
| 132 | - Use `skills/profilers/linux-perf` combined with `llvm-mca` for micro-architectural analysis |