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): Do not optimize for allocates in constant folding #2466

Merged
merged 2 commits into from
Aug 28, 2023
Merged
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
Next Next commit
don't optimize allocates in constant folding
vezenovm committed Aug 28, 2023
commit 0d5dcec5a16a927023b8d5fbbdf0d7e9b562c245
2 changes: 2 additions & 0 deletions crates/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
@@ -63,6 +63,8 @@ pub(crate) fn optimize_into_acir(
// and this pass is missed, slice merging will fail inside of flattening.
.mem2reg()
.print(print_ssa_passes, "After Mem2Reg:")
.fold_constants()
.print(print_ssa_passes, "After Constant Folding:")
.flatten_cfg()
.print(print_ssa_passes, "After Flattening:")
// Run mem2reg once more with the flattened CFG to catch any remaining loads/stores
4 changes: 3 additions & 1 deletion crates/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
@@ -102,7 +102,9 @@ impl Context {

// If the instruction doesn't have side-effects, cache the results so we can reuse them if
// the same instruction appears again later in the block.
if !instruction.has_side_effects(&function.dfg) {
if !instruction.has_side_effects(&function.dfg)
&& !matches!(instruction, Instruction::Allocate)
{
instruction_result_cache.insert(instruction, new_results.clone());
}
for (old_result, new_result) in old_results.iter().zip(new_results) {
3 changes: 2 additions & 1 deletion crates/noirc_frontend/src/hir/resolution/resolver.rs
Original file line number Diff line number Diff line change
@@ -371,7 +371,8 @@ impl<'a> Resolver<'a> {
// expect() here is valid, because the only places we don't have a span are omitted types
// e.g. a function without return type implicitly has a spanless UnresolvedType::Unit return type
// To get an invalid env type, the user must explicitly specify the type, which will have a span
let env_span = env.span.expect("Unexpected missing span for closure environment type");
let env_span =
env.span.expect("Unexpected missing span for closure environment type");

let env = Box::new(self.resolve_type_inner(*env, new_variables));

22 changes: 9 additions & 13 deletions crates/noirc_frontend/src/parser/parser.rs
Original file line number Diff line number Diff line change
@@ -40,8 +40,8 @@ use crate::{
BinaryOp, BinaryOpKind, BlockExpression, ConstrainStatement, Distinctness, FunctionDefinition,
FunctionReturnType, Ident, IfExpression, InfixExpression, LValue, Lambda, Literal,
NoirFunction, NoirStruct, NoirTrait, NoirTypeAlias, Path, PathKind, Pattern, Recoverable,
TraitConstraint, TraitImpl, TraitImplItem, TraitItem, TypeImpl, UnaryOp,
UnresolvedTypeExpression, UseTree, UseTreeKind, Visibility, TraitBound,
TraitBound, TraitConstraint, TraitImpl, TraitImplItem, TraitItem, TypeImpl, UnaryOp,
UnresolvedTypeExpression, UseTree, UseTreeKind, Visibility,
};

use chumsky::prelude::*;
@@ -527,19 +527,17 @@ fn trait_implementation_body() -> impl NoirParser<Vec<TraitImplItem>> {
}

fn where_clause() -> impl NoirParser<Vec<TraitConstraint>> {

struct MultiTraitConstraint {
typ: UnresolvedType,
trait_bounds: Vec<TraitBound>,
}

let constraints = parse_type()
.then_ignore(just(Token::Colon))
.then(trait_bounds())
.validate(|(typ, trait_bounds), span, emit| {
let constraints = parse_type().then_ignore(just(Token::Colon)).then(trait_bounds()).validate(
|(typ, trait_bounds), span, emit| {
emit(ParserError::with_reason(ParserErrorReason::ExperimentalFeature("Traits"), span));
MultiTraitConstraint { typ, trait_bounds }
});
},
);

keyword(Keyword::Where)
.ignore_then(constraints.separated_by(just(Token::Comma)))
@@ -549,18 +547,16 @@ fn where_clause() -> impl NoirParser<Vec<TraitConstraint>> {
let mut result: Vec<TraitConstraint> = Vec::new();
for constraint in x {
for bound in constraint.trait_bounds {
result.push(TraitConstraint { typ:constraint.typ.clone(), trait_bound:bound } );
result
.push(TraitConstraint { typ: constraint.typ.clone(), trait_bound: bound });
}
}
result
})
}

fn trait_bounds() -> impl NoirParser<Vec<TraitBound>> {
trait_bound()
.separated_by(just(Token::Plus))
.at_least(1)
.allow_trailing()
trait_bound().separated_by(just(Token::Plus)).at_least(1).allow_trailing()
}

fn trait_bound() -> impl NoirParser<TraitBound> {