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

feat: decompose Instruction::Cast to have an explicit truncation instruction #3946

Merged
merged 16 commits into from
Jan 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
41 changes: 3 additions & 38 deletions compiler/noirc_evaluator/src/ssa/acir_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -469,9 +469,9 @@ impl Context {
self.acir_context.assert_eq_var(lhs, rhs, assert_message.clone())?;
}
}
Instruction::Cast(value_id, typ) => {
let result_acir_var = self.convert_ssa_cast(value_id, typ, dfg)?;
self.define_result_var(dfg, instruction_id, result_acir_var);
Instruction::Cast(value_id, _) => {
let acir_var = self.convert_numeric_value(*value_id, dfg)?;
self.define_result_var(dfg, instruction_id, acir_var);
}
Instruction::Call { func, arguments } => {
let result_ids = dfg.instruction_results(instruction_id);
Expand Down Expand Up @@ -1633,41 +1633,6 @@ impl Context {
}
}

/// Returns an `AcirVar` that is constrained to fit in the target type by truncating the input.
/// If the target cast is to a `NativeField`, no truncation is required so the cast becomes a
/// no-op.
fn convert_ssa_cast(
&mut self,
value_id: &ValueId,
typ: &Type,
dfg: &DataFlowGraph,
) -> Result<AcirVar, RuntimeError> {
let (variable, incoming_type) = match self.convert_value(*value_id, dfg) {
AcirValue::Var(variable, typ) => (variable, typ),
AcirValue::DynamicArray(_) | AcirValue::Array(_) => {
unreachable!("Cast is only applied to numerics")
}
};
let target_numeric = match typ {
Type::Numeric(numeric) => numeric,
_ => unreachable!("Can only cast to a numeric"),
};
match target_numeric {
NumericType::NativeField => {
// Casting into a Field as a no-op
Ok(variable)
}
NumericType::Unsigned { bit_size } | NumericType::Signed { bit_size } => {
let max_bit_size = incoming_type.bit_size();
if max_bit_size <= *bit_size {
// Incoming variable already fits into target bit size - this is a no-op
return Ok(variable);
}
self.acir_context.truncate_var(variable, *bit_size, max_bit_size)
}
}
}

/// Returns an `AcirVar`that is constrained to be result of the truncation.
fn convert_ssa_truncate(
&mut self,
Expand Down
19 changes: 19 additions & 0 deletions compiler/noirc_evaluator/src/ssa/function_builder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,25 @@ impl FunctionBuilder {
/// Insert a cast instruction at the end of the current block.
/// Returns the result of the cast instruction.
pub(crate) fn insert_cast(&mut self, value: ValueId, typ: Type) -> ValueId {
fn get_type_size(typ: &Type) -> u32 {
match typ {
Type::Numeric(NumericType::NativeField) => FieldElement::max_num_bits(),
Type::Numeric(
NumericType::Unsigned { bit_size } | NumericType::Signed { bit_size },
) => *bit_size,
_ => unreachable!("Should only be called with primitive types"),
}
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

let incoming_type = self.type_of_value(value);
let incoming_type_size = get_type_size(&incoming_type);
let target_type_size = get_type_size(&typ);
let value = if target_type_size < incoming_type_size {
self.insert_truncate(value, target_type_size, incoming_type_size)
} else {
value
};
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

self.insert_instruction(Instruction::Cast(value, typ), None).first()
}
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
Loading