Skip to content

Commit 93ff904

Browse files
committed
Try LLVM CS PGO
1 parent ec55c61 commit 93ff904

File tree

2 files changed

+50
-10
lines changed

2 files changed

+50
-10
lines changed

src/bootstrap/native.rs

+26-5
Original file line numberDiff line numberDiff line change
@@ -298,15 +298,25 @@ impl Step for Llvm {
298298
// This flag makes sure `FileCheck` is copied in the final binaries directory.
299299
cfg.define("LLVM_INSTALL_UTILS", "ON");
300300

301+
let mut cxxflags = "".to_string();
301302
if builder.config.llvm_profile_generate {
302-
cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
303303
if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
304304
cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
305305
}
306+
307+
//cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
308+
if std::env::var("LLVM_USE_CS_PGO").is_ok() {
309+
cxxflags.push_str("-fcs-profile-generate=/tmp/llvm2");
310+
// cfg.define("LLVM_VP_COUNTERS_PER_SITE", "8");
311+
cxxflags.push_str(" -mllvm -vp-counters-per-site=10");
312+
} else {
313+
cxxflags.push_str("-fprofile-generate=/tmp/llvm1");
314+
}
306315
cfg.define("LLVM_BUILD_RUNTIME", "No");
307316
}
308317
if let Some(path) = builder.config.llvm_profile_use.as_ref() {
309-
cfg.define("LLVM_PROFDATA_FILE", &path);
318+
// cfg.define("LLVM_PROFDATA_FILE", &path);
319+
cxxflags.push_str(&format!(" -fprofile-use={path}"));
310320
}
311321

312322
if target != "aarch64-apple-darwin" && !target.contains("windows") {
@@ -442,7 +452,7 @@ impl Step for Llvm {
442452
cfg.define("LLVM_TEMPORARILY_ALLOW_OLD_TOOLCHAIN", "YES");
443453
}
444454

445-
configure_cmake(builder, target, &mut cfg, true, ldflags);
455+
configure_cmake(builder, target, &mut cfg, true, ldflags, cxxflags);
446456

447457
for (key, val) in &builder.config.llvm_build_config {
448458
cfg.define(key, val);
@@ -491,6 +501,7 @@ fn configure_cmake(
491501
cfg: &mut cmake::Config,
492502
use_compiler_launcher: bool,
493503
mut ldflags: LdFlags,
504+
cxxflags2: String,
494505
) {
495506
// Do not print installation messages for up-to-date files.
496507
// LLVM and LLD builds can produce a lot of those and hit CI limits on log size.
@@ -624,6 +635,9 @@ fn configure_cmake(
624635
if builder.config.llvm_clang_cl.is_some() {
625636
cxxflags.push(&format!(" --target={}", target));
626637
}
638+
if !cxxflags2.is_empty() {
639+
cxxflags.push(&format!(" {cxxflags2}"));
640+
}
627641
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
628642
if let Some(ar) = builder.ar(target) {
629643
if ar.is_absolute() {
@@ -717,7 +731,7 @@ impl Step for Lld {
717731
t!(fs::create_dir_all(&out_dir));
718732

719733
let mut cfg = cmake::Config::new(builder.src.join("src/llvm-project/lld"));
720-
configure_cmake(builder, target, &mut cfg, true, LdFlags::default());
734+
configure_cmake(builder, target, &mut cfg, true, LdFlags::default(), "".to_string());
721735

722736
// This is an awful, awful hack. Discovered when we migrated to using
723737
// clang-cl to compile LLVM/LLD it turns out that LLD, when built out of
@@ -919,7 +933,14 @@ impl Step for Sanitizers {
919933
// Unfortunately sccache currently lacks support to build them successfully.
920934
// Disable compiler launcher on Darwin targets to avoid potential issues.
921935
let use_compiler_launcher = !self.target.contains("apple-darwin");
922-
configure_cmake(builder, self.target, &mut cfg, use_compiler_launcher, LdFlags::default());
936+
configure_cmake(
937+
builder,
938+
self.target,
939+
&mut cfg,
940+
use_compiler_launcher,
941+
LdFlags::default(),
942+
"".to_string(),
943+
);
923944

924945
t!(fs::create_dir_all(&out_dir));
925946
cfg.out_dir(out_dir);

src/ci/pgo.sh

+24-5
Original file line numberDiff line numberDiff line change
@@ -76,19 +76,38 @@ LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
7676
# Merge the profile data we gathered for LLVM
7777
# Note that this uses the profdata from the clang we used to build LLVM,
7878
# which likely has a different version than our in-tree clang.
79-
/rustroot/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
79+
/rustroot/bin/llvm-profdata merge -o /tmp/llvm-pgo1.profdata /tmp/llvm1
8080

81-
echo "LLVM PGO statistics"
82-
du -sh ${LLVM_PROFILE_MERGED_FILE}
83-
du -sh ${LLVM_PROFILE_DIRECTORY_ROOT}
81+
echo "LLVM PGO 1 statistics"
82+
du -sh /tmp/llvm-pgo1.profdata
83+
du -sh /tmp/llvm1
8484
echo "Profile file count"
85-
find ${LLVM_PROFILE_DIRECTORY_ROOT} -type f | wc -l
85+
find /tmp/llvm1 -type f | wc -l
8686

8787
# Rustbuild currently doesn't support rebuilding LLVM when PGO options
8888
# change (or any other llvm-related options); so just clear out the relevant
8989
# directories ourselves.
9090
rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
9191

92+
LLVM_USE_CS_PGO=1 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
93+
--stage 2 library/std \
94+
--llvm-profile-use=/tmp/llvm-pgo1.profdata \
95+
--llvm-profile-generate
96+
97+
gather_profiles "Debug,Opt" "Full" \
98+
"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"
99+
100+
/rustroot/bin/llvm-profdata \
101+
merge -o /tmp/llvm-pgo.profdata /tmp/llvm-pgo1.profdata /tmp/llvm2
102+
103+
echo "LLVM PGO 2 statistics"
104+
du -sh /tmp/llvm-pgo.profdata
105+
du -sh /tmp/llvm2
106+
echo "Profile file count"
107+
find /tmp/llvm2 -type f | wc -l
108+
109+
rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld
110+
92111
# Okay, LLVM profiling is done, switch to rustc PGO.
93112

94113
# The path has to be absolute

0 commit comments

Comments
 (0)