]> git.proxmox.com Git - rustc.git/blame - src/ci/pgo.sh
New upstream version 1.63.0+dfsg1
[rustc.git] / src / ci / pgo.sh
CommitLineData
fc512014 1#!/bin/bash
923072b8 2# ignore-tidy-linelength
fc512014
XL
3
4set -euxo pipefail
5
5e7ed085 6# Compile several crates to gather execution PGO profiles.
04454e1e
FG
7# Arg0 => profiles (Debug, Opt)
8# Arg1 => scenarios (Full, IncrFull, All)
5e7ed085
FG
9# Arg2 => crates (syn, cargo, ...)
10gather_profiles () {
11 cd /checkout/obj
12
13 # 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
18
19 cd rustc-perf
20
21 # Run rustc-perf benchmarks
22 # Benchmark using profile_local with eprintln, which essentially just means
23 # don't actually benchmark -- just make sure we run rustc a bunch of times.
24 RUST_LOG=collector=debug \
25 RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
26 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 \
04454e1e
FG
31 --id Test \
32 --profiles $1 \
5e7ed085 33 --cargo /checkout/obj/build/$PGO_HOST/stage0/bin/cargo \
04454e1e 34 --scenarios $2 \
5e7ed085
FG
35 --include $3
36
37 cd /checkout/obj
38}
39
fc512014
XL
40rm -rf /tmp/rustc-pgo
41
923072b8
FG
42# This path has to be absolute
43LLVM_PROFILE_DIRECTORY_ROOT=/tmp/llvm-pgo
44
3c0e092e
XL
45# We collect LLVM profiling information and rustc profiling information in
46# separate phases. This increases build time -- though not by a huge amount --
47# but prevents any problems from arising due to different profiling runtimes
48# being simultaneously linked in.
923072b8
FG
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.
52LLVM_PROFILE_DIR=${LLVM_PROFILE_DIRECTORY_ROOT}/prof-%p python3 ../x.py build \
53 --target=$PGO_HOST \
54 --host=$PGO_HOST \
94222f64 55 --stage 2 library/std \
94222f64 56 --llvm-profile-generate
fc512014 57
5e7ed085
FG
58# Compile rustc perf
59cp -r /tmp/rustc-perf ./
60chown -R $(whoami): ./rustc-perf
61cd 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.
65RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
66RUSTC_BOOTSTRAP=1 \
67/checkout/obj/build/$PGO_HOST/stage0/bin/cargo build -p collector
68
04454e1e
FG
69# Here we're profiling LLVM, so we only care about `Debug` and `Opt`, because we want to stress
70# codegen. We also profile some of the most prolific crates.
71gather_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"
5e7ed085 73
923072b8
FG
74LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
75
3c0e092e
XL
76# Merge the profile data we gathered for LLVM
77# Note that this uses the profdata from the clang we used to build LLVM,
78# which likely has a different version than our in-tree clang.
923072b8
FG
79/rustroot/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
80
81echo "LLVM PGO statistics"
82du -sh ${LLVM_PROFILE_MERGED_FILE}
83du -sh ${LLVM_PROFILE_DIRECTORY_ROOT}
84echo "Profile file count"
85find ${LLVM_PROFILE_DIRECTORY_ROOT} -type f | wc -l
3c0e092e
XL
86
87# Rustbuild currently doesn't support rebuilding LLVM when PGO options
88# change (or any other llvm-related options); so just clear out the relevant
89# directories ourselves.
90rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
91
92# Okay, LLVM profiling is done, switch to rustc PGO.
93
923072b8
FG
94# The path has to be absolute
95RUSTC_PROFILE_DIRECTORY_ROOT=/tmp/rustc-pgo
96
3c0e092e
XL
97python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
98 --stage 2 library/std \
923072b8 99 --rust-profile-generate=${RUSTC_PROFILE_DIRECTORY_ROOT}
3c0e092e 100
04454e1e
FG
101# Here we're profiling the `rustc` frontend, so we also include `Check`.
102# The benchmark set includes various stress tests that put the frontend under pressure.
923072b8
FG
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.
107LLVM_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
111RUSTC_PROFILE_MERGED_FILE=/tmp/rustc-pgo.profdata
fc512014
XL
112
113# Merge the profile data we gathered
114./build/$PGO_HOST/llvm/bin/llvm-profdata \
923072b8
FG
115 merge -o ${RUSTC_PROFILE_MERGED_FILE} ${RUSTC_PROFILE_DIRECTORY_ROOT}
116
117echo "Rustc PGO statistics"
118du -sh ${RUSTC_PROFILE_MERGED_FILE}
119du -sh ${RUSTC_PROFILE_DIRECTORY_ROOT}
120echo "Profile file count"
121find ${RUSTC_PROFILE_DIRECTORY_ROOT} -type f | wc -l
fc512014 122
94222f64
XL
123# Rustbuild currently doesn't support rebuilding LLVM when PGO options
124# change (or any other llvm-related options); so just clear out the relevant
125# directories ourselves.
126rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
127
3c0e092e
XL
128# This produces the actual final set of artifacts, using both the LLVM and rustc
129# collected profiling data.
94222f64 130$@ \
923072b8
FG
131 --rust-profile-use=${RUSTC_PROFILE_MERGED_FILE} \
132 --llvm-profile-use=${LLVM_PROFILE_MERGED_FILE}