$npx -y skills add mohitmishra786/low-level-dev-skills --skill llvm-ir-and-passesLLVM IR and passes skill for reading compiler IR. Use when analyzing LLVM IR, understanding SSA, pass pipeline order, or running opt on bitcode. Activates on queries about LLVM IR, SSA form, opt passes, llvm-dis, pass pipeline, or reading .ll files.
| 1 | # LLVM IR and Passes |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Teach agents to read and reason about LLVM IR (SSA, types, terminators), navigate the standard pass pipeline, and use `opt`/`llvm-dis` for inspection. Complements `skills/compilers/llvm` (toolchain) and `skills/compiler-internals/llvm-passes` (writing plugins) — not merged. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - Understanding what `-O2` changed in generated IR |
| 10 | - Triaging miscompiles between Clang versions |
| 11 | - Preparing to write a custom pass |
| 12 | - Teaching SSA and dominance without writing C++ passes yet |
| 13 | |
| 14 | ## Workflow |
| 15 | |
| 16 | ### 1. LLVM IR structure |
| 17 | |
| 18 | ```llvm |
| 19 | ; Function in SSA form |
| 20 | define i32 @add(i32 %a, i32 %b) { |
| 21 | entry: |
| 22 | %sum = add i32 %a, %b |
| 23 | ret i32 %sum |
| 24 | } |
| 25 | ``` |
| 26 | |
| 27 | Key concepts: basic blocks, PHI nodes at merges, typed values (`i32`, `ptr`, vectors). |
| 28 | |
| 29 | ### 2. Emit IR from Clang |
| 30 | |
| 31 | ```bash |
| 32 | clang -S -emit-llvm -O0 -o foo.ll foo.c |
| 33 | clang -c -emit-llvm -O2 -o foo.bc foo.c |
| 34 | llvm-dis foo.bc -o foo.ll |
| 35 | ``` |
| 36 | |
| 37 | ### 3. Standard optimization pipeline (New PM) |
| 38 | |
| 39 | Clang `-O2` roughly runs: |
| 40 | |
| 41 | ``` |
| 42 | SimplifyCFG → InstCombine → GVN → LICM → LoopVectorize → ... |
| 43 | ``` |
| 44 | |
| 45 | Inspect with: |
| 46 | |
| 47 | ```bash |
| 48 | opt -passes='default<O2>' -S foo.ll -o foo-opt.ll |
| 49 | opt -passes='default<O2>' -print-passes foo.ll 2>&1 | head |
| 50 | ``` |
| 51 | |
| 52 | ### 4. Useful analysis passes |
| 53 | |
| 54 | | Pass | Shows | |
| 55 | |------|-------| |
| 56 | | `-passes=print<domtree>` | Dominator tree | |
| 57 | | `-passes=print<loops>` | Loop nests | |
| 58 | | `-passes=print-alias-sets` | Alias sets | |
| 59 | |
| 60 | ```bash |
| 61 | opt -passes='print<domtree>' -disable-output foo.ll |
| 62 | ``` |
| 63 | |
| 64 | ### 5. Reading PHI and UB |
| 65 | |
| 66 | ```llvm |
| 67 | merge: |
| 68 | %v = phi i32 [ %a, %then ], [ %b, %else ] |
| 69 | ``` |
| 70 | |
| 71 | `undef` and `poison` in IR model LLVM poison semantics — distinct from C UB but related. |
| 72 | |
| 73 | ### 6. Compare before/after |
| 74 | |
| 75 | ```bash |
| 76 | opt -passes='instcombine,simplifycfg' -S foo.ll -o - | diff -u foo.ll - |
| 77 | ``` |
| 78 | |
| 79 | ### 7. Agent usage |
| 80 | |
| 81 | ``` |
| 82 | /llvm-ir-and-passes Explain this PHI node and which pass likely created it |
| 83 | ``` |
| 84 | |
| 85 | ## Common Problems |
| 86 | |
| 87 | | Symptom | Cause | Fix | |
| 88 | |---------|-------|-----| |
| 89 | | Pass not found | LLVM version rename | `opt --print-passes` for your build | |
| 90 | | IR mismatch | Different LLVM major | Match clang/opt versions | |
| 91 | | "optnone" blocks opts | `-O0` attribute | Compile with `-O1+` | |
| 92 | | Huge IR | Inlined headers | `-fno-discard-value-names` off; filter function | |
| 93 | | Wrong pipeline | Legacy vs NPM | Use `-passes=` syntax | |
| 94 | |
| 95 | ## Related Skills |
| 96 | |
| 97 | - `skills/compilers/llvm` — toolchain overview |
| 98 | - `skills/compiler-internals/llvm-passes` — writing PassPlugins |
| 99 | - `skills/compiler-internals/compiler-optimizations-deep` — RA and ISel |
| 100 | - `skills/compiler-internals/compiler-frontend` — IR generation |
| 101 | - `skills/rust/rustc-basics` — `rustc --emit=llvm-ir` |