Skip to content

Commit 4727b16

Browse files
authored
fix: Remove unused brillig functions (#7102)
1 parent ad5a980 commit 4727b16

File tree

1 file changed

+58
-3
lines changed

1 file changed

+58
-3
lines changed

compiler/noirc_evaluator/src/ssa/opt/remove_unreachable.rs

+58-3
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,13 @@ impl Ssa {
1919
pub(crate) fn remove_unreachable_functions(mut self) -> Self {
2020
let mut used_functions = HashSet::default();
2121

22-
for function_id in self.functions.keys() {
23-
if self.is_entry_point(*function_id) {
24-
collect_reachable_functions(&self, *function_id, &mut used_functions);
22+
for (id, function) in self.functions.iter() {
23+
// XXX: `self.is_entry_point(*id)` could leave Brillig functions that nobody calls in the SSA.
24+
let is_entry_point = function.id() == self.main_id
25+
|| function.runtime().is_acir() && function.runtime().is_entry_point();
26+
27+
if is_entry_point {
28+
collect_reachable_functions(&self, *id, &mut used_functions);
2529
}
2630
}
2731

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

7983
used_function_ids
8084
}
85+
86+
#[cfg(test)]
87+
mod tests {
88+
use crate::ssa::opt::assert_normalized_ssa_equals;
89+
90+
use super::Ssa;
91+
92+
#[test]
93+
fn remove_unused_brillig() {
94+
let src = "
95+
brillig(inline) fn main f0 {
96+
b0(v0: u32):
97+
v2 = call f1(v0) -> u32
98+
v4 = add v0, u32 1
99+
v5 = eq v2, v4
100+
constrain v2 == v4
101+
return
102+
}
103+
brillig(inline) fn increment f1 {
104+
b0(v0: u32):
105+
v2 = add v0, u32 1
106+
return v2
107+
}
108+
brillig(inline) fn increment_acir f2 {
109+
b0(v0: u32):
110+
v2 = add v0, u32 1
111+
return v2
112+
}
113+
";
114+
115+
let ssa = Ssa::from_str(src).unwrap();
116+
let ssa = ssa.remove_unreachable_functions();
117+
118+
let expected = "
119+
brillig(inline) fn main f0 {
120+
b0(v0: u32):
121+
v2 = call f1(v0) -> u32
122+
v4 = add v0, u32 1
123+
v5 = eq v2, v4
124+
constrain v2 == v4
125+
return
126+
}
127+
brillig(inline) fn increment f1 {
128+
b0(v0: u32):
129+
v2 = add v0, u32 1
130+
return v2
131+
}
132+
";
133+
assert_normalized_ssa_equals(ssa, expected);
134+
}
135+
}

0 commit comments

Comments
 (0)