Skip to content

Commit

Permalink
Merge branch 'master' into tf/pull-acirgen-out-of-ssa
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAFrench authored Nov 18, 2024
2 parents 6b2ddb1 + c11a696 commit 343ab9e
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 220 deletions.
111 changes: 3 additions & 108 deletions compiler/noirc_frontend/src/elaborator/types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{borrow::Cow, collections::BTreeMap, rc::Rc};
use std::{borrow::Cow, rc::Rc};

use acvm::acir::AcirField;
use iter_extended::vecmap;
Expand All @@ -25,7 +25,7 @@ use crate::{
HirBinaryOp, HirCallExpression, HirExpression, HirLiteral, HirMemberAccess,
HirMethodReference, HirPrefixExpression, TraitMethod,
},
function::{FuncMeta, Parameters},
function::FuncMeta,
stmt::HirStatement,
traits::{NamedType, ResolvedTraitBound, Trait, TraitConstraint},
},
Expand All @@ -34,8 +34,7 @@ use crate::{
TraitImplKind, TraitMethodId,
},
token::SecondaryAttribute,
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeBindings, TypeVariable,
UnificationError,
Generics, Kind, ResolvedGeneric, Type, TypeBinding, TypeBindings, UnificationError,
};

use super::{lints, path_resolution::PathResolutionItem, Elaborator};
Expand Down Expand Up @@ -1725,110 +1724,6 @@ impl<'context> Elaborator<'context> {
}
}

pub fn find_numeric_generics(
parameters: &Parameters,
return_type: &Type,
) -> Vec<(String, TypeVariable)> {
let mut found = BTreeMap::new();
for (_, parameter, _) in &parameters.0 {
Self::find_numeric_generics_in_type(parameter, &mut found);
}
Self::find_numeric_generics_in_type(return_type, &mut found);
found.into_iter().collect()
}

fn find_numeric_generics_in_type(typ: &Type, found: &mut BTreeMap<String, TypeVariable>) {
match typ {
Type::FieldElement
| Type::Integer(_, _)
| Type::Bool
| Type::Unit
| Type::Error
| Type::TypeVariable(_)
| Type::Constant(..)
| Type::NamedGeneric(_, _)
| Type::Quoted(_)
| Type::Forall(_, _) => (),

Type::CheckedCast { from, to } => {
Self::find_numeric_generics_in_type(from, found);
Self::find_numeric_generics_in_type(to, found);
}

Type::TraitAsType(_, _, args) => {
for arg in &args.ordered {
Self::find_numeric_generics_in_type(arg, found);
}
for arg in &args.named {
Self::find_numeric_generics_in_type(&arg.typ, found);
}
}

Type::Array(length, element_type) => {
if let Type::NamedGeneric(type_variable, name) = length.as_ref() {
found.insert(name.to_string(), type_variable.clone());
}
Self::find_numeric_generics_in_type(element_type, found);
}

Type::Slice(element_type) => {
Self::find_numeric_generics_in_type(element_type, found);
}

Type::Tuple(fields) => {
for field in fields {
Self::find_numeric_generics_in_type(field, found);
}
}

Type::Function(parameters, return_type, _env, _unconstrained) => {
for parameter in parameters {
Self::find_numeric_generics_in_type(parameter, found);
}
Self::find_numeric_generics_in_type(return_type, found);
}

Type::Struct(struct_type, generics) => {
for (i, generic) in generics.iter().enumerate() {
if let Type::NamedGeneric(type_variable, name) = generic {
if struct_type.borrow().generics[i].is_numeric() {
found.insert(name.to_string(), type_variable.clone());
}
} else {
Self::find_numeric_generics_in_type(generic, found);
}
}
}
Type::Alias(alias, generics) => {
for (i, generic) in generics.iter().enumerate() {
if let Type::NamedGeneric(type_variable, name) = generic {
if alias.borrow().generics[i].is_numeric() {
found.insert(name.to_string(), type_variable.clone());
}
} else {
Self::find_numeric_generics_in_type(generic, found);
}
}
}
Type::MutableReference(element) => Self::find_numeric_generics_in_type(element, found),
Type::String(length) => {
if let Type::NamedGeneric(type_variable, name) = length.as_ref() {
found.insert(name.to_string(), type_variable.clone());
}
}
Type::FmtString(length, fields) => {
if let Type::NamedGeneric(type_variable, name) = length.as_ref() {
found.insert(name.to_string(), type_variable.clone());
}
Self::find_numeric_generics_in_type(fields, found);
}
Type::InfixExpr(lhs, _op, rhs) => {
Self::find_numeric_generics_in_type(lhs, found);
Self::find_numeric_generics_in_type(rhs, found);
}
}
}

/// Push a type variable into the current FunctionContext to be defaulted if needed
/// at the end of the earlier of either the current function or the current comptime scope.
fn push_type_variable(&mut self, typ: Type) {
Expand Down
108 changes: 0 additions & 108 deletions compiler/noirc_frontend/src/hir_def/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,6 @@ impl Kind {
}
}

pub(crate) fn is_numeric(&self) -> bool {
matches!(self.follow_bindings(), Self::Numeric { .. })
}

pub(crate) fn is_type_level_field_element(&self) -> bool {
let type_level = false;
self.is_field_element(type_level)
Expand Down Expand Up @@ -375,10 +371,6 @@ impl ResolvedGeneric {
pub fn kind(&self) -> Kind {
self.type_var.kind()
}

pub(crate) fn is_numeric(&self) -> bool {
self.kind().is_numeric()
}
}

enum FunctionCoercionResult {
Expand Down Expand Up @@ -524,13 +516,6 @@ impl StructType {
self.fields.iter().map(|field| field.name.clone()).collect()
}

/// Search the fields of a struct for any types with a `TypeKind::Numeric`
pub fn find_numeric_generics_in_fields(&self, found_names: &mut Vec<String>) {
for field in self.fields.iter() {
field.typ.find_numeric_type_vars(found_names);
}
}

/// Instantiate this struct type, returning a Vec of the new generic args (in
/// the same order as self.generics)
pub fn instantiate(&self, interner: &mut NodeInterner) -> Vec<Type> {
Expand Down Expand Up @@ -1102,99 +1087,6 @@ impl Type {
}
}

pub fn find_numeric_type_vars(&self, found_names: &mut Vec<String>) {
// Return whether the named generic has a Kind::Numeric and save its name
let named_generic_is_numeric = |typ: &Type, found_names: &mut Vec<String>| {
if let Type::NamedGeneric(var, name) = typ {
if var.kind().is_numeric() {
found_names.push(name.to_string());
true
} else {
false
}
} else {
false
}
};

match self {
Type::FieldElement
| Type::Integer(_, _)
| Type::Bool
| Type::Unit
| Type::Error
| Type::Constant(_, _)
| Type::Forall(_, _)
| Type::Quoted(_) => {}

Type::TypeVariable(type_var) => {
if let TypeBinding::Bound(typ) = &*type_var.borrow() {
named_generic_is_numeric(typ, found_names);
}
}

Type::NamedGeneric(_, _) => {
named_generic_is_numeric(self, found_names);
}
Type::CheckedCast { from, to } => {
to.find_numeric_type_vars(found_names);
from.find_numeric_type_vars(found_names);
}

Type::TraitAsType(_, _, args) => {
for arg in args.ordered.iter() {
arg.find_numeric_type_vars(found_names);
}
for arg in args.named.iter() {
arg.typ.find_numeric_type_vars(found_names);
}
}
Type::Array(length, elem) => {
elem.find_numeric_type_vars(found_names);
named_generic_is_numeric(length, found_names);
}
Type::Slice(elem) => elem.find_numeric_type_vars(found_names),
Type::Tuple(fields) => {
for field in fields.iter() {
field.find_numeric_type_vars(found_names);
}
}
Type::Function(parameters, return_type, env, _unconstrained) => {
for parameter in parameters.iter() {
parameter.find_numeric_type_vars(found_names);
}
return_type.find_numeric_type_vars(found_names);
env.find_numeric_type_vars(found_names);
}
Type::Struct(_, generics) => {
for generic in generics.iter() {
if !named_generic_is_numeric(generic, found_names) {
generic.find_numeric_type_vars(found_names);
}
}
}
Type::Alias(_, generics) => {
for generic in generics.iter() {
if !named_generic_is_numeric(generic, found_names) {
generic.find_numeric_type_vars(found_names);
}
}
}
Type::MutableReference(element) => element.find_numeric_type_vars(found_names),
Type::String(length) => {
named_generic_is_numeric(length, found_names);
}
Type::FmtString(length, elements) => {
elements.find_numeric_type_vars(found_names);
named_generic_is_numeric(length, found_names);
}
Type::InfixExpr(lhs, _op, rhs) => {
lhs.find_numeric_type_vars(found_names);
rhs.find_numeric_type_vars(found_names);
}
}
}

/// True if this type can be used as a parameter to `main` or a contract function.
/// This is only false for unsized types like slices or slices that do not make sense
/// as a program input such as named generics or mutable references.
Expand Down
11 changes: 7 additions & 4 deletions tooling/nargo_cli/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,10 @@ fn generate_test_cases(
if *brillig && inliner.value() < matrix_config.min_inliner {
continue;
}
test_cases.push(format!("#[test_case::test_case({brillig}, {})]", inliner.label()));
test_cases.push(format!(
"#[test_case::test_case(ForceBrillig({brillig}), Inliner({}))]",
inliner.label()
));
}
}
let test_cases = test_cases.join("\n");
Expand All @@ -183,7 +186,7 @@ lazy_static::lazy_static! {{
}}
{test_cases}
fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
fn test_{test_name}(force_brillig: ForceBrillig, inliner_aggressiveness: Inliner) {{
let test_program_dir = PathBuf::from("{test_dir}");
// Ignore poisoning errors if some of the matrix cases failed.
Expand All @@ -198,8 +201,8 @@ fn test_{test_name}(force_brillig: bool, inliner_aggressiveness: i64) {{
let mut nargo = Command::cargo_bin("nargo").unwrap();
nargo.arg("--program-dir").arg(test_program_dir);
nargo.arg("{test_command}").arg("--force");
nargo.arg("--inliner-aggressiveness").arg(inliner_aggressiveness.to_string());
if force_brillig {{
nargo.arg("--inliner-aggressiveness").arg(inliner_aggressiveness.0.to_string());
if force_brillig.0 {{
nargo.arg("--force-brillig");
}}
Expand Down
6 changes: 6 additions & 0 deletions tooling/nargo_cli/tests/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ mod tests {

test_binary::build_test_binary_once!(mock_backend, "../backend_interface/test-binaries");

// Utilities to keep the test matrix labels more intuitive.
#[derive(Debug, Clone, Copy)]
struct ForceBrillig(pub bool);
#[derive(Debug, Clone, Copy)]
struct Inliner(pub i64);

// include tests generated by `build.rs`
include!(concat!(env!("OUT_DIR"), "/execute.rs"));
}

0 comments on commit 343ab9e

Please sign in to comment.