rust/benches/helpers/miri_helper.rs

64 lines
1.9 KiB
Rust
Raw Normal View History

2016-05-30 14:05:50 +02:00
extern crate getopts;
extern crate miri;
extern crate rustc;
extern crate rustc_driver;
2019-02-01 23:26:42 +01:00
extern crate rustc_interface;
2016-05-30 14:05:50 +02:00
extern crate test;
2019-02-01 23:26:42 +01:00
use self::miri::eval_main;
2019-01-21 17:29:09 +01:00
use rustc::hir::def_id::LOCAL_CRATE;
2019-02-01 23:26:42 +01:00
use rustc_interface::interface;
use crate::test::Bencher;
struct MiriCompilerCalls<'a> {
bencher: &'a mut Bencher,
}
2019-02-08 20:07:49 +01:00
2019-02-01 23:26:42 +01:00
impl rustc_driver::Callbacks for MiriCompilerCalls<'_> {
2019-03-11 11:28:56 +01:00
fn after_analysis(&mut self, compiler: &interface::Compiler) -> bool {
2019-02-01 23:26:42 +01:00
compiler.session().abort_if_errors();
2019-02-08 20:07:49 +01:00
2019-02-01 23:26:42 +01:00
compiler.global_ctxt().unwrap().peek_mut().enter(|tcx| {
let (entry_def_id, _) = tcx.entry_fn(LOCAL_CRATE).expect(
"no main or start function found",
);
2016-05-30 14:05:50 +02:00
2019-02-01 23:26:42 +01:00
self.bencher.iter(|| {
2019-03-11 11:28:56 +01:00
let config = miri::MiriConfig { validate: true, args: vec![] };
2019-02-01 23:26:42 +01:00
eval_main(tcx, entry_def_id, config);
});
});
compiler.session().abort_if_errors();
// Don't continue execution
false
}
}
2016-05-30 14:05:50 +02:00
fn find_sysroot() -> String {
// Taken from https://github.com/Manishearth/rust-clippy/pull/911.
let home = option_env!("RUSTUP_HOME").or(option_env!("MULTIRUST_HOME"));
let toolchain = option_env!("RUSTUP_TOOLCHAIN").or(option_env!("MULTIRUST_TOOLCHAIN"));
match (home, toolchain) {
(Some(home), Some(toolchain)) => format!("{}/toolchains/{}", home, toolchain),
_ => {
option_env!("RUST_SYSROOT")
.expect(
"need to specify RUST_SYSROOT env var or use rustup or multirust",
)
.to_owned()
}
}
}
2016-05-30 14:05:50 +02:00
pub fn run(filename: &str, bencher: &mut Bencher) {
let args = &[
"miri".to_string(),
2016-06-17 22:35:34 -06:00
format!("benches/helpers/{}.rs", filename),
"--sysroot".to_string(),
find_sysroot(),
];
2019-03-11 12:41:11 +01:00
rustc_driver::run_compiler(args, &mut MiriCompilerCalls { bencher }, None, None).unwrap()
2016-05-30 14:05:50 +02:00
}