Skip to content

Commit

Permalink
Merge pull request bytecodealliance#128 from near/fibo_bench
Browse files Browse the repository at this point in the history
Add Fibonacci benchmark
  • Loading branch information
aborg-dev authored Dec 1, 2023
2 parents bef30bd + 91be164 commit 9fe2545
Show file tree
Hide file tree
Showing 8 changed files with 205 additions and 11 deletions.
5 changes: 4 additions & 1 deletion ci/spectest-zkasm.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,7 @@ set -eux

./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/i32"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/i64"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/conversions"
./ci/test-zkasm.sh "cranelift/zkasm_data/spectest/conversions"
# TODO(akashin): Move this out to a separate shell script or rename this shell script to be
# more general.
./ci/test-zkasm.sh "cranelift/zkasm_data/benchmarks/fibonacci"
6 changes: 5 additions & 1 deletion cranelift/filetests/src/test_zkasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ mod tests {
run_spectest("conversions");
}

#[test]
fn run_benchmarks() {
test_wat_in_directory(Path::new(&format!("../zkasm_data/benchmarks/fibonacci")));
}

macro_rules! testcases {
{ $($name:ident,)* } => {
$(
Expand All @@ -316,7 +321,6 @@ mod tests {
locals,
locals_simple,
counter,
fibonacci,
add_func,
mul,
i64_mul,
Expand Down
64 changes: 64 additions & 0 deletions cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
;; TODO(akashin): Add Rust code to generate this file to repository.
;; For now see https://github.com/near/wasmtime/issues/90#issuecomment-1823140768.
(module
(type (;0;) (func (param i64 i64)))
(type (;1;) (func))
(import "env" "assert_eq" (func (;0;) (type 0)))
(func (;1;) (type 1)
(local i64 i64 i32)
i64.const 1
local.set 0
i64.const 0
local.set 1
i32.const 10000
local.set 2
loop ;; label = @1
local.get 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.tee 0
local.get 1
i64.add
local.tee 1
local.get 0
i64.add
local.set 0
local.get 2
i32.const -10
i32.add
local.tee 2
br_if 0 (;@1;)
end
local.get 1
i64.const -2872092127636481573
call 0)
(start 1)
(memory (;0;) 16)
(global (;0;) (mut i32) (i32.const 1048576))
(global (;1;) i32 (i32.const 1048576))
(global (;2;) i32 (i32.const 1048576))
(export "memory" (memory 0))
(export "main" (func 1))
(export "__data_end" (global 1))
(export "__heap_base" (global 2)))
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
start:
zkPC + 2 => RR
:JMP(function_1)
:JMP(finalizeExecution)
function_1:
SP + 1 => SP
RR :MSTORE(SP - 1)
SP + 6 => SP
C :MSTORE(SP - 1)
D :MSTORE(SP - 2)
E :MSTORE(SP - 3)
B :MSTORE(SP - 4)
0n => A
A :MSTORE(SP + 8)
0n => A
1n => D
D => B
:JMP(label_1_1)
label_1_1:
$ => E :ADD
B :MSTORE(SP)
4294967296n => B
$ => A :MLOAD(SP + 8)
$ => A :ADD
A :MSTORE(SP + 8)
42949672960000n => B
$ => A :EQ
4294967296n => B
0 => D
0 => C
${A * B} => A :ARITH
A => B
0 => A
$ => A :EQ
A :JMPZ(label_1_3)
$ => A :MLOAD(SP)
E => B
:JMP(label_1_1)
label_1_3:
15574651946073070043n => B
$ => A :MLOAD(SP)
B :ASSERT
$ => C :MLOAD(SP - 1)
$ => D :MLOAD(SP - 2)
$ => E :MLOAD(SP - 3)
$ => B :MLOAD(SP - 4)
SP - 6 => SP
$ => RR :MLOAD(SP - 1)
SP - 1 => SP
:JMP(RR)
finalizeExecution:
${beforeLast()} :JMPN(finalizeExecution)
:JMP(start)
INCLUDE "helpers/2-exp.zkasm"
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
two_power:
0x1n => B :JMP(RR) ;2**0
0x2n => B :JMP(RR) ;2**1
0x4n => B :JMP(RR) ;2**2
0x8n => B :JMP(RR) ;2**3
0x10n => B :JMP(RR) ;2**4
0x20n => B :JMP(RR) ;2**5
0x40n => B :JMP(RR) ;2**6
0x80n => B :JMP(RR) ;2**7
0x100n => B :JMP(RR) ;2**8
0x200n => B :JMP(RR) ;2**9
0x400n => B :JMP(RR) ;2**10
0x800n => B :JMP(RR) ;2**11
0x1000n => B :JMP(RR) ;2**12
0x2000n => B :JMP(RR) ;2**13
0x4000n => B :JMP(RR) ;2**14
0x8000n => B :JMP(RR) ;2**15
0x10000n => B :JMP(RR) ;2**16
0x20000n => B :JMP(RR) ;2**17
0x40000n => B :JMP(RR) ;2**18
0x80000n => B :JMP(RR) ;2**19
0x100000n => B :JMP(RR) ;2**20
0x200000n => B :JMP(RR) ;2**21
0x400000n => B :JMP(RR) ;2**22
0x800000n => B :JMP(RR) ;2**23
0x1000000n => B :JMP(RR) ;2**24
0x2000000n => B :JMP(RR) ;2**25
0x4000000n => B :JMP(RR) ;2**26
0x8000000n => B :JMP(RR) ;2**27
0x10000000n => B :JMP(RR) ;2**28
0x20000000n => B :JMP(RR) ;2**29
0x40000000n => B :JMP(RR) ;2**30
0x80000000n => B :JMP(RR) ;2**31
0x100000000n => B :JMP(RR) ;2**32
0x200000000n => B :JMP(RR) ;2**33
0x400000000n => B :JMP(RR) ;2**34
0x800000000n => B :JMP(RR) ;2**35
0x1000000000n => B :JMP(RR) ;2**36
0x2000000000n => B :JMP(RR) ;2**37
0x4000000000n => B :JMP(RR) ;2**38
0x8000000000n => B :JMP(RR) ;2**39
0x10000000000n => B :JMP(RR) ;2**40
0x20000000000n => B :JMP(RR) ;2**41
0x40000000000n => B :JMP(RR) ;2**42
0x80000000000n => B :JMP(RR) ;2**43
0x100000000000n => B :JMP(RR) ;2**44
0x200000000000n => B :JMP(RR) ;2**45
0x400000000000n => B :JMP(RR) ;2**46
0x800000000000n => B :JMP(RR) ;2**47
0x1000000000000n => B :JMP(RR) ;2**48
0x2000000000000n => B :JMP(RR) ;2**49
0x4000000000000n => B :JMP(RR) ;2**50
0x8000000000000n => B :JMP(RR) ;2**51
0x10000000000000n => B :JMP(RR) ;2**52
0x20000000000000n => B :JMP(RR) ;2**53
0x40000000000000n => B :JMP(RR) ;2**54
0x80000000000000n => B :JMP(RR) ;2**55
0x100000000000000n => B :JMP(RR) ;2**56
0x200000000000000n => B :JMP(RR) ;2**57
0x400000000000000n => B :JMP(RR) ;2**58
0x800000000000000n => B :JMP(RR) ;2**59
0x1000000000000000n => B :JMP(RR) ;2**60
0x2000000000000000n => B :JMP(RR) ;2**61
0x4000000000000000n => B :JMP(RR) ;2**62
0x8000000000000000n => B :JMP(RR) ;2**63
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
(module
(import "env" "assert_eq" (func $assert_eq (param i32) (param i32)))
(import "env" "assert_eq" (func $assert_eq (param i64) (param i64)))
(func $main
(local $counter i32)
(local $fp i32)
(local $f i32)
(local $fp i64)
(local $f i64)
(local.set $counter (i32.const 0))
(local.set $fp (i32.const 0))
(local.set $f (i32.const 1))
(local.set $fp (i64.const 0))
(local.set $f (i64.const 1))
(block
(loop
(local.get $fp)
(local.get $f)
(local.set $fp (local.get $f))
(local.set $f (i32.add))
(local.set $f (i64.add))
(local.set $counter
(i32.add
(local.get $counter)
(i32.const 1)))
(br_if 1
(i32.eq
(local.get $counter)
(i32.const 10)
(i32.const 10000)
)
)
(br 0)
)
)
(local.get $f)
(i32.const 89)
(local.get $fp)
(i64.const -2872092127636481573)
call $assert_eq)
(start $main))
3 changes: 3 additions & 0 deletions cranelift/zkasm_data/benchmarks/fibonacci/state.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Test,Status,Cycles
from_rust,compilation failed,
handwritten_wat,pass,190024
1 change: 1 addition & 0 deletions docs/zkasm/test_summary.csv
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
Suite path,Passing count,Total count,Total cycles
cranelift/zkasm_data,25,26,1023
cranelift/zkasm_data/benchmarks/fibonacci,1,2,190024
cranelift/zkasm_data/spectest/conversions,3,24,45
cranelift/zkasm_data/spectest/i32,190,364,5716
cranelift/zkasm_data/spectest/i64,196,374,4903

0 comments on commit 9fe2545

Please sign in to comment.