]> git.lizzy.rs Git - rust.git/blob - benches/helpers/miri_helper.rs
Auto merge of #1281 - RalfJung:rustup, r=RalfJung
[rust.git] / benches / helpers / miri_helper.rs
1 extern crate rustc_driver;
2 extern crate rustc_hir;
3 extern crate rustc_interface;
4
5 use rustc_hir::def_id::LOCAL_CRATE;
6 use rustc_driver::Compilation;
7 use rustc_interface::{interface, Queries};
8
9 use crate::test::Bencher;
10
11 struct MiriCompilerCalls<'a> {
12     bencher: &'a mut Bencher,
13 }
14
15 impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
16     fn after_analysis<'tcx>(
17         &mut self,
18         compiler: &interface::Compiler,
19         queries: &'tcx Queries<'tcx>,
20     ) -> Compilation {
21         compiler.session().abort_if_errors();
22
23         queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
24             let (entry_def_id, _) =
25                 tcx.entry_fn(LOCAL_CRATE).expect("no main or start function found");
26
27             self.bencher.iter(|| {
28                 let config = miri::MiriConfig::default();
29                 miri::eval_main(tcx, entry_def_id, config);
30             });
31         });
32
33         compiler.session().abort_if_errors();
34
35         Compilation::Stop
36     }
37 }
38
39 fn find_sysroot() -> String {
40     // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
41     let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
42     let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
43     match (home, toolchain) {
44         (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
45         _ => option_env!("RUST_SYSROOT")
46             .expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
47             .to_owned(),
48     }
49 }
50
51 pub fn run(filename: &str, bencher: &mut Bencher) {
52     let args = &[
53         "miri".to_string(),
54         format!("benches/helpers/{}.rs", filename),
55         "--sysroot".to_string(),
56         find_sysroot(),
57     ];
58     rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()
59 }