Skip to content
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

test(pgo): ensure PGO works #14859

Merged
merged 1 commit into from
Nov 26, 2024
Merged
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
1 change: 1 addition & 0 deletions tests/testsuite/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ mod package_features;
mod patch;
mod path;
mod paths;
mod pgo;
mod pkgid;
mod precise_pre_release;
mod proc_macro;
Expand Down
111 changes: 111 additions & 0 deletions tests/testsuite/pgo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
//! Test if PGO works.

use std::path::PathBuf;
use std::process::Command;

use cargo_test_support::prelude::*;
use cargo_test_support::project;
use cargo_test_support::str;

fn llvm_profdata() -> Option<PathBuf> {
let output = Command::new("rustc")
.arg("--print=target-libdir")
.output()
.expect("rustc to run");
assert!(output.status.success());
let mut libdir = PathBuf::from(String::from_utf8(output.stdout).unwrap());
assert!(libdir.pop());
let mut bin = libdir.join("bin").join("llvm-profdata");
bin.exists().then(|| bin.clone()).or_else(|| {
bin.set_extension("exe");
bin.exists().then_some(bin)
})
}

#[cargo_test]
fn pgo_works() {
if cfg!(not(target_os = "linux")) {
// macOS may emit different LLVM PGO warnings.
// Windows LLVM has different requirements.
return;
}
Comment on lines +25 to +31
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you make sure to use the ignore attribute when disabling a test? Otherwise it can be confusing when tests don't seem to change when you expect them to, are say that they are passing when you expect them to fail.

I realize there's still a big hole when llvm-profdata is missing. We could maybe add that to our requires mechanism?

Suggested change
#[cargo_test]
fn pgo_works() {
if cfg!(not(target_os = "linux")) {
// macOS may emit different LLVM PGO warnings.
// Windows LLVM has different requirements.
return;
}
#[cargo_test]
// macOS may emit different LLVM PGO warnings.
// Windows LLVM has different requirements.
#[cfg_attr(not(target_os = "linux"), ignore = "linux only")]
fn pgo_works() {


let Some(llvm_profdata) = llvm_profdata() else {
return;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be possible to enforce that this exists in CI? Otherwise we don't know if this test is ever actually running.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I've considered that and found it needs more work than the current one.

I'll try to improve it!

};

let p = project()
.file(
"Cargo.toml",
r#"
[package]
name = "foo"
edition = "2021"
"#,
)
.file(
"src/main.rs",
r#"
fn fibonacci(n: u64) -> u64 {
match n {
0 => 0,
1 => 1,
_ => fibonacci(n - 1) + fibonacci(n - 2),
}
}

fn main() {
for i in [15, 20, 25] {
let _ = fibonacci(i);
}
}
"#,
)
.build();

let target_dir = p.build_dir();
let release_bin = target_dir.join("release").join("foo");
let pgo_data_dir = target_dir.join("pgo-data");
let profdata_path = target_dir.join("merged.profdata");

// Build the instrumented binary
p.cargo("build --release")
.env(
"RUSTFLAGS",
format!("-Cprofile-generate={}", pgo_data_dir.display()),
)
.run();
// Run the instrumented binary
cargo_test_support::execs()
.with_process_builder(cargo_test_support::process(release_bin))
.run();

cargo_test_support::process(llvm_profdata)
.arg("merge")
.arg("-o")
.arg(&profdata_path)
.arg(pgo_data_dir)
.status()
.unwrap();

// Use merged profdata during optimization.
//
// -Cllvm-args=-pgo-warn-missing-function is essential.
// If there are LLVM warnings, there might be something wrong.
p.cargo("build --release -v")
.env(
"RUSTFLAGS",
format!(
"-Cprofile-use={} -Cllvm-args=-pgo-warn-missing-function",
profdata_path.display()
),
)
.with_stderr_data(str![[r#"
[DIRTY] foo v0.0.0 ([ROOT]/foo): the rustflags changed
[COMPILING] foo v0.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]-Cprofile-use=[ROOT]/foo/target/merged.profdata -Cllvm-args=-pgo-warn-missing-function`
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s

"#]])
.run();
}