{"enrichment":{"faq":[{"a":"compiler-optimizations-deep covers vectorization failures across multiple dimensions. Common blockers include data dependencies (loop-carried or memory aliasing), unknown trip counts, non-unit strides, and unsupported operations. Enable diagnostics with `-Rpass=loop-vectorize -Rpass-missed=loop-vectorize` to see rejection reasons. Check for pointer aliasing with `restrict` qualifiers, ensure loop bounds are compile-time constants or predictable, and verify that inner operations map to SIMD instructions. Compiler optimization passes order matters\u2014vectorization runs after loop normalization and LICM, so invariant hoisting may unlock vectorization.","q":"Why didn't my loop vectorize with -O3?"},{"a":"compiler-optimizations-deep explains that LLVM's greedy register allocator assigns virtual registers to physical registers by processing live ranges in priority order. Live ranges define the instruction span where a value must reside; overlapping ranges compete for registers. When demand exceeds supply, the allocator spills\u2014writing values to stack memory. Register pressure (count of simultaneously live values) determines spill frequency. The allocator considers rematerialization (recomputing cheap values) versus spilling. Understanding live range pressure helps you restructure code to reduce simultaneous live values, cutting spills and improving cache locality.","q":"How does register allocation work in LLVM?"},{"a":"compiler-optimizations-deep identifies register spilling as the result of too many live values competing for too few physical registers. Spills occur when live ranges overlap and the allocator cannot fit all values in available registers. Common causes: long basic blocks with many intermediate results, complex expressions with deep dependency chains, and aggressive inlining that merges register demands. Spills force loads/stores to stack, destroying performance. Mitigation: break long blocks into smaller functions, reduce expression depth via intermediate variables, and use compiler flags like `-fno-inline` to control inlining pressure during optimization.","q":"What causes register pressure spills?"},{"a":"compiler-optimizations-deep covers both workflows. PGO (Profile-Guided Optimization) requires three steps: compile with `-fprofile-generate`, run representative training workloads to collect `.profraw` files, merge with `llvm-profdata merge`, then recompile with `-fprofile-use`. BOLT (post-link optimization) instruments the final binary, runs training, then reorders code blocks for cache locality. PGO works at compile time; BOLT operates on linked binaries without recompilation. Choose PGO for development; BOLT for production binaries where recompilation is costly. Both require representative training data\u2014use production traffic samples or synthetic workloads matching real usage patterns.","q":"How do I set up PGO or BOLT for production optimization?"},{"a":"compiler-optimizations-deep explains that LICM hoists computations outside loops when operands don't change across iterations. This eliminates redundant work: a loop-invariant multiplication executed N times moves outside, running once. LICM runs early in the optimization pipeline, enabling downstream passes like vectorization by simplifying loop bodies. It reduces register pressure inside loops and improves instruction-level parallelism. Limitations: LICM cannot hoist operations with side effects or memory dependencies it cannot prove safe. Use `-Rpass=licm` to see what moved; if expected hoisting doesn't occur, check for aliasing or side-effect annotations blocking the pass.","q":"How does LICM loop invariant code motion improve performance?"},{"a":"compiler-optimizations-deep identifies unexpected -O3 regressions as stemming from aggressive pass interactions: over-inlining exhausts register pressure, aggressive loop unrolling bloats instruction cache, or vectorization introduces expensive type conversions. Debug by profiling with perf or VTune to pinpoint hot regions, then selectively disable passes using `-fno-unroll-loops`, `-fno-inline`, or `-fno-vectorize`. Compiler optimization pass ordering matters\u2014a pass optimizing for one metric may degrade another. Profile-guided optimization (-fprofile-use) often recovers -O3 performance by making inlining and unrolling decisions based on actual execution frequency rather than heuristics.","q":"Why is my -O3 code slower than -O2?"}],"shadow_tags":["backend-optimization","register-pressure","vectorization-diagnostics","profile-guided-tuning","post-link-optimization","ir-transformation","code-generation-phase","performance-tuning-advanced"],"summary_rewrite":"Dive into compiler internals: understand why loops fail to vectorize, how register pressure causes spills, and when to deploy PGO or BOLT. Covers mid-level IR optimizations, instruction selection, and post-link optimization strategies with practical triage workflows."},"files":[{"bytes":3411,"path":"skills/compiler-internals/compiler-optimizations-deep/SKILL.md","sha256":"b2c8d0f07cb3cae29c3821f25b784156bf39395a3a93593f2f6be03f6635da92","url":"https://skillfed.io/files/mohitmishra786/low-level-dev-skills/compiler-optimizations-deep/590c652b/SKILL.md"}],"id":"mohitmishra786/low-level-dev-skills/compiler-optimizations-deep","links":{"html":"https://skillfed.io/mohitmishra786/low-level-dev-skills/compiler-optimizations-deep","md":"https://skillfed.io/mohitmishra786/low-level-dev-skills/compiler-optimizations-deep.md","repo":"https://github.com/mohitmishra786/low-level-dev-skills"},"meta":{"agents_supported":[],"first_seen":"2026-07-28","forks":19,"language":"JavaScript","last_updated":"2026-06-27","license":"MIT","name":"compiler-optimizations-deep","publisher":"mohitmishra786","stars":148},"relations":{"similar":[{"id":"mohitmishra786/low-level-dev-skills/pgo"},{"id":"mohitmishra786/low-level-dev-skills/llvm-ir-and-passes"},{"id":"mohitmishra786/low-level-dev-skills/clang"},{"id":"mohitmishra786/low-level-dev-skills/code-generation-and-backends"},{"id":"mohitmishra786/low-level-dev-skills/gcc"},{"id":"mohitmishra786/low-level-dev-skills/linkers-lto"},{"id":"mohitmishra786/low-level-dev-skills/llvm"},{"id":"athola/claude-night-market/loop-optimization"},{"id":"mohitmishra786/low-level-dev-skills/simd-intrinsics"},{"id":"mohitmishra786/low-level-dev-skills/msvc-cl"}]},"slug":{"owner":"mohitmishra786","repo":"low-level-dev-skills","skill":"compiler-optimizations-deep"},"version":"590c652b"}
