Skip to content

Commit 4bfa4ee

Browse files
committed
WIP: Add PGO for apple darwin targets
1 parent 50aa041 commit 4bfa4ee

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/bootstrap/src/core/build_steps/compile.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1388,12 +1388,19 @@ fn rustc_llvm_env(builder: &Builder<'_>, cargo: &mut Cargo, target: TargetSelect
13881388
// found. This is to avoid the linker errors about undefined references to
13891389
// `__llvm_profile_instrument_memop` when linking `rustc_driver`.
13901390
let mut llvm_linker_flags = String::new();
1391-
if builder.config.llvm_profile_generate && target.is_msvc() {
1392-
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
1393-
// Add clang's runtime library directory to the search path
1394-
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1395-
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1391+
if builder.config.llvm_profile_generate {
1392+
if target.is_msvc() {
1393+
if let Some(ref clang_cl_path) = builder.config.llvm_clang_cl {
1394+
// Add clang's runtime library directory to the search path
1395+
let clang_rt_dir = get_clang_cl_resource_dir(builder, clang_cl_path);
1396+
llvm_linker_flags.push_str(&format!("-L{}", clang_rt_dir.display()));
1397+
}
1398+
} else {
1399+
1400+
llvm_linker_flags.push_str("-L/opt/homebrew/Cellar/llvm/20.1.2/lib/clang/20/lib/darwin/lib -lclang_rt.profile_osx");
1401+
13961402
}
1403+
13971404
}
13981405

13991406
// The config can also specify its own llvm linker flags.

src/ci/github-actions/jobs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -425,7 +425,7 @@ auto:
425425

426426
- name: dist-aarch64-apple
427427
env:
428-
SCRIPT: ./x.py dist bootstrap --include-default-paths --host=aarch64-apple-darwin --target=aarch64-apple-darwin
428+
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
429429
RUST_CONFIGURE_ARGS: >-
430430
--enable-full-tools
431431
--enable-sanitizers

src/tools/opt-dist/src/main.rs

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ enum EnvironmentCmd {
109109
#[clap(flatten)]
110110
shared: SharedArgs,
111111
},
112+
MacCi {
113+
#[clap(flatten)]
114+
shared: SharedArgs,
115+
}
112116
}
113117

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

198202
(env, shared.build_args)
199203
}
204+
EnvironmentCmd::MacCi { shared } => {
205+
let target_triple =
206+
std::env::var("PGO_HOST").expect("PGO_HOST environment variable missing");
207+
208+
let checkout_dir: Utf8PathBuf = std::env::current_dir()?.try_into()?;
209+
let env = EnvironmentBuilder::default()
210+
.host_tuple(target_triple)
211+
.python_binary("python3".to_string())
212+
.checkout_dir(checkout_dir.clone())
213+
.host_llvm_dir("/opt/homebrew/Cellar/llvm/20.1.2".into())
214+
.artifact_dir(checkout_dir.join("opt-artifacts"))
215+
.build_dir(checkout_dir)
216+
.shared_llvm(false)
217+
.use_bolt(false)
218+
.run_tests(false)
219+
.skipped_tests(vec![])
220+
.build()?;
221+
222+
(env, shared.build_args)
223+
}
200224
};
201225
Ok((env, args))
202226
}
@@ -256,6 +280,9 @@ fn execute_pipeline(
256280
// Here we build a PGO instrumented LLVM, reusing the previously PGO optimized rustc.
257281
// Then we use the instrumented LLVM to gather LLVM PGO profiles.
258282
let llvm_pgo_profile = timer.section("Stage 2 (LLVM PGO)", |stage| {
283+
if 1 + 1 == 2 {
284+
return Err(anyhow::anyhow!("disabled"));
285+
}
259286
// Remove the previous, uninstrumented build of LLVM.
260287
clear_llvm_files(env)?;
261288

@@ -278,7 +305,7 @@ fn execute_pipeline(
278305
clear_llvm_files(env)?;
279306

280307
Ok(profile)
281-
})?;
308+
});
282309

283310
let bolt_profiles = if env.use_bolt() {
284311
// Stage 3: Build BOLT instrumented LLVM
@@ -290,7 +317,7 @@ fn execute_pipeline(
290317
stage.section("Build PGO optimized LLVM", |stage| {
291318
Bootstrap::build(env)
292319
.with_llvm_bolt_ldflags()
293-
.llvm_pgo_optimize(&llvm_pgo_profile)
320+
.llvm_pgo_optimize(&llvm_pgo_profile?)
294321
.avoid_rustc_rebuild()
295322
.run(stage)
296323
})?;
@@ -342,7 +369,6 @@ fn execute_pipeline(
342369
};
343370

344371
let mut dist = Bootstrap::dist(env, &dist_args)
345-
.llvm_pgo_optimize(&llvm_pgo_profile)
346372
.rustc_pgo_optimize(&rustc_pgo_profile)
347373
.avoid_rustc_rebuild();
348374

0 commit comments

Comments
 (0)