Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit cdee08f

Browse files
committedOct 9, 2023
Auto merge of rust-lang#116352 - Kobzol:rustc-driver-bolt, r=<try>
Optimize `librustc_driver.so` with BOLT This PR optimizes `librustc_driver.so` on 64-bit Linux CI with BOLT. ### Code One thing that's not clear yet to me how to resolve is how to best pass a linker flag that we need for BOLT (the second commit). It is currently passed unconditionally, which is not a good idea. We somehow have to: 1) Only pass it when we actually plan to use BOLT. How to best do that? `config.toml` entry? Environment variable? CLI flag for bootstrap? BOLT optimization is done by `opt-dist`, therefore bootstrap doesn't know about it by default. 2) Only pass it to `librustc_driver.so` (see performance below). Some discussion of this flag already happened on [Zulip](https://rust-lang.zulipchat.com/#narrow/stream/326414-t-infra.2Fbootstrap/topic/Adding.20a.20one-off.20linker.20flag). ### Performance Latest perf. results can be found [here](rust-lang#102487 (comment)). Note that instruction counts are not very interesting here, there are only regressions on hello world programs. Probably caused by a larger C++ libstd (?). Summary: - ✔️ `-1.8%` mean improvement in cycle counts across many primary benchmarks. - ✔️ `-1.8%` mean Max-RSS improvement. - ✖️ 34 MiB (+48%) artifact size regression of `librustc_driver.so`. - This is caused by building `librustc_driver.so` with relocations (which are required for BOLT). Hopefully, it will be [fixed](https://discourse.llvm.org/t/bolt-rfc-a-new-mode-to-rewrite-entire-binary/68674) in the future with BOLT improvements, but now trying to reduce this size increase is [tricky](rust-lang#114649). - Note that the size of this file was recently reduced in rust-lang#115554 by pretty much the same amount (33 MiB). So the size after this PR is basically the same as it was for the last ~year. - ✖️ 1.4 MiB (+53%) artifact size regression of `rustc`. - This is annoying and pretty much unnecessary. It is caused by the way relocations are currently applied in this PR, because they are applied both to `librustc_driver.so` (where they are needed) and for `rustc` (where they aren't needed), since both are built with a single cargo invocation. We might need e.g. some tricks in the bootstrap `rustc` shim to only apply the relocation flag for the shared library and not for `rustc`. ### CI time CI (try build) got slower by ~5 minutes, which is fine, IMO. It can be further reduced by running LLVM and `librustc_driver` BOLT profile gathering at the same time (now they are gathered separately for LLVM and `librustc_driver`). r? `@Mark-Simulacrum` Also CC `@onur-ozkan,` primarily for the bootstrap linker flag issue.
2 parents cdddcd3 + 9a0e90f commit cdee08f

File tree

12 files changed

+157
-59
lines changed

12 files changed

+157
-59
lines changed
 

‎src/bootstrap/bin/rustc.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,12 @@ fn main() {
217217
eprintln!("{prefix} libdir: {libdir:?}");
218218
}
219219

220+
if env::var_os("RUSTC_BOLT_LINK_FLAGS").is_some() {
221+
if let Some("rustc_driver") = crate_name {
222+
cmd.arg("-Clink-args=-Wl,-q");
223+
}
224+
}
225+
220226
let start = Instant::now();
221227
let (child, status) = {
222228
let errmsg = format!("\nFailed to run:\n{cmd:?}\n-------------");

‎src/bootstrap/compile.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -906,6 +906,11 @@ impl Step for Rustc {
906906
cargo.arg("-p").arg(krate);
907907
}
908908

909+
if builder.build.config.enable_bolt_settings && compiler.stage == 1 {
910+
// Relocations are required for BOLT to work.k
911+
cargo.env("RUSTC_BOLT_LINK_FLAGS", "1");
912+
}
913+
909914
let _guard = builder.msg_sysroot_tool(
910915
Kind::Build,
911916
compiler.stage,

‎src/bootstrap/config.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ pub struct Config {
234234
pub llvm_profile_use: Option<String>,
235235
pub llvm_profile_generate: bool,
236236
pub llvm_libunwind_default: Option<LlvmLibunwind>,
237+
pub enable_bolt_settings: bool,
237238

238239
pub reproducible_artifacts: Vec<String>,
239240

@@ -1128,6 +1129,7 @@ impl Config {
11281129
config.free_args = std::mem::take(&mut flags.free_args);
11291130
config.llvm_profile_use = flags.llvm_profile_use;
11301131
config.llvm_profile_generate = flags.llvm_profile_generate;
1132+
config.enable_bolt_settings = flags.enable_bolt_settings;
11311133

11321134
// Infer the rest of the configuration.
11331135

‎src/bootstrap/flags.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ pub struct Flags {
152152
/// generate PGO profile with llvm built for rustc
153153
#[arg(global(true), long)]
154154
pub llvm_profile_generate: bool,
155+
/// Enable BOLT link flags
156+
#[arg(global(true), long)]
157+
pub enable_bolt_settings: bool,
155158
/// Additional reproducible artifacts that should be added to the reproducible artifacts archive.
156159
#[arg(global(true), long)]
157160
pub reproducible_artifact: Vec<String>,

‎src/ci/docker/host-x86_64/dist-x86_64-linux/build-gcc.sh

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ sed -i'' 's|ftp://gcc\.gnu\.org/|https://gcc.gnu.org/|g' ./contrib/download_prer
2727
./contrib/download_prerequisites
2828
mkdir ../gcc-build
2929
cd ../gcc-build
30+
31+
# '-fno-reorder-blocks-and-partition' is required to
32+
# enable BOLT optimization of the C++ standard library,
33+
# which is included in librustc_driver.so
3034
hide_output ../gcc-$GCC/configure \
3135
--prefix=/rustroot \
3236
--enable-languages=c,c++ \
33-
--disable-gnu-unique-object
37+
--disable-gnu-unique-object \
38+
--enable-cxx-flags='-fno-reorder-blocks-and-partition'
3439
hide_output make -j$(nproc)
3540
hide_output make install
3641
ln -s gcc /rustroot/bin/cc

‎src/etc/completions/x.py.fish

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ complete -c x.py -n "__fish_use_subcommand" -l include-default-paths -d 'include
2727
complete -c x.py -n "__fish_use_subcommand" -l dry-run -d 'dry run; don\'t build anything'
2828
complete -c x.py -n "__fish_use_subcommand" -l json-output -d 'use message-format=json'
2929
complete -c x.py -n "__fish_use_subcommand" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
30+
complete -c x.py -n "__fish_use_subcommand" -l enable-bolt-settings -d 'Enable BOLT link flags'
3031
complete -c x.py -n "__fish_use_subcommand" -s h -l help -d 'Print help'
3132
complete -c x.py -n "__fish_use_subcommand" -f -a "build" -d 'Compile either the compiler or libraries'
3233
complete -c x.py -n "__fish_use_subcommand" -f -a "check" -d 'Compile either the compiler or libraries, using cargo check'
@@ -71,6 +72,7 @@ complete -c x.py -n "__fish_seen_subcommand_from build" -l include-default-paths
7172
complete -c x.py -n "__fish_seen_subcommand_from build" -l dry-run -d 'dry run; don\'t build anything'
7273
complete -c x.py -n "__fish_seen_subcommand_from build" -l json-output -d 'use message-format=json'
7374
complete -c x.py -n "__fish_seen_subcommand_from build" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
75+
complete -c x.py -n "__fish_seen_subcommand_from build" -l enable-bolt-settings -d 'Enable BOLT link flags'
7476
complete -c x.py -n "__fish_seen_subcommand_from build" -s h -l help -d 'Print help (see more with \'--help\')'
7577
complete -c x.py -n "__fish_seen_subcommand_from check" -l config -d 'TOML configuration file for build' -r -F
7678
complete -c x.py -n "__fish_seen_subcommand_from check" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -102,6 +104,7 @@ complete -c x.py -n "__fish_seen_subcommand_from check" -l include-default-paths
102104
complete -c x.py -n "__fish_seen_subcommand_from check" -l dry-run -d 'dry run; don\'t build anything'
103105
complete -c x.py -n "__fish_seen_subcommand_from check" -l json-output -d 'use message-format=json'
104106
complete -c x.py -n "__fish_seen_subcommand_from check" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
107+
complete -c x.py -n "__fish_seen_subcommand_from check" -l enable-bolt-settings -d 'Enable BOLT link flags'
105108
complete -c x.py -n "__fish_seen_subcommand_from check" -s h -l help -d 'Print help (see more with \'--help\')'
106109
complete -c x.py -n "__fish_seen_subcommand_from clippy" -s A -d 'clippy lints to allow' -r
107110
complete -c x.py -n "__fish_seen_subcommand_from clippy" -s D -d 'clippy lints to deny' -r
@@ -137,6 +140,7 @@ complete -c x.py -n "__fish_seen_subcommand_from clippy" -l include-default-path
137140
complete -c x.py -n "__fish_seen_subcommand_from clippy" -l dry-run -d 'dry run; don\'t build anything'
138141
complete -c x.py -n "__fish_seen_subcommand_from clippy" -l json-output -d 'use message-format=json'
139142
complete -c x.py -n "__fish_seen_subcommand_from clippy" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
143+
complete -c x.py -n "__fish_seen_subcommand_from clippy" -l enable-bolt-settings -d 'Enable BOLT link flags'
140144
complete -c x.py -n "__fish_seen_subcommand_from clippy" -s h -l help -d 'Print help (see more with \'--help\')'
141145
complete -c x.py -n "__fish_seen_subcommand_from fix" -l config -d 'TOML configuration file for build' -r -F
142146
complete -c x.py -n "__fish_seen_subcommand_from fix" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -167,6 +171,7 @@ complete -c x.py -n "__fish_seen_subcommand_from fix" -l include-default-paths -
167171
complete -c x.py -n "__fish_seen_subcommand_from fix" -l dry-run -d 'dry run; don\'t build anything'
168172
complete -c x.py -n "__fish_seen_subcommand_from fix" -l json-output -d 'use message-format=json'
169173
complete -c x.py -n "__fish_seen_subcommand_from fix" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
174+
complete -c x.py -n "__fish_seen_subcommand_from fix" -l enable-bolt-settings -d 'Enable BOLT link flags'
170175
complete -c x.py -n "__fish_seen_subcommand_from fix" -s h -l help -d 'Print help (see more with \'--help\')'
171176
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l config -d 'TOML configuration file for build' -r -F
172177
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -198,6 +203,7 @@ complete -c x.py -n "__fish_seen_subcommand_from fmt" -l include-default-paths -
198203
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l dry-run -d 'dry run; don\'t build anything'
199204
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l json-output -d 'use message-format=json'
200205
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
206+
complete -c x.py -n "__fish_seen_subcommand_from fmt" -l enable-bolt-settings -d 'Enable BOLT link flags'
201207
complete -c x.py -n "__fish_seen_subcommand_from fmt" -s h -l help -d 'Print help (see more with \'--help\')'
202208
complete -c x.py -n "__fish_seen_subcommand_from doc" -l config -d 'TOML configuration file for build' -r -F
203209
complete -c x.py -n "__fish_seen_subcommand_from doc" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -230,6 +236,7 @@ complete -c x.py -n "__fish_seen_subcommand_from doc" -l include-default-paths -
230236
complete -c x.py -n "__fish_seen_subcommand_from doc" -l dry-run -d 'dry run; don\'t build anything'
231237
complete -c x.py -n "__fish_seen_subcommand_from doc" -l json-output -d 'use message-format=json'
232238
complete -c x.py -n "__fish_seen_subcommand_from doc" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
239+
complete -c x.py -n "__fish_seen_subcommand_from doc" -l enable-bolt-settings -d 'Enable BOLT link flags'
233240
complete -c x.py -n "__fish_seen_subcommand_from doc" -s h -l help -d 'Print help (see more with \'--help\')'
234241
complete -c x.py -n "__fish_seen_subcommand_from test" -l skip -d 'skips tests matching SUBSTRING, if supported by test tool. May be passed multiple times' -r -F
235242
complete -c x.py -n "__fish_seen_subcommand_from test" -l test-args -d 'extra arguments to be passed for the test tool being used (e.g. libtest, compiletest or rustdoc)' -r
@@ -273,6 +280,7 @@ complete -c x.py -n "__fish_seen_subcommand_from test" -l include-default-paths
273280
complete -c x.py -n "__fish_seen_subcommand_from test" -l dry-run -d 'dry run; don\'t build anything'
274281
complete -c x.py -n "__fish_seen_subcommand_from test" -l json-output -d 'use message-format=json'
275282
complete -c x.py -n "__fish_seen_subcommand_from test" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
283+
complete -c x.py -n "__fish_seen_subcommand_from test" -l enable-bolt-settings -d 'Enable BOLT link flags'
276284
complete -c x.py -n "__fish_seen_subcommand_from test" -s h -l help -d 'Print help (see more with \'--help\')'
277285
complete -c x.py -n "__fish_seen_subcommand_from bench" -l test-args -r
278286
complete -c x.py -n "__fish_seen_subcommand_from bench" -l config -d 'TOML configuration file for build' -r -F
@@ -304,6 +312,7 @@ complete -c x.py -n "__fish_seen_subcommand_from bench" -l include-default-paths
304312
complete -c x.py -n "__fish_seen_subcommand_from bench" -l dry-run -d 'dry run; don\'t build anything'
305313
complete -c x.py -n "__fish_seen_subcommand_from bench" -l json-output -d 'use message-format=json'
306314
complete -c x.py -n "__fish_seen_subcommand_from bench" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
315+
complete -c x.py -n "__fish_seen_subcommand_from bench" -l enable-bolt-settings -d 'Enable BOLT link flags'
307316
complete -c x.py -n "__fish_seen_subcommand_from bench" -s h -l help -d 'Print help'
308317
complete -c x.py -n "__fish_seen_subcommand_from clean" -l stage -d 'Clean a specific stage without touching other artifacts. By default, every stage is cleaned if this option is not used' -r
309318
complete -c x.py -n "__fish_seen_subcommand_from clean" -l config -d 'TOML configuration file for build' -r -F
@@ -335,6 +344,7 @@ complete -c x.py -n "__fish_seen_subcommand_from clean" -l include-default-paths
335344
complete -c x.py -n "__fish_seen_subcommand_from clean" -l dry-run -d 'dry run; don\'t build anything'
336345
complete -c x.py -n "__fish_seen_subcommand_from clean" -l json-output -d 'use message-format=json'
337346
complete -c x.py -n "__fish_seen_subcommand_from clean" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
347+
complete -c x.py -n "__fish_seen_subcommand_from clean" -l enable-bolt-settings -d 'Enable BOLT link flags'
338348
complete -c x.py -n "__fish_seen_subcommand_from clean" -s h -l help -d 'Print help'
339349
complete -c x.py -n "__fish_seen_subcommand_from dist" -l config -d 'TOML configuration file for build' -r -F
340350
complete -c x.py -n "__fish_seen_subcommand_from dist" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -365,6 +375,7 @@ complete -c x.py -n "__fish_seen_subcommand_from dist" -l include-default-paths
365375
complete -c x.py -n "__fish_seen_subcommand_from dist" -l dry-run -d 'dry run; don\'t build anything'
366376
complete -c x.py -n "__fish_seen_subcommand_from dist" -l json-output -d 'use message-format=json'
367377
complete -c x.py -n "__fish_seen_subcommand_from dist" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
378+
complete -c x.py -n "__fish_seen_subcommand_from dist" -l enable-bolt-settings -d 'Enable BOLT link flags'
368379
complete -c x.py -n "__fish_seen_subcommand_from dist" -s h -l help -d 'Print help'
369380
complete -c x.py -n "__fish_seen_subcommand_from install" -l config -d 'TOML configuration file for build' -r -F
370381
complete -c x.py -n "__fish_seen_subcommand_from install" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -395,6 +406,7 @@ complete -c x.py -n "__fish_seen_subcommand_from install" -l include-default-pat
395406
complete -c x.py -n "__fish_seen_subcommand_from install" -l dry-run -d 'dry run; don\'t build anything'
396407
complete -c x.py -n "__fish_seen_subcommand_from install" -l json-output -d 'use message-format=json'
397408
complete -c x.py -n "__fish_seen_subcommand_from install" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
409+
complete -c x.py -n "__fish_seen_subcommand_from install" -l enable-bolt-settings -d 'Enable BOLT link flags'
398410
complete -c x.py -n "__fish_seen_subcommand_from install" -s h -l help -d 'Print help'
399411
complete -c x.py -n "__fish_seen_subcommand_from run" -l args -d 'arguments for the tool' -r
400412
complete -c x.py -n "__fish_seen_subcommand_from run" -l config -d 'TOML configuration file for build' -r -F
@@ -426,6 +438,7 @@ complete -c x.py -n "__fish_seen_subcommand_from run" -l include-default-paths -
426438
complete -c x.py -n "__fish_seen_subcommand_from run" -l dry-run -d 'dry run; don\'t build anything'
427439
complete -c x.py -n "__fish_seen_subcommand_from run" -l json-output -d 'use message-format=json'
428440
complete -c x.py -n "__fish_seen_subcommand_from run" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
441+
complete -c x.py -n "__fish_seen_subcommand_from run" -l enable-bolt-settings -d 'Enable BOLT link flags'
429442
complete -c x.py -n "__fish_seen_subcommand_from run" -s h -l help -d 'Print help (see more with \'--help\')'
430443
complete -c x.py -n "__fish_seen_subcommand_from setup" -l config -d 'TOML configuration file for build' -r -F
431444
complete -c x.py -n "__fish_seen_subcommand_from setup" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -456,6 +469,7 @@ complete -c x.py -n "__fish_seen_subcommand_from setup" -l include-default-paths
456469
complete -c x.py -n "__fish_seen_subcommand_from setup" -l dry-run -d 'dry run; don\'t build anything'
457470
complete -c x.py -n "__fish_seen_subcommand_from setup" -l json-output -d 'use message-format=json'
458471
complete -c x.py -n "__fish_seen_subcommand_from setup" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
472+
complete -c x.py -n "__fish_seen_subcommand_from setup" -l enable-bolt-settings -d 'Enable BOLT link flags'
459473
complete -c x.py -n "__fish_seen_subcommand_from setup" -s h -l help -d 'Print help (see more with \'--help\')'
460474
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l config -d 'TOML configuration file for build' -r -F
461475
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l build-dir -d 'Build directory, overrides `build.build-dir` in `config.toml`' -r -f -a "(__fish_complete_directories)"
@@ -487,4 +501,5 @@ complete -c x.py -n "__fish_seen_subcommand_from suggest" -l include-default-pat
487501
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l dry-run -d 'dry run; don\'t build anything'
488502
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l json-output -d 'use message-format=json'
489503
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l llvm-profile-generate -d 'generate PGO profile with llvm built for rustc'
504+
complete -c x.py -n "__fish_seen_subcommand_from suggest" -l enable-bolt-settings -d 'Enable BOLT link flags'
490505
complete -c x.py -n "__fish_seen_subcommand_from suggest" -s h -l help -d 'Print help (see more with \'--help\')'

‎src/etc/completions/x.py.ps1

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
5353
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
5454
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
5555
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
56+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
5657
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
5758
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
5859
[CompletionResult]::new('build', 'build', [CompletionResultType]::ParameterValue, 'Compile either the compiler or libraries')
@@ -104,6 +105,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
104105
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
105106
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
106107
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
108+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
107109
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
108110
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
109111
break
@@ -142,6 +144,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
142144
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
143145
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
144146
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
147+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
145148
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
146149
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
147150
break
@@ -184,6 +187,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
184187
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
185188
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
186189
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
190+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
187191
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
188192
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
189193
break
@@ -221,6 +225,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
221225
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
222226
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
223227
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
228+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
224229
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
225230
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
226231
break
@@ -259,6 +264,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
259264
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
260265
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
261266
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
267+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
262268
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
263269
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
264270
break
@@ -298,6 +304,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
298304
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
299305
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
300306
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
307+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
301308
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
302309
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
303310
break
@@ -348,6 +355,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
348355
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
349356
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
350357
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
358+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
351359
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
352360
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
353361
break
@@ -386,6 +394,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
386394
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
387395
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
388396
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
397+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
389398
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
390399
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
391400
break
@@ -424,6 +433,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
424433
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
425434
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
426435
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
436+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
427437
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
428438
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
429439
break
@@ -461,6 +471,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
461471
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
462472
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
463473
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
474+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
464475
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
465476
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
466477
break
@@ -498,6 +509,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
498509
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
499510
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
500511
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
512+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
501513
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help')
502514
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help')
503515
break
@@ -536,6 +548,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
536548
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
537549
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
538550
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
551+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
539552
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
540553
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
541554
break
@@ -573,6 +586,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
573586
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
574587
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
575588
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
589+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
576590
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
577591
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
578592
break
@@ -611,6 +625,7 @@ Register-ArgumentCompleter -Native -CommandName 'x.py' -ScriptBlock {
611625
[CompletionResult]::new('--dry-run', 'dry-run', [CompletionResultType]::ParameterName, 'dry run; don''t build anything')
612626
[CompletionResult]::new('--json-output', 'json-output', [CompletionResultType]::ParameterName, 'use message-format=json')
613627
[CompletionResult]::new('--llvm-profile-generate', 'llvm-profile-generate', [CompletionResultType]::ParameterName, 'generate PGO profile with llvm built for rustc')
628+
[CompletionResult]::new('--enable-bolt-settings', 'enable-bolt-settings', [CompletionResultType]::ParameterName, 'Enable BOLT link flags')
614629
[CompletionResult]::new('-h', 'h', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
615630
[CompletionResult]::new('--help', 'help', [CompletionResultType]::ParameterName, 'Print help (see more with ''--help'')')
616631
break

‎src/etc/completions/x.py.sh

Lines changed: 15 additions & 15 deletions
Large diffs are not rendered by default.

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use anyhow::Context;
22

33
use crate::exec::cmd;
4-
use crate::training::LlvmBoltProfile;
4+
use crate::training::BoltProfile;
55
use camino::{Utf8Path, Utf8PathBuf};
66

77
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,11 +42,11 @@ 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`.
43-
pub fn bolt_optimize(path: &Utf8Path, profile: &LlvmBoltProfile) -> anyhow::Result<()> {
49+
pub fn bolt_optimize(path: &Utf8Path, profile: &BoltProfile) -> anyhow::Result<()> {
4450
// Copy the artifact to a new location, so that we do not use the same input and output file.
4551
// BOLT cannot handle optimizing when the input and output is the same file, because it performs
4652
// in-place patching.

‎src/tools/opt-dist/src/exec.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::environment::Environment;
22
use crate::metrics::{load_metrics, record_metrics};
33
use crate::timer::TimerSection;
4-
use crate::training::{LlvmBoltProfile, LlvmPGOProfile, RustcPGOProfile};
4+
use crate::training::{BoltProfile, LlvmPGOProfile, RustcPGOProfile};
55
use camino::{Utf8Path, Utf8PathBuf};
66
use std::collections::BTreeMap;
77
use std::fs::File;
@@ -159,7 +159,12 @@ impl Bootstrap {
159159
self
160160
}
161161

162-
pub fn with_bolt_profile(mut self, profile: LlvmBoltProfile) -> Self {
162+
pub fn with_rustc_bolt_ldflags(mut self) -> Self {
163+
self.cmd = self.cmd.arg("--enable-bolt-settings");
164+
self
165+
}
166+
167+
pub fn with_bolt_profile(mut self, profile: BoltProfile) -> Self {
163168
self.cmd = self.cmd.arg("--reproducible-artifact").arg(profile.0.as_str());
164169
self
165170
}

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

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ 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_llvm_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+
rustc_benchmarks,
18+
};
1619
use crate::utils::artifact_size::print_binary_sizes;
1720
use crate::utils::io::{copy_directory, move_directory, reset_directory};
1821
use crate::utils::{
@@ -246,31 +249,33 @@ fn execute_pipeline(
246249
Ok(profile)
247250
})?;
248251

249-
let llvm_bolt_profile = if env.use_bolt() {
252+
let bolt_profiles = if env.use_bolt() {
250253
// Stage 3: Build BOLT instrumented LLVM
251254
// We build a PGO optimized LLVM in this step, then instrument it with BOLT and gather BOLT profiles.
252255
// Note that we don't remove LLVM artifacts after this step, so that they are reused in the final dist build.
253256
// BOLT instrumentation is performed "on-the-fly" when the LLVM library is copied to the sysroot of rustc,
254257
// therefore the LLVM artifacts on disk are not "tainted" with BOLT instrumentation and they can be reused.
255-
timer.section("Stage 3 (LLVM BOLT)", |stage| {
258+
timer.section("Stage 3 (BOLT)", |stage| {
256259
stage.section("Build PGO optimized LLVM", |stage| {
257260
Bootstrap::build(env)
258261
.with_llvm_bolt_ldflags()
262+
.with_rustc_bolt_ldflags()
259263
.llvm_pgo_optimize(&llvm_pgo_profile)
260264
.avoid_rustc_rebuild()
261265
.run(stage)
262266
})?;
263267

264-
// Find the path to the `libLLVM.so` file
265-
let llvm_lib = io::find_file_in_dir(
266-
&env.build_artifacts().join("stage2").join("lib"),
267-
"libLLVM",
268-
".so",
269-
)?;
268+
let libdir = env.build_artifacts().join("stage2").join("lib");
269+
let llvm_lib = io::find_file_in_dir(&libdir, "libLLVM", ".so")?;
270270

271-
// Instrument it and gather profiles
272-
let profile = with_bolt_instrumented(&llvm_lib, || {
273-
stage.section("Gather profiles", |_| gather_llvm_bolt_profiles(env))
271+
log::info!("Optimizing {llvm_lib} with BOLT");
272+
273+
// FIXME(kobzol: try gather profiles together, at once for LLVM and rustc
274+
// Instrument the libraries and gather profiles
275+
let llvm_profile = with_bolt_instrumented(&llvm_lib, |llvm_profile_dir| {
276+
stage.section("Gather profiles", |_| {
277+
gather_bolt_profiles(env, "LLVM", llvm_benchmarks(env), llvm_profile_dir)
278+
})
274279
})?;
275280
print_free_disk_space()?;
276281

@@ -279,27 +284,43 @@ fn execute_pipeline(
279284
// the final dist build. However, when BOLT optimizes an artifact, it does so *in-place*,
280285
// therefore it will actually optimize all the hard links, which means that the final
281286
// packaged `libLLVM.so` file *will* be BOLT optimized.
282-
bolt_optimize(&llvm_lib, &profile).context("Could not optimize LLVM with BOLT")?;
287+
bolt_optimize(&llvm_lib, &llvm_profile).context("Could not optimize LLVM with BOLT")?;
288+
289+
let rustc_lib = io::find_file_in_dir(&libdir, "librustc_driver", ".so")?;
290+
291+
log::info!("Optimizing {rustc_lib} with BOLT");
292+
293+
// Instrument it and gather profiles
294+
let rustc_profile = with_bolt_instrumented(&rustc_lib, |rustc_profile_dir| {
295+
stage.section("Gather profiles", |_| {
296+
gather_bolt_profiles(env, "rustc", rustc_benchmarks(env), rustc_profile_dir)
297+
})
298+
})?;
299+
print_free_disk_space()?;
300+
301+
// Now optimize the library with BOLT.
302+
bolt_optimize(&rustc_lib, &rustc_profile)
303+
.context("Could not optimize rustc with BOLT")?;
283304

284305
// LLVM is not being cleared here, we want to use the BOLT-optimized LLVM
285-
Ok(Some(profile))
306+
Ok(vec![llvm_profile, rustc_profile])
286307
})?
287308
} else {
288-
None
309+
vec![]
289310
};
290311

291312
let mut dist = Bootstrap::dist(env, &dist_args)
292313
.llvm_pgo_optimize(&llvm_pgo_profile)
293314
.rustc_pgo_optimize(&rustc_pgo_profile)
294315
.avoid_rustc_rebuild();
295316

296-
if let Some(llvm_bolt_profile) = llvm_bolt_profile {
297-
dist = dist.with_bolt_profile(llvm_bolt_profile);
317+
for bolt_profile in bolt_profiles {
318+
dist = dist.with_bolt_profile(bolt_profile);
298319
}
299320

300321
// Final stage: Assemble the dist artifacts
301322
// The previous PGO optimized rustc build and PGO optimized LLVM builds should be reused.
302-
timer.section("Stage 4 (final build)", |stage| dist.run(stage))?;
323+
timer.section("Stage 5 (final build)", |stage| dist.run(stage))?;
303324

304325
// After dist has finished, run a subset of the test suite on the optimized artifacts to discover
305326
// possible regressions.

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

Lines changed: 34 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ const RUSTC_PGO_CRATES: &[&str] = &[
2727
"bitmaps-3.1.0",
2828
];
2929

30-
const LLVM_BOLT_CRATES: &[&str] = LLVM_PGO_CRATES;
31-
3230
fn init_compiler_benchmarks(
3331
env: &Environment,
3432
profiles: &[&str],
@@ -113,6 +111,14 @@ fn log_profile_stats(
113111
Ok(())
114112
}
115113

114+
pub fn llvm_benchmarks(env: &Environment) -> CmdBuilder {
115+
init_compiler_benchmarks(env, &["Debug", "Opt"], &["Full"], LLVM_PGO_CRATES)
116+
}
117+
118+
pub fn rustc_benchmarks(env: &Environment) -> CmdBuilder {
119+
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["All"], RUSTC_PGO_CRATES)
120+
}
121+
116122
pub struct LlvmPGOProfile(pub Utf8PathBuf);
117123

118124
pub fn gather_llvm_profiles(
@@ -122,9 +128,7 @@ pub fn gather_llvm_profiles(
122128
log::info!("Running benchmarks with PGO instrumented LLVM");
123129

124130
with_log_group("Running benchmarks", || {
125-
init_compiler_benchmarks(env, &["Debug", "Opt"], &["Full"], LLVM_PGO_CRATES)
126-
.run()
127-
.context("Cannot gather LLVM PGO profiles")
131+
llvm_benchmarks(env).run().context("Cannot gather LLVM PGO profiles")
128132
})?;
129133

130134
let merged_profile = env.artifact_dir().join("llvm-pgo.profdata");
@@ -157,7 +161,7 @@ pub fn gather_rustc_profiles(
157161
// Here we're profiling the `rustc` frontend, so we also include `Check`.
158162
// The benchmark set includes various stress tests that put the frontend under pressure.
159163
with_log_group("Running benchmarks", || {
160-
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["All"], RUSTC_PGO_CRATES)
164+
rustc_benchmarks(env)
161165
.env("LLVM_PROFILE_FILE", profile_template.as_str())
162166
.run()
163167
.context("Cannot gather rustc PGO profiles")
@@ -176,23 +180,25 @@ pub fn gather_rustc_profiles(
176180
Ok(RustcPGOProfile(merged_profile))
177181
}
178182

179-
pub struct LlvmBoltProfile(pub Utf8PathBuf);
183+
pub struct BoltProfile(pub Utf8PathBuf);
180184

181-
pub fn gather_llvm_bolt_profiles(env: &Environment) -> anyhow::Result<LlvmBoltProfile> {
182-
log::info!("Running benchmarks with BOLT instrumented LLVM");
185+
pub fn gather_bolt_profiles(
186+
env: &Environment,
187+
name: &str,
188+
benchmarks: CmdBuilder,
189+
profile_prefix: &Utf8Path,
190+
) -> anyhow::Result<BoltProfile> {
191+
log::info!("Running benchmarks with BOLT instrumented {name}");
183192

184193
with_log_group("Running benchmarks", || {
185-
init_compiler_benchmarks(env, &["Check", "Debug", "Opt"], &["Full"], LLVM_BOLT_CRATES)
186-
.run()
187-
.context("Cannot gather LLVM BOLT profiles")
194+
benchmarks.run().with_context(|| "Cannot gather {name} BOLT profiles")
188195
})?;
189196

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

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

197203
let mut merge_args = vec!["merge-fdata"];
198204
merge_args.extend(profiles.iter().map(|p| p.to_str().unwrap()));
@@ -204,7 +210,7 @@ pub fn gather_llvm_bolt_profiles(env: &Environment) -> anyhow::Result<LlvmBoltPr
204210
.context("Cannot merge BOLT profiles")
205211
})?;
206212

207-
log::info!("LLVM BOLT statistics");
213+
log::info!("{name} BOLT statistics");
208214
log::info!(
209215
"{merged_profile}: {}",
210216
humansize::format_size(std::fs::metadata(merged_profile.as_std_path())?.len(), BINARY)
@@ -216,8 +222,17 @@ pub fn gather_llvm_bolt_profiles(env: &Environment) -> anyhow::Result<LlvmBoltPr
216222
.collect::<Result<Vec<_>, _>>()?
217223
.into_iter()
218224
.sum::<u64>();
219-
log::info!("{profile_root}: {}", humansize::format_size(size, BINARY));
225+
log::info!("{profile_prefix}: {}", humansize::format_size(size, BINARY));
220226
log::info!("Profile file count: {}", profiles.len());
221227

222-
Ok(LlvmBoltProfile(merged_profile))
228+
// Delete the gathered profiles
229+
for profile in glob::glob(&format!("{profile_prefix}*"))?.into_iter() {
230+
if let Ok(profile) = profile {
231+
if let Err(error) = std::fs::remove_file(&profile) {
232+
log::error!("Cannot delete BOLT profile {}: {error:?}", profile.display());
233+
}
234+
}
235+
}
236+
237+
Ok(BoltProfile(merged_profile))
223238
}

0 commit comments

Comments
 (0)
This repository has been archived.