@@ -19,9 +19,13 @@ impl Ssa {
19
19
pub ( crate ) fn remove_unreachable_functions ( mut self ) -> Self {
20
20
let mut used_functions = HashSet :: default ( ) ;
21
21
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) ;
25
29
}
26
30
}
27
31
@@ -78,3 +82,54 @@ fn used_functions(func: &Function) -> BTreeSet<FunctionId> {
78
82
79
83
used_function_ids
80
84
}
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