Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Nov 26, 2024
1 parent 536ae6e commit 9e7576f
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 2 deletions.
4 changes: 3 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,9 @@ jobs:
- name: Configure extra test environment
run: echo CARGO_CONTAINER_TESTS=1 >> $GITHUB_ENV
if: matrix.os == 'ubuntu-latest'
- run: cargo test -p cargo
- run: |
export PATH="$(basename $(rustc --print target-libdir)/bin):$PATH"
cargo test -p cargo -- pgo
- name: Clear intermediate test output
run: ci/clean-test-output.sh

Expand Down
17 changes: 16 additions & 1 deletion crates/cargo-test-macro/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,18 @@ pub fn cargo_test(attr: TokenStream, item: TokenStream) -> TokenStream {
let command = &s[9..];
set_ignore!(!has_command(command), "{command} not installed");
}
s if s.starts_with("requires=") => {
let command = &s[9..];
let Ok(command) = command.parse::<Literal>() else {
panic!("expected a string literal for `requires`, got: {command}");
};
let command = command.to_string();
let Some(command) = command.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
else {
panic!("expected a string literal for `requires`, got: {command}");
};
set_ignore!(!has_command(command), "{command} not installed");
}
s if s.starts_with(">=1.") => {
requires_reason = true;
let min_minor = s[4..].parse().unwrap();
Expand Down Expand Up @@ -274,13 +286,16 @@ fn check_command(command_path: &Path, args: &[&str]) -> bool {
// environments like Docker. Consider installing it if Cargo
// gains more hg support, but otherwise it isn't critical.
// * lldb is not pre-installed on Ubuntu and Windows, so skip.
if is_ci() && !matches!(command_name.as_str(), "hg" | "lldb") {
if is_ci() && !matches!(command_name.as_str(), "hg" | "lldb" | "lldb-profdata") {
panic!("expected command `{command_name}` to be somewhere in PATH: {e}",);
}
return false;
}
};
if !output.status.success() {
if command_name.as_str() == "llvm-profdata" {
return true;
}
panic!(
"expected command `{command_name}` to be runnable, got error {}:\n\
stderr:{}\n\
Expand Down
1 change: 1 addition & 0 deletions src/cargo/core/compiler/build_runner/compilation_files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,7 @@ fn compute_metadata(
let c_metadata = UnitHash(c_metadata_hasher.finish());
let c_extra_filename = UnitHash(c_extra_filename_hasher.finish());
let unit_id = c_extra_filename;
// let c_metadata = c_extra_filename;

let c_extra_filename = use_extra_filename.then_some(c_extra_filename);

Expand Down
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
115 changes: 115 additions & 0 deletions tests/testsuite/pgo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
//! 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() {
let Some(llvm_profdata) = llvm_profdata() else {
return;
};

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();

#[cfg(target_os = "macos")]
let stderr = str![[r#"
[COMPILING] foo v0.0.0 ([ROOT]/foo)
[RUNNING] `rustc [..]-Cprofile-use=[ROOT]/foo/target/merged.profdata -Cllvm-args=-pgo-warn-missing-function`
[WARNING] [..] no profile data available for function [..]
[WARNING] `foo` (bin "foo") generated 1 warning
[FINISHED] `release` profile [optimized] target(s) in [ELAPSED]s
"#]];

#[cfg(not(target_os = "macos"))]
let stderr = str![[r#"
[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
"#]];

// Use merged profdata during optimization.
p.cargo("build --release -v")
.env(
"RUSTFLAGS",
format!(
"-Cprofile-use={} -Cllvm-args=-pgo-warn-missing-function",
profdata_path.display()
),
)
.with_stderr_data(stderr)
.run();
}

0 comments on commit 9e7576f

Please sign in to comment.