Skip to content

Commit 6e27807

Browse files
committed
Auto merge of #126957 - Oneirical:artestic-inspiration, r=jieyouxu,kobzol
Migrate `pgo-gen`, `pgo-use` and `profile` `run-make` tests to rmake.rs Part of #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
2 parents 9a21ac8 + c9be69f commit 6e27807

File tree

7 files changed

+95
-70
lines changed

7 files changed

+95
-70
lines changed

src/tools/tidy/src/allowed_run_make_makefiles.txt

-3
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,10 @@ run-make/pass-non-c-like-enum-to-c/Makefile
109109
run-make/pdb-buildinfo-cl-cmd/Makefile
110110
run-make/pgo-gen-lto/Makefile
111111
run-make/pgo-gen-no-imp-symbols/Makefile
112-
run-make/pgo-gen/Makefile
113112
run-make/pgo-indirect-call-promotion/Makefile
114-
run-make/pgo-use/Makefile
115113
run-make/pointer-auth-link-with-c/Makefile
116114
run-make/print-calling-conventions/Makefile
117115
run-make/print-target-list/Makefile
118-
run-make/profile/Makefile
119116
run-make/prune-link-args/Makefile
120117
run-make/raw-dylib-alt-calling-convention/Makefile
121118
run-make/raw-dylib-c/Makefile

tests/run-make/pgo-gen/Makefile

-11
This file was deleted.

tests/run-make/pgo-gen/rmake.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// -C profile-generate, when used with rustc, is supposed to output
2+
// profile files (.profraw) after running a binary to analyze how the compiler
3+
// optimizes code. This test checks that these files are generated.
4+
// See https://github.com/rust-lang/rust/pull/48346
5+
6+
//@ needs-profiler-support
7+
//@ ignore-cross-compile
8+
9+
use run_make_support::{cwd, has_extension, has_prefix, run, rustc, shallow_find_files};
10+
11+
fn main() {
12+
rustc().arg("-g").profile_generate(cwd()).input("test.rs").run();
13+
run("test");
14+
let profraw_files = shallow_find_files(cwd(), |path| {
15+
has_prefix(path, "default") && has_extension(path, "profraw")
16+
});
17+
assert!(!profraw_files.is_empty(), "no .profraw file generated");
18+
}

tests/run-make/pgo-use/Makefile

-43
This file was deleted.

tests/run-make/pgo-use/rmake.rs

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// This test makes sure that PGO profiling data leads to cold functions being
2+
// marked as `cold` and hot functions with `inlinehint`.
3+
// The test program contains an `if` where actual execution only ever takes the
4+
// `else` branch. Accordingly, we expect the function that is never called to
5+
// be marked as cold.
6+
// See https://github.com/rust-lang/rust/pull/60262
7+
8+
//@ needs-profiler-support
9+
//@ ignore-cross-compile
10+
11+
use run_make_support::{
12+
cwd, fs_wrapper, has_extension, has_prefix, llvm_filecheck, llvm_profdata, run_with_args,
13+
rustc, shallow_find_files,
14+
};
15+
16+
fn main() {
17+
// Compile the test program with instrumentation
18+
// Disable the pre-inlining pass (i.e. a pass that does some inlining before
19+
// it adds the profiling instrumentation). Disabling this pass leads to
20+
// rather predictable IR which we need for this test to be stable.
21+
rustc()
22+
.opt_level("2")
23+
.codegen_units(1)
24+
.arg("-Cllvm-args=-disable-preinline")
25+
.profile_generate(cwd())
26+
.input("main.rs")
27+
.run();
28+
// Run it in order to generate some profiling data
29+
run_with_args("main", &["some-argument"]);
30+
// Postprocess the profiling data so it can be used by the compiler
31+
let profraw_files = shallow_find_files(cwd(), |path| {
32+
has_prefix(path, "default") && has_extension(path, "profraw")
33+
});
34+
let profraw_file = profraw_files.get(0).unwrap();
35+
llvm_profdata().merge().output("merged.profdata").input(profraw_file).run();
36+
// Compile the test program again, making use of the profiling data
37+
rustc()
38+
.opt_level("2")
39+
.codegen_units(1)
40+
.arg("-Cllvm-args=-disable-preinline")
41+
.profile_use("merged.profdata")
42+
.emit("llvm-ir")
43+
.input("main.rs")
44+
.run();
45+
// Check that the generate IR contains some things that we expect.
46+
// We feed the file into LLVM FileCheck tool *with its lines reversed* so that we see the
47+
// line with the function name before the line with the function attributes.
48+
// FileCheck only supports checking that something matches on the next line,
49+
// but not if something matches on the previous line.
50+
let ir = fs_wrapper::read_to_string("main.ll");
51+
let lines: Vec<_> = ir.lines().rev().collect();
52+
let mut reversed_ir = lines.join("\n");
53+
reversed_ir.push('\n');
54+
llvm_filecheck().patterns("filecheck-patterns.txt").stdin(reversed_ir.as_bytes()).run();
55+
}

tests/run-make/profile/Makefile

-13
This file was deleted.

tests/run-make/profile/rmake.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test revolves around the rustc flag -Z profile, which should
2+
// generate a .gcno file (initial profiling information) as well
3+
// as a .gcda file (branch counters). The path where these are emitted
4+
// should also be configurable with -Z profile-emit. This test checks
5+
// that the files are produced, and then that the latter flag is respected.
6+
// See https://github.com/rust-lang/rust/pull/42433
7+
8+
//@ ignore-cross-compile
9+
//@ needs-profiler-support
10+
11+
use run_make_support::{run, rustc};
12+
use std::path::Path;
13+
14+
fn main() {
15+
rustc().arg("-g").arg("-Zprofile").input("test.rs").run();
16+
run("test");
17+
assert!(Path::new("test.gcno").exists(), "no .gcno file");
18+
assert!(Path::new("test.gcda").exists(), "no .gcda file");
19+
rustc().arg("-g").arg("-Zprofile").arg("-Zprofile-emit=abc/abc.gcda").input("test.rs").run();
20+
run("test");
21+
assert!(Path::new("abc/abc.gcda").exists(), "gcda file not emitted to defined path");
22+
}

0 commit comments

Comments
 (0)