Skip to content

Commit 4d4602f

Browse files
committed
Simplify reachable brillig function analysis
1 parent 734d7ef commit 4d4602f

File tree

1 file changed

+5
-42
lines changed
  • crates/noirc_evaluator/src/brillig

1 file changed

+5
-42
lines changed

crates/noirc_evaluator/src/brillig/mod.rs

+5-42
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,10 @@ use self::{
66
brillig_ir::artifact::{BrilligArtifact, Label},
77
};
88
use crate::ssa::{
9-
ir::{
10-
function::{Function, FunctionId, RuntimeType},
11-
value::Value,
12-
},
9+
ir::function::{Function, FunctionId, RuntimeType},
1310
ssa_gen::Ssa,
1411
};
15-
use std::collections::{HashMap, HashSet};
12+
use std::collections::{BTreeSet, HashMap};
1613

1714
/// Context structure for the brillig pass.
1815
/// It stores brillig-related data required for brillig generation.
@@ -53,45 +50,11 @@ impl Ssa {
5350
pub(crate) fn to_brillig(&self, enable_debug_trace: bool) -> Brillig {
5451
// Collect all the function ids that are reachable from brillig
5552
// That means all the functions marked as brillig and ACIR functions called by them
56-
let mut brillig_reachable_function_ids: HashSet<FunctionId> = HashSet::new();
57-
58-
// Initialize the queue with all the functions marked as brillig
59-
let mut reachability_queue: Vec<FunctionId> = self
53+
let brillig_reachable_function_ids = self
6054
.functions
6155
.iter()
62-
.filter_map(
63-
|(id, func)| {
64-
if func.runtime() == RuntimeType::Brillig {
65-
Some(*id)
66-
} else {
67-
None
68-
}
69-
},
70-
)
71-
.collect();
72-
73-
while let Some(func_id) = reachability_queue.pop() {
74-
let func = &self.functions[&func_id];
75-
brillig_reachable_function_ids.insert(func.id());
76-
77-
// Explore all functions that are reachable from this function
78-
for (_, value) in func.dfg.values_iter() {
79-
// All reachable functions appear as literals after defunctionalization of the SSA
80-
let reachable_function = match value {
81-
Value::Function(function_id) => function_id,
82-
_ => continue,
83-
};
84-
85-
// If the function is already reachable by brillig or enqueued, skip it.
86-
if brillig_reachable_function_ids.contains(reachable_function)
87-
|| reachability_queue.contains(reachable_function)
88-
{
89-
continue;
90-
}
91-
92-
reachability_queue.push(*reachable_function);
93-
}
94-
}
56+
.filter_map(|(id, func)| (func.runtime() == RuntimeType::Brillig).then_some(*id))
57+
.collect::<BTreeSet<_>>();
9558

9659
let mut brillig = Brillig::default();
9760
for brillig_function_id in brillig_reachable_function_ids {

0 commit comments

Comments
 (0)