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: Remove unused brillig functions #7102

Merged
merged 2 commits into from
Jan 17, 2025
Merged
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
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);
}
}
Loading