1 use std::path::Path;
2 use std::process::Command;
3
4 /// Uses the `llvm-bolt` binary to instrument the artifact at the given `path` with BOLT.
5 /// When the instrumented artifact is executed, it will generate BOLT profiles into
6 /// `/tmp/prof.fdata.<pid>.fdata`.
7 /// Creates the instrumented artifact at `output_path`.
instrument_with_bolt(path: &Path, output_path: &Path)8 pub fn instrument_with_bolt(path: &Path, output_path: &Path) {
9 let status = Command::new("llvm-bolt")
10 .arg("-instrument")
11 .arg(&path)
12 // Make sure that each process will write its profiles into a separate file
13 .arg("--instrumentation-file-append-pid")
14 .arg("-o")
15 .arg(output_path)
16 .status()
17 .expect("Could not instrument artifact using BOLT");
18
19 if !status.success() {
20 panic!("Could not instrument {} with BOLT, exit code {:?}", path.display(), status.code());
21 }
22 }
23
24 /// Uses the `llvm-bolt` binary to optimize the artifact at the given `path` with BOLT,
25 /// using merged profiles from `profile_path`.
26 ///
27 /// The recorded profiles have to be merged using the `merge-fdata` tool from LLVM and the merged
28 /// profile path should be then passed to this function.
29 ///
30 /// Creates the optimized artifact at `output_path`.
optimize_with_bolt(path: &Path, profile_path: &Path, output_path: &Path)31 pub fn optimize_with_bolt(path: &Path, profile_path: &Path, output_path: &Path) {
32 let status = Command::new("llvm-bolt")
33 .arg(&path)
34 .arg("-data")
35 .arg(&profile_path)
36 .arg("-o")
37 .arg(output_path)
38 // Reorder basic blocks within functions
39 .arg("-reorder-blocks=ext-tsp")
40 // Reorder functions within the binary
41 .arg("-reorder-functions=hfsort+")
42 // Split function code into hot and code regions
43 .arg("-split-functions")
44 // Split as many basic blocks as possible
45 .arg("-split-all-cold")
46 // Move jump tables to a separate section
47 .arg("-jump-tables=move")
48 // Fold functions with identical code
49 .arg("-icf=1")
50 // Update DWARF debug info in the final binary
51 .arg("-update-debug-sections")
52 // Print optimization statistics
53 .arg("-dyno-stats")
54 .status()
55 .expect("Could not optimize artifact using BOLT");
56
57 if !status.success() {
58 panic!("Could not optimize {} with BOLT, exit code {:?}", path.display(), status.code());
59 }
60 }
61