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 1 commit 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/lib -lclang_rt.profile_osx");

}

}

// The config can also specify its own llvm linker flags.
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
32 changes: 29 additions & 3 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,
}
}

fn is_try_build() -> bool {
Expand Down Expand Up @@ -191,6 +195,26 @@ 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![])
.build()?;

(env, shared.build_args)
}
};
Ok((env, args))
}
Expand Down Expand Up @@ -250,6 +274,9 @@ fn execute_pipeline(
// Here we build a PGO instrumented LLVM, reusing the previously PGO optimized rustc.
// Then we use the instrumented LLVM to gather LLVM PGO profiles.
let llvm_pgo_profile = timer.section("Stage 2 (LLVM PGO)", |stage| {
if 1 + 1 == 2 {
return Err(anyhow::anyhow!("disabled"));
}
// Remove the previous, uninstrumented build of LLVM.
clear_llvm_files(env)?;

Expand All @@ -272,7 +299,7 @@ fn execute_pipeline(
clear_llvm_files(env)?;

Ok(profile)
})?;
});

let bolt_profiles = if env.use_bolt() {
// Stage 3: Build BOLT instrumented LLVM
Expand All @@ -284,7 +311,7 @@ fn execute_pipeline(
stage.section("Build PGO optimized LLVM", |stage| {
Bootstrap::build(env)
.with_llvm_bolt_ldflags()
.llvm_pgo_optimize(&llvm_pgo_profile)
.llvm_pgo_optimize(&llvm_pgo_profile?)
.avoid_rustc_rebuild()
.run(stage)
})?;
Expand Down Expand Up @@ -336,7 +363,6 @@ fn execute_pipeline(
};

let mut dist = Bootstrap::dist(env, &dist_args)
.llvm_pgo_optimize(&llvm_pgo_profile)
.rustc_pgo_optimize(&rustc_pgo_profile)
.avoid_rustc_rebuild();

Expand Down
Loading