forked from rust-lang/cargo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
//! Test if PGO works. | ||
|
||
use cargo_test_support::prelude::*; | ||
use cargo_test_support::project; | ||
use cargo_test_support::str; | ||
|
||
#[cargo_test(requires = "llvm-profdata")] | ||
fn pgo_works() { | ||
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() { | ||
let inputs = [2, 5, 8, 11, 14, 17, 20]; | ||
for input in inputs { | ||
let result = fibonacci(input); | ||
println!("{result}"); | ||
} | ||
} | ||
"#, | ||
) | ||
.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)) | ||
.with_stdout_data(str![[r#" | ||
1 | ||
5 | ||
21 | ||
89 | ||
377 | ||
1597 | ||
6765 | ||
"#]]) | ||
.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. | ||
p.cargo("build --release -v") | ||
.env( | ||
"RUSTFLAGS", | ||
format!( | ||
"-Cprofile-use={} -Cllvm-args=-pgo-warn-missing-function", | ||
profdata_path.display() | ||
), | ||
) | ||
.with_stderr_data(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 | ||
"#]]) | ||
.run(); | ||
} |