Skip to content

Commit 8a33254

Browse files
committed
Auto merge of #96978 - lqd:win_pgo2, r=Mark-Simulacrum
Utilize PGO for windows x64 rustc dist builds This PR adds PGO support for the CI x64 windows dist builds. These are the results from running the rustc-perf benchmarks: ![image](https://user-images.githubusercontent.com/247183/177662869-683a8034-7c95-42bf-9900-9ffd66677fcf.png) Thanks to `@Kobzol,` `@michaelwoerister,` `@wesleywiser,` `@Mark-Simulacrum` for their precious help.
2 parents 38b7215 + 9027f82 commit 8a33254

File tree

8 files changed

+190
-55
lines changed

8 files changed

+190
-55
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ jobs:
410410
- name: dist-x86_64-msvc
411411
env:
412412
RUST_CONFIGURE_ARGS: "--build=x86_64-pc-windows-msvc --host=x86_64-pc-windows-msvc --target=x86_64-pc-windows-msvc --enable-full-tools --enable-profiler"
413-
SCRIPT: python x.py dist
413+
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc src/ci/pgo.sh python x.py dist
414414
DIST_REQUIRE_ALL_TOOLS: 1
415415
os: windows-latest-xl
416416
- name: dist-i686-msvc

src/bootstrap/compile.rs

+31-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::config::{LlvmLibunwind, TargetSelection};
2525
use crate::dist;
2626
use crate::native;
2727
use crate::tool::SourceType;
28+
use crate::util::get_clang_cl_resource_dir;
2829
use crate::util::{exe, is_debug_info, is_dylib, output, symlink_dir, t, up_to_date};
2930
use crate::LLVM_TOOLS;
3031
use crate::{CLang, Compiler, DependencyType, GitRepo, Mode};
@@ -772,10 +773,38 @@ pub fn rustc_cargo_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetS
772773
if let Some(s) = target_config.and_then(|c| c.llvm_config.as_ref()) {
773774
cargo.env("CFG_LLVM_ROOT", s);
774775
}
775-
// Some LLVM linker flags (-L and -l) may be needed to link rustc_llvm.
776+
777+
// Some LLVM linker flags (-L and -l) may be needed to link `rustc_llvm`. Its build script
778+
// expects these to be passed via the `LLVM_LINKER_FLAGS` env variable, separated by
779+
// whitespace.
780+
//
781+
// For example:
782+
// - on windows, when `clang-cl` is used with instrumentation, we need to manually add
783+
// clang's runtime library resource directory so that the profiler runtime library can be
784+
// found. This is to avoid the linker errors about undefined references to
785+
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
786+
let mut llvm_linker_flags = String::new();
787+
if builder.config.llvm_profile_generate && target.contains("msvc") {
788+
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
789+
// Add clang's runtime library directory to the search path
790+
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
791+
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
792+
}
793+
}
794+
795+
// The config can also specify its own llvm linker flags.
776796
if let Some(ref s) = builder.config.llvm_ldflags {
777-
cargo.env("LLVM_LINKER_FLAGS", s);
797+
if !llvm_linker_flags.is_empty() {
798+
llvm_linker_flags.push_str(" ");
799+
}
800+
llvm_linker_flags.push_str(s);
801+
}
802+
803+
// Set the linker flags via the env var that `rustc_llvm`'s build script will read.
804+
if !llvm_linker_flags.is_empty() {
805+
cargo.env("LLVM_LINKER_FLAGS", llvm_linker_flags);
778806
}
807+
779808
// Building with a static libstdc++ is only supported on linux right now,
780809
// not for MSVC or macOS
781810
if builder.config.llvm_static_stdcpp

src/bootstrap/native.rs

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

1919
use crate::builder::{Builder, RunConfig, ShouldRun, Step};
2020
use crate::config::TargetSelection;
21+
use crate::util::get_clang_cl_resource_dir;
2122
use crate::util::{self, exe, output, program_out_of_date, t, up_to_date};
2223
use crate::{CLang, GitRepo};
2324

@@ -776,7 +777,22 @@ impl Step for Lld {
776777
t!(fs::create_dir_all(&out_dir));
777778

778779
let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/lld"));
779-
configure_cmake(builder, target, &mut cfg, true, LdFlags::default());
780+
let mut ldflags = LdFlags::default();
781+
782+
// When building LLD as part of a build with instrumentation on windows, for example
783+
// when doing PGO on CI, cmake or clang-cl don't automatically link clang's
784+
// profiler runtime in. In that case, we need to manually ask cmake to do it, to avoid
785+
// linking errors, much like LLVM's cmake setup does in that situation.
786+
if builder.config.llvm_profile_generate && target.contains("msvc") {
787+
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
788+
// Find clang's runtime library directory and push that as a search path to the
789+
// cmake linker flags.
790+
let clang_rt_dir = get_clang_cl_resource_dir(clang_cl_path);
791+
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
792+
}
793+
}
794+
795+
configure_cmake(builder, target, &mut cfg, true, ldflags);
780796

781797
// This is an awful, awful hack. Discovered when we migrated to using
782798
// clang-cl to compile LLVM/LLD it turns out that LLD, when built out of

src/bootstrap/util.rs

+24
Original file line numberDiff line numberDiff line change
@@ -576,3 +576,27 @@ fn absolute_windows(path: &std::path::Path) -> std::io::Result<std::path::PathBu
576576
}
577577
}
578578
}
579+
580+
/// Adapted from https://github.com/llvm/llvm-project/blob/782e91224601e461c019e0a4573bbccc6094fbcd/llvm/cmake/modules/HandleLLVMOptions.cmake#L1058-L1079
581+
///
582+
/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource
583+
/// directory to the linker flags, otherwise there will be linker errors about the profiler runtime
584+
/// missing. This function returns the path to that directory.
585+
pub fn get_clang_cl_resource_dir(clang_cl_path: &str) -> PathBuf {
586+
// Similar to how LLVM does it, to find clang's library runtime directory:
587+
// - we ask `clang-cl` to locate the `clang_rt.builtins` lib.
588+
let mut builtins_locator = Command::new(clang_cl_path);
589+
builtins_locator.args(&["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
590+
591+
let clang_rt_builtins = output(&mut builtins_locator);
592+
let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
593+
assert!(
594+
clang_rt_builtins.exists(),
595+
"`clang-cl` must correctly locate the library runtime directory"
596+
);
597+
598+
// - the profiler runtime will be located in the same directory as the builtins lib, like
599+
// `$LLVM_DISTRO_ROOT/lib/clang/$LLVM_VERSION/lib/windows`.
600+
let clang_rt_dir = clang_rt_builtins.parent().expect("The clang lib folder should exist");
601+
clang_rt_dir.to_path_buf()
602+
}

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ set -ex
44

55
source shared.sh
66

7-
LLVM=llvmorg-14.0.2
7+
LLVM=llvmorg-14.0.5
88

99
mkdir llvm-project
1010
cd llvm-project

src/ci/github-actions/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,7 @@ jobs:
625625
--target=x86_64-pc-windows-msvc
626626
--enable-full-tools
627627
--enable-profiler
628-
SCRIPT: python x.py dist
628+
SCRIPT: PGO_HOST=x86_64-pc-windows-msvc src/ci/pgo.sh python x.py dist
629629
DIST_REQUIRE_ALL_TOOLS: 1
630630
<<: *job-windows-xl
631631

src/ci/pgo.sh

+113-48
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,82 @@
33

44
set -euxo pipefail
55

6+
ci_dir=`cd $(dirname $0) && pwd`
7+
source "$ci_dir/shared.sh"
8+
9+
# The root checkout, where the source is located
10+
CHECKOUT=/checkout
11+
12+
DOWNLOADED_LLVM=/rustroot
13+
14+
# The main directory where the build occurs, which can be different between linux and windows
15+
BUILD_ROOT=$CHECKOUT/obj
16+
17+
if isWindows; then
18+
CHECKOUT=$(pwd)
19+
DOWNLOADED_LLVM=$CHECKOUT/citools/clang-rust
20+
BUILD_ROOT=$CHECKOUT
21+
fi
22+
23+
# The various build artifacts used in other commands: to launch rustc builds, build the perf
24+
# collector, and run benchmarks to gather profiling data
25+
BUILD_ARTIFACTS=$BUILD_ROOT/build/$PGO_HOST
26+
RUSTC_STAGE_0=$BUILD_ARTIFACTS/stage0/bin/rustc
27+
CARGO_STAGE_0=$BUILD_ARTIFACTS/stage0/bin/cargo
28+
RUSTC_STAGE_2=$BUILD_ARTIFACTS/stage2/bin/rustc
29+
30+
# Windows needs these to have the .exe extension
31+
if isWindows; then
32+
RUSTC_STAGE_0="${RUSTC_STAGE_0}.exe"
33+
CARGO_STAGE_0="${CARGO_STAGE_0}.exe"
34+
RUSTC_STAGE_2="${RUSTC_STAGE_2}.exe"
35+
fi
36+
37+
# Make sure we have a temporary PGO work folder
38+
PGO_TMP=/tmp/tmp-pgo
39+
mkdir -p $PGO_TMP
40+
rm -rf $PGO_TMP/*
41+
42+
RUSTC_PERF=$PGO_TMP/rustc-perf
43+
644
# Compile several crates to gather execution PGO profiles.
745
# Arg0 => profiles (Debug, Opt)
846
# Arg1 => scenarios (Full, IncrFull, All)
947
# Arg2 => crates (syn, cargo, ...)
1048
gather_profiles () {
11-
cd /checkout/obj
49+
cd $BUILD_ROOT
1250

1351
# Compile libcore, both in opt-level=0 and opt-level=3
14-
RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
15-
--edition=2021 --crate-type=lib ../library/core/src/lib.rs
16-
RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
17-
--edition=2021 --crate-type=lib -Copt-level=3 ../library/core/src/lib.rs
52+
RUSTC_BOOTSTRAP=1 $RUSTC_STAGE_2 \
53+
--edition=2021 --crate-type=lib $CHECKOUT/library/core/src/lib.rs \
54+
--out-dir $PGO_TMP
55+
RUSTC_BOOTSTRAP=1 $RUSTC_STAGE_2 \
56+
--edition=2021 --crate-type=lib -Copt-level=3 $CHECKOUT/library/core/src/lib.rs \
57+
--out-dir $PGO_TMP
1858

19-
cd rustc-perf
59+
cd $RUSTC_PERF
2060

2161
# Run rustc-perf benchmarks
2262
# Benchmark using profile_local with eprintln, which essentially just means
2363
# don't actually benchmark -- just make sure we run rustc a bunch of times.
2464
RUST_LOG=collector=debug \
25-
RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
65+
RUSTC=$RUSTC_STAGE_0 \
2666
RUSTC_BOOTSTRAP=1 \
27-
/checkout/obj/build/$PGO_HOST/stage0/bin/cargo run -p collector --bin collector -- \
28-
profile_local \
29-
eprintln \
30-
/checkout/obj/build/$PGO_HOST/stage2/bin/rustc \
31-
--id Test \
32-
--profiles $1 \
33-
--cargo /checkout/obj/build/$PGO_HOST/stage0/bin/cargo \
34-
--scenarios $2 \
35-
--include $3
36-
37-
cd /checkout/obj
67+
$CARGO_STAGE_0 run -p collector --bin collector -- \
68+
profile_local \
69+
eprintln \
70+
$RUSTC_STAGE_2 \
71+
--id Test \
72+
--profiles $1 \
73+
--cargo $CARGO_STAGE_0 \
74+
--scenarios $2 \
75+
--include $3
76+
77+
cd $BUILD_ROOT
3878
}
3979

40-
rm -rf /tmp/rustc-pgo
41-
4280
# This path has to be absolute
43-
LLVM_PROFILE_DIRECTORY_ROOT=/tmp/llvm-pgo
81+
LLVM_PROFILE_DIRECTORY_ROOT=$PGO_TMP/llvm-pgo
4482

4583
# We collect LLVM profiling information and rustc profiling information in
4684
# separate phases. This increases build time -- though not by a huge amount --
@@ -49,69 +87,93 @@ LLVM_PROFILE_DIRECTORY_ROOT=/tmp/llvm-pgo
4987
# LLVM IR PGO does not respect LLVM_PROFILE_FILE, so we have to set the profiling file
5088
# path through our custom environment variable. We include the PID in the directory path
5189
# to avoid updates to profile files being lost because of race conditions.
52-
LLVM_PROFILE_DIR=${LLVM_PROFILE_DIRECTORY_ROOT}/prof-%p python3 ../x.py build \
90+
LLVM_PROFILE_DIR=${LLVM_PROFILE_DIRECTORY_ROOT}/prof-%p python3 $CHECKOUT/x.py build \
5391
--target=$PGO_HOST \
5492
--host=$PGO_HOST \
5593
--stage 2 library/std \
5694
--llvm-profile-generate
5795

58-
# Compile rustc perf
59-
cp -r /tmp/rustc-perf ./
60-
chown -R $(whoami): ./rustc-perf
61-
cd rustc-perf
62-
63-
# Build the collector ahead of time, which is needed to make sure the rustc-fake
64-
# binary used by the collector is present.
65-
RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
96+
# Compile rustc-perf:
97+
# - get the expected commit source code: on linux, the Dockerfile downloads a source archive before
98+
# running this script. On Windows, we do that here.
99+
if isLinux; then
100+
cp -r /tmp/rustc-perf $RUSTC_PERF
101+
chown -R $(whoami): $RUSTC_PERF
102+
else
103+
# rustc-perf version from 2022-05-18
104+
PERF_COMMIT=f66cc8f3e04392b0e2fd811f21fd1ece6ebaded3
105+
retry curl -LS -o $PGO_TMP/perf.zip \
106+
https://github.com/rust-lang/rustc-perf/archive/$PERF_COMMIT.zip && \
107+
cd $PGO_TMP && unzip -q perf.zip && \
108+
mv rustc-perf-$PERF_COMMIT $RUSTC_PERF && \
109+
rm perf.zip
110+
fi
111+
112+
# - build rustc-perf's collector ahead of time, which is needed to make sure the rustc-fake binary
113+
# used by the collector is present.
114+
cd $RUSTC_PERF
115+
116+
RUSTC=$RUSTC_STAGE_0 \
66117
RUSTC_BOOTSTRAP=1 \
67-
/checkout/obj/build/$PGO_HOST/stage0/bin/cargo build -p collector
118+
$CARGO_STAGE_0 build -p collector
68119

69120
# Here we're profiling LLVM, so we only care about `Debug` and `Opt`, because we want to stress
70121
# codegen. We also profile some of the most prolific crates.
71122
gather_profiles "Debug,Opt" "Full" \
72-
"syn-1.0.89,cargo-0.60.0,serde-1.0.136,ripgrep-13.0.0,regex-1.5.5,clap-3.1.6,hyper-0.14.18"
123+
"syn-1.0.89,cargo-0.60.0,serde-1.0.136,ripgrep-13.0.0,regex-1.5.5,clap-3.1.6,hyper-0.14.18"
73124

74-
LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
125+
LLVM_PROFILE_MERGED_FILE=$PGO_TMP/llvm-pgo.profdata
75126

76127
# Merge the profile data we gathered for LLVM
77128
# Note that this uses the profdata from the clang we used to build LLVM,
78129
# which likely has a different version than our in-tree clang.
79-
/rustroot/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
130+
$DOWNLOADED_LLVM/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
80131

81132
echo "LLVM PGO statistics"
82133
du -sh ${LLVM_PROFILE_MERGED_FILE}
83134
du -sh ${LLVM_PROFILE_DIRECTORY_ROOT}
84135
echo "Profile file count"
85136
find ${LLVM_PROFILE_DIRECTORY_ROOT} -type f | wc -l
86137

138+
# We don't need the individual .profraw files now that they have been merged into a final .profdata
139+
rm -r $LLVM_PROFILE_DIRECTORY_ROOT
140+
87141
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
88142
# change (or any other llvm-related options); so just clear out the relevant
89143
# directories ourselves.
90-
rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
144+
rm -r $BUILD_ARTIFACTS/llvm $BUILD_ARTIFACTS/lld
91145

92146
# Okay, LLVM profiling is done, switch to rustc PGO.
93147

94148
# The path has to be absolute
95-
RUSTC_PROFILE_DIRECTORY_ROOT=/tmp/rustc-pgo
149+
RUSTC_PROFILE_DIRECTORY_ROOT=$PGO_TMP/rustc-pgo
96150

97-
python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
151+
python3 $CHECKOUT/x.py build --target=$PGO_HOST --host=$PGO_HOST \
98152
--stage 2 library/std \
99153
--rust-profile-generate=${RUSTC_PROFILE_DIRECTORY_ROOT}
100154

101155
# Here we're profiling the `rustc` frontend, so we also include `Check`.
102156
# The benchmark set includes various stress tests that put the frontend under pressure.
103-
# The profile data is written into a single filepath that is being repeatedly merged when each
104-
# rustc invocation ends. Empirically, this can result in some profiling data being lost.
105-
# That's why we override the profile path to include the PID. This will produce many more profiling
106-
# files, but the resulting profile will produce a slightly faster rustc binary.
107-
LLVM_PROFILE_FILE=${RUSTC_PROFILE_DIRECTORY_ROOT}/default_%m_%p.profraw gather_profiles \
108-
"Check,Debug,Opt" "All" \
109-
"externs,ctfe-stress-5,cargo-0.60.0,token-stream-stress,match-stress,tuple-stress,diesel-1.4.8,bitmaps-3.1.0"
110-
111-
RUSTC_PROFILE_MERGED_FILE=/tmp/rustc-pgo.profdata
157+
if isLinux; then
158+
# The profile data is written into a single filepath that is being repeatedly merged when each
159+
# rustc invocation ends. Empirically, this can result in some profiling data being lost. That's
160+
# why we override the profile path to include the PID. This will produce many more profiling
161+
# files, but the resulting profile will produce a slightly faster rustc binary.
162+
LLVM_PROFILE_FILE=${RUSTC_PROFILE_DIRECTORY_ROOT}/default_%m_%p.profraw gather_profiles \
163+
"Check,Debug,Opt" "All" \
164+
"externs,ctfe-stress-5,cargo-0.60.0,token-stream-stress,match-stress,tuple-stress,diesel-1.4.8,bitmaps-3.1.0"
165+
else
166+
# On windows, we don't do that yet (because it generates a lot of data, hitting disk space
167+
# limits on the builder), and use the default profraw merging behavior.
168+
gather_profiles \
169+
"Check,Debug,Opt" "All" \
170+
"externs,ctfe-stress-5,cargo-0.60.0,token-stream-stress,match-stress,tuple-stress,diesel-1.4.8,bitmaps-3.1.0"
171+
fi
172+
173+
RUSTC_PROFILE_MERGED_FILE=$PGO_TMP/rustc-pgo.profdata
112174

113175
# Merge the profile data we gathered
114-
./build/$PGO_HOST/llvm/bin/llvm-profdata \
176+
$BUILD_ARTIFACTS/llvm/bin/llvm-profdata \
115177
merge -o ${RUSTC_PROFILE_MERGED_FILE} ${RUSTC_PROFILE_DIRECTORY_ROOT}
116178

117179
echo "Rustc PGO statistics"
@@ -120,10 +182,13 @@ du -sh ${RUSTC_PROFILE_DIRECTORY_ROOT}
120182
echo "Profile file count"
121183
find ${RUSTC_PROFILE_DIRECTORY_ROOT} -type f | wc -l
122184

185+
# We don't need the individual .profraw files now that they have been merged into a final .profdata
186+
rm -r $RUSTC_PROFILE_DIRECTORY_ROOT
187+
123188
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
124189
# change (or any other llvm-related options); so just clear out the relevant
125190
# directories ourselves.
126-
rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
191+
rm -r $BUILD_ARTIFACTS/llvm $BUILD_ARTIFACTS/lld
127192

128193
# This produces the actual final set of artifacts, using both the LLVM and rustc
129194
# collected profiling data.

src/ci/scripts/install-clang.sh

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#!/bin/bash
2+
# ignore-tidy-linelength
23
# This script installs clang on the local machine. Note that we don't install
34
# clang on Linux since its compiler story is just so different. Each container
45
# has its own toolchain configured appropriately already.
@@ -9,7 +10,7 @@ IFS=$'\n\t'
910
source "$(cd "$(dirname "$0")" && pwd)/../shared.sh"
1011

1112
# Update both macOS's and Windows's tarballs when bumping the version here.
12-
LLVM_VERSION="14.0.2"
13+
LLVM_VERSION="14.0.5"
1314

1415
if isMacOS; then
1516
# If the job selects a specific Xcode version, use that instead of

0 commit comments

Comments
 (0)