Skip to content

0x kitsune/cost estimation #56

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
[workspace]
members = [
"crates/papyrus",
"crates/miden-integration-tests",
"bin/scribe",
"bin/repl",
]
2 changes: 1 addition & 1 deletion bin/scribe/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use papyrus::ast_optimization::optimize_ast;
use papyrus::miden_generator;
use papyrus::optimizer::optimize_ast;
use papyrus::parser;
use papyrus::type_inference::infer_types;

Expand Down
2 changes: 1 addition & 1 deletion crates/miden-integration-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ harness = true


[dependencies]
scribe = {path = "../transpiler/"}
papyrus = { path = "../papyrus" }
miden-assembly = { git = "http://github.com/maticnetwork/miden", branch = "next" }
miden-processor = { git = "http://github.com/maticnetwork/miden", branch = "next" }
miden-core = { git = "http://github.com/maticnetwork/miden", branch = "next" }
Expand Down
4 changes: 3 additions & 1 deletion crates/miden-integration-tests/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@

pub fn test() {
println!("Hello, world!");
}
12 changes: 6 additions & 6 deletions crates/miden-integration-tests/test_output.masm
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use.std::math::u256


begin
# and() #
# u256 literal: 0 #
push.0 push.0 push.0 push.0 push.0 push.0 push.0 push.0
# eq() #
# u256 literal: 2 #
push.2 push.0 push.0 push.0 push.0 push.0 push.0 push.0

# u256 literal: 0 #
push.0 push.0 push.0 push.0 push.0 push.0 push.0 push.0
# u256 literal: 2 #
push.2 push.0 push.0 push.0 push.0 push.0 push.0 push.0

exec.u256::and
exec.u256::eq_unsafe
end
2 changes: 1 addition & 1 deletion crates/miden-integration-tests/tests/future.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use primitive_types::U256;
use crate::utils::{run_example, MidenResult};
use primitive_types::U256;

#[ignore]
#[test]
Expand Down
3 changes: 1 addition & 2 deletions crates/miden-integration-tests/tests/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#[macro_use]
extern crate quickcheck;
mod future;
mod milestone_1;
mod milestone_2;
mod optimization;
mod quickcheck_tests;
mod test;
mod utils;
77 changes: 77 additions & 0 deletions crates/miden-integration-tests/tests/optimization.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::utils::compile_example;
use indoc::indoc;
use papyrus::optimizer::optimize_ast;

#[test]
fn optimization_basic_constant_replacement() {
Expand Down Expand Up @@ -143,3 +144,79 @@ fn optimization_let_old_vars_die_v2() {
"},
);
}

#[test]
fn test_for_loop_to_repeat_statement_optimization() {
//Conditional is lt
let source_code = "
let result := 0

for { let i := 0 } lt(i, 100) { i := add(i, 1) }
{
result := mul(result, 2)
}
";

let parsed = papyrus::parser::parse_yul_syntax(source_code);
let ast = optimize_ast(parsed);

println!("{:#?}", ast);

// insta::assert_debug_snapshot!(ast);

// //Conditional is lt, mul
// let source_code = "
// let result := 0

// for { let i := 0 } lt(i, 100) { i := mul(i, 2) }
// {
// result := mul(result, 2)
// }
// "

// let parsed = parser::parse_yul_syntax(source_code);
// let ast = optimize_ast(parsed);
// insta::assert_debug_snapshot!(ast);

// //Conditional is gt, div
// let source_code = "
// let result := 0

// for { let i := 100 } gt(i, 0) { i := div(i, 2) }
// {
// result := mul(result, 2)
// }
// "

// let parsed = parser::parse_yul_syntax(source_code);
// let ast = optimize_ast(parsed);
// insta::assert_debug_snapshot!(ast);

// //Conditional is slt
// let source_code = "
// let result := 0

// for { let i := 100 } gt(i, 0) { i := sub(i, 1) }
// {
// result := mul(result, 2)
// }
// "

// let parsed = parser::parse_yul_syntax(source_code);
// let ast = optimize_ast(parsed);
// insta::assert_debug_snapshot!(ast);

// //Conditional is sgt
// let source_code = "
// let result := 0

// for { let i := 100 } gt(i, 0) { i := sub(i, 1) }
// {
// result := mul(result, 2)
// }
// "

// let parsed = parser::parse_yul_syntax(source_code);
// let ast = optimize_ast(parsed);
// insta::assert_debug_snapshot!(ast);
}
6 changes: 3 additions & 3 deletions crates/miden-integration-tests/tests/quickcheck_tests.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use crate::utils::{miden_to_u256, MidenResult};
use miden_core::StarkField;
use quickcheck::{Arbitrary, Gen, TestResult};
use quickcheck_macros::quickcheck;
use scribe::{
use papyrus::{
executor::execute,
utils::{convert_u256_to_pushes, join_u32s_to_u256, load_all_procs, split_u256_to_u32s},
};
use quickcheck::{Arbitrary, Gen, TestResult};
use quickcheck_macros::quickcheck;

#[derive(Clone, Debug)]
struct U256(primitive_types::U256);
Expand Down
2 changes: 1 addition & 1 deletion crates/miden-integration-tests/tests/test.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use primitive_types::U256;
use crate::utils::{run_example, MidenResult};
use primitive_types::U256;

#[test]
fn integration_math() {
Expand Down
39 changes: 17 additions & 22 deletions crates/miden-integration-tests/tests/utils.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,29 @@
use colored::*;
use miden_core::StarkField;
use papyrus::executor;
use papyrus::miden_generator;
use papyrus::miden_generator::CompileOptions;
use papyrus::optimizer::optimize_ast;
use papyrus::parser;
use papyrus::type_inference::infer_types;
use papyrus::types::expressions_to_tree;
use papyrus::types::YulFile;
use primitive_types::U256;
use scribe::ast_optimization::optimize_ast;
use scribe::executor;
use scribe::miden_generator;
use scribe::miden_generator::CompileOptions;
use scribe::parser;
use scribe::type_inference::infer_types;
use scribe::types::expressions_to_tree;
use scribe::types::YulFile;
use std::fs;
pub enum MidenResult {
U256(primitive_types::U256),
U32(u32),
}

fn print_title(s: &str) {
let s1 = format!("=== {} ===", s).blue().bold();
println!("{}", s1);
println!(" ");
}

//Function to display transpile Yul code and display each step of the transpilation process in the terminal.
//This function is only used to demonstrate what Scribe does in a easy to read format.
pub fn run_example(yul_code: &str, expected_output: MidenResult) {
fn print_title(s: &str) {
let s1 = format!("=== {} ===", s).blue().bold();
println!("{}", s1);
println!(" ");
}
println!();
println!();
print_title("Input Yul");
Expand All @@ -38,7 +39,7 @@ pub fn run_example(yul_code: &str, expected_output: MidenResult) {
println!("{}", expressions_to_tree(&ast));
println!();

let (transpiler, miden_code) = miden_generator::transpile_program(ast, Default::default());
let miden_code = miden_generator::transpile_program(ast, Default::default());
let mut trimmed_miden_code = miden_code
.split('\n')
// .skip_while(|line| *line != "# end std lib #")
Expand All @@ -49,7 +50,7 @@ pub fn run_example(yul_code: &str, expected_output: MidenResult) {
print_title("Generated Miden Assembly");
println!("{}", trimmed_miden_code);
println!();
println!("Estimated cost: {}", transpiler.cost);
// println!("Estimated cost: {}", transpiler.cost);
println!();
fs::write(format!("./test_output.masm",), trimmed_miden_code)
.expect("Unable to write Miden to file.");
Expand Down Expand Up @@ -81,19 +82,13 @@ pub fn run_example(yul_code: &str, expected_output: MidenResult) {
}

pub fn compile_example(yul_code: &str, expected_output: &str) {
fn print_title(s: &str) {
let s1 = format!("=== {} ===", s).blue().bold();
println!("{}", s1);
println!(" ");
}

let parsed = parser::parse_yul_syntax(yul_code);

let ast = optimize_ast(parsed);

let ast = infer_types(&ast);

let (_, miden_code) = miden_generator::transpile_program(
let miden_code = miden_generator::transpile_program(
ast,
CompileOptions {
comments: false,
Expand Down
Loading