]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_cranelift/build_system/bench.rs
Auto merge of #106938 - GuillaumeGomez:normalize-projection-field-ty, r=oli-obk
[rust.git] / compiler / rustc_codegen_cranelift / build_system / bench.rs
1 use std::env;
2 use std::fs;
3 use std::path::Path;
4
5 use super::path::{Dirs, RelPath};
6 use super::prepare::GitRepo;
7 use super::rustc_info::get_file_name;
8 use super::utils::{hyperfine_command, is_ci, spawn_and_wait, CargoProject, Compiler};
9
10 pub(crate) static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github(
11     "ebobby",
12     "simple-raytracer",
13     "804a7a21b9e673a482797aa289a18ed480e4d813",
14     "<none>",
15 );
16
17 // Use a separate target dir for the initial LLVM build to reduce unnecessary recompiles
18 pub(crate) static SIMPLE_RAYTRACER_LLVM: CargoProject =
19     CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer_llvm");
20
21 pub(crate) static SIMPLE_RAYTRACER: CargoProject =
22     CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");
23
24 pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
25     benchmark_simple_raytracer(dirs, bootstrap_host_compiler);
26 }
27
28 fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
29     if std::process::Command::new("hyperfine").output().is_err() {
30         eprintln!("Hyperfine not installed");
31         eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
32         std::process::exit(1);
33     }
34
35     eprintln!("[LLVM BUILD] simple-raytracer");
36     let build_cmd = SIMPLE_RAYTRACER_LLVM.build(bootstrap_host_compiler, dirs);
37     spawn_and_wait(build_cmd);
38     fs::copy(
39         SIMPLE_RAYTRACER_LLVM
40             .target_dir(dirs)
41             .join(&bootstrap_host_compiler.triple)
42             .join("debug")
43             .join(get_file_name("main", "bin")),
44         RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")),
45     )
46     .unwrap();
47
48     let run_runs = env::var("RUN_RUNS")
49         .unwrap_or(if is_ci() { "2" } else { "10" }.to_string())
50         .parse()
51         .unwrap();
52
53     eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
54     let cargo_clif =
55         RelPath::DIST.to_path(dirs).join(get_file_name("cargo_clif", "bin").replace('_', "-"));
56     let manifest_path = SIMPLE_RAYTRACER.manifest_path(dirs);
57     let target_dir = SIMPLE_RAYTRACER.target_dir(dirs);
58
59     let clean_cmd = format!(
60         "cargo clean --manifest-path {manifest_path} --target-dir {target_dir}",
61         manifest_path = manifest_path.display(),
62         target_dir = target_dir.display(),
63     );
64     let llvm_build_cmd = format!(
65         "cargo build --manifest-path {manifest_path} --target-dir {target_dir}",
66         manifest_path = manifest_path.display(),
67         target_dir = target_dir.display(),
68     );
69     let clif_build_cmd = format!(
70         "{cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}",
71         cargo_clif = cargo_clif.display(),
72         manifest_path = manifest_path.display(),
73         target_dir = target_dir.display(),
74     );
75
76     let bench_compile =
77         hyperfine_command(1, run_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd);
78
79     spawn_and_wait(bench_compile);
80
81     eprintln!("[BENCH RUN] ebobby/simple-raytracer");
82     fs::copy(
83         target_dir.join("debug").join(get_file_name("main", "bin")),
84         RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_clif", "bin")),
85     )
86     .unwrap();
87
88     let mut bench_run = hyperfine_command(
89         0,
90         run_runs,
91         None,
92         Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(),
93         Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(),
94     );
95     bench_run.current_dir(RelPath::BUILD.to_path(dirs));
96     spawn_and_wait(bench_run);
97 }