From 91be1641a15a95a954752ff9dbc5feb7514bca43 Mon Sep 17 00:00:00 2001 From: Andrei Kashin Date: Tue, 28 Nov 2023 15:33:11 +0000 Subject: [PATCH] Add Fibonacci benchmark This includes two versions: - Generated from Rust program - Written by hand --- ci/spectest-zkasm.sh | 5 +- cranelift/filetests/src/test_zkasm.rs | 6 +- .../benchmarks/fibonacci/from_rust.wat | 64 ++++++++++++++++++ .../fibonacci/generated/handwritten_wat.zkasm | 54 +++++++++++++++ .../fibonacci/generated/helpers/2-exp.zkasm | 65 +++++++++++++++++++ .../fibonacci/handwritten_wat.wat} | 18 ++--- .../zkasm_data/benchmarks/fibonacci/state.csv | 3 + docs/zkasm/test_summary.csv | 1 + 8 files changed, 205 insertions(+), 11 deletions(-) create mode 100644 cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat create mode 100644 cranelift/zkasm_data/benchmarks/fibonacci/generated/handwritten_wat.zkasm create mode 100644 cranelift/zkasm_data/benchmarks/fibonacci/generated/helpers/2-exp.zkasm rename cranelift/zkasm_data/{fibonacci.wat => benchmarks/fibonacci/handwritten_wat.wat} (56%) create mode 100644 cranelift/zkasm_data/benchmarks/fibonacci/state.csv diff --git a/ci/spectest-zkasm.sh b/ci/spectest-zkasm.sh index 71901fe35678..10ccc4065d34 100755 --- a/ci/spectest-zkasm.sh +++ b/ci/spectest-zkasm.sh @@ -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" \ No newline at end of file +./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" diff --git a/cranelift/filetests/src/test_zkasm.rs b/cranelift/filetests/src/test_zkasm.rs index a67abd9f3e01..87374b82ea8e 100644 --- a/cranelift/filetests/src/test_zkasm.rs +++ b/cranelift/filetests/src/test_zkasm.rs @@ -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,)* } => { $( @@ -316,7 +321,6 @@ mod tests { locals, locals_simple, counter, - fibonacci, add_func, mul, i64_mul, diff --git a/cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat b/cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat new file mode 100644 index 000000000000..122a633c8b46 --- /dev/null +++ b/cranelift/zkasm_data/benchmarks/fibonacci/from_rust.wat @@ -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))) diff --git a/cranelift/zkasm_data/benchmarks/fibonacci/generated/handwritten_wat.zkasm b/cranelift/zkasm_data/benchmarks/fibonacci/generated/handwritten_wat.zkasm new file mode 100644 index 000000000000..f441ab83b997 --- /dev/null +++ b/cranelift/zkasm_data/benchmarks/fibonacci/generated/handwritten_wat.zkasm @@ -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" \ No newline at end of file diff --git a/cranelift/zkasm_data/benchmarks/fibonacci/generated/helpers/2-exp.zkasm b/cranelift/zkasm_data/benchmarks/fibonacci/generated/helpers/2-exp.zkasm new file mode 100644 index 000000000000..1bf26c1b3daf --- /dev/null +++ b/cranelift/zkasm_data/benchmarks/fibonacci/generated/helpers/2-exp.zkasm @@ -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 \ No newline at end of file diff --git a/cranelift/zkasm_data/fibonacci.wat b/cranelift/zkasm_data/benchmarks/fibonacci/handwritten_wat.wat similarity index 56% rename from cranelift/zkasm_data/fibonacci.wat rename to cranelift/zkasm_data/benchmarks/fibonacci/handwritten_wat.wat index 9727835c3759..faed005503d7 100644 --- a/cranelift/zkasm_data/fibonacci.wat +++ b/cranelift/zkasm_data/benchmarks/fibonacci/handwritten_wat.wat @@ -1,18 +1,18 @@ (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) @@ -20,13 +20,13 @@ (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)) diff --git a/cranelift/zkasm_data/benchmarks/fibonacci/state.csv b/cranelift/zkasm_data/benchmarks/fibonacci/state.csv new file mode 100644 index 000000000000..530eeaa8111f --- /dev/null +++ b/cranelift/zkasm_data/benchmarks/fibonacci/state.csv @@ -0,0 +1,3 @@ +Test,Status,Cycles +from_rust,compilation failed, +handwritten_wat,pass,190024 diff --git a/docs/zkasm/test_summary.csv b/docs/zkasm/test_summary.csv index 6a6e3908dfa4..3b56a1141626 100644 --- a/docs/zkasm/test_summary.csv +++ b/docs/zkasm/test_summary.csv @@ -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,155,364,4589 cranelift/zkasm_data/spectest/i64,161,374,4042