1
0
Fork 0
mirror of synced 2025-09-23 12:18:44 +00:00

add release flag

This commit is contained in:
schaeff 2020-06-29 16:58:59 +02:00
parent 46dfcbbea9
commit 832d325088
6 changed files with 57 additions and 19 deletions

View file

@ -16,7 +16,7 @@ use std::io::{stdin, BufReader, BufWriter, Read, Write};
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::string::String; use std::string::String;
use zokrates_abi::Encode; use zokrates_abi::Encode;
use zokrates_core::compile::{check, compile, CompilationArtifacts, CompileError}; use zokrates_core::compile::{check, compile, CompilationArtifacts, CompileConfig, CompileError};
use zokrates_core::ir::{self, ProgEnum}; use zokrates_core::ir::{self, ProgEnum};
use zokrates_core::proof_system::*; use zokrates_core::proof_system::*;
use zokrates_core::typed_absy::abi::Abi; use zokrates_core::typed_absy::abi::Abi;
@ -261,6 +261,8 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
let hr_output_path = bin_output_path.to_path_buf().with_extension("ztf"); let hr_output_path = bin_output_path.to_path_buf().with_extension("ztf");
let is_release = sub_matches.occurrences_of("release") > 0;
let file = File::open(path.clone()) let file = File::open(path.clone())
.map_err(|why| format!("Couldn't open input file {}: {}", path.display(), why))?; .map_err(|why| format!("Couldn't open input file {}: {}", path.display(), why))?;
@ -281,9 +283,11 @@ fn cli_compile<T: Field>(sub_matches: &ArgMatches) -> Result<(), String> {
) )
}; };
let compilation_config = CompileConfig::default().with_is_release(is_release);
let resolver = FileSystemResolver::new(); let resolver = FileSystemResolver::new();
let artifacts: CompilationArtifacts<T> = let artifacts: CompilationArtifacts<T> =
compile(source, path, Some(&resolver)).map_err(|e| { compile(source, path, Some(&resolver), &compilation_config).map_err(|e| {
format!( format!(
"Compilation failed:\n\n{}", "Compilation failed:\n\n{}",
e.0.iter() e.0.iter()
@ -460,6 +464,10 @@ fn cli() -> Result<(), String> {
.long("light") .long("light")
.help("Skip logs and human readable output") .help("Skip logs and human readable output")
.required(false) .required(false)
).arg(Arg::with_name("release")
.long("release")
.help("Apply release optimisations to minimise constraint count. This increases compilation time.")
.required(false)
) )
) )
.subcommand(SubCommand::with_name("check") .subcommand(SubCommand::with_name("check")
@ -934,7 +942,7 @@ mod tests {
let resolver = FileSystemResolver::new(); let resolver = FileSystemResolver::new();
let _: CompilationArtifacts<Bn128Field> = let _: CompilationArtifacts<Bn128Field> =
compile(source, path, Some(&resolver)).unwrap(); compile(source, path, Some(&resolver), &CompileConfig::default()).unwrap();
} }
} }
@ -956,7 +964,7 @@ mod tests {
let resolver = FileSystemResolver::new(); let resolver = FileSystemResolver::new();
let artifacts: CompilationArtifacts<Bn128Field> = let artifacts: CompilationArtifacts<Bn128Field> =
compile(source, path, Some(&resolver)).unwrap(); compile(source, path, Some(&resolver), &CompileConfig::default()).unwrap();
let interpreter = ir::Interpreter::default(); let interpreter = ir::Interpreter::default();
@ -985,7 +993,7 @@ mod tests {
let resolver = FileSystemResolver::new(); let resolver = FileSystemResolver::new();
let artifacts: CompilationArtifacts<Bn128Field> = let artifacts: CompilationArtifacts<Bn128Field> =
compile(source, path, Some(&resolver)).unwrap(); compile(source, path, Some(&resolver), &CompileConfig::default()).unwrap();
let interpreter = ir::Interpreter::default(); let interpreter = ir::Interpreter::default();

View file

@ -9,7 +9,6 @@ use imports::{self, Importer};
use ir; use ir;
use macros; use macros;
use macros::process_macros; use macros::process_macros;
use optimizer::Optimize;
use semantics::{self, Checker}; use semantics::{self, Checker};
use static_analysis::Analyse; use static_analysis::Analyse;
use std::collections::HashMap; use std::collections::HashMap;
@ -141,12 +140,29 @@ impl fmt::Display for CompileErrorInner {
} }
} }
#[derive(Debug, Default)]
pub struct CompileConfig {
is_release: bool,
}
impl CompileConfig {
pub fn with_is_release(mut self, is_release: bool) -> Self {
self.is_release = is_release;
self
}
pub fn is_release(&self) -> bool {
self.is_release
}
}
type FilePath = PathBuf; type FilePath = PathBuf;
pub fn compile<T: Field, E: Into<imports::Error>>( pub fn compile<T: Field, E: Into<imports::Error>>(
source: String, source: String,
location: FilePath, location: FilePath,
resolver: Option<&dyn Resolver<E>>, resolver: Option<&dyn Resolver<E>>,
config: &CompileConfig,
) -> Result<CompilationArtifacts<T>, CompileErrors> { ) -> Result<CompilationArtifacts<T>, CompileErrors> {
let arena = Arena::new(); let arena = Arena::new();
@ -164,7 +180,7 @@ pub fn compile<T: Field, E: Into<imports::Error>>(
let ir_prog = ir::Prog::from(program_flattened); let ir_prog = ir::Prog::from(program_flattened);
// optimize // optimize
let optimized_ir_prog = ir_prog.optimize(); let optimized_ir_prog = ir_prog.optimize(config);
// analyse (check for unused constraints) // analyse (check for unused constraints)
let optimized_ir_prog = optimized_ir_prog.analyse(); let optimized_ir_prog = optimized_ir_prog.analyse();
@ -264,6 +280,7 @@ mod test {
source, source,
"./path/to/file".into(), "./path/to/file".into(),
None::<&dyn Resolver<io::Error>>, None::<&dyn Resolver<io::Error>>,
&CompileConfig::default(),
); );
assert!(res.unwrap_err().0[0] assert!(res.unwrap_err().0[0]
.value() .value()
@ -282,6 +299,7 @@ mod test {
source, source,
"./path/to/file".into(), "./path/to/file".into(),
None::<&dyn Resolver<io::Error>>, None::<&dyn Resolver<io::Error>>,
&CompileConfig::default(),
); );
assert!(res.is_ok()); assert!(res.is_ok());
} }
@ -362,6 +380,7 @@ struct Bar { field a }
main.to_string(), main.to_string(),
"main".into(), "main".into(),
Some(&CustomResolver), Some(&CustomResolver),
&CompileConfig::default(),
) )
.unwrap(); .unwrap();

View file

@ -11,18 +11,19 @@ mod tautology;
use self::duplicate::DuplicateOptimizer; use self::duplicate::DuplicateOptimizer;
use self::redefinition::RedefinitionOptimizer; use self::redefinition::RedefinitionOptimizer;
use self::tautology::TautologyOptimizer; use self::tautology::TautologyOptimizer;
use compile::CompileConfig;
use crate::ir::Prog; use crate::ir::Prog;
use zokrates_field::Field; use zokrates_field::Field;
pub trait Optimize { impl<T: Field> Prog<T> {
fn optimize(self) -> Self; pub fn optimize(self, config: &CompileConfig) -> Self {
} let r = if config.is_release() {
// remove redefinitions
impl<T: Field> Optimize for Prog<T> { RedefinitionOptimizer::optimize(self)
fn optimize(self) -> Self { } else {
// remove redefinitions self
let r = RedefinitionOptimizer::optimize(self); };
// remove constraints that are always satisfied // remove constraints that are always satisfied
let r = TautologyOptimizer::optimize(r); let r = TautologyOptimizer::optimize(r);
// remove duplicate constraints // remove duplicate constraints

View file

@ -5,7 +5,7 @@ extern crate zokrates_field;
use std::io; use std::io;
use zokrates_common::Resolver; use zokrates_common::Resolver;
use zokrates_core::{ use zokrates_core::{
compile::{compile, CompilationArtifacts}, compile::{compile, CompilationArtifacts, CompileConfig},
ir::Interpreter, ir::Interpreter,
}; };
use zokrates_field::Bn128Field; use zokrates_field::Bn128Field;
@ -28,6 +28,7 @@ fn out_of_range() {
source, source,
"./path/to/file".into(), "./path/to/file".into(),
None::<&dyn Resolver<io::Error>>, None::<&dyn Resolver<io::Error>>,
&CompileConfig::default(),
) )
.unwrap(); .unwrap();

View file

@ -5,7 +5,9 @@ use std::path::PathBuf;
use wasm_bindgen::prelude::*; use wasm_bindgen::prelude::*;
use zokrates_abi::{parse_strict, Decode, Encode, Inputs}; use zokrates_abi::{parse_strict, Decode, Encode, Inputs};
use zokrates_common::Resolver; use zokrates_common::Resolver;
use zokrates_core::compile::{compile as core_compile, CompilationArtifacts, CompileError}; use zokrates_core::compile::{
compile as core_compile, CompilationArtifacts, CompileConfig, CompileError,
};
use zokrates_core::imports::Error; use zokrates_core::imports::Error;
use zokrates_core::ir; use zokrates_core::ir;
use zokrates_core::proof_system::{self, ProofSystem, SolidityAbi}; use zokrates_core::proof_system::{self, ProofSystem, SolidityAbi};
@ -104,6 +106,7 @@ pub fn compile(
source.as_string().unwrap(), source.as_string().unwrap(),
PathBuf::from(location.as_string().unwrap()), PathBuf::from(location.as_string().unwrap()),
Some(&resolver), Some(&resolver),
&CompileConfig::default().with_is_release(true),
) )
.map_err(|ce| { .map_err(|ce| {
JsValue::from_str(&format!( JsValue::from_str(&format!(

View file

@ -75,7 +75,7 @@ fn compare<T: Field>(result: ir::ExecutionResult<T>, expected: TestResult) -> Re
} }
use std::io::{BufReader, Read}; use std::io::{BufReader, Read};
use zokrates_core::compile::compile; use zokrates_core::compile::{compile, CompileConfig};
use zokrates_fs_resolver::FileSystemResolver; use zokrates_fs_resolver::FileSystemResolver;
pub fn test_inner(test_path: &str) { pub fn test_inner(test_path: &str) {
@ -96,7 +96,13 @@ fn compile_and_run<T: Field>(t: Tests) {
let code = std::fs::read_to_string(&t.entry_point).unwrap(); let code = std::fs::read_to_string(&t.entry_point).unwrap();
let resolver = FileSystemResolver::new(); let resolver = FileSystemResolver::new();
let artifacts = compile::<T, _>(code, t.entry_point.clone(), Some(&resolver)).unwrap(); let artifacts = compile::<T, _>(
code,
t.entry_point.clone(),
Some(&resolver),
&CompileConfig::default().with_is_release(true),
)
.unwrap();
let bin = artifacts.prog(); let bin = artifacts.prog();