Skip to content

Commit f3923ee

Browse files
committed
Merge pull request #13 from oli-obk/master
benchmarks
2 parents 8961063 + 8e1fa8c commit f3923ee

7 files changed

+176
-4
lines changed

benches/fibonacci.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#![feature(custom_attribute, test)]
2+
#![feature(rustc_private)]
3+
#![allow(unused_attributes)]
4+
5+
extern crate test;
6+
use test::Bencher;
7+
8+
mod fibonacci_helper;
9+
10+
#[bench]
11+
fn fib(bencher: &mut Bencher) {
12+
bencher.iter(|| {
13+
fibonacci_helper::main();
14+
})
15+
}
16+
17+
mod miri_helper;
18+
19+
#[bench]
20+
fn fib_miri(bencher: &mut Bencher) {
21+
miri_helper::run("fibonacci_helper", bencher);
22+
}
23+
24+
mod fibonacci_helper_iterative;
25+
26+
#[bench]
27+
fn fib_iter(bencher: &mut Bencher) {
28+
bencher.iter(|| {
29+
fibonacci_helper_iterative::main();
30+
})
31+
}
32+
33+
#[bench]
34+
fn fib_iter_miri(bencher: &mut Bencher) {
35+
miri_helper::run("fibonacci_helper_iterative", bencher);
36+
}

benches/fibonacci_helper.rs

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#![feature(custom_attribute)]
2+
#![allow(unused_attributes)]
3+
4+
#[miri_run]
5+
#[inline(never)]
6+
pub fn main() {
7+
assert_eq!(fib(10), 55);
8+
}
9+
10+
fn fib(n: usize) -> usize {
11+
if n <= 2 {
12+
1
13+
} else {
14+
fib(n - 1) + fib(n - 2)
15+
}
16+
}

benches/fibonacci_helper_iterative.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(custom_attribute)]
2+
#![allow(unused_attributes)]
3+
4+
#[miri_run]
5+
#[inline(never)]
6+
pub fn main() {
7+
assert_eq!(fib(10), 55);
8+
}
9+
10+
fn fib(n: usize) -> usize {
11+
let mut a = 0;
12+
let mut b = 1;
13+
for _ in 0..n {
14+
let c = a;
15+
a = b;
16+
b = c + b;
17+
}
18+
a
19+
}

benches/miri_helper.rs

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#![feature(custom_attribute, test)]
2+
#![feature(rustc_private)]
3+
#![allow(unused_attributes)]
4+
5+
extern crate getopts;
6+
extern crate miri;
7+
extern crate rustc;
8+
extern crate rustc_driver;
9+
extern crate test;
10+
11+
use self::miri::interpreter;
12+
use self::rustc::session::Session;
13+
use self::rustc_driver::{driver, CompilerCalls};
14+
use std::cell::RefCell;
15+
use std::rc::Rc;
16+
use std::env::var;
17+
use test::Bencher;
18+
19+
pub struct MiriCompilerCalls<'a>(Rc<RefCell<&'a mut Bencher>>);
20+
21+
pub fn run(filename: &str, bencher: &mut Bencher) {
22+
let path = var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
23+
rustc_driver::run_compiler(&[
24+
"miri".to_string(), format!("benches/{}.rs", filename), "--sysroot".to_string(), path.to_string(),
25+
], &mut MiriCompilerCalls(Rc::new(RefCell::new(bencher))));
26+
}
27+
28+
impl<'a> CompilerCalls<'a> for MiriCompilerCalls<'a> {
29+
fn build_controller(
30+
&mut self,
31+
_: &Session,
32+
_: &getopts::Matches
33+
) -> driver::CompileController<'a> {
34+
let mut control: driver::CompileController<'a> = driver::CompileController::basic();
35+
36+
let bencher = self.0.clone();
37+
38+
control.after_analysis.callback = Box::new(move |state| {
39+
state.session.abort_if_errors();
40+
bencher.borrow_mut().iter(|| {
41+
interpreter::interpret_start_points(state.tcx.unwrap(), state.mir_map.unwrap());
42+
})
43+
});
44+
45+
control
46+
}
47+
}

benches/smoke.rs

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#![feature(custom_attribute, test)]
2+
#![feature(rustc_private)]
3+
#![allow(unused_attributes)]
4+
5+
extern crate test;
6+
use test::Bencher;
7+
8+
mod smoke_helper;
9+
10+
#[bench]
11+
fn noop(bencher: &mut Bencher) {
12+
bencher.iter(|| {
13+
smoke_helper::main();
14+
})
15+
}
16+
17+
/*
18+
// really slow
19+
#[bench]
20+
fn noop_miri_full(bencher: &mut Bencher) {
21+
let path = std::env::var("RUST_SYSROOT").expect("env variable `RUST_SYSROOT` not set");
22+
bencher.iter(|| {
23+
let mut process = std::process::Command::new("target/release/miri");
24+
process.arg("benches/smoke_helper.rs")
25+
.arg("--sysroot").arg(&path);
26+
let output = process.output().unwrap();
27+
if !output.status.success() {
28+
println!("{}", String::from_utf8(output.stdout).unwrap());
29+
println!("{}", String::from_utf8(output.stderr).unwrap());
30+
panic!("failed to run miri");
31+
}
32+
})
33+
}
34+
*/
35+
36+
mod miri_helper;
37+
38+
#[bench]
39+
fn noop_miri_interpreter(bencher: &mut Bencher) {
40+
miri_helper::run("smoke_helper", bencher);
41+
}

benches/smoke_helper.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#![feature(custom_attribute)]
2+
#![allow(unused_attributes)]
3+
4+
#[miri_run]
5+
#[inline(never)]
6+
pub fn main() {
7+
}

src/interpreter.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -1365,7 +1365,7 @@ impl<'mir, 'tcx: 'mir> Deref for CachedMir<'mir, 'tcx> {
13651365
fn deref(&self) -> &mir::Mir<'tcx> {
13661366
match *self {
13671367
CachedMir::Ref(r) => r,
1368-
CachedMir::Owned(ref rc) => &rc,
1368+
CachedMir::Owned(ref rc) => rc,
13691369
}
13701370
}
13711371
}
@@ -1422,20 +1422,26 @@ pub fn interpret_start_points<'a, 'tcx>(
14221422
if attr.check_name("miri_run") {
14231423
let item = tcx.map.expect_item(id);
14241424

1425-
println!("Interpreting: {}", item.name);
1425+
if TRACE_EXECUTION {
1426+
println!("Interpreting: {}", item.name);
1427+
}
14261428

14271429
let mut gecx = GlobalEvalContext::new(tcx, mir_map);
14281430
let mut fecx = FnEvalContext::new(&mut gecx);
14291431
match fecx.call_nested(mir) {
1430-
Ok(Some(return_ptr)) => fecx.memory.dump(return_ptr.alloc_id),
1432+
Ok(Some(return_ptr)) => if TRACE_EXECUTION {
1433+
fecx.memory.dump(return_ptr.alloc_id);
1434+
},
14311435
Ok(None) => println!("(diverging function returned)"),
14321436
Err(_e) => {
14331437
// TODO(solson): Detect whether the error was already reported or not.
14341438
// tcx.sess.err(&e.to_string());
14351439
}
14361440
}
14371441

1438-
println!("");
1442+
if TRACE_EXECUTION {
1443+
println!("");
1444+
}
14391445
}
14401446
}
14411447
}

0 commit comments

Comments
 (0)