Skip to content

[WIP] Try using call-site aware IR PGO for LLVM #97153

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 26 additions & 5 deletions src/bootstrap/native.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,25 @@ impl Step for Llvm {
// This flag makes sure `FileCheck` is copied in the final binaries directory.
cfg.define("LLVM_INSTALL_UTILS", "ON");

let mut cxxflags = "".to_string();
if builder.config.llvm_profile_generate {
cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
}

//cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
if std::env::var("LLVM_USE_CS_PGO").is_ok() {
cxxflags.push_str("-fcs-profile-generate=/tmp/llvm2");
// cfg.define("LLVM_VP_COUNTERS_PER_SITE", "8");
cxxflags.push_str(" -mllvm -vp-counters-per-site=10");
} else {
cxxflags.push_str("-fprofile-generate=/tmp/llvm1");
}
cfg.define("LLVM_BUILD_RUNTIME", "No");
}
if let Some(path) = builder.config.llvm_profile_use.as_ref() {
cfg.define("LLVM_PROFDATA_FILE", &path);
// cfg.define("LLVM_PROFDATA_FILE", &path);
cxxflags.push_str(&format!(" -fprofile-use={path}"));
}

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

configure_cmake(builder, target, &mut cfg, true, ldflags);
configure_cmake(builder, target, &mut cfg, true, ldflags, cxxflags);

for (key, val) in &builder.config.llvm_build_config {
cfg.define(key, val);
Expand Down Expand Up @@ -491,6 +501,7 @@ fn configure_cmake(
cfg: &mut cmake::Config,
use_compiler_launcher: bool,
mut ldflags: LdFlags,
cxxflags2: String,
) {
// Do not print installation messages for up-to-date files.
// LLVM and LLD builds can produce a lot of those and hit CI limits on log size.
Expand Down Expand Up @@ -624,6 +635,9 @@ fn configure_cmake(
if builder.config.llvm_clang_cl.is_some() {
cxxflags.push(&format!(" --target={}", target));
}
if !cxxflags2.is_empty() {
cxxflags.push(&format!(" {cxxflags2}"));
}
cfg.define("CMAKE_CXX_FLAGS", cxxflags);
if let Some(ar) = builder.ar(target) {
if ar.is_absolute() {
Expand Down Expand Up @@ -717,7 +731,7 @@ impl Step for Lld {
t!(fs::create_dir_all(&out_dir));

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

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

t!(fs::create_dir_all(&out_dir));
cfg.out_dir(out_dir);
Expand Down
2 changes: 1 addition & 1 deletion src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ set -ex

source shared.sh

LLVM=llvmorg-14.0.2
LLVM=llvmorg-14.0.4

mkdir llvm-project
cd llvm-project
Expand Down
29 changes: 24 additions & 5 deletions src/ci/pgo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,38 @@ LLVM_PROFILE_MERGED_FILE=/tmp/llvm-pgo.profdata
# Merge the profile data we gathered for LLVM
# Note that this uses the profdata from the clang we used to build LLVM,
# which likely has a different version than our in-tree clang.
/rustroot/bin/llvm-profdata merge -o ${LLVM_PROFILE_MERGED_FILE} ${LLVM_PROFILE_DIRECTORY_ROOT}
/rustroot/bin/llvm-profdata merge -o /tmp/llvm-pgo1.profdata /tmp/llvm1

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

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

LLVM_USE_CS_PGO=1 python3 ../x.py build --target=$PGO_HOST --host=$PGO_HOST \
--stage 2 library/std \
--llvm-profile-use=/tmp/llvm-pgo1.profdata \
--llvm-profile-generate

gather_profiles "Debug,Opt" "Full" \
"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"

/rustroot/bin/llvm-profdata \
merge -o /tmp/llvm-pgo.profdata /tmp/llvm-pgo1.profdata /tmp/llvm2

echo "LLVM PGO 2 statistics"
du -sh /tmp/llvm-pgo.profdata
du -sh /tmp/llvm2
echo "Profile file count"
find /tmp/llvm2 -type f | wc -l

rm -r ./build/$PGO_HOST/llvm ./build/$PGO_HOST/lld

# Okay, LLVM profiling is done, switch to rustc PGO.

# The path has to be absolute
Expand Down