forked from bytecodealliance/wasmtime
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #233 from mooori/emit-inst-profiling
feat(bench): add `analyze-zkasm` CLI and `instrument-inst` command
- Loading branch information
Showing
10 changed files
with
138 additions
and
5 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "analyze-zkasm" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html | ||
|
||
[dependencies] | ||
anyhow = { workspace = true } | ||
clap = { workspace = true, default-features = true, features = ["std", "derive"] } | ||
cranelift-filetests = { workspace = true } | ||
wat = { workspace = true } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
use std::path::PathBuf; | ||
|
||
use clap::{Parser, Subcommand}; | ||
|
||
use cranelift_filetests::zkasm_codegen::{generate_zkasm, ZkasmSettings}; | ||
|
||
/// A CLI to analyze zkasm generated by cranelift. | ||
#[derive(Parser)] | ||
#[command(version, about, long_about = None)] | ||
#[command(propagate_version = true)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Command, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
enum Command { | ||
/// Generate zkasm that calls zkevm-proverjs helpers to trace executed instructions. | ||
#[command[arg_required_else_help = true]] | ||
InstrumentInst { | ||
/// Path to the input WAT file. | ||
wat_path: PathBuf, | ||
/// Path to the file where generated zkasm is written. | ||
out_path: PathBuf, | ||
}, | ||
} | ||
|
||
fn main() -> anyhow::Result<()> { | ||
let cli = Cli::parse(); | ||
|
||
match &cli.command { | ||
Command::InstrumentInst { wat_path, out_path } => { | ||
let zkasm = instrument_inst(wat_path)?; | ||
std::fs::write(out_path, zkasm.as_bytes())?; | ||
println!("wrote instrumented zkASM to {}", out_path.display()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} | ||
|
||
fn instrument_inst(wat_path: &PathBuf) -> anyhow::Result<String> { | ||
let wasm_module = wat::parse_file(wat_path)?; | ||
let zkasm_settings = ZkasmSettings { | ||
emit_profiling_info: true, | ||
}; | ||
Ok(generate_zkasm(&zkasm_settings, &wasm_module)) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use std::path::PathBuf; | ||
|
||
use super::instrument_inst; | ||
|
||
#[test] | ||
fn test_instrument_inst() -> anyhow::Result<()> { | ||
let zkasm = instrument_inst(&PathBuf::from("./testfiles/simple.wat"))?; | ||
let expected_zkasm = | ||
std::fs::read_to_string("./testfiles/simple_instrumented.zkasm").unwrap(); | ||
assert_eq!(zkasm, expected_zkasm); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
# About | ||
|
||
Contains files used in tests. | ||
|
||
# Updating files containing expected zkASM | ||
|
||
Use the CLI, for instance: | ||
|
||
``` | ||
# In the analyze-zkasm directory run | ||
cargo run -- instrument-inst ./testfiles/simple.wat ./testfiles/simple_instrumented.zkasm | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(module | ||
(func $main | ||
nop) | ||
(start $main)) |
21 changes: 21 additions & 0 deletions
21
cranelift/zkasm_data/analyze-zkasm/testfiles/simple_instrumented.zkasm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
start: | ||
0xffff => SP | ||
zkPC + 2 => RR | ||
:JMP(function_0) | ||
:JMP(finalizeExecution) | ||
function_0: | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
SP - 1 => SP | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
RR :MSTORE(SP) | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
$ => RR :MLOAD(SP) | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
SP + 1 => SP | ||
;; TODO(mooori) call the helper to trace `MInst` execution | ||
:JMP(RR) | ||
finalizeExecution: | ||
${beforeLast()} :JMPN(finalizeExecution) | ||
:JMP(start) | ||
INCLUDE "helpers/2-exp.zkasm" |