|
| 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 | +} |
0 commit comments