Skip to content
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

chore: generate brillig opcode for simple identity unconstrained function #1536

Merged
merged 10 commits into from
Jun 6, 2023
Merged
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
251 changes: 116 additions & 135 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ edition = "2021"
rust-version = "1.66"

[workspace.dependencies]
acvm = "0.13.0"
acvm = "=0.13.3"
arena = { path = "crates/arena" }
fm = { path = "crates/fm" }
iter-extended = { path = "crates/iter-extended" }
Expand All @@ -38,7 +38,7 @@ noirc_frontend = { path = "crates/noirc_frontend" }
noir_wasm = { path = "crates/wasm" }

cfg-if = "1.0.0"
clap = { version = "4.1.4", features = ["derive"]}
clap = { version = "4.1.4", features = ["derive"] }
codespan = "0.9.5"
codespan-reporting = "0.9.5"
chumsky = { git = "https://github.com/jfecher/chumsky", rev = "ad9d312" }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.1"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
x = "3"

10 changes: 10 additions & 0 deletions crates/nargo_cli/tests/test_data_ssa_refactor/brillig/src/main.nr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// Tests a very simple program.
//
// The features being tested is assertion
fn main(x : Field) {
assert(x == identity(x));
}

unconstrained fn identity(x : Field) -> Field {
x
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use crate::ssa_refactor::ir::{instruction::Endian, types::NumericType};
use acvm::acir::brillig_vm::Opcode as BrilligOpcode;
use acvm::acir::{
brillig_vm::Opcode as BrilligOpcode,
circuit::brillig::{BrilligInputs, BrilligOutputs},
};

use super::{
errors::AcirGenError,
Expand Down Expand Up @@ -697,8 +700,21 @@ impl AcirContext {
id
}

pub(crate) fn brillig(&mut self, _code: Vec<BrilligOpcode>) {
todo!();
pub(crate) fn brillig(
&mut self,
code: Vec<BrilligOpcode>,
inputs: Vec<AcirVar>,
output_len: usize,
) -> Vec<AcirVar> {
let b_inputs =
vecmap(inputs, |i| BrilligInputs::Single(self.data[&i].to_expression().into_owned()));
let outputs = vecmap(0..output_len, |_| self.acir_ir.next_witness_index());
let outputs_var =
vecmap(&outputs, |witness_index| self.add_data(AcirVarData::Witness(*witness_index)));
let b_outputs = vecmap(outputs, BrilligOutputs::Simple);
self.acir_ir.brillig(code, b_inputs, b_outputs);

outputs_var
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
//! program as it is being converted from SSA form.
use super::errors::AcirGenError;
use acvm::acir::{
brillig_vm::Opcode as BrilligOpcode,
circuit::{
brillig::{Brillig as AcvmBrillig, BrilligInputs, BrilligOutputs},
directives::{LogInfo, QuotientDirective},
opcodes::{BlackBoxFuncCall, FunctionInput, Opcode as AcirOpcode},
},
Expand Down Expand Up @@ -464,6 +466,22 @@ impl GeneratedAcir {

Ok(q_witness)
}

pub(crate) fn brillig(
&mut self,
code: Vec<BrilligOpcode>,
inputs: Vec<BrilligInputs>,
outputs: Vec<BrilligOutputs>,
) {
let opcode = AcirOpcode::Brillig(AcvmBrillig {
inputs,
outputs,
foreign_call_results: Vec::new(),
bytecode: code,
predicate: None,
});
self.push_opcode(opcode);
}
}

/// This function will return the number of inputs that a blackbox function
Expand Down
11 changes: 4 additions & 7 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use super::{
ssa_gen::Ssa,
};
use crate::brillig::{artifact::BrilligArtifact, Brillig};
use iter_extended::vecmap;
use noirc_abi::{AbiType, FunctionSignature, Sign};

pub(crate) use acir_ir::generated_acir::GeneratedAcir;
Expand Down Expand Up @@ -90,11 +91,6 @@ impl Context {
brillig: Brillig,
allow_log_ops: bool,
) -> GeneratedAcir {
assert_eq!(
ssa.functions.len(),
1,
"expected only a single function to be present with all other functions being inlined."
);
let main_func = ssa.main();
let dfg = &main_func.dfg;
let entry_block = &dfg[main_func.entry_block()];
Expand Down Expand Up @@ -214,10 +210,11 @@ impl Context {
"expected an intrinsic/brillig call, but found {func:?}. All ACIR methods should be inlined"
),
RuntimeType::Brillig => {
let inputs = vecmap(arguments, |&a| {self.convert_ssa_value(a, dfg)});
// Generate the brillig code of the function
let code = BrilligArtifact::default().link(&brillig[*id]);
self.acir_context.brillig(code);
(result_ids.to_vec(), Vec::new())
let outputs = self.acir_context.brillig(code, inputs, result_ids.len());
(result_ids.to_vec(), outputs)
}
}
}
Expand Down
15 changes: 9 additions & 6 deletions crates/noirc_evaluator/src/ssa_refactor/opt/inlining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,7 @@ impl<'function> PerFunctionContext<'function> {
match self.context.builder[id] {
Value::Function(id) => Some(id),
Value::Intrinsic(_) => None,
_ => {
self.context.failed_to_inline_a_call = true;
None
}
_ => None,
}
}

Expand Down Expand Up @@ -326,9 +323,15 @@ impl<'function> PerFunctionContext<'function> {
Instruction::Call { func, arguments } => match self.get_function(*func) {
Some(function) => match ssa.functions[&function].runtime() {
RuntimeType::Acir => self.inline_function(ssa, *id, function, arguments),
RuntimeType::Brillig => self.push_instruction(*id),
RuntimeType::Brillig => {
self.context.failed_to_inline_a_call = true;
self.push_instruction(*id);
}
},
None => self.push_instruction(*id),
None => {
self.context.failed_to_inline_a_call = true;
self.push_instruction(*id);
}
},
_ => self.push_instruction(*id),
}
Expand Down
4 changes: 4 additions & 0 deletions crates/wasm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,9 @@ wasm-logger = "0.2.0"
console_error_panic_hook = "0.1.7"
gloo-utils = { version = "0.1", features = ["serde"] }

# This is an unused dependency, we are adding it
# so that we can enable the js feature in getrandom.
getrandom = { version = "*", features = ["js"] }

[build-dependencies]
build-data = "0.1.3"
7 changes: 7 additions & 0 deletions crates/wasm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@ use serde::{Deserialize, Serialize};
use std::str::FromStr;
use wasm_bindgen::prelude::*;

// This dependency is not used. We import it
// to bypass the `unused_crate_dependencies` lint.
//
// It is being imported as we get errors regarding the
// js feature not being enabled.
use getrandom as _;

mod circuit;
mod compile;

Expand Down