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): rename impl method to follow Rust guideline #782

Merged
merged 7 commits into from
Feb 11, 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/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl Anchor {
ctx: &SsaContext,
id: NodeId,
) -> Result<(), RuntimeError> {
let ins = ctx.get_instruction(id);
let ins = ctx.instruction(id);
let (array_id, index, is_load) = Anchor::get_mem_op(&ins.operation);
self.use_array(array_id, ctx.mem[array_id].len as usize);
let prev_list = self.mem_list.get_mut(&array_id).unwrap();
Expand Down
8 changes: 4 additions & 4 deletions crates/noirc_evaluator/src/ssa/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ pub fn short_circuit_instructions(
Some(target),
));
let nop = instructions[0];
debug_assert_eq!(ctx.get_instruction(nop).operation, node::Operation::Nop);
debug_assert_eq!(ctx.instruction(nop).operation, node::Operation::Nop);
let mut stack = vec![nop, unreachable_ins];
//return:
for &i in instructions.iter() {
Expand Down Expand Up @@ -463,13 +463,13 @@ pub fn zero_instructions(ctx: &mut SsaContext, instructions: &[NodeId], avoid: O
let mut zeros = HashMap::new();
let mut zero_keys = Vec::new();
for i in instructions {
let ins = ctx.get_instruction(*i);
let ins = ctx.instruction(*i);
if ins.res_type != node::ObjectType::NotAnObject {
zeros.insert(ins.res_type, ctx.zero_with_type(ins.res_type));
} else if let node::Operation::Return(ret) = &ins.operation {
for i in ret {
if *i != NodeId::dummy() {
let typ = ctx.get_object_type(*i);
let typ = ctx.object_type(*i);
assert_ne!(typ, node::ObjectType::NotAnObject);
zero_keys.push(typ);
} else {
Expand All @@ -488,7 +488,7 @@ pub fn zero_instructions(ctx: &mut SsaContext, instructions: &[NodeId], avoid: O
}

for i in instructions.iter().filter(|x| Some(*x) != avoid) {
let ins = ctx.get_mut_instruction(*i);
let ins = ctx.instruction_mut(*i);
if ins.res_type != node::ObjectType::NotAnObject {
ins.mark = Mark::ReplaceWith(zeros[&ins.res_type]);
} else if ins.operation.opcode() != Opcode::Nop {
Expand Down
12 changes: 6 additions & 6 deletions crates/noirc_evaluator/src/ssa/code_gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ impl IRGenerator {
rhs: NodeId,
op: UnaryOp,
) -> Result<NodeId, RuntimeError> {
let rhs_type = self.context.get_object_type(rhs);
let rhs_type = self.context.object_type(rhs);
match op {
UnaryOp::Minus => {
let lhs = self.context.zero_with_type(rhs_type);
Expand All @@ -288,7 +288,7 @@ impl IRGenerator {
rhs: NodeId,
op: BinaryOpKind,
) -> Result<NodeId, RuntimeError> {
let lhs_type = self.context.get_object_type(lhs);
let lhs_type = self.context.object_type(lhs);
// Get the opcode from the infix operator
let opcode = Operation::Binary(Binary::from_ast(op, lhs_type, lhs, rhs));
let op_type = self.context.get_result_type(&opcode, lhs_type);
Expand Down Expand Up @@ -429,7 +429,7 @@ impl IRGenerator {
let definition = Definition::Local(id);
match value {
Value::Single(node_id) => {
let object_type = self.context.get_object_type(node_id);
let object_type = self.context.object_type(node_id);
let value = self.bind_variable(
name.to_owned(),
Some(definition.clone()),
Expand All @@ -453,7 +453,7 @@ impl IRGenerator {
fn bind_fresh_pattern(&mut self, basename: &str, value: Value) -> Result<Value, RuntimeError> {
match value {
Value::Single(node_id) => {
let object_type = self.context.get_object_type(node_id);
let object_type = self.context.object_type(node_id);
self.bind_variable(basename.to_owned(), None, object_type, node_id)
}
Value::Tuple(field_values) => {
Expand Down Expand Up @@ -622,7 +622,7 @@ impl IRGenerator {
Expression::Index(indexed_expr) => {
// Evaluate the 'array' expression
let expr_node = self.codegen_expression(&indexed_expr.collection)?.unwrap_id();
let array = match self.context.get_object_type(expr_node) {
let array = match self.context.object_type(expr_node) {
ObjectType::Pointer(array_id) => &self.context.mem[array_id],
other => unreachable!("Expected Pointer type, found {:?}", other),
};
Expand Down Expand Up @@ -827,7 +827,7 @@ impl IRGenerator {

//Fixup the jump
if let node::Instruction { operation: Operation::Jeq(_, target), .. } =
self.context.get_mut_instruction(jump_ins)
self.context.instruction_mut(jump_ins)
{
*target = block2;
}
Expand Down
32 changes: 16 additions & 16 deletions crates/noirc_evaluator/src/ssa/conditional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl DecisionTree {
ass_value = self[predicate].value.unwrap_or_else(NodeId::dummy);
}
assert!(!ctx.is_zero(ass_value), "code should have been already simplified");
let ins1 = ctx.get_instruction(ins_id);
let ins1 = ctx.instruction(ins_id);
match &ins1.operation {
Operation::Call { returned_arrays, .. } => {
for a in returned_arrays {
Expand All @@ -529,7 +529,7 @@ impl DecisionTree {
let ins = ins1.clone();
if short_circuit {
stack.set_zero(ctx, ins.res_type);
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
if ins2.res_type == ObjectType::NotAnObject {
ins2.mark = Mark::Deleted;
} else {
Expand All @@ -540,7 +540,7 @@ impl DecisionTree {
Operation::Phi { block_args, .. } => {
if ctx[stack.block].kind == BlockType::IfJoin {
assert_eq!(block_args.len(), 2);
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
ins2.operation = Operation::Cond {
condition: ass_cond,
val_true: block_args[0].0,
Expand Down Expand Up @@ -604,7 +604,7 @@ impl DecisionTree {
return Ok(false);
}
if ctx.under_assumption(cond) {
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
ins2.operation = Operation::Binary(crate::node::Binary {
lhs: binary_op.lhs,
rhs: binary_op.rhs,
Expand Down Expand Up @@ -651,7 +651,7 @@ impl DecisionTree {
stack.push(dummy);
stack.push(cond);
//store the conditional value
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
ins2.operation = Operation::Store {
array_id: *array_id,
index: *index,
Expand All @@ -670,7 +670,7 @@ impl DecisionTree {
let name = array.name.to_string() + DUPLICATED;
ctx.new_array(&name, array.element_type, array.len, None);
let array_dup = ctx.mem.last_id();
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
ins2.res_type = ObjectType::Pointer(array_dup);

let mut memcpy_stack = StackFrame::new(stack.block);
Expand Down Expand Up @@ -699,7 +699,7 @@ impl DecisionTree {
} => {
if ctx.under_assumption(ass_value) {
assert!(*ins_pred == AssumptionId::dummy());
let mut ins2 = ctx.get_mut_instruction(ins_id);
let mut ins2 = ctx.instruction_mut(ins_id);
ins2.operation = Operation::Call {
func: *func_id,
arguments: arguments.clone(),
Expand All @@ -726,7 +726,7 @@ impl DecisionTree {
Some(stack.block),
));
stack.push(cond);
let ins2 = ctx.get_mut_instruction(ins_id);
let ins2 = ctx.instruction_mut(ins_id);
ins2.operation = Operation::Constrain(cond, *loc);
if ctx.is_zero(*expr) {
stack.push(ins_id);
Expand Down Expand Up @@ -760,15 +760,15 @@ impl DecisionTree {
// 1. find potential matches between the two blocks
let mut candidates = Vec::new();
let keep_call_and_store = |node_id: NodeId| -> bool {
let ins = ctx.get_instruction(node_id);
let ins = ctx.instruction(node_id);
matches!(ins.operation.opcode(), Opcode::Call(_) | Opcode::Store(_))
};
let l_iter = left.iter().enumerate().filter(|&i| keep_call_and_store(*i.1));
let mut r_iter = right.iter().enumerate().filter(|&i| keep_call_and_store(*i.1));
for left_node in l_iter {
let left_ins = ctx.get_instruction(*left_node.1);
let left_ins = ctx.instruction(*left_node.1);
for right_node in r_iter.by_ref() {
let right_ins = ctx.get_instruction(*right_node.1);
let right_ins = ctx.instruction(*right_node.1);
match (&left_ins.operation, &right_ins.operation) {
(
Operation::Call { func: left_func, returned_arrays: left_arrays, .. },
Expand Down Expand Up @@ -809,8 +809,8 @@ impl DecisionTree {
result.extend_from_slice(&right[right_pos..i.right.0]);
right_pos = i.right.0;
//merge i:
let left_ins = ctx.get_instruction(left[left_pos]);
let right_ins = ctx.get_instruction(right[right_pos]);
let left_ins = ctx.instruction(left[left_pos]);
let right_ins = ctx.instruction(right[right_pos]);
let assumption = &self[ctx[block_id].assumption];

let mut to_merge = Vec::new();
Expand All @@ -832,7 +832,7 @@ impl DecisionTree {
val_true: *a.1,
val_false: right_arg[a.0],
};
let typ = ctx.get_object_type(*a.1);
let typ = ctx.object_type(*a.1);
to_merge.push(Instruction::new(op, typ, Some(block_id)));
}
Operation::Call {
Expand Down Expand Up @@ -876,10 +876,10 @@ impl DecisionTree {
if let Operation::Call { arguments, .. } = &mut merged_op {
*arguments = merge_ids;
}
let left_ins = ctx.get_mut_instruction(left[left_pos]);
let left_ins = ctx.instruction_mut(left[left_pos]);
left_ins.mark = node::Mark::ReplaceWith(right[right_pos]);
}
let ins1 = ctx.get_mut_instruction(right[right_pos]);
let ins1 = ctx.instruction_mut(right[right_pos]);
ins1.operation = merged_op;
result.push(ins1.id);
left_pos += 1;
Expand Down
32 changes: 16 additions & 16 deletions crates/noirc_evaluator/src/ssa/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ impl SsaContext {
if id == NodeId::dummy() {
return false;
}
let typ = self.get_object_type(id);
let typ = self.object_type(id);
if let Some(one) = self.find_const_with_type(&BigUint::one(), typ) {
id == one
} else {
Expand All @@ -95,7 +95,7 @@ impl SsaContext {
if id == NodeId::dummy() {
return false;
}
let typ = self.get_object_type(id);
let typ = self.object_type(id);
if let Some(zero) = self.find_const_with_type(&BigUint::zero(), typ) {
id == zero
} else {
Expand Down Expand Up @@ -374,7 +374,7 @@ impl SsaContext {
id
}

pub fn get_ssa_func(&self, func_id: FuncId) -> Option<&SSAFunction> {
pub fn ssa_func(&self, func_id: FuncId) -> Option<&SSAFunction> {
self.functions.get(&func_id)
}

Expand All @@ -386,7 +386,7 @@ impl SsaContext {
}

pub fn try_get_ssa_func(&self, id: NodeId) -> Option<&SSAFunction> {
self.try_get_func_id(id).and_then(|id| self.get_ssa_func(id))
self.try_get_func_id(id).and_then(|id| self.ssa_func(id))
}

pub fn dummy_id() -> arena::Index {
Expand All @@ -401,7 +401,7 @@ impl SsaContext {
self.nodes.get_mut(id.0)
}

pub fn get_object_type(&self, id: NodeId) -> node::ObjectType {
pub fn object_type(&self, id: NodeId) -> node::ObjectType {
self[id].get_type()
}

Expand All @@ -413,11 +413,11 @@ impl SsaContext {
None
}

pub fn get_instruction(&self, id: NodeId) -> &node::Instruction {
pub fn instruction(&self, id: NodeId) -> &node::Instruction {
self.try_get_instruction(id).expect("Index not found or not an instruction")
}

pub fn get_mut_instruction(&mut self, id: NodeId) -> &mut node::Instruction {
pub fn instruction_mut(&mut self, id: NodeId) -> &mut node::Instruction {
self.try_get_mut_instruction(id).expect("Index not found or not an instruction")
}

Expand Down Expand Up @@ -753,7 +753,7 @@ impl SsaContext {
let mut fb = Some(&self[self.first_block]);
while let Some(block) = fb {
for iter in &block.instructions {
let ins = self.get_instruction(*iter);
let ins = self.instruction(*iter);
acir.acir_gen_instruction(ins, evaluator, self).map_err(RuntimeError::from)?;
}
//TODO we should rather follow the jumps
Expand All @@ -775,7 +775,7 @@ impl SsaContext {
}
}

let v_type = self.get_object_type(phi_root);
let v_type = self.object_type(phi_root);
let operation = Operation::Phi { root: phi_root, block_args: vec![] };
let new_phi = Instruction::new(operation, v_type, Some(target_block));
let phi_id = self.add_instruction(new_phi);
Expand Down Expand Up @@ -823,8 +823,8 @@ impl SsaContext {
index: Option<NodeId>,
rhs: NodeId,
) -> Result<NodeId, RuntimeError> {
let lhs_type = self.get_object_type(lhs);
let rhs_type = self.get_object_type(rhs);
let lhs_type = self.object_type(lhs);
let rhs_type = self.object_type(rhs);

let mut ret_array = None;
if let Some(Instruction {
Expand All @@ -849,7 +849,7 @@ impl SsaContext {
//Issue #579: we initialize the array, unless it is also in arguments in which case it is already initialized.
let mut init = false;
for i in arguments.clone() {
if let ObjectType::Pointer(b) = self.get_object_type(i) {
if let ObjectType::Pointer(b) = self.object_type(i) {
if a == b {
init = true;
}
Expand Down Expand Up @@ -979,8 +979,8 @@ impl SsaContext {
stack_frame: &mut inline::StackFrame,
block_id: BlockId,
) -> NodeId {
let lhs_type = self.get_object_type(lhs);
let rhs_type = self.get_object_type(rhs);
let lhs_type = self.object_type(lhs);
let rhs_type = self.object_type(rhs);
if let ObjectType::Pointer(a) = lhs_type {
//Array
let b = stack_frame.get_or_default(a);
Expand Down Expand Up @@ -1040,7 +1040,7 @@ impl SsaContext {
let block1 = self[exit_block].predecessor[0];
let block2 = self[exit_block].predecessor[1];

let a_type = self.get_object_type(a);
let a_type = self.object_type(a);

let name = format!("if_{}_ret{c}", exit_block.0.into_raw_parts().0);
*c += 1;
Expand Down Expand Up @@ -1093,7 +1093,7 @@ impl SsaContext {
}

pub fn function_already_compiled(&self, func_id: FuncId) -> bool {
self.get_ssa_func(func_id).is_some()
self.ssa_func(func_id).is_some()
}

pub fn get_or_create_opcode_node_id(&mut self, opcode: builtin::Opcode) -> NodeId {
Expand Down
10 changes: 5 additions & 5 deletions crates/noirc_evaluator/src/ssa/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl SSAFunction {
let mut decision = DecisionTree::new(&ir_gen.context);
let mut builder = TreeBuilder::new(self.entry_block);
for (arg, _) in &self.arguments {
if let ObjectType::Pointer(a) = ir_gen.context.get_object_type(*arg) {
if let ObjectType::Pointer(a) = ir_gen.context.object_type(*arg) {
builder.stack.created_arrays.insert(a, self.entry_block);
}
}
Expand All @@ -88,7 +88,7 @@ impl SSAFunction {
);
if self.entry_block != exit {
for i in &stack {
ir_gen.context.get_mut_instruction(*i).parent_block = self.entry_block;
ir_gen.context.instruction_mut(*i).parent_block = self.entry_block;
}
}

Expand Down Expand Up @@ -249,7 +249,7 @@ impl IRGenerator {
self.context.new_instruction(call_op.clone(), ObjectType::NotAnObject)?;

if let Some(id) = self.context.try_get_func_id(func) {
let callee = self.context.get_ssa_func(id).unwrap().idx;
let callee = self.context.ssa_func(id).unwrap().idx;
if let Some(caller) = self.function_context {
update_call_graph(&mut self.context.call_graph, caller, callee);
}
Expand All @@ -269,7 +269,7 @@ impl IRGenerator {
*i.1,
)?);
}
let ssa_func = self.context.get_ssa_func(func_id).unwrap();
let ssa_func = self.context.ssa_func(func_id).unwrap();
let func_arguments = ssa_func.arguments.clone();
for (caller_arg, func_arg) in arguments.iter().zip(func_arguments) {
let mut is_array_result = false;
Expand Down Expand Up @@ -299,7 +299,7 @@ impl IRGenerator {

// Fixup the returned_arrays, they will be incorrectly tracked for higher order functions
// otherwise.
self.context.get_mut_instruction(call_instruction).operation = call_op;
self.context.instruction_mut(call_instruction).operation = call_op;
result_ids
}

Expand Down
Loading