-
Notifications
You must be signed in to change notification settings - Fork 361
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
Introduce compilation mode #988
Open
Schaeff
wants to merge
6
commits into
develop
Choose a base branch
from
add-compiler-mode-flag
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
900bcef
introduce compilation mode, remove entry point checks for lib mode
Schaeff 2a33d92
use bin artifacts for compile
Schaeff 3fbfe17
fix zjs
Schaeff fa7f3d8
fix test
Schaeff 272c002
merge dev, fix conflicts, use default on compile config, add changelog
Schaeff 5c318ad
Update 988-schaeff
Schaeff File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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 @@ | ||
Introduce a check mode flag `--mode` and stop requiring a main function in `lib` mode |
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,5 @@ | ||
def main(): | ||
return | ||
|
||
def main(field a): | ||
return |
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 |
---|---|---|
|
@@ -25,13 +25,19 @@ use zokrates_field::Field; | |
use zokrates_pest_ast as pest; | ||
|
||
#[derive(Debug)] | ||
pub struct CompilationArtifacts<T: Field> { | ||
prog: ir::Prog<T>, | ||
pub struct BinCompilationArtifacts<P> { | ||
prog: P, | ||
abi: Abi, | ||
} | ||
|
||
impl<T: Field> CompilationArtifacts<T> { | ||
pub fn prog(&self) -> &ir::Prog<T> { | ||
#[derive(Debug)] | ||
pub enum CompilationArtifacts<P> { | ||
Bin(BinCompilationArtifacts<P>), | ||
Lib, | ||
} | ||
|
||
impl<P> BinCompilationArtifacts<P> { | ||
pub fn prog(&self) -> &P { | ||
&self.prog | ||
} | ||
|
||
|
@@ -162,9 +168,25 @@ impl fmt::Display for CompileErrorInner { | |
} | ||
} | ||
|
||
#[derive(Debug, Serialize, Deserialize, Clone, Copy, PartialEq)] | ||
pub enum CompileMode { | ||
Bin, | ||
Lib, | ||
} | ||
|
||
impl Default for CompileMode { | ||
fn default() -> Self { | ||
Self::Bin | ||
} | ||
} | ||
|
||
#[derive(Debug, Default, Serialize, Deserialize, Clone)] | ||
pub struct CompileConfig { | ||
#[serde(default)] | ||
pub mode: CompileMode, | ||
#[serde(default)] | ||
pub allow_unconstrained_variables: bool, | ||
#[serde(default)] | ||
pub isolate_branches: bool, | ||
} | ||
|
||
|
@@ -177,6 +199,10 @@ impl CompileConfig { | |
self.isolate_branches = flag; | ||
self | ||
} | ||
pub fn mode(mut self, mode: CompileMode) -> Self { | ||
self.mode = mode; | ||
self | ||
} | ||
} | ||
|
||
type FilePath = PathBuf; | ||
|
@@ -186,37 +212,42 @@ pub fn compile<T: Field, E: Into<imports::Error>>( | |
location: FilePath, | ||
resolver: Option<&dyn Resolver<E>>, | ||
config: &CompileConfig, | ||
) -> Result<CompilationArtifacts<T>, CompileErrors> { | ||
) -> Result<BinCompilationArtifacts<ir::Prog<T>>, CompileErrors> { | ||
let arena = Arena::new(); | ||
|
||
let (typed_ast, abi) = check_with_arena(source, location.clone(), resolver, config, &arena)?; | ||
|
||
// flatten input program | ||
log::debug!("Flatten"); | ||
let program_flattened = Flattener::flatten(typed_ast, config); | ||
|
||
// constant propagation after call resolution | ||
log::debug!("Propagate flat program"); | ||
let program_flattened = program_flattened.propagate(); | ||
|
||
// convert to ir | ||
log::debug!("Convert to IR"); | ||
let ir_prog = ir::Prog::from(program_flattened); | ||
|
||
// optimize | ||
log::debug!("Optimise IR"); | ||
let optimized_ir_prog = ir_prog.optimize(); | ||
|
||
// analyse ir (check constraints) | ||
log::debug!("Analyse IR"); | ||
let optimized_ir_prog = optimized_ir_prog | ||
.analyse() | ||
.map_err(|e| CompileErrorInner::from(e).in_file(location.as_path()))?; | ||
|
||
Ok(CompilationArtifacts { | ||
prog: optimized_ir_prog, | ||
abi, | ||
}) | ||
let artifacts = check_with_arena(source, location.clone(), resolver, config, &arena)?; | ||
|
||
match artifacts { | ||
CompilationArtifacts::Lib => unreachable!(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be reachable when called from If my understanding is correct, one could set the mode to lib and call compile which would throw here with no clear message. |
||
CompilationArtifacts::Bin(artifacts) => { | ||
// flatten input program | ||
log::debug!("Flatten"); | ||
let program_flattened = Flattener::flatten(artifacts.prog, config); | ||
|
||
// constant propagation after call resolution | ||
log::debug!("Propagate flat program"); | ||
let program_flattened = program_flattened.propagate(); | ||
|
||
// convert to ir | ||
log::debug!("Convert to IR"); | ||
let ir_prog = ir::Prog::from(program_flattened); | ||
|
||
// optimize | ||
log::debug!("Optimise IR"); | ||
let optimized_ir_prog = ir_prog.optimize(); | ||
|
||
// analyse ir (check constraints) | ||
log::debug!("Analyse IR"); | ||
let optimized_ir_prog = optimized_ir_prog | ||
.analyse() | ||
.map_err(|e| CompileErrorInner::from(e).in_file(location.as_path()))?; | ||
|
||
Ok(BinCompilationArtifacts { | ||
prog: optimized_ir_prog, | ||
abi: artifacts.abi, | ||
}) | ||
} | ||
} | ||
} | ||
|
||
pub fn check<T: Field, E: Into<imports::Error>>( | ||
|
@@ -236,7 +267,7 @@ fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>( | |
resolver: Option<&dyn Resolver<E>>, | ||
config: &CompileConfig, | ||
arena: &'ast Arena<String>, | ||
) -> Result<(ZirProgram<'ast, T>, Abi), CompileErrors> { | ||
) -> Result<CompilationArtifacts<ZirProgram<'ast, T>>, CompileErrors> { | ||
let source = arena.alloc(source); | ||
|
||
log::debug!("Parse program with entry file {}", location.display()); | ||
|
@@ -246,17 +277,27 @@ fn check_with_arena<'ast, T: Field, E: Into<imports::Error>>( | |
log::debug!("Check semantics"); | ||
|
||
// check semantics | ||
let typed_ast = Checker::check(compiled) | ||
let typed_ast = Checker::check(compiled, config.mode) | ||
.map_err(|errors| CompileErrors(errors.into_iter().map(CompileError::from).collect()))?; | ||
|
||
let main_module = typed_ast.main.clone(); | ||
match config.mode { | ||
CompileMode::Bin => { | ||
let main_module = typed_ast.main.clone(); | ||
|
||
log::debug!("Run static analysis"); | ||
|
||
log::debug!("Run static analysis"); | ||
// analyse (unroll and constant propagation) | ||
let (prog, abi) = typed_ast.analyse(config).map_err(|e| { | ||
CompileErrors(vec![CompileErrorInner::from(e).in_file(&main_module)]) | ||
})?; | ||
|
||
// analyse (unroll and constant propagation) | ||
typed_ast | ||
.analyse(config) | ||
.map_err(|e| CompileErrors(vec![CompileErrorInner::from(e).in_file(&main_module)])) | ||
Ok(CompilationArtifacts::Bin(BinCompilationArtifacts { | ||
prog, | ||
abi, | ||
})) | ||
} | ||
CompileMode::Lib => Ok(CompilationArtifacts::Lib), | ||
} | ||
} | ||
|
||
pub fn parse_program<'ast, T: Field, E: Into<imports::Error>>( | ||
|
@@ -322,7 +363,7 @@ mod test { | |
return foo() | ||
"# | ||
.to_string(); | ||
let res: Result<CompilationArtifacts<Bn128Field>, CompileErrors> = compile( | ||
let res: Result<BinCompilationArtifacts<ir::Prog<Bn128Field>>, CompileErrors> = compile( | ||
source, | ||
"./path/to/file".into(), | ||
None::<&dyn Resolver<io::Error>>, | ||
|
@@ -341,7 +382,7 @@ mod test { | |
return 1 | ||
"# | ||
.to_string(); | ||
let res: Result<CompilationArtifacts<Bn128Field>, CompileErrors> = compile( | ||
let res: Result<BinCompilationArtifacts<ir::Prog<Bn128Field>>, CompileErrors> = compile( | ||
source, | ||
"./path/to/file".into(), | ||
None::<&dyn Resolver<io::Error>>, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should use something like this here
so it's a bit cleaner in js:
mode: "lib"
instead ofmode: "Lib"
. It's also possible to put#[serde(rename_all = "lowercase")]
before the enum definition.We should also update typescript type information file:
https://github.com/Zokrates/ZoKrates/blob/add-compiler-mode-flag/zokrates_js/index.d.ts#L12