$npx -y skills add mohitmishra786/low-level-dev-skills --skill compiler-optimizations-deepDeep compiler optimizations skill for RA, ISel, and PGO. Use when explaining register allocation, instruction selection, LICM, vectorization limits, or profile-guided optimization beyond -O3. Activates on queries about register allocation, instruction selection, LICM, auto-vector
| 1 | # Compiler Optimizations (Deep) |
| 2 | |
| 3 | ## Purpose |
| 4 | |
| 5 | Explain optimization phases beyond flags: mid-level IR opts, register allocation, instruction selection/scheduling, vectorization boundaries, PGO, and post-link BOLT — bridging `skills/compilers/pgo` and LLVM/GCC internals. |
| 6 | |
| 7 | ## When to Use |
| 8 | |
| 9 | - `-O3` did not vectorize a hot loop |
| 10 | - Teaching why register pressure causes spills |
| 11 | - Planning PGO or BOLT deployment |
| 12 | - Understanding pass interaction (e.g., LICM before vectorize) |
| 13 | |
| 14 | ## Workflow |
| 15 | |
| 16 | ### 1. Compiler pipeline map |
| 17 | |
| 18 | ``` |
| 19 | Frontend → LLVM IR / GCC GIMPLE |
| 20 | ├── Mid-level: DCE, GVN, LICM, inlining |
| 21 | ├── Loop opts: unroll, vectorize |
| 22 | ├── Codegen prep: legalize types |
| 23 | ├── Instruction selection (DAG → machine ops) |
| 24 | ├── Register allocation (greedy, linear scan) |
| 25 | └── Peephole / scheduling |
| 26 | ``` |
| 27 | |
| 28 | ### 2. Vectorization failure triage |
| 29 | |
| 30 | ```bash |
| 31 | clang -O3 -Rpass=loop-vectorize -Rpass-missed=loop-vectorize foo.c |
| 32 | ``` |
| 33 | |
| 34 | | Miss reason | Typical fix | |
| 35 | |-------------|-------------| |
| 36 | | Unknown trip count | peel loop; assert count | |
| 37 | | Dependence | reorder / separate accumulators | |
| 38 | | Function call in loop | inline or outline | |
| 39 | | Alignment unknown | `__builtin_assume_aligned` | |
| 40 | |
| 41 | ### 3. Register allocation intuition |
| 42 | |
| 43 | When live ranges exceed physical registers, the allocator **spills** to stack slots — costly loads/stores. Reducing live ranges (splitting variables, rematerialization) helps. |
| 44 | |
| 45 | GCC/LLVM both use graph coloring variants (LLVM "greedy regalloc"). |
| 46 | |
| 47 | ### 4. PGO workflow (Clang) |
| 48 | |
| 49 | ```bash |
| 50 | clang -fprofile-instr-generate -O2 -o app foo.c |
| 51 | ./app # training workload |
| 52 | llvm-profdata merge default.profraw -o default.profdata |
| 53 | clang -fprofile-instr-use=default.profdata -O2 -o app_pgo foo.c |
| 54 | ``` |
| 55 | |
| 56 | Improves branch layout, inlining, and vectorization thresholds. |
| 57 | |
| 58 | See `skills/compilers/pgo` for GCC and BOLT. |
| 59 | |
| 60 | ### 5. BOLT (post-link) |
| 61 | |
| 62 | ```bash |
| 63 | llvm-bolt -instrument app -o app.inst |
| 64 | ./app.inst |
| 65 | llvm-bolt -data=perf.fdata -reorder-blocks=+ -o app.bolt app |
| 66 | ``` |
| 67 | |
| 68 | Optimizes layout after linker — needs relocations (`-Wl,--emit-relocs`). |
| 69 | |
| 70 | ### 6. LICM example |
| 71 | |
| 72 | Loop-invariant code motion hoists `x * scale` out of inner loop when legal — reduces work per iteration. |
| 73 | |
| 74 | ### 7. Agent usage |
| 75 | |
| 76 | ``` |
| 77 | /compiler-optimizations-deep Why did LLVM fail to vectorize this reduction loop? |
| 78 | ``` |
| 79 | |
| 80 | ## Common Problems |
| 81 | |
| 82 | | Symptom | Cause | Fix | |
| 83 | |---------|-------|-----| |
| 84 | | PGO no gain | Unrepresentative training | Match production input | |
| 85 | | BOLT crash | Stripped binary | Keep symbols + relocs | |
| 86 | | Spills in asm | Register pressure | Simplify live ranges | |
| 87 | | `-O3` slower | Code bloat / cache | Try `-O2` or PGO | |
| 88 | | Different GCC/Clang | Pass ordering differs | Compare IR + asm | |
| 89 | |
| 90 | ## Related Skills |
| 91 | |
| 92 | - `skills/compilers/pgo` — PGO and BOLT detail |
| 93 | - `skills/compiler-internals/llvm-ir-and-passes` — IR-level opts |
| 94 | - `skills/compiler-internals/code-generation-and-backends` — ISel and backends |
| 95 | - `skills/computer-architecture/cpu-pipelines-and-hazards` — scheduling context |
| 96 | - `skills/low-level-programming/simd-intrinsics` — manual vectorization |