Skip to content

Commit ad5d889

Browse files
authored
chore(ssa): Remove values from array type (#771)
* intialise abi arrays with store instructions * code review - style * code review * code review
1 parent e981c7c commit ad5d889

File tree

7 files changed

+49
-53
lines changed

7 files changed

+49
-53
lines changed

crates/noirc_evaluator/src/ssa/acir_gen/acir_mem.rs

+7-20
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,9 @@ impl AcirMem {
5555
// Loads the associated `InternalVar` for the element
5656
// in the `array` at the given `offset`.
5757
//
58-
// First we check if the address of the array element
59-
// is in the memory_map. If not, then we check the `array`
58+
// We check if the address of the array element
59+
// is in the memory_map.
6060
//
61-
// We do not check the `MemArray` initially because the
62-
// `MemoryMap` holds the most updated InternalVar
63-
// associated to the array element.
64-
// TODO: specify what could change between the two?
6561
//
6662
// Returns `None` if `offset` is out of bounds.
6763
pub(crate) fn load_array_element_constant_index(
@@ -76,19 +72,10 @@ impl AcirMem {
7672
}
7773

7874
// Check the memory_map to see if the element is there
79-
if let Some(internal_var) = self.array_map_mut(array.id).get(&offset) {
80-
return Some(internal_var.clone());
81-
};
82-
83-
let array_element = array.values[offset as usize].clone();
84-
85-
// Compiler sanity check
86-
//
87-
// Since the only time we take the array values
88-
// from the array is when it has been defined in the
89-
// ABI. We know that it must have been initialized with a `Witness`
90-
array_element.cached_witness().expect("ICE: since the value is not in the memory_map it must have came from the ABI, so it is a Witness");
91-
92-
Some(array_element)
75+
let array_element = self
76+
.array_map_mut(array.id)
77+
.get(&offset)
78+
.expect("ICE: Could not find value at index {offset}");
79+
Some(array_element.clone())
9380
}
9481
}

crates/noirc_evaluator/src/ssa/acir_gen/internal_var.rs

-3
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,6 @@ impl InternalVar {
4545
pub(crate) fn set_id(&mut self, id: NodeId) {
4646
self.id = Some(id)
4747
}
48-
pub(crate) fn cached_witness(&self) -> &Option<Witness> {
49-
&self.cached_witness
50-
}
5148

5249
pub fn to_expression(&self) -> Expression {
5350
if let Some(w) = self.cached_witness {

crates/noirc_evaluator/src/ssa/conditional.rs

+4-15
Original file line numberDiff line numberDiff line change
@@ -438,17 +438,6 @@ impl DecisionTree {
438438
Ok(())
439439
}
440440

441-
//assigns the arrays to the block where they are seen for the first time
442-
fn new_array(ctx: &SsaContext, array_id: super::mem::ArrayId, stack: &mut StackFrame) {
443-
if let std::collections::hash_map::Entry::Vacant(e) = stack.created_arrays.entry(array_id) {
444-
if !ctx.mem[array_id].values.is_empty() {
445-
e.insert(ctx.first_block);
446-
} else {
447-
e.insert(stack.block);
448-
}
449-
}
450-
}
451-
452441
fn short_circuit(
453442
ctx: &mut SsaContext,
454443
stack: &mut StackFrame,
@@ -511,17 +500,17 @@ impl DecisionTree {
511500
match &ins1.operation {
512501
Operation::Call { returned_arrays, .. } => {
513502
for a in returned_arrays {
514-
DecisionTree::new_array(ctx, a.0, stack);
503+
stack.new_array(a.0);
515504
}
516505
}
517506
Operation::Store { array_id, index, .. } => {
518507
if *index != NodeId::dummy() {
519-
DecisionTree::new_array(ctx, *array_id, stack);
508+
stack.new_array(*array_id);
520509
}
521510
}
522511
_ => {
523512
if let ObjectType::Pointer(a) = ins1.res_type {
524-
DecisionTree::new_array(ctx, a, stack);
513+
stack.new_array(a);
525514
}
526515
}
527516
}
@@ -718,7 +707,7 @@ impl DecisionTree {
718707
val_false: ctx.one(),
719708
};
720709
if ctx.is_zero(*expr) {
721-
stack.clear();
710+
stack.stack.clear();
722711
}
723712
let cond = ctx.add_instruction(Instruction::new(
724713
operation,

crates/noirc_evaluator/src/ssa/context.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -907,12 +907,25 @@ impl SsaContext {
907907
}
908908

909909
fn init_array(&mut self, array_id: ArrayId, stack_frame: &mut StackFrame) {
910-
let len = self.mem[array_id].len;
910+
let len = self.mem[array_id].len as usize;
911911
let e_type = self.mem[array_id].element_type;
912-
for i in 0..len {
912+
let values = vec![self.zero_with_type(e_type); len];
913+
self.init_array_from_values(array_id, values, stack_frame);
914+
}
915+
916+
pub fn init_array_from_values(
917+
&mut self,
918+
array_id: ArrayId,
919+
values: Vec<NodeId>,
920+
stack_frame: &mut StackFrame,
921+
) {
922+
let len = self.mem[array_id].len as usize;
923+
let e_type = self.mem[array_id].element_type;
924+
assert_eq!(len, values.len());
925+
for (i, v) in values.iter().enumerate() {
913926
let index =
914927
self.get_or_create_const(FieldElement::from(i as i128), ObjectType::Unsigned(32));
915-
let op_a = Operation::Store { array_id, index, value: self.zero_with_type(e_type) };
928+
let op_a = Operation::Store { array_id, index, value: *v };
916929
self.new_instruction_inline(op_a, e_type, stack_frame);
917930
}
918931
}

crates/noirc_evaluator/src/ssa/inline.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,6 @@ impl StackFrame {
114114
}
115115
}
116116

117-
pub fn clear(&mut self) {
118-
self.stack.clear();
119-
self.array_map.clear();
120-
self.created_arrays.clear();
121-
self.lca_cache.clear();
122-
}
123-
124117
pub fn push(&mut self, ins_id: NodeId) {
125118
self.stack.push(ins_id);
126119
}
@@ -179,6 +172,13 @@ impl StackFrame {
179172
true
180173
}
181174
}
175+
176+
//assigns the arrays to the block where they are seen for the first time
177+
pub fn new_array(&mut self, array_id: ArrayId) {
178+
if let std::collections::hash_map::Entry::Vacant(e) = self.created_arrays.entry(array_id) {
179+
e.insert(self.block);
180+
}
181+
}
182182
}
183183

184184
//inline a function call

crates/noirc_evaluator/src/ssa/mem.rs

-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::ssa::{
2-
acir_gen::InternalVar,
32
context::SsaContext,
43
node,
54
node::{Node, NodeId},
@@ -30,7 +29,6 @@ impl ArrayId {
3029
pub struct MemArray {
3130
pub id: ArrayId,
3231
pub element_type: node::ObjectType, //type of elements
33-
pub values: Vec<InternalVar>,
3432
pub name: String,
3533
pub def: Definition,
3634
pub len: u32, //number of elements
@@ -51,7 +49,6 @@ impl MemArray {
5149
id,
5250
element_type: of,
5351
name: name.to_string(),
54-
values: Vec::new(),
5552
def: definition,
5653
len,
5754
adr: 0,

crates/noirc_evaluator/src/ssa/ssa_gen.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,21 @@ impl IrGenerator {
9696
) -> NodeId {
9797
let element_type = self.get_object_type_from_abi(el_type);
9898
let (v_id, array_idx) = self.new_array(name, element_type, len as u32, ident_def);
99-
self.context.mem[array_idx].values = vecmap(witness, |w| w.into());
100-
self.context.get_current_block_mut().update_variable(v_id, v_id);
99+
let values = vecmap(witness.iter().enumerate(), |(i, w)| {
100+
let mut var = Variable::new(
101+
element_type,
102+
format!("{name}_{i}"),
103+
None,
104+
self.context.current_block,
105+
);
106+
var.witness = Some(*w);
107+
self.context.add_variable(var, None)
108+
});
109+
let mut stack_frame = crate::ssa::inline::StackFrame::new(self.context.current_block);
110+
self.context.init_array_from_values(array_idx, values, &mut stack_frame);
111+
let block = self.context.get_current_block_mut();
112+
block.instructions.extend_from_slice(&stack_frame.stack);
113+
block.update_variable(v_id, v_id);
101114
v_id
102115
}
103116

0 commit comments

Comments
 (0)