Skip to content

Commit

Permalink
Auto merge of rust-lang#126957 - Oneirical:artestic-inspiration, r=<try>
Browse files Browse the repository at this point in the history
Migrate `pgo-gen`, `pgo-use` and `profile` `run-make` tests to rmake.rs

Part of rust-lang#121876 and the associated [Google Summer of Code project](https://blog.rust-lang.org/2024/05/01/gsoc-2024-selected-projects.html).

try-job: aarch64-apple
try-job: x86_64-msvc
  • Loading branch information
bors committed Jul 3, 2024
2 parents c872a14 + a5a35d6 commit fe6c204
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 70 deletions.
29 changes: 29 additions & 0 deletions src/tools/run-make-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,35 @@ pub fn test_while_readonly<P: AsRef<Path>, F: FnOnce() + std::panic::UnwindSafe>
success.unwrap();
}

/// Browse the directory `path` non-recursively and return all files which respect the parameters
/// outlined by `closure`.
#[track_caller]
pub fn shallow_find_files<P: AsRef<Path>, F: Fn(&PathBuf) -> bool>(
path: P,
closure: F,
) -> Vec<PathBuf> {
let mut matching_files = Vec::new();
for entry in fs_wrapper::read_dir(path) {
let entry = entry.expect("failed to read directory entry.");
let path = entry.path();

if path.is_file() && closure(&path) {
matching_files.push(path);
}
}
matching_files
}

/// Returns true if the filename at `path` starts with `prefix`.
pub fn has_prefix<P: AsRef<Path>>(path: P, prefix: &str) -> bool {
path.as_ref().file_name().is_some_and(|name| name.to_str().unwrap().starts_with(prefix))
}

/// Returns true if the filename at `path` has the extension `extension`.
pub fn has_extension<P: AsRef<Path>>(path: P, extension: &str) -> bool {
path.as_ref().extension().is_some_and(|ext| ext == extension)
}

/// Use `cygpath -w` on a path to get a Windows path string back. This assumes that `cygpath` is
/// available on the platform!
#[track_caller]
Expand Down
3 changes: 0 additions & 3 deletions src/tools/tidy/src/allowed_run_make_makefiles.txt
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,10 @@ run-make/pass-non-c-like-enum-to-c/Makefile
run-make/pdb-buildinfo-cl-cmd/Makefile
run-make/pgo-gen-lto/Makefile
run-make/pgo-gen-no-imp-symbols/Makefile
run-make/pgo-gen/Makefile
run-make/pgo-indirect-call-promotion/Makefile
run-make/pgo-use/Makefile
run-make/pointer-auth-link-with-c/Makefile
run-make/print-calling-conventions/Makefile
run-make/print-target-list/Makefile
run-make/profile/Makefile
run-make/prune-link-args/Makefile
run-make/raw-dylib-alt-calling-convention/Makefile
run-make/raw-dylib-c/Makefile
Expand Down
11 changes: 0 additions & 11 deletions tests/run-make/pgo-gen/Makefile

This file was deleted.

18 changes: 18 additions & 0 deletions tests/run-make/pgo-gen/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// -C profile-generate, when used with rustc, is supposed to output
// profile files (.profraw) after running a binary to analyze how the compiler
// optimizes code. This test checks that these files are generated.
// See https://github.com/rust-lang/rust/pull/48346

//@ needs-profiler-support
//@ ignore-cross-compile

use run_make_support::{cwd, has_extension, has_prefix, run, rustc, shallow_find_files};

fn main() {
rustc().arg("-g").profile_generate(cwd()).input("test.rs").run();
run("test");
let profraw_files = shallow_find_files(cwd(), |path| {
has_prefix(path, "default") && has_extension(path, "profraw")
});
assert!(!profraw_files.is_empty(), "no .profraw file generated");
}
43 changes: 0 additions & 43 deletions tests/run-make/pgo-use/Makefile

This file was deleted.

55 changes: 55 additions & 0 deletions tests/run-make/pgo-use/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// This test makes sure that PGO profiling data leads to cold functions being
// marked as `cold` and hot functions with `inlinehint`.
// The test program contains an `if` where actual execution only ever takes the
// `else` branch. Accordingly, we expect the function that is never called to
// be marked as cold.
// See https://github.com/rust-lang/rust/pull/60262

//@ needs-profiler-support
//@ ignore-cross-compile

use run_make_support::{
cwd, fs_wrapper, has_extension, has_prefix, llvm_filecheck, llvm_profdata, run_with_args,
rustc, shallow_find_files,
};

fn main() {
// Compile the test program with instrumentation
// Disable the pre-inlining pass (i.e. a pass that does some inlining before
// it adds the profiling instrumentation). Disabling this pass leads to
// rather predictable IR which we need for this test to be stable.
rustc()
.opt_level("2")
.codegen_units(1)
.arg("-Cllvm-args=-disable-preinline")
.profile_generate(cwd())
.input("main.rs")
.run();
// Run it in order to generate some profiling data
run_with_args("main", &["some-argument"]);
// Postprocess the profiling data so it can be used by the compiler
let profraw_files = shallow_find_files(cwd(), |path| {
has_prefix(path, "default") && has_extension(path, "profraw")
});
let profraw_file = profraw_files.get(0).unwrap();
llvm_profdata().merge().output("merged.profdata").input(profraw_file).run();
// Compile the test program again, making use of the profiling data
rustc()
.opt_level("2")
.codegen_units(1)
.arg("-Cllvm-args=-disable-preinline")
.profile_use("merged.profdata")
.emit("llvm-ir")
.input("main.rs")
.run();
// Check that the generate IR contains some things that we expect.
// We feed the file into LLVM FileCheck tool *with its lines reversed* so that we see the
// line with the function name before the line with the function attributes.
// FileCheck only supports checking that something matches on the next line,
// but not if something matches on the previous line.
let ir = fs_wrapper::read_to_string("main.ll");
let lines: Vec<_> = ir.lines().rev().collect();
let mut reversed_ir = lines.join("\n");
reversed_ir.push('\n');
llvm_filecheck().patterns("filecheck-patterns.txt").stdin(reversed_ir.as_bytes()).run();
}
13 changes: 0 additions & 13 deletions tests/run-make/profile/Makefile

This file was deleted.

22 changes: 22 additions & 0 deletions tests/run-make/profile/rmake.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This test revolves around the rustc flag -Z profile, which should
// generate a .gcno file (initial profiling information) as well
// as a .gcda file (branch counters). The path where these are emitted
// should also be configurable with -Z profile-emit. This test checks
// that the files are produced, and then that the latter flag is respected.
// See https://github.com/rust-lang/rust/pull/42433

//@ ignore-cross-compile
//@ needs-profiler-support

use run_make_support::{run, rustc};
use std::path::Path;

fn main() {
rustc().arg("-g").arg("-Zprofile").input("test.rs").run();
run("test");
assert!(Path::new("test.gcno").exists(), "no .gcno file");
assert!(Path::new("test.gcda").exists(), "no .gcda file");
rustc().arg("-g").arg("-Zprofile").arg("-Zprofile-emit=abc/abc.gcda").input("test.rs").run();
run("test");
assert!(Path::new("abc/abc.gcda").exists(), "gcda file not emitted to defined path");
}

0 comments on commit fe6c204

Please sign in to comment.