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

fix(ssa refactor): Fix panic in acir-gen from multiplying values of different types #1769

Merged
merged 4 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion crates/noirc_evaluator/src/ssa_refactor/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ impl Context {
(Type::Numeric(lhs_type), Type::Numeric(rhs_type)) => {
assert_eq!(
lhs_type, rhs_type,
"lhs and rhs types in a binary operation are always the same"
"lhs and rhs types in {:?} are not the same",
binary
);
Type::Numeric(lhs_type)
}
Expand Down
19 changes: 11 additions & 8 deletions crates/noirc_evaluator/src/ssa_refactor/ir/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,12 @@ impl Instruction {
if let (Some((array, _)), Some(index)) = (array, index) {
let index =
index.try_to_u64().expect("Expected array index to fit in u64") as usize;
assert!(index < array.len());
SimplifiedTo(array[index])
} else {
None

if index < array.len() {
return SimplifiedTo(array[index]);
}
}
None
}
Instruction::ArraySet { array, index, value } => {
let array = dfg.get_array_constant(*array);
Expand All @@ -290,11 +291,13 @@ impl Instruction {
if let (Some((array, element_type)), Some(index)) = (array, index) {
let index =
index.try_to_u64().expect("Expected array index to fit in u64") as usize;
assert!(index < array.len());
SimplifiedTo(dfg.make_array(array.update(index, *value), element_type))
} else {
None

if index < array.len() {
let new_array = dfg.make_array(array.update(index, *value), element_type);
return SimplifiedTo(new_array);
}
}
None
}
Instruction::Truncate { value, bit_size, .. } => {
if let Some((numeric_constant, typ)) = dfg.get_numeric_constant_with_type(*value) {
Expand Down
68 changes: 66 additions & 2 deletions crates/noirc_evaluator/src/ssa_refactor/opt/flatten_cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,16 +396,80 @@ impl<'f> Context<'f> {
self.insert_instruction_with_typevars(enable_side_effects, None);
}

/// Merge two values a and b from separate basic blocks to a single value. This
/// function would return the result of `if c { a } else { b }` as `c*a + (!c)*b`.
/// Merge two values a and b from separate basic blocks to a single value.
/// If these two values are numeric, the result will be
/// `then_condition * then_value + else_condition * else_value`.
/// Otherwise, if the values being merged are arrays, a new array will be made
/// recursively from combining each element of both input arrays.
///
/// It is currently an error to call this function on reference or function values
/// as it is less clear how to merge these.
fn merge_values(
&mut self,
then_condition: ValueId,
else_condition: ValueId,
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
match self.inserter.function.dfg.type_of_value(then_value) {
Type::Numeric(_) => {
self.merge_numeric_values(then_condition, else_condition, then_value, else_value)
}
Type::Array(element_types, len) => {
let mut merged = im::Vector::new();

for i in 0..len {
for (element_index, element_type) in element_types.iter().enumerate() {
let index = ((i * element_types.len() + element_index) as u128).into();
let index = self.inserter.function.dfg.make_constant(index, Type::field());

let typevars = Some(vec![element_type.clone()]);

let mut get_element = |array, typevars| {
let get = Instruction::ArrayGet { array, index };
self.insert_instruction_with_typevars(get, typevars).first()
};

let then_element = get_element(then_value, typevars.clone());
let else_element = get_element(else_value, typevars);

merged.push_back(self.merge_values(
then_condition,
else_condition,
then_element,
else_element,
));
}
}

self.inserter.function.dfg.make_array(merged, element_types)
}
Type::Reference => panic!("Cannot return references from an if expression"),
Type::Function => panic!("Cannot return functions from an if expression"),
}
}

/// Merge two numeric values a and b from separate basic blocks to a single value. This
/// function would return the result of `if c { a } else { b }` as `c*a + (!c)*b`.
fn merge_numeric_values(
&mut self,
then_condition: ValueId,
else_condition: ValueId,
then_value: ValueId,
else_value: ValueId,
) -> ValueId {
let block = self.inserter.function.entry_block();
let then_type = self.inserter.function.dfg.type_of_value(then_value);
let else_type = self.inserter.function.dfg.type_of_value(else_value);
assert_eq!(
then_type, else_type,
"Expected values merged to be of the same type but found {then_type} and {else_type}"
);

// We must cast the bool conditions to the actual numeric type used by each value.
let then_condition = self.insert_instruction(Instruction::Cast(then_condition, then_type));
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
let else_condition = self.insert_instruction(Instruction::Cast(else_condition, else_type));

let mul = Instruction::binary(BinaryOp::Mul, then_condition, then_value);
let then_value =
self.inserter.function.dfg.insert_instruction_and_results(mul, block, None).first();
Expand Down