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): Predicate for store instruction #829

Merged
merged 9 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
2 changes: 1 addition & 1 deletion crates/noirc_evaluator/src/ssa/acir_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ impl Acir {
load::evaluate(*array_id, *index, acir_mem, var_cache, evaluator, ctx)
}
Operation::Store { .. } => {
store::evaluate(&ins.operation, memory_map, var_cache, evaluator, ctx)
store::evaluate(&ins.operation, acir_mem, var_cache, evaluator, ctx)
}
Operation::Nop => None,
i @ Operation::Jne(..)
Expand Down
24 changes: 17 additions & 7 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/condition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
},
Evaluator,
};
use acvm::FieldElement;
use acvm::{acir::native_types::Expression, FieldElement};

pub(crate) fn evaluate(
condition: NodeId,
Expand All @@ -19,11 +19,21 @@ pub(crate) fn evaluate(
let cond = var_cache.get_or_compute_internal_var_unwrap(condition, evaluator, ctx);
let l_c = var_cache.get_or_compute_internal_var_unwrap(lhs, evaluator, ctx);
let r_c = var_cache.get_or_compute_internal_var_unwrap(rhs, evaluator, ctx);
let sub = constraints::subtract(l_c.expression(), FieldElement::one(), r_c.expression());
let result = constraints::add(
&constraints::mul_with_witness(evaluator, cond.expression(), &sub),
FieldElement::one(),
r_c.expression(),
);
let result =
evaluate_expression(cond.expression(), l_c.expression(), r_c.expression(), evaluator);
Some(result.into())
}

pub fn evaluate_expression(
condition: &Expression,
lhs: &Expression,
rhs: &Expression,
evaluator: &mut Evaluator,
) -> Expression {
let sub = constraints::subtract(lhs, FieldElement::one(), rhs);
constraints::add(
&constraints::mul_with_witness(evaluator, condition, &sub),
FieldElement::one(),
rhs,
)
}
31 changes: 12 additions & 19 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/store.rs
Original file line number Diff line number Diff line change
@@ -1,23 +1,18 @@
use acvm::FieldElement;

use crate::{
ssa::{
acir_gen::{
constraints::{add, mul_with_witness, subtract},
internal_var_cache::InternalVarCache,
memory_map::MemoryMap,
InternalVar,
},
acir_gen::{acir_mem::AcirMem, internal_var_cache::InternalVarCache, InternalVar},
context::SsaContext,
mem::{self},
node::Operation,
},
Evaluator,
};

use super::condition;

pub(crate) fn evaluate(
store: &Operation,
memory_map: &mut MemoryMap,
acir_mem: &mut AcirMem,
var_cache: &mut InternalVarCache,
evaluator: &mut Evaluator,
ctx: &SsaContext,
Expand All @@ -30,7 +25,6 @@ pub(crate) fn evaluate(
match index.to_const() {
Some(index) => {
let idx = mem::Memory::as_u32(index);
let absolute_adr = ctx.mem[array_id].absolute_adr(idx);
let value_with_predicate = if let Some(predicate) = predicate {
if predicate.is_dummy() || ctx.is_one(predicate) {
value
Expand All @@ -39,22 +33,21 @@ pub(crate) fn evaluate(
} else {
let pred =
var_cache.get_or_compute_internal_var_unwrap(predicate, evaluator, ctx);
let dummy = memory_map
let dummy_load = acir_mem
.load_array_element_constant_index(&ctx.mem[array_id], idx)
.unwrap();
let x = mul_with_witness(evaluator, pred.expression(), value.expression());
let y = subtract(
dummy.expression(),
FieldElement::one(),
&mul_with_witness(evaluator, pred.expression(), dummy.expression()),
let result = condition::evaluate_expression(
pred.expression(),
value.expression(),
dummy_load.expression(),
evaluator,
);
InternalVar::from(add(&x, FieldElement::one(), &y))
result.into()
}
} else {
value
};

memory_map.insert(absolute_adr, value_with_predicate);
acir_mem.insert(array_id, idx, value_with_predicate);
//we do not generate constraint, so no output.
None
}
Expand Down
5 changes: 2 additions & 3 deletions crates/noirc_evaluator/src/ssa/anchor.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::errors::{RuntimeError, RuntimeErrorKind};
use crate::ssa::{
conditional::DecisionTree,
context::SsaContext,
mem::ArrayId,
node::{NodeId, ObjectType, Opcode, Operation},
Expand Down Expand Up @@ -252,9 +253,7 @@ impl Anchor {
}
Operation::Store { index, value, predicate, .. } => {
if !ctx.maybe_distinct(*index, b_idx) {
if ctx.is_one(crate::ssa::conditional::DecisionTree::unwrap_predicate(
ctx, predicate,
)) {
if ctx.is_one(DecisionTree::unwrap_predicate(ctx, predicate)) {
return Some(CseAction::ReplaceWith(*value));
} else {
return Some(CseAction::Keep);
Expand Down
47 changes: 19 additions & 28 deletions crates/noirc_evaluator/src/ssa/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -374,11 +374,13 @@ impl DecisionTree {
merged_ins = self.synchronize(ctx, &left_ins, &right_ins, left);
}
let mut modified = false;
// write the merged instructions to the block
super::optimizations::cse_block(ctx, left, &mut merged_ins, &mut modified)?;
if modified {
// A second round is necessary when the synchronization optimizes function calls between the two branches.
// In that case, the first cse updates the result instructions to the same call and then
// the second cse can (and must) then simplify identical result instructions.
// We clear the list because we want to perform the cse on the block instructions
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
merged_ins.clear();
guipublic marked this conversation as resolved.
Show resolved Hide resolved
super::optimizations::cse_block(ctx, left, &mut merged_ins, &mut modified)?;
}
Expand Down Expand Up @@ -731,36 +733,25 @@ impl DecisionTree {
) -> Option<NodeId> {
match (condition1, condition2) {
(None, None) => None,
(Some(condition), None) | (None, Some(condition)) => {
if condition.is_dummy() {
None
} else {
Some(condition)
}
(Some(cond), other) | (other, Some(cond)) if cond.is_dummy() => {
Self::and_conditions(None, other, stack_frame, ctx)
}
(Some(cond), None) | (None, Some(cond)) => Some(cond),
jfecher marked this conversation as resolved.
Show resolved Hide resolved
(Some(cond1), Some(cond2)) => {
if cond1.is_dummy() {
Self::and_conditions(None, condition2, stack_frame, ctx)
} else if cond2.is_dummy() {
Self::and_conditions(None, condition1, stack_frame, ctx)
} else if cond1 == cond2 {
condition1
} else {
let op = Operation::Binary(node::Binary {
lhs: cond1,
rhs: cond2,
operator: BinaryOp::Mul,
predicate: None,
});
let cond = ctx.add_instruction(Instruction::new(
op,
ObjectType::Boolean,
Some(stack_frame.block),
));
optimizations::simplify_id(ctx, cond).unwrap();
stack_frame.push(cond);
Some(cond)
}
let op = Operation::Binary(node::Binary {
lhs: cond1,
rhs: cond2,
operator: BinaryOp::Mul,
predicate: None,
});
let cond = ctx.add_instruction(Instruction::new(
op,
ObjectType::Boolean,
Some(stack_frame.block),
));
optimizations::simplify_id(ctx, cond).unwrap();
stack_frame.push(cond);
Some(cond)
}
}
}
Expand Down
7 changes: 1 addition & 6 deletions crates/noirc_evaluator/src/ssa/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -937,12 +937,7 @@ impl SsaContext {
for (i, v) in values.iter().enumerate() {
let index =
self.get_or_create_const(FieldElement::from(i as i128), ObjectType::Unsigned(32));
let op_a = Operation::Store {
array_id,
index,
value: self.zero_with_type(e_type),
predicate: None,
};
let op_a = Operation::Store { array_id, index, value: *v, predicate: None };
self.new_instruction_inline(op_a, e_type, stack_frame);
}
}
Expand Down