]> git.lizzy.rs Git - rust.git/blob - src/ci/pgo.sh
Rollup merge of #95298 - jhorstmann:fix-double-drop-of-allocator-in-vec-into-iter...
[rust.git] / src / ci / pgo.sh
1 #!/bin/bash
2
3 set -euxo pipefail
4
5 # Compile several crates to gather execution PGO profiles.
6 # Arg0 => builds (Debug, Opt)
7 # Arg1 => runs (Full, IncrFull, All)
8 # Arg2 => crates (syn, cargo, ...)
9 gather_profiles () {
10   cd /checkout/obj
11
12   # Compile libcore, both in opt-level=0 and opt-level=3
13   RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
14       --edition=2021 --crate-type=lib ../library/core/src/lib.rs
15   RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
16       --edition=2021 --crate-type=lib -Copt-level=3 ../library/core/src/lib.rs
17
18   cd rustc-perf
19
20   # Run rustc-perf benchmarks
21   # Benchmark using profile_local with eprintln, which essentially just means
22   # don't actually benchmark -- just make sure we run rustc a bunch of times.
23   RUST_LOG=collector=debug \
24   RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
25   RUSTC_BOOTSTRAP=1 \
26   /checkout/obj/build/$PGO_HOST/stage0/bin/cargo run -p collector --bin collector -- \
27           profile_local \
28           eprintln \
29           /checkout/obj/build/$PGO_HOST/stage2/bin/rustc \
30           Test \
31           --builds $1 \
32           --cargo /checkout/obj/build/$PGO_HOST/stage0/bin/cargo \
33           --runs $2 \
34           --include $3
35
36   cd /checkout/obj
37 }
38
39 rm -rf /tmp/rustc-pgo
40
41 # We collect LLVM profiling information and rustc profiling information in
42 # separate phases. This increases build time -- though not by a huge amount --
43 # but prevents any problems from arising due to different profiling runtimes
44 # being simultaneously linked in.
45
46 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
47     --stage 2 library/std \
48     --llvm-profile-generate
49
50 # Profile libcore compilation in opt-level=0 and opt-level=3
51 RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
52     --edition=2021 --crate-type=lib ../library/core/src/lib.rs
53 RUSTC_BOOTSTRAP=1 ./build/$PGO_HOST/stage2/bin/rustc \
54     --edition=2021 --crate-type=lib -Copt-level=3 ../library/core/src/lib.rs
55
56 # Compile rustc perf
57 cp -r /tmp/rustc-perf ./
58 chown -R $(whoami): ./rustc-perf
59 cd rustc-perf
60
61 # Build the collector ahead of time, which is needed to make sure the rustc-fake
62 # binary used by the collector is present.
63 RUSTC=/checkout/obj/build/$PGO_HOST/stage0/bin/rustc \
64 RUSTC_BOOTSTRAP=1 \
65 /checkout/obj/build/$PGO_HOST/stage0/bin/cargo build -p collector
66
67 gather_profiles "Debug,Opt" "Full" "syn,cargo,serde,ripgrep,regex,clap-rs,hyper-2"
68
69 # Merge the profile data we gathered for LLVM
70 # Note that this uses the profdata from the clang we used to build LLVM,
71 # which likely has a different version than our in-tree clang.
72 /rustroot/bin/llvm-profdata \
73     merge -o /tmp/llvm-pgo.profdata ./build/$PGO_HOST/llvm/build/profiles
74
75 # Rustbuild currently doesn't support rebuilding LLVM when PGO options
76 # change (or any other llvm-related options); so just clear out the relevant
77 # directories ourselves.
78 rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
79
80 # Okay, LLVM profiling is done, switch to rustc PGO.
81
82 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
83     --stage 2 library/std \
84     --rust-profile-generate=/tmp/rustc-pgo
85
86 gather_profiles "Check,Debug,Opt" "All" \
87   "externs,ctfe-stress-4,inflate,cargo,token-stream-stress,match-stress-enum"
88
89 # Merge the profile data we gathered
90 ./build/$PGO_HOST/llvm/bin/llvm-profdata \
91     merge -o /tmp/rustc-pgo.profdata /tmp/rustc-pgo
92
93 # Rustbuild currently doesn't support rebuilding LLVM when PGO options
94 # change (or any other llvm-related options); so just clear out the relevant
95 # directories ourselves.
96 rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
97
98 # This produces the actual final set of artifacts, using both the LLVM and rustc
99 # collected profiling data.
100 $@ \
101     --rust-profile-use=/tmp/rustc-pgo.profdata \
102     --llvm-profile-use=/tmp/llvm-pgo.profdata