Skip to content

Commit 87e97c9

Browse files
committed
Add PID to LLVM PGO profiles generated in CI and measure PGO statistics
1 parent b2eba05 commit 87e97c9

File tree

2 files changed

+39
-10
lines changed

2 files changed

+39
-10
lines changed

src/bootstrap/native.rs

+3
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,9 @@ impl Step for Llvm {
503503

504504
if builder.config.llvm_profile_generate {
505505
cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
506+
if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
507+
cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
508+
}
506509
cfg.define("LLVM_BUILD_RUNTIME", "No");
507510
}
508511
if let Some(path) = builder.config.llvm_profile_use.as_ref() {

src/ci/pgo.sh

+36-10
Original file line numberDiff line numberDiff line change
@@ -39,12 +39,19 @@ gather_profiles () {
3939

4040
rm -rf /tmp/rustc-pgo
4141

42+
# This path has to be absolute
43+
LLVM_PROFILE_DIRECTORY_ROOT=/tmp/llvm-pgo
44+
4245
# We collect LLVM profiling information and rustc profiling information in
4346
# separate phases. This increases build time -- though not by a huge amount --
4447
# but prevents any problems from arising due to different profiling runtimes
4548
# being simultaneously linked in.
46-
47-
python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
49+
# LLVM IR PGO does not respect LLVM_PROFILE_FILE, so we have to set the profiling file
50+
# path through our custom environment variable. We include the PID in the directory path
51+
# 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 \
53+
--target=$PGO_HOST \
54+
--host=$PGO_HOST \
4855
--stage 2 library/std \
4956
--llvm-profile-generate
5057

@@ -64,11 +71,18 @@ RUSTC_BOOTSTRAP=1 \
6471
gather_profiles "Debug,Opt" "Full" \
6572
"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"
6673

74+
LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
75+
6776
# Merge the profile data we gathered for LLVM
6877
# Note that this uses the profdata from the clang we used to build LLVM,
6978
# which likely has a different version than our in-tree clang.
70-
/rustroot/bin/llvm-profdata \
71-
merge -o /tmp/llvm-pgo.profdata ./build/$PGO_HOST/llvm/build/profiles
79+
/rustroot/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
80+
81+
echo "LLVM PGO statistics"
82+
du -sh ${LLVM_PROFILE_MERGED_FILE}
83+
du -sh ${LLVM_PROFILE_DIRECTORY_ROOT}
84+
echo "Profile file count"
85+
find ${LLVM_PROFILE_DIRECTORY_ROOT} -type f | wc -l
7286

7387
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
7488
# change (or any other llvm-related options); so just clear out the relevant
@@ -77,22 +91,34 @@ rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
7791

7892
# Okay, LLVM profiling is done, switch to rustc PGO.
7993

94+
# The path has to be absolute
95+
RUSTC_PROFILE_DIRECTORY_ROOT=/tmp/rustc-pgo
96+
8097
python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
8198
--stage 2 library/std \
82-
--rust-profile-generate=/tmp/rustc-pgo
99+
--rust-profile-generate=${RUSTC_PROFILE_DIRECTORY_ROOT}
83100

84101
# Here we're profiling the `rustc` frontend, so we also include `Check`.
85102
# The benchmark set includes various stress tests that put the frontend under pressure.
86103
# The profile data is written into a single filepath that is being repeatedly merged when each
87104
# rustc invocation ends. Empirically, this can result in some profiling data being lost.
88105
# That's why we override the profile path to include the PID. This will produce many more profiling
89106
# files, but the resulting profile will produce a slightly faster rustc binary.
90-
LLVM_PROFILE_FILE=/tmp/rustc-pgo/default_%m_%p.profraw gather_profiles "Check,Debug,Opt" "All" \
91-
"externs,ctfe-stress-5,cargo-0.60.0,token-stream-stress,match-stress,tuple-stress,diesel-1.4.8,bitmaps-3.1.0"
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
92112

93113
# Merge the profile data we gathered
94114
./build/$PGO_HOST/llvm/bin/llvm-profdata \
95-
merge -o /tmp/rustc-pgo.profdata /tmp/rustc-pgo
115+
merge -o ${RUSTC_PROFILE_MERGED_FILE} ${RUSTC_PROFILE_DIRECTORY_ROOT}
116+
117+
echo "Rustc PGO statistics"
118+
du -sh ${RUSTC_PROFILE_MERGED_FILE}
119+
du -sh ${RUSTC_PROFILE_DIRECTORY_ROOT}
120+
echo "Profile file count"
121+
find ${RUSTC_PROFILE_DIRECTORY_ROOT} -type f | wc -l
96122

97123
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
98124
# change (or any other llvm-related options); so just clear out the relevant
@@ -102,5 +128,5 @@ rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
102128
# This produces the actual final set of artifacts, using both the LLVM and rustc
103129
# collected profiling data.
104130
$@ \
105-
--rust-profile-use=/tmp/rustc-pgo.profdata \
106-
--llvm-profile-use=/tmp/llvm-pgo.profdata
131+
--rust-profile-use=${RUSTC_PROFILE_MERGED_FILE} \
132+
--llvm-profile-use=${LLVM_PROFILE_MERGED_FILE}

0 commit comments

Comments
 (0)