Skip to content

Commit 7ccf8e9

Browse files
committed
working non-homogenous arr accesses for block params
1 parent 45b3787 commit 7ccf8e9

File tree

5 files changed

+314
-65
lines changed

5 files changed

+314
-65
lines changed

compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

+85-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,11 @@ impl AcirType {
6161
AcirType::NumericType(NumericType::NativeField)
6262
}
6363

64+
/// Returns an unsigned type of the specified bit size
65+
pub(crate) fn unsigned(bit_size: u32) -> Self {
66+
AcirType::NumericType(NumericType::Unsigned { bit_size })
67+
}
68+
6469
/// Returns a boolean type
6570
fn boolean() -> Self {
6671
AcirType::NumericType(NumericType::Unsigned { bit_size: 1 })
@@ -74,6 +79,16 @@ impl AcirType {
7479
};
7580
matches!(numeric_type, NumericType::Signed { .. })
7681
}
82+
83+
pub(crate) fn from_slice(value: &SsaType, size: usize) -> Self {
84+
match value {
85+
SsaType::Slice(elements) => {
86+
let elements = elements.iter().map(|e| e.into()).collect();
87+
AcirType::Array(elements, size)
88+
}
89+
_ => unreachable!("Attempted to use {value} where a slice was expected"),
90+
}
91+
}
7792
}
7893

7994
impl From<SsaType> for AcirType {
@@ -90,7 +105,7 @@ impl<'a> From<&'a SsaType> for AcirType {
90105
let elements = elements.iter().map(|e| e.into()).collect();
91106
AcirType::Array(elements, *size)
92107
}
93-
_ => unreachable!("The type {value} cannot be represented in ACIR"),
108+
_ => unreachable!("The type {value} cannot be represented in ACIR"),
94109
}
95110
}
96111
}
@@ -1220,6 +1235,8 @@ impl AcirContext {
12201235
if let Ok(some_value) = value.clone().into_var() {
12211236
values.push(self.var_to_witness(some_value)?);
12221237
} else {
1238+
dbg!(optional_values.clone());
1239+
dbg!("got here");
12231240
nested = true;
12241241
break;
12251242
}
@@ -1228,10 +1245,74 @@ impl AcirContext {
12281245
}
12291246
};
12301247
// we do not initialize nested arrays. This means that non-const indexes are not supported for nested arrays
1231-
if !nested {
1232-
self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
1233-
}
1248+
// if !nested {
1249+
// self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
1250+
// }
1251+
dbg!(nested);
1252+
self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
1253+
1254+
1255+
Ok(())
1256+
}
1257+
1258+
pub(crate) fn initialize_array_new(
1259+
&mut self,
1260+
block_id: BlockId,
1261+
len: usize,
1262+
optional_value: Option<AcirValue>,
1263+
) -> Result<(), InternalError> {
1264+
let initialized_values = match optional_value {
1265+
None => {
1266+
let zero = self.add_constant(FieldElement::zero());
1267+
let zero_witness = self.var_to_witness(zero)?;
1268+
vec![zero_witness; len]
1269+
}
1270+
Some(optional_value) => {
1271+
let mut values = Vec::new();
1272+
self.initialize_array_inner(&mut values, optional_value)?;
1273+
values
1274+
}
1275+
};
12341276

1277+
self.acir_ir.push_opcode(Opcode::MemoryInit { block_id, init: initialized_values });
1278+
1279+
Ok(())
1280+
}
1281+
1282+
fn initialize_array_inner(
1283+
&mut self,
1284+
witnesses: &mut Vec<Witness>,
1285+
input: AcirValue,
1286+
) -> Result<(), InternalError> {
1287+
match input {
1288+
AcirValue::Var(var, _) => {
1289+
witnesses.push(self.var_to_witness(var)?);
1290+
}
1291+
AcirValue::Array(values) => {
1292+
for value in values {
1293+
self.initialize_array_inner(witnesses, value)?;
1294+
}
1295+
}
1296+
// AcirValue::DynamicArray(AcirDynamicArray { block_id, len }) => {
1297+
AcirValue::DynamicArray(_) => {
1298+
panic!("dyn array should already be initialized");
1299+
}
1300+
// AcirValue::DynamicArray(AcirDynamicArray { block_id, len }) => {
1301+
// for i in 0..len {
1302+
// // We generate witnesses corresponding to the array values
1303+
// let index = AcirValue::Var(
1304+
// self.add_constant(FieldElement::from(i as u128)),
1305+
// AcirType::NumericType(NumericType::NativeField),
1306+
// );
1307+
1308+
// let index_var = index.into_var()?;
1309+
// let value_read_var =
1310+
// self.read_from_memory(block_id, &index_var)?;
1311+
1312+
// witnesses.push(self.var_to_witness(value_read_var)?);
1313+
// }
1314+
// }
1315+
}
12351316
Ok(())
12361317
}
12371318
}

0 commit comments

Comments
 (0)