|
| 1 | +use anyhow::Context; |
| 2 | + |
| 3 | +use crate::exec::cmd; |
| 4 | +use crate::training::LlvmBoltProfile; |
| 5 | +use camino::{Utf8Path, Utf8PathBuf}; |
| 6 | + |
| 7 | +use crate::utils::io::copy_file; |
| 8 | + |
| 9 | +/// Instruments an artifact at the given `path` (in-place) with BOLT and then calls `func`. |
| 10 | +/// After this function finishes, the original file will be restored. |
| 11 | +pub fn with_bolt_instrumented<F: FnOnce() -> anyhow::Result<R>, R>( |
| 12 | + path: &Utf8Path, |
| 13 | + func: F, |
| 14 | +) -> anyhow::Result<R> { |
| 15 | + // Back up the original file. |
| 16 | + // It will be restored to its original state when this function exits. |
| 17 | + // By copying it, we break any existing hard links, so that they are not affected by the |
| 18 | + // instrumentation. |
| 19 | + let _backup_file = BackedUpFile::new(path)?; |
| 20 | + |
| 21 | + let instrumented_path = tempfile::NamedTempFile::new()?.into_temp_path(); |
| 22 | + |
| 23 | + // Instrument the original file with BOLT, saving the result into `instrumented_path` |
| 24 | + cmd(&["llvm-bolt"]) |
| 25 | + .arg("-instrument") |
| 26 | + .arg(path) |
| 27 | + // Make sure that each process will write its profiles into a separate file |
| 28 | + .arg("--instrumentation-file-append-pid") |
| 29 | + .arg("-o") |
| 30 | + .arg(instrumented_path.display()) |
| 31 | + .run() |
| 32 | + .with_context(|| anyhow::anyhow!("Could not instrument {path} using BOLT"))?; |
| 33 | + |
| 34 | + // Copy the instrumented artifact over the original one |
| 35 | + copy_file(&instrumented_path, path)?; |
| 36 | + |
| 37 | + // Run the function that will make use of the instrumented artifact. |
| 38 | + // The original file will be restored when `_backup_file` is dropped. |
| 39 | + func() |
| 40 | +} |
| 41 | + |
| 42 | +/// Optimizes the file at `path` with BOLT in-place using the given `profile`. |
| 43 | +pub fn bolt_optimize(path: &Utf8Path, profile: LlvmBoltProfile) -> anyhow::Result<()> { |
| 44 | + // Copy the artifact to a new location, so that we do not use the same input and output file. |
| 45 | + // BOLT cannot handle optimizing when the input and output is the same file, because it performs |
| 46 | + // in-place patching. |
| 47 | + let temp_path = tempfile::NamedTempFile::new()?.into_temp_path(); |
| 48 | + copy_file(path, &temp_path)?; |
| 49 | + |
| 50 | + cmd(&["llvm-bolt"]) |
| 51 | + .arg(temp_path.display()) |
| 52 | + .arg("-data") |
| 53 | + .arg(&profile.0) |
| 54 | + .arg("-o") |
| 55 | + .arg(path) |
| 56 | + // Reorder basic blocks within functions |
| 57 | + .arg("-reorder-blocks=ext-tsp") |
| 58 | + // Reorder functions within the binary |
| 59 | + .arg("-reorder-functions=hfsort+") |
| 60 | + // Split function code into hot and code regions |
| 61 | + .arg("-split-functions") |
| 62 | + // Split as many basic blocks as possible |
| 63 | + .arg("-split-all-cold") |
| 64 | + // Move jump tables to a separate section |
| 65 | + .arg("-jump-tables=move") |
| 66 | + // Fold functions with identical code |
| 67 | + .arg("-icf=1") |
| 68 | + // Try to reuse old text segments to reduce binary size |
| 69 | + .arg("--use-old-text") |
| 70 | + // Update DWARF debug info in the final binary |
| 71 | + .arg("-update-debug-sections") |
| 72 | + // Print optimization statistics |
| 73 | + .arg("-dyno-stats") |
| 74 | + .run() |
| 75 | + .with_context(|| anyhow::anyhow!("Could not optimize {path} with BOLT"))?; |
| 76 | + |
| 77 | + Ok(()) |
| 78 | +} |
| 79 | + |
| 80 | +/// Copies a file to a temporary location and restores it (copies it back) when it is dropped. |
| 81 | +pub struct BackedUpFile { |
| 82 | + original: Utf8PathBuf, |
| 83 | + backup: tempfile::TempPath, |
| 84 | +} |
| 85 | + |
| 86 | +impl BackedUpFile { |
| 87 | + pub fn new(file: &Utf8Path) -> anyhow::Result<Self> { |
| 88 | + let temp_path = tempfile::NamedTempFile::new()?.into_temp_path(); |
| 89 | + copy_file(file, &temp_path)?; |
| 90 | + Ok(Self { backup: temp_path, original: file.to_path_buf() }) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +impl Drop for BackedUpFile { |
| 95 | + fn drop(&mut self) { |
| 96 | + copy_file(&self.backup, &self.original).expect("Cannot restore backed up file"); |
| 97 | + } |
| 98 | +} |
0 commit comments