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(ssa refactor): Add support for the distinct keyword #1810

Merged
merged 4 commits into from
Jun 23, 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
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 @@
x = "3"
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Example that uses the distinct keyword
fn main(x: pub Field) -> distinct pub [Field;2] {
[x+1, x]
}
3 changes: 2 additions & 1 deletion crates/noirc_evaluator/src/ssa_refactor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ pub(crate) fn optimize_into_acir(
allow_log_ops: bool,
print_ssa_passes: bool,
) -> GeneratedAcir {
let abi_distinctness = program.return_distinctness;
let ssa = ssa_gen::generate_ssa(program).print(print_ssa_passes, "Initial SSA:");
let brillig = ssa.to_brillig();
ssa.inline_functions()
Expand All @@ -49,7 +50,7 @@ pub(crate) fn optimize_into_acir(
.print(print_ssa_passes, "After Constant Folding:")
.dead_instruction_elimination()
.print(print_ssa_passes, "After Dead Instruction Elimination:")
.into_acir(brillig, allow_log_ops)
.into_acir(brillig, abi_distinctness, allow_log_ops)
}

/// Compiles the Program into ACIR and applies optimizations to the arithmetic gates
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl GeneratedAcir {
/// This means you cannot multiply an infinite amount of `Expression`s together.
/// Once the `Expression` goes over degree-2, then it needs to be reduced to a `Witness`
/// which has degree-1 in order to be able to continue the multiplication chain.
fn create_witness_for_expression(&mut self, expression: &Expression) -> Witness {
pub(crate) fn create_witness_for_expression(&mut self, expression: &Expression) -> Witness {
let fresh_witness = self.next_witness_index();

// Create a constraint that sets them to be equal to each other
Expand Down
32 changes: 29 additions & 3 deletions crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ use super::{
},
ssa_gen::Ssa,
};
use acvm::FieldElement;
use acvm::{acir::native_types::Expression, FieldElement};
use iter_extended::vecmap;

pub(crate) use acir_ir::generated_acir::GeneratedAcir;
use noirc_abi::AbiDistinctness;

mod acir_ir;

Expand Down Expand Up @@ -71,9 +72,34 @@ impl AcirValue {
}

impl Ssa {
pub(crate) fn into_acir(self, brillig: Brillig, allow_log_ops: bool) -> GeneratedAcir {
pub(crate) fn into_acir(
self,
brillig: Brillig,
abi_distinctness: AbiDistinctness,
allow_log_ops: bool,
) -> GeneratedAcir {
let context = Context::default();
context.convert_ssa(self, brillig, allow_log_ops)
let mut generated_acir = context.convert_ssa(self, brillig, allow_log_ops);

match abi_distinctness {
AbiDistinctness::Distinct => {
// Create a witness for each return witness we have
// to guarantee that the return witnesses are distinct
let distinct_return_witness: Vec<_> = generated_acir
.return_witnesses
.clone()
.into_iter()
.map(|return_witness| {
generated_acir
.create_witness_for_expression(&Expression::from(return_witness))
})
.collect();

generated_acir.return_witnesses = distinct_return_witness;
generated_acir
}
AbiDistinctness::DuplicationAllowed => generated_acir,
}
}
}

Expand Down