Skip to content

Commit

Permalink
fix: Remove unused brillig functions (#7102)
Browse files Browse the repository at this point in the history
  • Loading branch information
aakoshh authored Jan 17, 2025
1 parent ad5a980 commit 4727b16
Showing 1 changed file with 58 additions and 3 deletions.
61 changes: 58 additions & 3 deletions compiler/noirc_evaluator/src/ssa/opt/remove_unreachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ impl Ssa {
pub(crate) fn remove_unreachable_functions(mut self) -> Self {
let mut used_functions = HashSet::default();

for function_id in self.functions.keys() {
if self.is_entry_point(*function_id) {
collect_reachable_functions(&self, *function_id, &mut used_functions);
for (id, function) in self.functions.iter() {
// XXX: `self.is_entry_point(*id)` could leave Brillig functions that nobody calls in the SSA.
let is_entry_point = function.id() == self.main_id
|| function.runtime().is_acir() && function.runtime().is_entry_point();

if is_entry_point {
collect_reachable_functions(&self, *id, &mut used_functions);
}
}

Expand Down Expand Up @@ -78,3 +82,54 @@ fn used_functions(func: &Function) -> BTreeSet<FunctionId> {

used_function_ids
}

#[cfg(test)]
mod tests {
use crate::ssa::opt::assert_normalized_ssa_equals;

use super::Ssa;

#[test]
fn remove_unused_brillig() {
let src = "
brillig(inline) fn main f0 {
b0(v0: u32):
v2 = call f1(v0) -> u32
v4 = add v0, u32 1
v5 = eq v2, v4
constrain v2 == v4
return
}
brillig(inline) fn increment f1 {
b0(v0: u32):
v2 = add v0, u32 1
return v2
}
brillig(inline) fn increment_acir f2 {
b0(v0: u32):
v2 = add v0, u32 1
return v2
}
";

let ssa = Ssa::from_str(src).unwrap();
let ssa = ssa.remove_unreachable_functions();

let expected = "
brillig(inline) fn main f0 {
b0(v0: u32):
v2 = call f1(v0) -> u32
v4 = add v0, u32 1
v5 = eq v2, v4
constrain v2 == v4
return
}
brillig(inline) fn increment f1 {
b0(v0: u32):
v2 = add v0, u32 1
return v2
}
";
assert_normalized_ssa_equals(ssa, expected);
}
}

0 comments on commit 4727b16

Please sign in to comment.