Skip to content

Commit

Permalink
chore(ssa): Do not run passes on Brillig functions post Brillig gen (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
vezenovm authored Feb 26, 2025
1 parent f0c1c7b commit 8a54402
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub(crate) fn optimize_into_acir(
// It could happen that we inlined all calls to a given brillig function.
// In that case it's unused so we can remove it. This is what we check next.
.run_pass(Ssa::remove_unreachable_functions, "Removing Unreachable Functions (4th)")
.run_pass(Ssa::dead_instruction_elimination, "Dead Instruction Elimination (3rd)")
.run_pass(Ssa::dead_instruction_elimination_acir, "Dead Instruction Elimination (3rd)")
.finish();

if !options.skip_underconstrained_check {
Expand Down
5 changes: 5 additions & 0 deletions compiler/noirc_evaluator/src/ssa/opt/constant_folding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,11 @@ impl Ssa {
let brillig_info = Some(BrilligInfo { brillig, brillig_functions: &brillig_functions });

for function in self.functions.values_mut() {
// We have already performed our final Brillig generation, so constant folding
// Brillig functions is unnecessary work.
if function.dfg.runtime().is_brillig() {
continue;
}
function.constant_fold(false, brillig_info);
}

Expand Down
21 changes: 16 additions & 5 deletions compiler/noirc_evaluator/src/ssa/opt/die.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,21 @@ impl Ssa {
/// This step should come after the flattening of the CFG and mem2reg.
#[tracing::instrument(level = "trace", skip(self))]
pub(crate) fn dead_instruction_elimination(self) -> Ssa {
self.dead_instruction_elimination_inner(true)
self.dead_instruction_elimination_inner(true, false)
}

fn dead_instruction_elimination_inner(mut self, flattened: bool) -> Ssa {
/// Post the Brillig generation we do not need to run this pass on Brillig functions.
#[tracing::instrument(level = "trace", skip(self))]
pub(crate) fn dead_instruction_elimination_acir(self) -> Ssa {
self.dead_instruction_elimination_inner(true, true)
}

fn dead_instruction_elimination_inner(mut self, flattened: bool, skip_brillig: bool) -> Ssa {
let mut used_globals_map: HashMap<_, _> = self
.functions
.par_iter_mut()
.filter_map(|(id, func)| {
let set = func.dead_instruction_elimination(true, flattened);
let set = func.dead_instruction_elimination(true, flattened, skip_brillig);
if func.runtime().is_brillig() {
Some((*id, set))
} else {
Expand Down Expand Up @@ -79,7 +85,12 @@ impl Function {
&mut self,
insert_out_of_bounds_checks: bool,
flattened: bool,
skip_brillig: bool,
) -> HashSet<ValueId> {
if skip_brillig && self.dfg.runtime().is_brillig() {
return HashSet::default();
}

let mut context = Context { flattened, ..Default::default() };

context.mark_function_parameter_arrays_as_used(self);
Expand All @@ -103,7 +114,7 @@ impl Function {
// instructions (we don't want to remove those checks, or instructions that are
// dependencies of those checks)
if inserted_out_of_bounds_checks {
return self.dead_instruction_elimination(false, flattened);
return self.dead_instruction_elimination(false, flattened, skip_brillig);
}

context.remove_rc_instructions(&mut self.dfg);
Expand Down Expand Up @@ -1099,7 +1110,7 @@ mod test {
let ssa = Ssa::from_str(src).unwrap();

// Even though these ACIR functions only have 1 block, we have not inlined and flattened anything yet.
let ssa = ssa.dead_instruction_elimination_inner(false);
let ssa = ssa.dead_instruction_elimination_inner(false, false);

let expected = "
acir(inline) fn main f0 {
Expand Down
2 changes: 1 addition & 1 deletion compiler/noirc_evaluator/src/ssa/opt/preprocess_fns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Ssa {
// Try to reduce the number of blocks.
function.simplify_function();
// Remove leftover instructions.
function.dead_instruction_elimination(true, false);
function.dead_instruction_elimination(true, false, false);

// Put it back into the SSA, so the next functions can pick it up.
self.functions.insert(id, function);
Expand Down

0 comments on commit 8a54402

Please sign in to comment.