Skip to content

WIP: Add PGO for apple darwin targets #140699

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

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
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
17 changes: 12 additions & 5 deletions src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1388,12 +1388,19 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
// found. This is to avoid the linker errors about undefined references to
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
let mut llvm_linker_flags = String::new();
if builder.config.llvm_profile_generate && target.is_msvc() {
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
// Add clang's runtime library directory to the search path
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
if builder.config.llvm_profile_generate {
if target.is_msvc() {
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
// Add clang's runtime library directory to the search path
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
}
} else {

llvm_linker_flags.push_str("-L/opt/homebrew/Cellar/llvm/20.1.2/lib/clang/20/lib/darwin -lclang_rt.profile_osx");

}

}

// The config can also specify its own llvm linker flags.
Expand Down
17 changes: 11 additions & 6 deletions src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1037,13 +1037,18 @@ impl Step for Lld {
// when doing PGO on CI, cmake or clang-cl don't automatically link clang's
// profiler runtime in. In that case, we need to manually ask cmake to do it, to avoid
// linking errors, much like LLVM's cmake setup does in that situation.
if builder.config.llvm_profile_generate && target.is_msvc() {
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
// Find clang's runtime library directory and push that as a search path to the
// cmake linker flags.
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
if builder.config.llvm_profile_generate {
if target.is_msvc() {
if let Some(clang_cl_path) = builder.config.llvm_clang_cl.as_ref() {
// Find clang's runtime library directory and push that as a search path to the
// cmake linker flags.
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
ldflags.push_all(format!("/libpath:{}", clang_rt_dir.display()));
}
} else {
ldflags.push_all("-L/opt/homebrew/Cellar/llvm/20.1.2/lib/clang/20/lib/darwin -lclang_rt.profile_osx");
}

}

// LLD is built as an LLVM tool, but is distributed outside of the `llvm-tools` component,
Expand Down
2 changes: 1 addition & 1 deletion src/ci/github-actions/jobs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ auto:

- name: dist-aarch64-apple
env:
SCRIPT: ./x.py dist bootstrap --include-default-paths --host=aarch64-apple-darwin --target=aarch64-apple-darwin
SCRIPT: ./x.py build --set rust.debug=true opt-dist && PGO_HOST=aarch64-apple-darwin ./build/aarch64-apple-darwin/stage0-tools-bin/opt-dist mac-ci -- python3 x.py dist bootstrap --include-default-paths
RUST_CONFIGURE_ARGS: >-
--enable-full-tools
--enable-sanitizers
Expand Down
25 changes: 25 additions & 0 deletions src/tools/opt-dist/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ enum EnvironmentCmd {
#[clap(flatten)]
shared: SharedArgs,
},
MacCi {
#[clap(flatten)]
shared: SharedArgs,
}
}

/// For a fast try build, we want to only build the bare minimum of components to get a
Expand Down Expand Up @@ -197,6 +201,27 @@ fn create_environment(args: Args) -> anyhow::Result<(Environment, Vec<String>)>

(env, shared.build_args)
}
EnvironmentCmd::MacCi { shared } => {
let target_triple =
std::env::var("PGO_HOST").expect("PGO_HOST environment variable missing");

let checkout_dir: Utf8PathBuf = std::env::current_dir()?.try_into()?;
let env = EnvironmentBuilder::default()
.host_tuple(target_triple)
.python_binary("python3".to_string())
.checkout_dir(checkout_dir.clone())
.host_llvm_dir("/opt/homebrew/Cellar/llvm/20.1.2".into())
.artifact_dir(checkout_dir.join("opt-artifacts"))
.build_dir(checkout_dir)
.shared_llvm(false)
.use_bolt(false)
.run_tests(false)
.skipped_tests(vec![])
.fast_try_build(is_fast_try_build)
.build()?;

(env, shared.build_args)
}
};
Ok((env, args))
}
Expand Down
Loading