]> git.lizzy.rs Git - rust.git/blob - src/ci/pgo.sh
Rollup merge of #97627 - lcnr:comment-tick, r=Dylan-DPC
[rust.git] / src / ci / pgo.sh
1 #!/bin/bash
2 # ignore-tidy-linelength
3
4 set -euxo pipefail
5
6 # Compile several crates to gather execution PGO profiles.
7 # Arg0 => profiles (Debug, Opt)
8 # Arg1 => scenarios (Full, IncrFull, All)
9 # Arg2 => crates (syn, cargo, ...)
10 gather_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 \
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
38 }
39
40 rm -rf /tmp/rustc-pgo
41
42 # We collect LLVM profiling information and rustc profiling information in
43 # separate phases. This increases build time -- though not by a huge amount --
44 # but prevents any problems from arising due to different profiling runtimes
45 # being simultaneously linked in.
46
47 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
48     --stage 2 library/std \
49     --llvm-profile-generate
50
51 # Compile rustc perf
52 cp -r /tmp/rustc-perf ./
53 chown -R $(whoami): ./rustc-perf
54 cd rustc-perf
55
56 # Build the collector ahead of time, which is needed to make sure the rustc-fake
57 # binary used by the collector is present.
58 RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
59 RUSTC_BOOTSTRAP=1 \
60 /checkout/obj/build/$PGO_HOST/stage0/bin/cargo build -p collector
61
62 # Here we're profiling LLVM, so we only care about `Debug` and `Opt`, because we want to stress
63 # codegen. We also profile some of the most prolific crates.
64 gather_profiles "Debug,Opt" "Full" \
65 "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"
66
67 # Merge the profile data we gathered for LLVM
68 # Note that this uses the profdata from the clang we used to build LLVM,
69 # 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
72
73 # Rustbuild currently doesn't support rebuilding LLVM when PGO options
74 # change (or any other llvm-related options); so just clear out the relevant
75 # directories ourselves.
76 rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
77
78 # Okay, LLVM profiling is done, switch to rustc PGO.
79
80 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
81     --stage 2 library/std \
82     --rust-profile-generate=/tmp/rustc-pgo
83
84 # Here we're profiling the `rustc` frontend, so we also include `Check`.
85 # The benchmark set includes various stress tests that put the frontend under pressure.
86 # The profile data is written into a single filepath that is being repeatedly merged when each
87 # rustc invocation ends. Empirically, this can result in some profiling data being lost.
88 # That's why we override the profile path to include the PID. This will produce many more profiling
89 # 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"
92
93 # Merge the profile data we gathered
94 ./build/$PGO_HOST/llvm/bin/llvm-profdata \
95     merge -o /tmp/rustc-pgo.profdata /tmp/rustc-pgo
96
97 # Rustbuild currently doesn't support rebuilding LLVM when PGO options
98 # change (or any other llvm-related options); so just clear out the relevant
99 # directories ourselves.
100 rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
101
102 # This produces the actual final set of artifacts, using both the LLVM and rustc
103 # collected profiling data.
104 $@ \
105     --rust-profile-use=/tmp/rustc-pgo.profdata \
106     --llvm-profile-use=/tmp/llvm-pgo.profdata