Skip to content

Commit 93053d8

Browse files
committed
enable PGO on x86_64-apple-darwin
1 parent 90c8cd0 commit 93053d8

File tree

7 files changed

+69
-28
lines changed

7 files changed

+69
-28
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -317,7 +317,7 @@ jobs:
317317
os: ubuntu-20.04-8core-32gb
318318
- name: dist-x86_64-apple
319319
env:
320-
SCRIPT: "./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin"
320+
SCRIPT: PGO_HOST=x86_64-apple-darwin python3 src/ci/stage-build.py python3 ./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin
321321
RUST_CONFIGURE_ARGS: "--enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin"
322322
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
323323
MACOSX_DEPLOYMENT_TARGET: 10.7

src/bootstrap/compile.rs

+1
Original file line numberDiff line numberDiff line change
@@ -950,6 +950,7 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
950950
} else {
951951
panic!("clang has no clang_rt.profile library for {target}");
952952
};
953+
953954
let clang = builder.cc(target);
954955
let clang_rt_dir = get_clang_rt_dir(clang, false);
955956
let clang_rt_profile_lib = format!("libclang_rt.profile_{clang_rt_profile_lib_suffix}");

src/bootstrap/llvm.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,10 @@ impl Step for Llvm {
331331
// This flag makes sure `FileCheck` is copied in the final binaries directory.
332332
cfg.define("LLVM_INSTALL_UTILS", "ON");
333333

334+
let mut enabled_llvm_projects = Vec::new();
335+
334336
if builder.config.llvm_profile_generate {
337+
enabled_llvm_projects.push("compiler-rt");
335338
cfg.define("LLVM_BUILD_INSTRUMENTED", "IR");
336339
if let Ok(llvm_profile_dir) = std::env::var("LLVM_PROFILE_DIR") {
337340
cfg.define("LLVM_PROFILE_DATA_DIR", llvm_profile_dir);
@@ -344,6 +347,7 @@ impl Step for Llvm {
344347
if builder.config.llvm_bolt_profile_generate
345348
|| builder.config.llvm_bolt_profile_use.is_some()
346349
{
350+
enabled_llvm_projects.push("bolt");
347351
// Relocations are required for BOLT to work.
348352
ldflags.push_all("-Wl,-q");
349353
}
@@ -403,8 +407,6 @@ impl Step for Llvm {
403407
cfg.define("LLVM_BUILD_32_BITS", "ON");
404408
}
405409

406-
let mut enabled_llvm_projects = Vec::new();
407-
408410
if util::forcing_clang_based_tests() {
409411
enabled_llvm_projects.push("clang");
410412
enabled_llvm_projects.push("compiler-rt");
@@ -828,7 +830,7 @@ impl Step for Lld {
828830
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
829831
// Find clang's runtime library directory and push that as a search path to the
830832
// cmake linker flags.
831-
let clang_rt_dir = get_clang_rt_dir(clang_cl_path);
833+
let clang_rt_dir = get_clang_rt_dir(clang_cl_path, true);
832834
ldflags.push_all(&format!("/libpath:{}", clang_rt_dir.display()));
833835
}
834836
}

src/bootstrap/util.rs

+9-19
Original file line numberDiff line numberDiff line change
@@ -488,32 +488,22 @@ fn absolute_windows(path: &std::path::Path) -> std::io::Result<std::path::PathBu
488488
}
489489
}
490490

491-
/// Adapted from https://github.com/llvm/llvm-project/blob/782e91224601e461c019e0a4573bbccc6094fbcd/llvm/cmake/modules/HandleLLVMOptions.cmake#L1058-L1079
492-
///
493-
/// When `clang-cl` is used with instrumentation, we need to add clang's runtime library resource
494-
/// directory to the linker flags, otherwise there will be linker errors about the profiler runtime
495-
/// missing. This function returns the path to that directory.
496491
pub fn get_clang_rt_dir(clang: impl AsRef<Path>, is_msvc: bool) -> PathBuf {
497-
// Similar to how LLVM does it, to find clang's library runtime directory:
498-
// - we ask `clang-cl` to locate the `clang_rt.builtins` lib.
499-
let mut builtins_locator = Command::new(clang.as_ref());
492+
let mut cmd = Command::new(clang.as_ref());
500493
if is_msvc {
501-
builtins_locator.args(&["/clang:-print-libgcc-file-name", "/clang:--rtlib=compiler-rt"]);
494+
cmd.args(&["/clang:-print-runtime-dir"]);
502495
} else {
503-
builtins_locator.args(&["-print-libgcc-file-name", "-rtlib=compiler-rt"]);
496+
cmd.args(&["-print-runtime-dir"]);
504497
};
505498

506-
let clang_rt_builtins = output(&mut builtins_locator);
507-
let clang_rt_builtins = Path::new(clang_rt_builtins.trim());
499+
let dir = output(&mut cmd);
500+
let dir = PathBuf::from(dir.trim());
508501
assert!(
509-
clang_rt_builtins.exists(),
510-
"`clang-cl` must correctly locate the library runtime directory"
502+
dir.exists(),
503+
"`{}` must correctly locate the library runtime directory",
504+
clang.as_ref().display()
511505
);
512-
513-
// - the profiler runtime will be located in the same directory as the builtins lib, like
514-
// `$LLVM_DISTRO_ROOT/lib/clang/$LLVM_VERSION/lib/windows`.
515-
let clang_rt_dir = clang_rt_builtins.parent().expect("The clang lib folder should exist");
516-
clang_rt_dir.to_path_buf()
506+
dir
517507
}
518508

519509
pub fn lld_flag_no_threads(is_windows: bool) -> &'static str {

src/ci/github-actions/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ jobs:
491491

492492
- name: dist-x86_64-apple
493493
env:
494-
SCRIPT: ./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin
494+
SCRIPT: PGO_HOST=x86_64-apple-darwin python3 src/ci/stage-build.py python3 ./x.py dist bootstrap --include-default-paths --host=x86_64-apple-darwin --target=x86_64-apple-darwin
495495
RUST_CONFIGURE_ARGS: --enable-full-tools --enable-sanitizers --enable-profiler --set rust.jemalloc --set llvm.ninja=false --set rust.lto=thin
496496
RUSTC_RETRY_LINKER_ON_SEGFAULT: 1
497497
MACOSX_DEPLOYMENT_TARGET: 10.7

src/ci/scripts/install-clang.sh

+9-4
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,15 @@ if isMacOS; then
1818
if [[ ${USE_XCODE_CLANG-0} -eq 1 ]]; then
1919
bindir="$(xcode-select --print-path)/Toolchains/XcodeDefault.xctoolchain/usr/bin"
2020
else
21-
file="${MIRRORS_BASE}/clang%2Bllvm-${LLVM_VERSION}-x86_64-apple-darwin.tar.xz"
22-
retry curl -f "${file}" -o "clang+llvm-${LLVM_VERSION}-x86_64-apple-darwin.tar.xz"
23-
tar xJf "clang+llvm-${LLVM_VERSION}-x86_64-apple-darwin.tar.xz"
24-
bindir="$(pwd)/clang+llvm-${LLVM_VERSION}-x86_64-apple-darwin/bin"
21+
OSX_ARCH=$(uname -m)
22+
file="${MIRRORS_BASE}/clang%2Bllvm-${LLVM_VERSION}-${OSX_ARCH}-apple-darwin.tar.xz"
23+
retry curl -f "${file}" -o "clang+llvm-${LLVM_VERSION}-${OSX_ARCH}-apple-darwin.tar.xz"
24+
25+
mkdir -p citools && cd citools
26+
27+
tar xJf "clang+llvm-${LLVM_VERSION}-${OSX_ARCH}-apple-darwin.tar.xz"
28+
mv "clang+llvm-${LLVM_VERSION}-${OSX_ARCH}-apple-darwin" clang-rust
29+
bindir="$(pwd)/clang-rust/bin"
2530
fi
2631

2732
ciCommandSetEnv CC "${bindir}/clang"

src/ci/stage-build.py

+43
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,47 @@ def rustc_profile_template_path(self) -> Path:
207207
def supports_bolt(self) -> bool:
208208
return False
209209

210+
class DarwinPipeline(Pipeline):
211+
def __init__(self):
212+
self.checkout_dir = Path(os.getcwd())
213+
214+
def checkout_path(self) -> Path:
215+
return self.checkout_dir
216+
217+
def downloaded_llvm_dir(self) -> Path:
218+
return self.checkout_path() / "citools" / "clang-rust"
219+
220+
def build_root(self) -> Path:
221+
return self.checkout_path()
222+
223+
def opt_artifacts(self) -> Path:
224+
return Path("/tmp/tmp-multistage/opt-artifacts")
225+
226+
def build_rustc_perf(self):
227+
# rustc-perf version from 2022-07-22
228+
perf_commit = "9dfaa35193154b690922347ee1141a06ec87a199"
229+
rustc_perf_zip_path = self.opt_artifacts() / "perf.zip"
230+
231+
def download_rustc_perf():
232+
download_file(
233+
f"https://github.com/rust-lang/rustc-perf/archive/{perf_commit}.zip",
234+
rustc_perf_zip_path
235+
)
236+
with change_cwd(self.opt_artifacts()):
237+
unpack_archive(rustc_perf_zip_path)
238+
move_path(Path(f"rustc-perf-{perf_commit}"), self.rustc_perf_dir())
239+
delete_file(rustc_perf_zip_path)
240+
241+
retry_action(download_rustc_perf, "Download rustc-perf")
242+
243+
with change_cwd(self.rustc_perf_dir()):
244+
cmd([self.cargo_stage_0(), "build", "-p", "collector"], env=dict(
245+
RUSTC=str(self.rustc_stage_0()),
246+
RUSTC_BOOTSTRAP="1"
247+
))
248+
249+
def supports_bolt(self) -> bool:
250+
return False
210251

211252
def get_timestamp() -> float:
212253
return time.time()
@@ -576,6 +617,8 @@ def create_pipeline() -> Pipeline:
576617
return LinuxPipeline()
577618
elif sys.platform in ("cygwin", "win32"):
578619
return WindowsPipeline()
620+
elif sys.platform == "darwin":
621+
return DarwinPipeline()
579622
else:
580623
raise Exception(f"Optimized build is not supported for platform {sys.platform}")
581624

0 commit comments

Comments
 (0)