Skip to content

Commit

Permalink
Merge pull request #13 from oli-obk/master
Browse files Browse the repository at this point in the history
benchmarks
  • Loading branch information
solson committed May 30, 2016
2 parents 8961063 + 8e1fa8c commit f3923ee
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 4 deletions.
36 changes: 36 additions & 0 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -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);
}
16 changes: 16 additions & 0 deletions benches/fibonacci_helper.rs
Original file line number Diff line number Diff line change
@@ -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)
}
}
19 changes: 19 additions & 0 deletions benches/fibonacci_helper_iterative.rs
Original file line number Diff line number Diff line change
@@ -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
}
47 changes: 47 additions & 0 deletions benches/miri_helper.rs
Original file line number Diff line number Diff line change
@@ -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<RefCell<&'a mut Bencher>>);

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
}
}
41 changes: 41 additions & 0 deletions benches/smoke.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#![feature(custom_attribute, test)]
#![feature(rustc_private)]
#![allow(unused_attributes)]

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");
}
})
}
*/

mod miri_helper;

#[bench]
fn noop_miri_interpreter(bencher: &mut Bencher) {
miri_helper::run("smoke_helper", bencher);
}
7 changes: 7 additions & 0 deletions benches/smoke_helper.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#![feature(custom_attribute)]
#![allow(unused_attributes)]

#[miri_run]
#[inline(never)]
pub fn main() {
}
14 changes: 10 additions & 4 deletions src/interpreter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
Expand Down Expand Up @@ -1422,20 +1422,26 @@ 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.
// tcx.sess.err(&e.to_string());
}
}

println!("");
if TRACE_EXECUTION {
println!("");
}
}
}
}
Expand Down

0 comments on commit f3923ee

Please sign in to comment.