Skip to content

Commit

Permalink
feat: add transform functions
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench committed Jan 22, 2024
1 parent bb1aa68 commit 0570f70
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
5 changes: 3 additions & 2 deletions compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,8 @@ pub fn compile(
})?
.0;

let optimized_contract = nargo::ops::optimize_contract(compiled_contract, expression_width);
let optimized_contract =
nargo::ops::transform_contract(compiled_contract, expression_width);

let compile_output = generate_contract_artifact(optimized_contract);
Ok(JsCompileResult::new(compile_output))
Expand All @@ -205,7 +206,7 @@ pub fn compile(
})?
.0;

let optimized_program = nargo::ops::optimize_program(compiled_program, expression_width);
let optimized_program = nargo::ops::transform_program(compiled_program, expression_width);

let compile_output = generate_program_artifact(optimized_program);
Ok(JsCompileResult::new(compile_output))
Expand Down
6 changes: 4 additions & 2 deletions tooling/nargo/src/ops/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ use crate::{package::Package, workspace::Workspace};

use rayon::prelude::*;

use super::{transform_contract, transform_program};

/// Compiles workspace.
///
/// # Errors
Expand Down Expand Up @@ -89,7 +91,7 @@ pub fn compile_program(
noirc_driver::compile_main(&mut context, crate_id, compile_options, cached_program)?;

// Apply backend specific optimizations.
let optimized_program = crate::ops::optimize_program(program, expression_width);
let optimized_program = transform_program(program, expression_width);

Ok((optimized_program, warnings))
}
Expand All @@ -105,7 +107,7 @@ pub fn compile_contract(
let (contract, warnings) =
noirc_driver::compile_contract(&mut context, crate_id, compile_options)?;

let optimized_contract = crate::ops::optimize_contract(contract, expression_width);
let optimized_contract = transform_contract(contract, expression_width);

Ok((optimized_contract, warnings))
}
Expand Down
3 changes: 3 additions & 0 deletions tooling/nargo/src/ops/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ pub use self::compile::{compile_contract, compile_program, compile_workspace};
pub use self::execute::execute_circuit;
pub use self::foreign_calls::{DefaultForeignCallExecutor, ForeignCallExecutor};
pub use self::optimize::{optimize_contract, optimize_program};
pub use self::transform::{transform_contract, transform_program};

pub use self::test::{run_test, TestStatus};

mod compile;
mod execute;
mod foreign_calls;
mod optimize;
mod test;
mod transform;
18 changes: 4 additions & 14 deletions tooling/nargo/src/ops/optimize.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
use acvm::ExpressionWidth;
use iter_extended::vecmap;
use noirc_driver::{CompiledContract, CompiledProgram};

pub fn optimize_program(
mut program: CompiledProgram,
expression_width: ExpressionWidth,
) -> CompiledProgram {
let (optimized_circuit, location_map) =
acvm::compiler::compile(program.circuit, expression_width);

pub fn optimize_program(mut program: CompiledProgram) -> CompiledProgram {
let (optimized_circuit, location_map) = acvm::compiler::optimize(program.circuit);
program.circuit = optimized_circuit;
program.debug.update_acir(location_map);
program
}

pub fn optimize_contract(
contract: CompiledContract,
expression_width: ExpressionWidth,
) -> CompiledContract {
pub fn optimize_contract(contract: CompiledContract) -> CompiledContract {
let functions = vecmap(contract.functions, |mut func| {
let (optimized_bytecode, location_map) =
acvm::compiler::compile(func.bytecode, expression_width);
let (optimized_bytecode, location_map) = acvm::compiler::optimize(func.bytecode);
func.bytecode = optimized_bytecode;
func.debug.update_acir(location_map);
func
Expand Down
30 changes: 30 additions & 0 deletions tooling/nargo/src/ops/transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
use acvm::ExpressionWidth;
use iter_extended::vecmap;
use noirc_driver::{CompiledContract, CompiledProgram};

pub fn transform_program(
mut program: CompiledProgram,
expression_width: ExpressionWidth,
) -> CompiledProgram {
let (optimized_circuit, location_map) =
acvm::compiler::compile(program.circuit, expression_width);

program.circuit = optimized_circuit;
program.debug.update_acir(location_map);
program
}

pub fn transform_contract(
contract: CompiledContract,
expression_width: ExpressionWidth,
) -> CompiledContract {
let functions = vecmap(contract.functions, |mut func| {
let (optimized_bytecode, location_map) =
acvm::compiler::compile(func.bytecode, expression_width);
func.bytecode = optimized_bytecode;
func.debug.update_acir(location_map);
func
});

CompiledContract { functions, ..contract }
}

0 comments on commit 0570f70

Please sign in to comment.