From 93ff904ecb54e27ecb827fa21372e358c06ece65 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 10 Jun 2022 18:08:46 +0200 Subject: [PATCH 1/2] Try LLVM CS PGO --- src/bootstrap/native.rs | 31 ++++++++++++++++++++++++++----- src/ci/pgo.sh | 29 ++++++++++++++++++++++++----- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/src/bootstrap/native.rs b/src/bootstrap/native.rs index 79f2338b7abb9..a31d44ff10955 100644 --- a/src/bootstrap/native.rs +++ b/src/bootstrap/native.rs @@ -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") { @@ -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); @@ -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. @@ -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() { @@ -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 @@ -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); diff --git a/src/ci/pgo.sh b/src/ci/pgo.sh index 9de970c9c2aa5..09e3b77538765 100755 --- a/src/ci/pgo.sh +++ b/src/ci/pgo.sh @@ -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 From 44ad613ac9f01e87ba6188a373fb6c9b268f9fac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ber=C3=A1nek?= Date: Fri, 10 Jun 2022 18:12:08 +0200 Subject: [PATCH 2/2] Update LLVM --- src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh b/src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh index 495c539d06988..ddd08c449db96 100755 --- a/src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh +++ b/src/ci/docker/host-x86_64/dist-x86_64-linux/build-clang.sh @@ -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