Skip to content

Commit ef1a0e9

Browse files
committed
create helper stdin fn
1 parent df0caf7 commit ef1a0e9

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

src/tools/run-make-support/src/llvm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ crate::impl_common_helpers!(LlvmReadobj);
3434
pub fn llvm_bin_dir() -> PathBuf {
3535
let llvm_bin_dir = env::var("LLVM_BIN_DIR")
3636
.expect("`LLVM_BIN_DIR` not specified, but this is required to find `llvm-readobj`");
37-
PathBuf::from(llvm_bin_dir);
37+
PathBuf::from(llvm_bin_dir)
3838
}
3939

4040
impl LlvmReadobj {

src/tools/run-make-support/src/run.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,27 @@ fn run_common(name: &str) -> (Command, Output) {
3737
(cmd, output)
3838
}
3939

40+
/// Run a command taking in standard input from a file, and passing an argument
41+
/// referring to a different file.
42+
pub fn stdin_command<P: AsRef<Path>>(command: &str, stdin: P, argument: P) {
43+
let file = File::open(stdin).unwrap();
44+
let reader = BufReader::new(file);
45+
46+
let mut child = Command::new(command)
47+
.stdin(std::process::Stdio::piped())
48+
.stdout(std::process::Stdio::piped())
49+
.stderr(std::process::Stdio::piped())
50+
.arg(argument)
51+
.spawn()
52+
.unwrap();
53+
54+
let mut child_stdin = child.stdin.take().unwrap();
55+
child_stdin.write_all(reader.bytes().collect::<Vec<_>>().as_slice()).unwrap();
56+
child_stdin.flush().unwrap();
57+
58+
child.wait_with_output().unwrap();
59+
}
60+
4061
/// Run a built binary and make sure it succeeds.
4162
#[track_caller]
4263
pub fn run(name: &str) -> Output {

tests/run-make/pgo-branch-weights/rmake.rs

Lines changed: 27 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -14,61 +14,51 @@
1414
//@ ignore-windows-gnu
1515
//@ ignore-cross-compile
1616

17-
use run_make_support::{llvm_profdata, rustc, rustdoc, target, tmp_dir};
17+
use run_make_support::{llvm_profdata, rustc, rustdoc, stdin_command, target, tmp_dir};
1818
use std::io::{self, Write};
1919
use std::process::Command;
2020

2121
fn main() {
2222
// For some very small programs GNU ld seems to not properly handle
2323
// instrumentation sections correctly. Neither Gold nor LLD have that
2424
// problem.
25-
let common_flags = if cfg!(target_os = "linux") && target().contains("x86") {
26-
"-Clink-args=-fuse-ld=gold"
27-
} else {
28-
""
29-
};
25+
let link_args = (cfg!(target_os = "linux") && target().contains("x86")).then_some("-Clink-args=-fuse-ld=gold");
26+
3027
let path_prof_data_dir = tmp_dir().join("prof_data_dir");
3128
let path_merged_profdata = path_prof_data_dir.join("merged.profdata");
32-
rustc().arg(common_flags).input("opaque.rs").run();
29+
let invoc = rustc().input("opaque.rs");
30+
if let Some(link_args) = link_args {
31+
invoc.arg(link_args);
32+
}
33+
invoc.run();
3334
fs::create_dir_all(&path_prof_data_dir);
34-
rustc().arg(common_flags)
35-
.input("interesting.rs")
35+
let invoc = rustc().input("interesting.rs")
3636
.profile_generate(&path_prof_data_dir)
3737
.opt()
38-
.codegen_units(1)
39-
.run();
40-
rustc().arg(common_flags)
41-
.input("main.rs")
38+
.codegen_units(1);
39+
if let Some(link_args) = link_args {
40+
invoc.arg(link_args);
41+
}
42+
invoc.run();
43+
let invoc = rustc().input("main.rs")
4244
.profile_generate(&path_prof_data_dir)
43-
.opt()
44-
.run();
45+
.opt();
46+
if let Some(link_args) = link_args {
47+
invoc.arg(link_args);
48+
}
49+
invoc.run();
4550
run("main");
4651
llvm_profdata().merge().output(&path_merged_profdata).input(path_prof_data_dir).command_output();
47-
rustc().arg(common_flags)
48-
.input("interesting.rs")
52+
let invoc = rustc().input("interesting.rs")
4953
.profile_use(path_merged_profdata)
5054
.opt()
5155
.codegen_units(1)
52-
.emit("llvm-ir")
53-
.run();
54-
55-
// FIXME This part below is a rough attempt
56+
.emit("llvm-ir");
57+
if let Some(link_args) = link_args {
58+
invoc.arg(link_args);
59+
}
60+
invoc.run();
5661

5762
let interesting_ll = tmp_dir().join("interesting.ll");
58-
let file_interesting_ll = File::open(interesting_ll)?;
59-
let reader = BufReader::new(file_interesting_ll);
60-
61-
let mut child = Command::new("llvm-filecheck")
62-
.stdin(std::process::Stdio::piped())
63-
.stdout(std::process::Stdio::piped())
64-
.stderr(std::process::Stdio::piped())
65-
.arg("filecheck-patterns.txt")
66-
.spawn()
67-
.unwrap();
68-
69-
let mut child_stdin = child.stdin.take().unwrap();
70-
child_stdin.write_all(reader.bytes().collect::<Vec<_>>().as_slice()).unwrap();
71-
child_stdin.flush().unwrap();
72-
73-
let output = child.wait_with_output().unwrap();
63+
stdin_command("llvm-filecheck", interesting_ll, "filecheck-patterns.txt");
7464
}

0 commit comments

Comments
 (0)