Skip to content

Commit

Permalink
fix(wasm): Apply transformation map to circuit debug information in `…
Browse files Browse the repository at this point in the history
…noir_wasm` (noir-lang#2635)
  • Loading branch information
TomAFrench authored Sep 11, 2023
1 parent a8a5395 commit 9da822f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 35 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions compiler/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ crate-type = ["cdylib"]
[dependencies]
acvm.workspace = true
fm.workspace = true
iter-extended.workspace = true
nargo.workspace = true
noirc_driver.workspace = true
noirc_frontend.workspace = true
wasm-bindgen.workspace = true
Expand Down
52 changes: 17 additions & 35 deletions compiler/wasm/src/compile.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use acvm::acir::circuit::Circuit;
use fm::FileManager;
use gloo_utils::format::JsValueSerdeExt;
use iter_extended::try_vecmap;
use log::debug;
use noirc_driver::{
add_dep, check_crate, compile_contracts, compile_no_check, prepare_crate, prepare_dependency,
CompileOptions, CompiledContract,
add_dep, compile_contracts, compile_main, prepare_crate, prepare_dependency, CompileOptions,
};
use noirc_frontend::{graph::CrateGraph, hir::Context};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -111,49 +110,32 @@ pub fn compile(args: JsValue) -> JsValue {
add_noir_lib(&mut context, dependency.as_str());
}

check_crate(&mut context, crate_id, false).expect("Crate check failed");
// For now we default to plonk width = 3, though we can add it as a parameter
let np_language = acvm::Language::PLONKCSat { width: 3 };
#[allow(deprecated)]
let is_opcode_supported = acvm::pwg::default_is_opcode_supported(np_language);

if options.contracts {
let compiled_contracts =
compile_contracts(&mut context, crate_id, &options.compile_options)
.expect("Contract compilation failed")
.0;

let optimized_contracts: Vec<CompiledContract> =
compiled_contracts.into_iter().map(optimize_contract).collect();
let optimized_contracts = try_vecmap(compiled_contracts, |contract| {
nargo::ops::optimize_contract(contract, np_language, &is_opcode_supported)
})
.expect("Contract optimization failed");

<JsValue as JsValueSerdeExt>::from_serde(&optimized_contracts).unwrap()
} else {
let main = context.get_main_function(&crate_id).expect("Could not find main function!");
let mut compiled_program =
compile_no_check(&context, &options.compile_options, main).expect("Compilation failed");
let compiled_program = compile_main(&mut context, crate_id, &options.compile_options)
.expect("Compilation failed")
.0;

compiled_program.circuit = optimize_circuit(compiled_program.circuit);

<JsValue as JsValueSerdeExt>::from_serde(&compiled_program).unwrap()
}
}
let optimized_program =
nargo::ops::optimize_program(compiled_program, np_language, &is_opcode_supported)
.expect("Program optimization failed");

fn optimize_contract(contract: CompiledContract) -> CompiledContract {
CompiledContract {
name: contract.name,
functions: contract
.functions
.into_iter()
.map(|mut func| {
func.bytecode = optimize_circuit(func.bytecode);
func
})
.collect(),
<JsValue as JsValueSerdeExt>::from_serde(&optimized_program).unwrap()
}
}

fn optimize_circuit(circuit: Circuit) -> Circuit {
// For now we default to plonk width = 3, though we can add it as a parameter
let language = acvm::Language::PLONKCSat { width: 3 };
#[allow(deprecated)]
let opcode_supported = acvm::pwg::default_is_opcode_supported(language);
acvm::compiler::compile(circuit, language, opcode_supported)
.expect("Circuit optimization failed")
.0
}

0 comments on commit 9da822f

Please sign in to comment.