From c55b3666eaeb74c33eea5d4b6a8cc4b974912d9e Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Fri, 27 May 2016 16:12:17 +0200 Subject: [PATCH 1/4] clippy nit --- src/interpreter.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index a7df8748ae..d83c8eb2da 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1365,7 +1365,7 @@ impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> { fn deref(&self) -> &mir::Mir<'tcx> { match *self { CachedMir::Ref(r) => r, - CachedMir::Owned(ref rc) => &rc, + CachedMir::Owned(ref rc) => rc, } } } From 3ec813e4e508f3608e51c278626ea838d0351c2d Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 30 May 2016 13:40:20 +0200 Subject: [PATCH 2/4] add benchmarks --- benches/smoke.rs | 76 +++++++++++++++++++++++++++++++++++++++++ benches/smoke_helper.rs | 7 ++++ 2 files changed, 83 insertions(+) create mode 100644 benches/smoke.rs create mode 100644 benches/smoke_helper.rs diff --git a/benches/smoke.rs b/benches/smoke.rs new file mode 100644 index 0000000000..992f435a50 --- /dev/null +++ b/benches/smoke.rs @@ -0,0 +1,76 @@ +#![feature(custom_attribute, test)] +#![feature(rustc_private)] +#![allow(unused_attributes)] + +extern crate getopts; +extern crate miri; +extern crate rustc; +extern crate rustc_driver; + +use miri::interpreter; +use rustc::session::Session; +use rustc_driver::{driver, CompilerCalls}; +use std::cell::RefCell; +use std::rc::Rc; + +extern crate test; +use test::Bencher; + +mod smoke_helper; + +#[bench] +fn noop(bencher: &mut Bencher) { + bencher.iter(|| { + smoke_helper::main(); + }) +} + +/* +// really slow +#[bench] +fn noop_miri_full(bencher: &mut Bencher) { + let path = std::env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set"); + bencher.iter(|| { + let mut process = std::process::Command::new("target/release/miri"); + process.arg("benches/smoke_helper.rs") + .arg("--sysroot").arg(&path); + let output = process.output().unwrap(); + if !output.status.success() { + println!("{}", String::from_utf8(output.stdout).unwrap()); + println!("{}", String::from_utf8(output.stderr).unwrap()); + panic!("failed to run miri"); + } + }) +} +*/ + +#[bench] +fn noop_miri_interpreter(bencher: &mut Bencher) { + let path = std::env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set"); + rustc_driver::run_compiler(&[ + "miri".to_string(), "benches/smoke_helper.rs".to_string(), "--sysroot".to_string(), path.to_string(), + ], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)))); +} + +struct MiriCompilerCalls<'a>(Rc>); + +impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> { + fn build_controller( + &mut self, + _: &Session, + _: &getopts::Matches + ) -> driver::CompileController<'a> { + let mut control: driver::CompileController<'a> = driver::CompileController::basic(); + + let bencher = self.0.clone(); + + control.after_analysis.callback = Box::new(move |state| { + state.session.abort_if_errors(); + bencher.borrow_mut().iter(|| { + interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap()); + }) + }); + + control + } +} diff --git a/benches/smoke_helper.rs b/benches/smoke_helper.rs new file mode 100644 index 0000000000..e8691f244c --- /dev/null +++ b/benches/smoke_helper.rs @@ -0,0 +1,7 @@ +#![feature(custom_attribute)] +#![allow(unused_attributes)] + +#[miri_run] +#[inline(never)] +pub fn main() { +} From cecae8050eb30f282c574867ffd84e9a2bbf8e45 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 30 May 2016 13:40:46 +0200 Subject: [PATCH 3/4] remove unnecessary printlns for benchmarks --- src/interpreter.rs | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/interpreter.rs b/src/interpreter.rs index d83c8eb2da..8e8565b50c 100644 --- a/src/interpreter.rs +++ b/src/interpreter.rs @@ -1422,12 +1422,16 @@ pub fn interpret_start_points<'a, 'tcx>( if attr.check_name("miri_run") { let item = tcx.map.expect_item(id); - println!("Interpreting: {}", item.name); + if TRACE_EXECUTION { + println!("Interpreting: {}", item.name); + } let mut gecx = GlobalEvalContext::new(tcx, mir_map); let mut fecx = FnEvalContext::new(&mut gecx); match fecx.call_nested(mir) { - Ok(Some(return_ptr)) => fecx.memory.dump(return_ptr.alloc_id), + Ok(Some(return_ptr)) => if TRACE_EXECUTION { + fecx.memory.dump(return_ptr.alloc_id); + }, Ok(None) => println!("(diverging function returned)"), Err(_e) => { // TODO(solson): Detect whether the error was already reported or not. @@ -1435,7 +1439,9 @@ pub fn interpret_start_points<'a, 'tcx>( } } - println!(""); + if TRACE_EXECUTION { + println!(""); + } } } } From 8e1fa8c13c7cd1a2145414ef4e4fa4a3dc8d1159 Mon Sep 17 00:00:00 2001 From: Oliver Schneider Date: Mon, 30 May 2016 14:05:50 +0200 Subject: [PATCH 4/4] add more benchmarks --- benches/fibonacci.rs | 36 ++++++++++++++++++++ benches/fibonacci_helper.rs | 16 +++++++++ benches/fibonacci_helper_iterative.rs | 19 +++++++++++ benches/miri_helper.rs | 47 +++++++++++++++++++++++++++ benches/smoke.rs | 41 ++--------------------- 5 files changed, 121 insertions(+), 38 deletions(-) create mode 100644 benches/fibonacci.rs create mode 100644 benches/fibonacci_helper.rs create mode 100644 benches/fibonacci_helper_iterative.rs create mode 100644 benches/miri_helper.rs diff --git a/benches/fibonacci.rs b/benches/fibonacci.rs new file mode 100644 index 0000000000..1f8a2aafc9 --- /dev/null +++ b/benches/fibonacci.rs @@ -0,0 +1,36 @@ +#![feature(custom_attribute, test)] +#![feature(rustc_private)] +#![allow(unused_attributes)] + +extern crate test; +use test::Bencher; + +mod fibonacci_helper; + +#[bench] +fn fib(bencher: &mut Bencher) { + bencher.iter(|| { + fibonacci_helper::main(); + }) +} + +mod miri_helper; + +#[bench] +fn fib_miri(bencher: &mut Bencher) { + miri_helper::run("fibonacci_helper", bencher); +} + +mod fibonacci_helper_iterative; + +#[bench] +fn fib_iter(bencher: &mut Bencher) { + bencher.iter(|| { + fibonacci_helper_iterative::main(); + }) +} + +#[bench] +fn fib_iter_miri(bencher: &mut Bencher) { + miri_helper::run("fibonacci_helper_iterative", bencher); +} diff --git a/benches/fibonacci_helper.rs b/benches/fibonacci_helper.rs new file mode 100644 index 0000000000..cddfff9c2c --- /dev/null +++ b/benches/fibonacci_helper.rs @@ -0,0 +1,16 @@ +#![feature(custom_attribute)] +#![allow(unused_attributes)] + +#[miri_run] +#[inline(never)] +pub fn main() { + assert_eq!(fib(10), 55); +} + +fn fib(n: usize) -> usize { + if n <= 2 { + 1 + } else { + fib(n - 1) + fib(n - 2) + } +} diff --git a/benches/fibonacci_helper_iterative.rs b/benches/fibonacci_helper_iterative.rs new file mode 100644 index 0000000000..486d8c2e8a --- /dev/null +++ b/benches/fibonacci_helper_iterative.rs @@ -0,0 +1,19 @@ +#![feature(custom_attribute)] +#![allow(unused_attributes)] + +#[miri_run] +#[inline(never)] +pub fn main() { + assert_eq!(fib(10), 55); +} + +fn fib(n: usize) -> usize { + let mut a = 0; + let mut b = 1; + for _ in 0..n { + let c = a; + a = b; + b = c + b; + } + a +} diff --git a/benches/miri_helper.rs b/benches/miri_helper.rs new file mode 100644 index 0000000000..54c15a27ed --- /dev/null +++ b/benches/miri_helper.rs @@ -0,0 +1,47 @@ +#![feature(custom_attribute, test)] +#![feature(rustc_private)] +#![allow(unused_attributes)] + +extern crate getopts; +extern crate miri; +extern crate rustc; +extern crate rustc_driver; +extern crate test; + +use self::miri::interpreter; +use self::rustc::session::Session; +use self::rustc_driver::{driver, CompilerCalls}; +use std::cell::RefCell; +use std::rc::Rc; +use std::env::var; +use test::Bencher; + +pub struct MiriCompilerCalls<'a>(Rc>); + +pub fn run(filename: &str, bencher: &mut Bencher) { + let path = var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set"); + rustc_driver::run_compiler(&[ + "miri".to_string(), format!("benches/{}.rs", filename), "--sysroot".to_string(), path.to_string(), + ], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)))); +} + +impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> { + fn build_controller( + &mut self, + _: &Session, + _: &getopts::Matches + ) -> driver::CompileController<'a> { + let mut control: driver::CompileController<'a> = driver::CompileController::basic(); + + let bencher = self.0.clone(); + + control.after_analysis.callback = Box::new(move |state| { + state.session.abort_if_errors(); + bencher.borrow_mut().iter(|| { + interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap()); + }) + }); + + control + } +} diff --git a/benches/smoke.rs b/benches/smoke.rs index 992f435a50..43baf486df 100644 --- a/benches/smoke.rs +++ b/benches/smoke.rs @@ -2,17 +2,6 @@ #![feature(rustc_private)] #![allow(unused_attributes)] -extern crate getopts; -extern crate miri; -extern crate rustc; -extern crate rustc_driver; - -use miri::interpreter; -use rustc::session::Session; -use rustc_driver::{driver, CompilerCalls}; -use std::cell::RefCell; -use std::rc::Rc; - extern crate test; use test::Bencher; @@ -44,33 +33,9 @@ fn noop_miri_full(bencher: &mut Bencher) { } */ +mod miri_helper; + #[bench] fn noop_miri_interpreter(bencher: &mut Bencher) { - let path = std::env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set"); - rustc_driver::run_compiler(&[ - "miri".to_string(), "benches/smoke_helper.rs".to_string(), "--sysroot".to_string(), path.to_string(), - ], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher)))); -} - -struct MiriCompilerCalls<'a>(Rc>); - -impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> { - fn build_controller( - &mut self, - _: &Session, - _: &getopts::Matches - ) -> driver::CompileController<'a> { - let mut control: driver::CompileController<'a> = driver::CompileController::basic(); - - let bencher = self.0.clone(); - - control.after_analysis.callback = Box::new(move |state| { - state.session.abort_if_errors(); - bencher.borrow_mut().iter(|| { - interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap()); - }) - }); - - control - } + miri_helper::run("smoke_helper", bencher); }