Skip to content

Commit

Permalink
chore: generate brillig opcode for simple identity unconstrained func…
Browse files Browse the repository at this point in the history
…tion (#1536)
  • Loading branch information
guipublic authored Jun 6, 2023
1 parent 6184f52 commit 823bd84
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 16 deletions.
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,6 +1,9 @@
use crate::ssa_refactor::ir::types::Type as SsaType;
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 @@ -727,8 +730,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

0 comments on commit 823bd84

Please sign in to comment.