Skip to content

Commit 7c903da

Browse files
authored
fix: DeadFuncElimPass+CallGraph w/ non-module-child entrypoint (#2390)
#2336 tried to update CallGraph wrt. entrypoint, but managed to break it by searching for the entrypoint only among the module-root's children; if the entrypoint is deeper, this meant it was never scanned or included in the CallGraph, and dead-func removal would remove....too much. This fixes that by scanning the entrypoint separately. Note this produces two nodes in the callgraph: one for whatever function contains the entrypoint; and one for the entrypoint. The former will have outgoing edges to all the same targets as the latter, possibly plus some more. (One could argue the function containing the entrypoint should "call" the entrypoint, but this is not a proper call, so, no.) Fixes #2387
1 parent 7902ebc commit 7c903da

File tree

2 files changed

+11
-7
lines changed

2 files changed

+11
-7
lines changed

hugr-passes/src/call_graph.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,19 +48,20 @@ impl<N: HugrNode> CallGraph<N> {
4848
/// Makes a new `CallGraph` for a Hugr.
4949
pub fn new(hugr: &impl HugrView<Node = N>) -> Self {
5050
let mut g = Graph::default();
51-
let non_func_root =
52-
(!hugr.get_optype(hugr.entrypoint()).is_module()).then_some(hugr.entrypoint());
53-
let node_to_g = hugr
51+
let mut node_to_g = hugr
5452
.children(hugr.module_root())
5553
.filter_map(|n| {
5654
let weight = match hugr.get_optype(n) {
5755
OpType::FuncDecl(_) => CallGraphNode::FuncDecl(n),
5856
OpType::FuncDefn(_) => CallGraphNode::FuncDefn(n),
59-
_ => (Some(n) == non_func_root).then_some(CallGraphNode::NonFuncRoot)?,
57+
_ => return None,
6058
};
6159
Some((n, g.add_node(weight)))
6260
})
6361
.collect::<HashMap<_, _>>();
62+
if !hugr.entrypoint_optype().is_module() && !node_to_g.contains_key(&hugr.entrypoint()) {
63+
node_to_g.insert(hugr.entrypoint(), g.add_node(CallGraphNode::NonFuncRoot));
64+
}
6465
for (func, cg_node) in &node_to_g {
6566
traverse(hugr, *cg_node, *func, &mut g, &node_to_g);
6667
}

hugr-passes/src/dead_funcs.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,15 @@ mod test {
174174
let f_inp = fm.input_wires();
175175
let fm = fm.finish_with_outputs(f_inp)?;
176176
let mut m = hb.define_function("main", Signature::new_endo(usize_t()))?;
177-
let mc = m.call(fm.handle(), &[], m.input_wires())?;
178-
let m = m.finish_with_outputs(mc.outputs())?;
177+
let m_in = m.input_wires();
178+
let mut dfg = m.dfg_builder(Signature::new_endo(usize_t()), m_in)?;
179+
let c = dfg.call(fm.handle(), &[], dfg.input_wires())?;
180+
let dfg = dfg.finish_with_outputs(c.outputs()).unwrap();
181+
m.finish_with_outputs(dfg.outputs())?;
179182

180183
let mut hugr = hb.finish_hugr()?;
181184
if use_hugr_entrypoint {
182-
hugr.set_entrypoint(m.node());
185+
hugr.set_entrypoint(dfg.node());
183186
}
184187

185188
let avail_funcs = hugr

0 commit comments

Comments
 (0)