Skip to content

Commit bcff055

Browse files
committed
Specify directory for BOLT profiles
1 parent 41b9a02 commit bcff055

File tree

3 files changed

+51
-43
lines changed

3 files changed

+51
-43
lines changed

src/tools/opt-dist/src/bolt.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use crate::utils::io::copy_file;
88

99
/// Instruments an artifact at the given `path` (in-place) with BOLT and then calls `func`.
1010
/// After this function finishes, the original file will be restored.
11-
pub fn with_bolt_instrumented<F: FnOnce() -> anyhow::Result<R>, R>(
11+
pub fn with_bolt_instrumented<F: FnOnce(&Utf8Path) -> anyhow::Result<R>, R>(
1212
path: &Utf8Path,
1313
func: F,
1414
) -> anyhow::Result<R> {
@@ -20,10 +20,16 @@ pub fn with_bolt_instrumented<F: FnOnce() -> anyhow::Result<R>, R>(
2020

2121
let instrumented_path = tempfile::NamedTempFile::new()?.into_temp_path();
2222

23+
let profile_dir =
24+
tempfile::TempDir::new().context("Could not create directory for BOLT profiles")?;
25+
let profile_prefix = profile_dir.path().join("prof.fdata");
26+
let profile_prefix = Utf8Path::from_path(&profile_prefix).unwrap();
27+
2328
// Instrument the original file with BOLT, saving the result into `instrumented_path`
2429
cmd(&["llvm-bolt"])
2530
.arg("-instrument")
2631
.arg(path)
32+
.arg(&format!("--instrumentation-file={profile_prefix}"))
2733
// Make sure that each process will write its profiles into a separate file
2834
.arg("--instrumentation-file-append-pid")
2935
.arg("-o")
@@ -36,7 +42,7 @@ pub fn with_bolt_instrumented<F: FnOnce() -> anyhow::Result<R>, R>(
3642

3743
// Run the function that will make use of the instrumented artifact.
3844
// The original file will be restored when `_backup_file` is dropped.
39-
func()
45+
func(profile_prefix)
4046
}
4147

4248
/// Optimizes the file at `path` with BOLT in-place using the given `profile`.

src/tools/opt-dist/src/main.rs

+36-34
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use crate::environment::{Environment, EnvironmentBuilder};
1212
use crate::exec::{cmd, Bootstrap};
1313
use crate::tests::run_tests;
1414
use crate::timer::Timer;
15-
use crate::training::{gather_bolt_profiles, gather_llvm_profiles, gather_rustc_profiles};
15+
use crate::training::{
16+
gather_bolt_profiles, gather_llvm_profiles, gather_rustc_profiles, llvm_benchmarks,
17+
};
1618
use crate::utils::io::{copy_directory, move_directory, reset_directory};
1719
use crate::utils::{
1820
clear_llvm_files, format_env_variables, print_binary_sizes, print_free_disk_space,
@@ -270,9 +272,9 @@ fn execute_pipeline(
270272
log::info!("Optimizing {llvm_lib} with BOLT");
271273

272274
// Instrument it and gather profiles
273-
let profile = with_bolt_instrumented(&llvm_lib, || {
275+
let profile = with_bolt_instrumented(&llvm_lib, |llvm_profile_dir| {
274276
stage.section("Gather profiles", |_| {
275-
gather_bolt_profiles(env, "LLVM", llvm_benchmarks(env))
277+
gather_bolt_profiles(env, "LLVM", llvm_benchmarks(env), llvm_profile_dir)
276278
})
277279
})?;
278280
print_free_disk_space()?;
@@ -291,34 +293,34 @@ fn execute_pipeline(
291293
None
292294
};
293295

294-
let rustc_bolt_profile = if env.supports_bolt() {
295-
// Stage 4: Build BOLT instrumented rustc
296-
timer.section("Stage 4 (Rustc BOLT)", |stage| {
297-
// Find the path to the `librustc_driver.so` file
298-
let rustc_lib = io::find_file_in_dir(
299-
&env.build_artifacts().join("stage2").join("lib"),
300-
"librustc_driver",
301-
".so",
302-
)?;
303-
304-
log::info!("Optimizing {rustc_lib} with BOLT");
305-
306-
// Instrument it and gather profiles
307-
let profile = with_bolt_instrumented(&rustc_lib, || {
308-
stage.section("Gather profiles", |_| {
309-
gather_bolt_profiles(env, "rustc", rustc_benchmarks(env))
310-
})
311-
})?;
312-
print_free_disk_space()?;
313-
314-
// Now optimize the library with BOLT.
315-
bolt_optimize(&rustc_lib, &profile).context("Could not optimize rustc with BOLT")?;
316-
317-
Ok(Some(profile))
318-
})?
319-
} else {
320-
None
321-
};
296+
// let rustc_bolt_profile = if env.use_bolt() {
297+
// // Stage 4: Build BOLT instrumented rustc
298+
// timer.section("Stage 4 (Rustc BOLT)", |stage| {
299+
// // Find the path to the `librustc_driver.so` file
300+
// let rustc_lib = io::find_file_in_dir(
301+
// &env.build_artifacts().join("stage2").join("lib"),
302+
// "librustc_driver",
303+
// ".so",
304+
// )?;
305+
//
306+
// log::info!("Optimizing {rustc_lib} with BOLT");
307+
//
308+
// // Instrument it and gather profiles
309+
// let profile = with_bolt_instrumented(&rustc_lib, || {
310+
// stage.section("Gather profiles", |_| {
311+
// gather_bolt_profiles(env, "rustc", rustc_benchmarks(env))
312+
// })
313+
// })?;
314+
// print_free_disk_space()?;
315+
//
316+
// // Now optimize the library with BOLT.
317+
// bolt_optimize(&rustc_lib, &profile).context("Could not optimize rustc with BOLT")?;
318+
//
319+
// Ok(Some(profile))
320+
// })?
321+
// } else {
322+
// None
323+
// };
322324

323325
let mut dist = Bootstrap::dist(env, &dist_args)
324326
.llvm_pgo_optimize(&llvm_pgo_profile)
@@ -328,9 +330,9 @@ fn execute_pipeline(
328330
if let Some(llvm_bolt_profile) = llvm_bolt_profile {
329331
dist = dist.with_bolt_profile(llvm_bolt_profile);
330332
}
331-
if let Some(rustc_bolt_profile) = rustc_bolt_profile {
332-
dist = dist.with_bolt_profile(rustc_bolt_profile);
333-
}
333+
// if let Some(rustc_bolt_profile) = rustc_bolt_profile {
334+
// dist = dist.with_bolt_profile(rustc_bolt_profile);
335+
// }
334336

335337
// Final stage: Assemble the dist artifacts
336338
// The previous PGO optimized rustc build and PGO optimized LLVM builds should be reused.

src/tools/opt-dist/src/training.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -111,11 +111,11 @@ fn log_profile_stats(
111111
Ok(())
112112
}
113113

114-
pub fn llvm_benchmarks(env: &dyn Environment) -> CmdBuilder {
114+
pub fn llvm_benchmarks(env: &Environment) -> CmdBuilder {
115115
init_compiler_benchmarks(env, &["Debug", "Opt"], &["Full"], LLVM_PGO_CRATES)
116116
}
117117

118-
pub fn rustc_benchmarks(env: &dyn Environment) -> CmdBuilder {
118+
pub fn rustc_benchmarks(env: &Environment) -> CmdBuilder {
119119
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["All"], RUSTC_PGO_CRATES)
120120
}
121121

@@ -186,6 +186,7 @@ pub fn gather_bolt_profiles(
186186
env: &Environment,
187187
name: &str,
188188
benchmarks: CmdBuilder,
189+
profile_prefix: &Utf8Path,
189190
) -> anyhow::Result<BoltProfile> {
190191
log::info!("Running benchmarks with BOLT instrumented {name}");
191192

@@ -194,11 +195,10 @@ pub fn gather_bolt_profiles(
194195
})?;
195196

196197
let merged_profile = env.artifact_dir().join(format!("{name}-bolt.profdata"));
197-
let profile_root = Utf8PathBuf::from("/tmp/prof.fdata");
198-
log::info!("Merging {name} BOLT profiles to {merged_profile}");
198+
log::info!("Merging {name} BOLT profiles from {profile_prefix} to {merged_profile}");
199199

200200
let profiles: Vec<_> =
201-
glob::glob(&format!("{profile_root}*"))?.collect::<Result<Vec<_>, _>>()?;
201+
glob::glob(&format!("{profile_prefix}*"))?.collect::<Result<Vec<_>, _>>()?;
202202

203203
let mut merge_args = vec!["merge-fdata"];
204204
merge_args.extend(profiles.iter().map(|p| p.to_str().unwrap()));
@@ -222,11 +222,11 @@ pub fn gather_bolt_profiles(
222222
.collect::<Result<Vec<_>, _>>()?
223223
.into_iter()
224224
.sum::<u64>();
225-
log::info!("{profile_root}: {}", humansize::format_size(size, BINARY));
225+
log::info!("{profile_prefix}: {}", humansize::format_size(size, BINARY));
226226
log::info!("Profile file count: {}", profiles.len());
227227

228228
// Delete the gathered profiles
229-
for profile in glob::glob(&format!("{profile_root}*"))?.into_iter() {
229+
for profile in glob::glob(&format!("{profile_prefix}*"))?.into_iter() {
230230
if let Ok(profile) = profile {
231231
if let Err(error) = std::fs::remove_file(&profile) {
232232
log::error!("Cannot delete BOLT profile {}: {error:?}", profile.display());

0 commit comments

Comments
 (0)