@@ -39,12 +39,19 @@ gather_profiles () {
39
39
40
40
rm -rf /tmp/rustc-pgo
41
41
42
+ # This path has to be absolute
43
+ LLVM_PROFILE_DIRECTORY_ROOT=/tmp/llvm-pgo
44
+
42
45
# We collect LLVM profiling information and rustc profiling information in
43
46
# separate phases. This increases build time -- though not by a huge amount --
44
47
# but prevents any problems from arising due to different profiling runtimes
45
48
# 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 \
48
55
--stage 2 library/std \
49
56
--llvm-profile-generate
50
57
@@ -64,11 +71,18 @@ RUSTC_BOOTSTRAP=1 \
64
71
gather_profiles " Debug,Opt" " Full" \
65
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"
66
73
74
+ LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
75
+
67
76
# Merge the profile data we gathered for LLVM
68
77
# Note that this uses the profdata from the clang we used to build LLVM,
69
78
# 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
72
86
73
87
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
74
88
# 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
77
91
78
92
# Okay, LLVM profiling is done, switch to rustc PGO.
79
93
94
+ # The path has to be absolute
95
+ RUSTC_PROFILE_DIRECTORY_ROOT=/tmp/rustc-pgo
96
+
80
97
python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
81
98
--stage 2 library/std \
82
- --rust-profile-generate=/tmp/rustc-pgo
99
+ --rust-profile-generate=${RUSTC_PROFILE_DIRECTORY_ROOT}
83
100
84
101
# Here we're profiling the `rustc` frontend, so we also include `Check`.
85
102
# The benchmark set includes various stress tests that put the frontend under pressure.
86
103
# The profile data is written into a single filepath that is being repeatedly merged when each
87
104
# rustc invocation ends. Empirically, this can result in some profiling data being lost.
88
105
# That's why we override the profile path to include the PID. This will produce many more profiling
89
106
# 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
92
112
93
113
# Merge the profile data we gathered
94
114
./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
96
122
97
123
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
98
124
# 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
102
128
# This produces the actual final set of artifacts, using both the LLVM and rustc
103
129
# collected profiling data.
104
130
$@ \
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