summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_codegen_cranelift/build_system/bench.rs
blob: a9a851d0a8afc882e0c24cc7f60c8259c0905fbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
use std::env;
use std::fs;
use std::path::Path;

use super::path::{Dirs, RelPath};
use super::prepare::GitRepo;
use super::rustc_info::get_file_name;
use super::utils::{hyperfine_command, spawn_and_wait, CargoProject, Compiler};

static SIMPLE_RAYTRACER_REPO: GitRepo = GitRepo::github(
    "ebobby",
    "simple-raytracer",
    "804a7a21b9e673a482797aa289a18ed480e4d813",
    "<none>",
);

// Use a separate target dir for the initial LLVM build to reduce unnecessary recompiles
static SIMPLE_RAYTRACER_LLVM: CargoProject =
    CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer_llvm");

static SIMPLE_RAYTRACER: CargoProject =
    CargoProject::new(&SIMPLE_RAYTRACER_REPO.source_dir(), "simple_raytracer");

pub(crate) fn benchmark(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
    benchmark_simple_raytracer(dirs, bootstrap_host_compiler);
}

fn benchmark_simple_raytracer(dirs: &Dirs, bootstrap_host_compiler: &Compiler) {
    if std::process::Command::new("hyperfine").output().is_err() {
        eprintln!("Hyperfine not installed");
        eprintln!("Hint: Try `cargo install hyperfine` to install hyperfine");
        std::process::exit(1);
    }

    if !SIMPLE_RAYTRACER_REPO.source_dir().to_path(dirs).exists() {
        SIMPLE_RAYTRACER_REPO.fetch(dirs);
        spawn_and_wait(SIMPLE_RAYTRACER.fetch(
            &bootstrap_host_compiler.cargo,
            &bootstrap_host_compiler.rustc,
            dirs,
        ));
    }

    eprintln!("[LLVM BUILD] simple-raytracer");
    let build_cmd = SIMPLE_RAYTRACER_LLVM.build(bootstrap_host_compiler, dirs);
    spawn_and_wait(build_cmd);
    fs::copy(
        SIMPLE_RAYTRACER_LLVM
            .target_dir(dirs)
            .join(&bootstrap_host_compiler.triple)
            .join("debug")
            .join(get_file_name("main", "bin")),
        RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_llvm", "bin")),
    )
    .unwrap();

    let bench_runs = env::var("BENCH_RUNS").unwrap_or_else(|_| "10".to_string()).parse().unwrap();

    eprintln!("[BENCH COMPILE] ebobby/simple-raytracer");
    let cargo_clif =
        RelPath::DIST.to_path(dirs).join(get_file_name("cargo_clif", "bin").replace('_', "-"));
    let manifest_path = SIMPLE_RAYTRACER.manifest_path(dirs);
    let target_dir = SIMPLE_RAYTRACER.target_dir(dirs);

    let clean_cmd = format!(
        "RUSTC=rustc cargo clean --manifest-path {manifest_path} --target-dir {target_dir}",
        manifest_path = manifest_path.display(),
        target_dir = target_dir.display(),
    );
    let llvm_build_cmd = format!(
        "RUSTC=rustc cargo build --manifest-path {manifest_path} --target-dir {target_dir}",
        manifest_path = manifest_path.display(),
        target_dir = target_dir.display(),
    );
    let clif_build_cmd = format!(
        "RUSTC=rustc {cargo_clif} build --manifest-path {manifest_path} --target-dir {target_dir}",
        cargo_clif = cargo_clif.display(),
        manifest_path = manifest_path.display(),
        target_dir = target_dir.display(),
    );

    let bench_compile =
        hyperfine_command(1, bench_runs, Some(&clean_cmd), &llvm_build_cmd, &clif_build_cmd);

    spawn_and_wait(bench_compile);

    eprintln!("[BENCH RUN] ebobby/simple-raytracer");
    fs::copy(
        target_dir.join("debug").join(get_file_name("main", "bin")),
        RelPath::BUILD.to_path(dirs).join(get_file_name("raytracer_cg_clif", "bin")),
    )
    .unwrap();

    let mut bench_run = hyperfine_command(
        0,
        bench_runs,
        None,
        Path::new(".").join(get_file_name("raytracer_cg_llvm", "bin")).to_str().unwrap(),
        Path::new(".").join(get_file_name("raytracer_cg_clif", "bin")).to_str().unwrap(),
    );
    bench_run.current_dir(RelPath::BUILD.to_path(dirs));
    spawn_and_wait(bench_run);
}