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): codegen missing check for unary minus #2413

Merged
merged 4 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "unary_operators"
type = "bin"
authors = [""]
compiler_version = "0.10.3"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
fn main() {
let x = -1;
assert(x == 1 - 2);

let y: i32 = -1;
assert(x == 1 - 2);
}
1 change: 1 addition & 0 deletions crates/noirc_evaluator/src/ssa/ssa_gen/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ impl<'a> FunctionContext<'a> {
if operator_requires_swapped_operands(operator) {
std::mem::swap(&mut lhs, &mut rhs);
}

self.builder.set_location(location).insert_binary(lhs, op, rhs)
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/noirc_evaluator/src/ssa/ssa_gen/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ impl<'a> FunctionContext<'a> {
let rhs = rhs.into_leaf().eval(self);
let typ = self.builder.type_of_value(rhs);
let zero = self.builder.numeric_constant(0u128, typ);
self.builder.insert_binary(zero, BinaryOp::Sub, rhs).into()
self.insert_binary(zero, noirc_frontend::BinaryOpKind::Subtract, rhs, unary.location)
}
noirc_frontend::UnaryOp::MutableReference => {
self.codegen_reference(&unary.rhs).map(|rhs| {
Expand Down
6 changes: 6 additions & 0 deletions crates/noirc_frontend/src/hir/type_check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,15 @@ impl<'interner> TypeChecker<'interner> {
// inserted by a field access expression `foo.bar` on a mutable reference `foo`.
if self.try_remove_implicit_dereference(method_call.object).is_none() {
// If that didn't work, then wrap the whole expression in an `&mut`
let location = self.interner.id_location(method_call.object);

method_call.object =
self.interner.push_expr(HirExpression::Prefix(HirPrefixExpression {
operator: UnaryOp::MutableReference,
rhs: method_call.object,
}));
self.interner.push_expr_type(&method_call.object, new_type);
self.interner.push_expr_location(method_call.object, location.span, location.file);
}
}
}
Expand Down Expand Up @@ -567,6 +570,9 @@ impl<'interner> TypeChecker<'interner> {
}));
this.interner.push_expr_type(&old_lhs, lhs_type);
this.interner.push_expr_type(access_lhs, element);

let old_location = this.interner.id_location(old_lhs);
this.interner.push_expr_location(*access_lhs, span, old_location.file);
};

match self.check_field_access(&lhs_type, &access.rhs.0.contents, span, dereference_lhs) {
Expand Down
1 change: 1 addition & 0 deletions crates/noirc_frontend/src/monomorphization/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub struct Unary {
pub operator: crate::UnaryOp,
pub rhs: Box<Expression>,
pub result_type: Type,
pub location: Location,
}

pub type BinaryOp = BinaryOpKind;
Expand Down
39 changes: 24 additions & 15 deletions crates/noirc_frontend/src/monomorphization/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,15 @@ impl<'interner> Monomorphizer<'interner> {
HirExpression::Literal(HirLiteral::Unit) => ast::Expression::Block(vec![]),
HirExpression::Block(block) => self.block(block.0),

HirExpression::Prefix(prefix) => ast::Expression::Unary(ast::Unary {
operator: prefix.operator,
rhs: Box::new(self.expr(prefix.rhs)),
result_type: Self::convert_type(&self.interner.id_type(expr)),
}),
HirExpression::Prefix(prefix) => {
let location = self.interner.expr_location(&expr);
ast::Expression::Unary(ast::Unary {
operator: prefix.operator,
rhs: Box::new(self.expr(prefix.rhs)),
result_type: Self::convert_type(&self.interner.id_type(expr)),
location,
})
}

HirExpression::Infix(infix) => {
let lhs = Box::new(self.expr(infix.lhs));
Expand Down Expand Up @@ -816,7 +820,7 @@ impl<'interner> Monomorphizer<'interner> {
};

let call = self
.try_evaluate_call(&func, &return_type)
.try_evaluate_call(&func, &id, &return_type)
.unwrap_or(ast::Expression::Call(ast::Call { func, arguments, return_type, location }));

if !block_expressions.is_empty() {
Expand Down Expand Up @@ -897,6 +901,7 @@ impl<'interner> Monomorphizer<'interner> {
fn try_evaluate_call(
&mut self,
func: &ast::Expression,
expr_id: &node_interner::ExprId,
result_type: &ast::Type,
) -> Option<ast::Expression> {
if let ast::Expression::Ident(ident) = func {
Expand All @@ -907,7 +912,10 @@ impl<'interner> Monomorphizer<'interner> {
(FieldElement::max_num_bits() as u128).into(),
ast::Type::Field,
))),
"zeroed" => Some(self.zeroed_value_of_type(result_type)),
"zeroed" => {
let location = self.interner.expr_location(expr_id);
Some(self.zeroed_value_of_type(result_type, location))
}
"modulus_le_bits" => {
let bits = FieldElement::modulus().to_radix_le(2);
Some(self.modulus_array_literal(bits, 1))
Expand Down Expand Up @@ -1179,7 +1187,7 @@ impl<'interner> Monomorphizer<'interner> {
/// Implements std::unsafe::zeroed by returning an appropriate zeroed
/// ast literal or collection node for the given type. Note that for functions
/// there is no obvious zeroed value so this should be considered unsafe to use.
fn zeroed_value_of_type(&mut self, typ: &ast::Type) -> ast::Expression {
fn zeroed_value_of_type(&mut self, typ: &ast::Type, location: noirc_errors::Location) -> ast::Expression {
match typ {
ast::Type::Field | ast::Type::Integer(..) => {
ast::Expression::Literal(ast::Literal::Integer(0_u128.into(), typ.clone()))
Expand All @@ -1189,7 +1197,7 @@ impl<'interner> Monomorphizer<'interner> {
// anyway.
ast::Type::Unit => ast::Expression::Literal(ast::Literal::Bool(false)),
ast::Type::Array(length, element_type) => {
let element = self.zeroed_value_of_type(element_type.as_ref());
let element = self.zeroed_value_of_type(element_type.as_ref(), location);
ast::Expression::Literal(ast::Literal::Array(ast::ArrayLiteral {
contents: vec![element; *length as usize],
typ: ast::Type::Array(*length, element_type.clone()),
Expand All @@ -1199,7 +1207,7 @@ impl<'interner> Monomorphizer<'interner> {
ast::Expression::Literal(ast::Literal::Str("\0".repeat(*length as usize)))
}
ast::Type::FmtString(length, fields) => {
let zeroed_tuple = self.zeroed_value_of_type(fields);
let zeroed_tuple = self.zeroed_value_of_type(fields, location);
let fields_len = match &zeroed_tuple {
ast::Expression::Tuple(fields) => fields.len() as u64,
_ => unreachable!("ICE: format string fields should be structured in a tuple, but got a {zeroed_tuple}"),
Expand All @@ -1211,10 +1219,10 @@ impl<'interner> Monomorphizer<'interner> {
))
}
ast::Type::Tuple(fields) => {
ast::Expression::Tuple(vecmap(fields, |field| self.zeroed_value_of_type(field)))
ast::Expression::Tuple(vecmap(fields, |field| self.zeroed_value_of_type(field, location)))
}
ast::Type::Function(parameter_types, ret_type, env) => {
self.create_zeroed_function(parameter_types, ret_type, env)
self.create_zeroed_function(parameter_types, ret_type, env, location)
}
ast::Type::Slice(element_type) => {
ast::Expression::Literal(ast::Literal::Array(ast::ArrayLiteral {
Expand All @@ -1224,9 +1232,9 @@ impl<'interner> Monomorphizer<'interner> {
}
ast::Type::MutableReference(element) => {
use crate::UnaryOp::MutableReference;
let rhs = Box::new(self.zeroed_value_of_type(element));
let rhs = Box::new(self.zeroed_value_of_type(element, location));
let result_type = typ.clone();
ast::Expression::Unary(ast::Unary { rhs, result_type, operator: MutableReference })
ast::Expression::Unary(ast::Unary { rhs, result_type, operator: MutableReference, location })
}
}
}
Expand All @@ -1242,14 +1250,15 @@ impl<'interner> Monomorphizer<'interner> {
parameter_types: &[ast::Type],
ret_type: &ast::Type,
env_type: &ast::Type,
location: noirc_errors::Location,
) -> ast::Expression {
let lambda_name = "zeroed_lambda";

let parameters = vecmap(parameter_types, |parameter_type| {
(self.next_local_id(), false, "_".into(), parameter_type.clone())
});

let body = self.zeroed_value_of_type(ret_type);
let body = self.zeroed_value_of_type(ret_type, location);

let id = self.next_function_id();
let return_type = ret_type.clone();
Expand Down