Merge ends, store locals in registers where possible

This commit is contained in:
Jef
2019-01-17 11:07:51 +01:00
parent a7fa7da7d4
commit 74d168ec4b
6 changed files with 155 additions and 50 deletions

View File

@@ -698,6 +698,18 @@ fn wrong_index() {
);
}
fn iterative_fib_baseline(n: u32) -> u32 {
let (mut a, mut b) = (1, 1);
for _ in 0..n {
let old_a = a;
a = b;
b += old_a;
}
a
}
const FIBONACCI: &str = r#"
(module
(func $fib (param $n i32) (result i32)
@@ -745,25 +757,59 @@ const FIBONACCI: &str = r#"
#[test]
fn fib() {
fn fib(n: u32) -> u32 {
let (mut a, mut b) = (1, 1);
for _ in 0..n {
let old_a = a;
a = b;
b += old_a;
}
a
}
let translated = translate_wat(FIBONACCI);
translated.disassemble();
for x in 0..30 {
assert_eq!(
translated.execute_func::<_, u32>(0, (x,)),
Ok(fib(x)),
Ok(iterative_fib_baseline(x)),
"Failed for x={}",
x
);
}
}
// Generated by Rust for the `fib` function in `bench_fibonacci_baseline`
const FIBONACCI_OPT: &str = r"
(module
(func $fib (param $p0 i32) (result i32)
(local $l1 i32)
(set_local $l1
(i32.const 1))
(block $B0
(br_if $B0
(i32.lt_u
(get_local $p0)
(i32.const 2)))
(set_local $l1
(i32.const 1))
(loop $L1
(set_local $l1
(i32.add
(call $fib
(i32.add
(get_local $p0)
(i32.const -1)))
(get_local $l1)))
(br_if $L1
(i32.gt_u
(tee_local $p0
(i32.add
(get_local $p0)
(i32.const -2)))
(i32.const 1)))))
(get_local $l1)))";
#[test]
fn fib_opt() {
let translated = translate_wat(FIBONACCI_OPT);
translated.disassemble();
for x in 0..30 {
assert_eq!(
translated.execute_func::<_, u32>(0, (x,)),
Ok(iterative_fib_baseline(x)),
"Failed for x={}",
x
);
@@ -940,7 +986,7 @@ fn bench_fibonacci_compile(b: &mut test::Bencher) {
#[bench]
fn bench_fibonacci_run(b: &mut test::Bencher) {
let wasm = wabt::wat2wasm(FIBONACCI).unwrap();
let wasm = wabt::wat2wasm(FIBONACCI_OPT).unwrap();
let module = translate(&wasm).unwrap();
b.iter(|| module.execute_func::<_, u32>(0, (20,)));