4 extern crate rustc_driver;
5 extern crate rustc_hir;
6 extern crate rustc_interface;
9 use self::miri::eval_main;
10 use crate::test::Bencher;
11 use rustc_hir::def_id::LOCAL_CRATE;
12 use rustc_driver::Compilation;
13 use rustc_interface::{interface, Queries};
15 struct MiriCompilerCalls<'a> {
16 bencher: &'a mut Bencher,
19 impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
20 fn after_analysis<'tcx>(
22 compiler: &interface::Compiler,
23 queries: &'tcx Queries<'tcx>,
25 compiler.session().abort_if_errors();
27 queries.global_ctxt().unwrap().peek_mut().enter(|tcx| {
28 let (entry_def_id, _) =
29 tcx.entry_fn(LOCAL_CRATE).expect("no main or start function found");
31 self.bencher.iter(|| {
32 let config = miri::MiriConfig {
34 stacked_borrows: true,
37 excluded_env_vars: vec![],
40 tracked_pointer_tag: None,
42 eval_main(tcx, entry_def_id, config);
46 compiler.session().abort_if_errors();
52 fn find_sysroot() -> String {
53 // Taken from https://github.com/Manishearth/rust-clippy/pull/911.
54 let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
55 let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
56 match (home, toolchain) {
57 (Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
58 _ => option_env!("RUST_SYSROOT")
59 .expect("need to specify RUST_SYSROOT env var or use rustup or multirust")
64 pub fn run(filename: &str, bencher: &mut Bencher) {
67 format!("benches/helpers/{}.rs", filename),
68 "--sysroot".to_string(),
71 rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()