Optimizing Linux Kernel with BOLT
Maksim Panchenko
Facebook
BOLT
Overview
• What is BOLT
• How it works
• Linux Kernel Challenges
BOLT
What is BOLT?
Binary Optimization and Layout Tool
• Binary
• Compiled and linked executable or dynamic library
• Suffers from CPU front-end stalls
• Optimization
• Double digit speedup on top of PGO+LTO
• Layout
• Code Layout is the main optimization
BOLT
Code Layout Optimizations
Un-optimized Better Best
BOLT
Clang and GCC Speedup
60.00%
49.42%
50.00%
40.00% 36.22%
29.93%
30.00%
24.35%
21.26%
20.00% 15.73%
10.00%
0.00%
Clang GCC
BOLT PGO PGO+BOLT
BOLT
How to use BOLT?
• Collect profile
• Sample run
• Production environment
• Methods
• Sampling (Linux perf tool)
• BOLT instrumentation
• Link with –emit-relocs
• Apply optimizations by running BOLT
• $ llvm-bolt a.out –data perf.data –o a.out.fast <…>
BOLT
VS Compiler
Compiler
• Uses profile for optimizations – PGO/FDO/AutoFDO
• Layout is the last step in optimization pipeline
• Profile no longer accurate
Compiler Solution
• Use two profiles
• Compile twice
• (*) Context-sensitive profiling
BOLT
BOLT Pipeline
Discover • Find functions and data in code
Disassemble • Identify instructions inside functions
Build CFG • Analyze instructions and build CFG
Read Profile • Read profile and attribute to CFG edges
Optimize • Execute local and global optimization passes
Emit • Generate code and process relocations
Rewrite • Write code to file and update ELF
BOLT
Linux Kernel Challenges for BOLT
• Self-modifying and self-patching code
• Custom unwinding and exception handling
• Re-writing the binary
BOLT
Linux Kernel Challenges for BOLT
• Alternative instruction sequences
• .altinstructions, etc.
• Exceptions
• __ex_table, .pci_fixup
• __bug_table
• ORC
• Possible approach: selective optimizations
Facebook company
BOLT
Linux Kernel Challenges for BOLT
• Rewriting the binary
• Regular ELF
• Allocate new segments(s) for code and data
• Kernel
• Pre-allocate at link time
• Expand existing segments
Facebook company
Thank you
http://github.com/facebookincubator/BOLT