Skip to content

Commit 35636f9

Browse files
committed
Auto merge of #107723 - Kobzol:bootstrap-bolt, r=Mark-Simulacrum
Apply BOLT optimizations without rebuilding LLVM This PR adds an explicit BOLT bootstrap step which applies BOLT on the fly when LLVM artifacts are copied to a sysroot (it only does this once per bootstrap invocation, the result is cached). This avoids one LLVM rebuild in the Linux CI dist build. r? `@jyn514`
2 parents a512c6c + 9aad2ad commit 35636f9

File tree

4 files changed

+146
-43
lines changed

4 files changed

+146
-43
lines changed

src/bootstrap/bolt.rs

+9-18
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,40 @@
11
use std::path::Path;
22
use std::process::Command;
33

4-
/// Uses the `llvm-bolt` binary to instrument the binary/library at the given `path` with BOLT.
4+
/// Uses the `llvm-bolt` binary to instrument the artifact at the given `path` with BOLT.
55
/// When the instrumented artifact is executed, it will generate BOLT profiles into
66
/// `/tmp/prof.fdata.<pid>.fdata`.
7-
pub fn instrument_with_bolt_inplace(path: &Path) {
8-
let dir = std::env::temp_dir();
9-
let instrumented_path = dir.join("instrumented.so");
10-
7+
/// Creates the instrumented artifact at `output_path`.
8+
pub fn instrument_with_bolt(path: &Path, output_path: &Path) {
119
let status = Command::new("llvm-bolt")
1210
.arg("-instrument")
1311
.arg(&path)
1412
// Make sure that each process will write its profiles into a separate file
1513
.arg("--instrumentation-file-append-pid")
1614
.arg("-o")
17-
.arg(&instrumented_path)
15+
.arg(output_path)
1816
.status()
1917
.expect("Could not instrument artifact using BOLT");
2018

2119
if !status.success() {
2220
panic!("Could not instrument {} with BOLT, exit code {:?}", path.display(), status.code());
2321
}
24-
25-
std::fs::copy(&instrumented_path, path).expect("Cannot copy instrumented artifact");
26-
std::fs::remove_file(instrumented_path).expect("Cannot delete instrumented artifact");
2722
}
2823

29-
/// Uses the `llvm-bolt` binary to optimize the binary/library at the given `path` with BOLT,
24+
/// Uses the `llvm-bolt` binary to optimize the artifact at the given `path` with BOLT,
3025
/// using merged profiles from `profile_path`.
3126
///
3227
/// The recorded profiles have to be merged using the `merge-fdata` tool from LLVM and the merged
3328
/// profile path should be then passed to this function.
34-
pub fn optimize_library_with_bolt_inplace(path: &Path, profile_path: &Path) {
35-
let dir = std::env::temp_dir();
36-
let optimized_path = dir.join("optimized.so");
37-
29+
///
30+
/// Creates the optimized artifact at `output_path`.
31+
pub fn optimize_with_bolt(path: &Path, profile_path: &Path, output_path: &Path) {
3832
let status = Command::new("llvm-bolt")
3933
.arg(&path)
4034
.arg("-data")
4135
.arg(&profile_path)
4236
.arg("-o")
43-
.arg(&optimized_path)
37+
.arg(output_path)
4438
// Reorder basic blocks within functions
4539
.arg("-reorder-blocks=ext-tsp")
4640
// Reorder functions within the binary
@@ -65,7 +59,4 @@ pub fn optimize_library_with_bolt_inplace(path: &Path, profile_path: &Path) {
6559
if !status.success() {
6660
panic!("Could not optimize {} with BOLT, exit code {:?}", path.display(), status.code());
6761
}
68-
69-
std::fs::copy(&optimized_path, path).expect("Cannot copy optimized artifact");
70-
std::fs::remove_file(optimized_path).expect("Cannot delete optimized artifact");
7162
}

src/bootstrap/dist.rs

+134-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ use std::process::Command;
1818

1919
use object::read::archive::ArchiveFile;
2020
use object::BinaryFormat;
21+
use sha2::Digest;
2122

23+
use crate::bolt::{instrument_with_bolt, optimize_with_bolt};
2224
use crate::builder::{Builder, Kind, RunConfig, ShouldRun, Step};
2325
use crate::cache::{Interned, INTERNER};
2426
use crate::channel;
@@ -1904,6 +1906,26 @@ fn add_env(builder: &Builder<'_>, cmd: &mut Command, target: TargetSelection) {
19041906
}
19051907
}
19061908

1909+
fn install_llvm_file(builder: &Builder<'_>, source: &Path, destination: &Path) {
1910+
if builder.config.dry_run() {
1911+
return;
1912+
}
1913+
1914+
// After LLVM is built, we modify (instrument or optimize) the libLLVM.so library file.
1915+
// This is not done in-place so that the built LLVM files are not "tainted" with BOLT.
1916+
// We perform the instrumentation/optimization here, on the fly, just before they are being
1917+
// packaged into some destination directory.
1918+
let postprocessed = if builder.config.llvm_bolt_profile_generate {
1919+
builder.ensure(BoltInstrument::new(source.to_path_buf()))
1920+
} else if let Some(path) = &builder.config.llvm_bolt_profile_use {
1921+
builder.ensure(BoltOptimize::new(source.to_path_buf(), path.into()))
1922+
} else {
1923+
source.to_path_buf()
1924+
};
1925+
1926+
builder.install(&postprocessed, destination, 0o644);
1927+
}
1928+
19071929
/// Maybe add LLVM object files to the given destination lib-dir. Allows either static or dynamic linking.
19081930
///
19091931
/// Returns whether the files were actually copied.
@@ -1955,7 +1977,7 @@ fn maybe_install_llvm(builder: &Builder<'_>, target: TargetSelection, dst_libdir
19551977
} else {
19561978
PathBuf::from(file)
19571979
};
1958-
builder.install(&file, dst_libdir, 0o644);
1980+
install_llvm_file(builder, &file, dst_libdir);
19591981
}
19601982
!builder.config.dry_run()
19611983
} else {
@@ -1986,6 +2008,117 @@ pub fn maybe_install_llvm_runtime(builder: &Builder<'_>, target: TargetSelection
19862008
}
19872009
}
19882010

2011+
/// Creates an output path to a BOLT-manipulated artifact for the given `file`.
2012+
/// The hash of the file is used to make sure that we don't mix BOLT artifacts amongst different
2013+
/// files with the same name.
2014+
///
2015+
/// We need to keep the file-name the same though, to make sure that copying the manipulated file
2016+
/// to a directory will not change the final file path.
2017+
fn create_bolt_output_path(builder: &Builder<'_>, file: &Path, hash: &str) -> PathBuf {
2018+
let directory = builder.out.join("bolt").join(hash);
2019+
t!(fs::create_dir_all(&directory));
2020+
directory.join(file.file_name().unwrap())
2021+
}
2022+
2023+
/// Instrument the provided file with BOLT.
2024+
/// Returns a path to the instrumented artifact.
2025+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
2026+
pub struct BoltInstrument {
2027+
file: PathBuf,
2028+
hash: String,
2029+
}
2030+
2031+
impl BoltInstrument {
2032+
fn new(file: PathBuf) -> Self {
2033+
let mut hasher = sha2::Sha256::new();
2034+
hasher.update(t!(fs::read(&file)));
2035+
let hash = hex::encode(hasher.finalize().as_slice());
2036+
2037+
Self { file, hash }
2038+
}
2039+
}
2040+
2041+
impl Step for BoltInstrument {
2042+
type Output = PathBuf;
2043+
2044+
const ONLY_HOSTS: bool = false;
2045+
const DEFAULT: bool = false;
2046+
2047+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2048+
run.never()
2049+
}
2050+
2051+
fn run(self, builder: &Builder<'_>) -> PathBuf {
2052+
if builder.build.config.dry_run() {
2053+
return self.file.clone();
2054+
}
2055+
2056+
if builder.build.config.llvm_from_ci {
2057+
println!("warning: trying to use BOLT with LLVM from CI, this will probably not work");
2058+
}
2059+
2060+
println!("Instrumenting {} with BOLT", self.file.display());
2061+
2062+
let output_path = create_bolt_output_path(builder, &self.file, &self.hash);
2063+
if !output_path.is_file() {
2064+
instrument_with_bolt(&self.file, &output_path);
2065+
}
2066+
output_path
2067+
}
2068+
}
2069+
2070+
/// Optimize the provided file with BOLT.
2071+
/// Returns a path to the optimized artifact.
2072+
///
2073+
/// The hash is stored in the step to make sure that we don't optimize the same file
2074+
/// twice (even under different file paths).
2075+
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
2076+
pub struct BoltOptimize {
2077+
file: PathBuf,
2078+
profile: PathBuf,
2079+
hash: String,
2080+
}
2081+
2082+
impl BoltOptimize {
2083+
fn new(file: PathBuf, profile: PathBuf) -> Self {
2084+
let mut hasher = sha2::Sha256::new();
2085+
hasher.update(t!(fs::read(&file)));
2086+
hasher.update(t!(fs::read(&profile)));
2087+
let hash = hex::encode(hasher.finalize().as_slice());
2088+
2089+
Self { file, profile, hash }
2090+
}
2091+
}
2092+
2093+
impl Step for BoltOptimize {
2094+
type Output = PathBuf;
2095+
2096+
const ONLY_HOSTS: bool = false;
2097+
const DEFAULT: bool = false;
2098+
2099+
fn should_run(run: ShouldRun<'_>) -> ShouldRun<'_> {
2100+
run.never()
2101+
}
2102+
2103+
fn run(self, builder: &Builder<'_>) -> PathBuf {
2104+
if builder.build.config.dry_run() {
2105+
return self.file.clone();
2106+
}
2107+
2108+
if builder.build.config.llvm_from_ci {
2109+
println!("warning: trying to use BOLT with LLVM from CI, this will probably not work");
2110+
}
2111+
2112+
println!("Optimizing {} with BOLT", self.file.display());
2113+
2114+
let output_path = create_bolt_output_path(builder, &self.file, &self.hash);
2115+
if !output_path.is_file() {
2116+
optimize_with_bolt(&self.file, &self.profile, &output_path);
2117+
}
2118+
output_path
2119+
}
2120+
}
2121+
19892122
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
19902123
pub struct LlvmTools {
19912124
pub target: TargetSelection,

src/bootstrap/native.rs

-23
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use std::io;
1616
use std::path::{Path, PathBuf};
1717
use std::process::Command;
1818

19-
use crate::bolt::{instrument_with_bolt_inplace, optimize_library_with_bolt_inplace};
2019
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
2120
use crate::channel;
2221
use crate::config::{Config, TargetSelection};
@@ -523,34 +522,12 @@ impl Step for Llvm {
523522
}
524523
}
525524

526-
// After LLVM is built, we modify (instrument or optimize) the libLLVM.so library file
527-
// in place. This is fine, because currently we do not support incrementally rebuilding
528-
// LLVM after a configuration change, so to rebuild it the build files have to be removed,
529-
// which will also remove these modified files.
530-
if builder.config.llvm_bolt_profile_generate {
531-
instrument_with_bolt_inplace(&get_built_llvm_lib_path(&res.llvm_config));
532-
}
533-
if let Some(path) = &builder.config.llvm_bolt_profile_use {
534-
optimize_library_with_bolt_inplace(
535-
&get_built_llvm_lib_path(&res.llvm_config),
536-
&Path::new(path),
537-
);
538-
}
539-
540525
t!(stamp.write());
541526

542527
res
543528
}
544529
}
545530

546-
/// Returns path to a built LLVM library (libLLVM.so).
547-
/// Assumes that we have built LLVM into a single library file.
548-
fn get_built_llvm_lib_path(llvm_config_path: &Path) -> PathBuf {
549-
let mut cmd = Command::new(llvm_config_path);
550-
cmd.arg("--libfiles");
551-
PathBuf::from(output(&mut cmd).trim())
552-
}
553-
554531
fn check_llvm_version(builder: &Builder<'_>, llvm_config: &Path) {
555532
if !builder.config.llvm_version_check {
556533
return;

src/ci/stage-build.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -798,14 +798,16 @@ def execute_build_pipeline(timer: Timer, pipeline: Pipeline, final_build_args: L
798798
"--llvm-profile-use",
799799
pipeline.llvm_profile_merged_file(),
800800
"--llvm-bolt-profile-generate",
801+
"--rust-profile-use",
802+
pipeline.rustc_profile_merged_file()
801803
])
802804
record_metrics(pipeline, rustc_build)
803805

804806
with stage3.section("Gather profiles"):
805807
gather_llvm_bolt_profiles(pipeline)
806808

809+
# LLVM is not being cleared here, we want to reuse the previous build
807810
print_free_disk_space(pipeline)
808-
clear_llvm_files(pipeline)
809811
final_build_args += [
810812
"--llvm-bolt-profile-use",
811813
pipeline.llvm_bolt_profile_merged_file()

0 commit comments

Comments
 (0)