compiler/rust: Add a DepthFirstSearch trait

There are four depth first searches in cfg.rs.  This adds a DFS
abstraction which we can later optimize.

Reviewed-by: Mel Henning <mhenning@darkrefraction.com>
Reviewed-by: Lorenzo Rossi <git@rossilorenzo.dev>
Reviewed-by: Mary Guillemard <mary@mary.zone>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/37536>
This commit is contained in:
Faith Ekstrand 2025-09-23 14:33:05 -04:00 committed by Marge Bot
parent fcd1c4b9e7
commit 993f5c9e30
3 changed files with 56 additions and 0 deletions

View file

@ -0,0 +1,54 @@
// Copyright © 2025 Collabora, Ltd.
// SPDX-License-Identifier: MIT
use crate::bitset::BitSet;
/// A trait implementing a depth-first search over a graph
pub trait DepthFirstSearch {
type ChildIter;
/// Pre-visit a node. This is called before any children or edges are
/// visited. Returns an iterator to this node's children in the graph.
fn pre(&mut self, id: usize) -> Self::ChildIter;
/// Visit an edge. An edge is visited before the child at the end of that
/// edge is pre-visited. Every edge is visited, even if if the node this
/// edge points to has already been visited.
fn edge(&mut self, _parent: usize, _child: usize) {
// Does nothing by default
}
/// Post-visit a node. This is called after all the children have been
/// visited.
fn post(&mut self, _id: usize) {
// Does nothing by default
}
}
fn dfs_impl<I, D>(dfs: &mut D, seen: &mut BitSet, id: usize)
where
I: Iterator<Item = usize>,
D: DepthFirstSearch<ChildIter = I>,
{
if seen.contains(id) {
return;
}
seen.insert(id);
let children = dfs.pre(id);
for child in children {
dfs.edge(id, child);
dfs_impl(dfs, seen, child);
}
dfs.post(id);
}
pub fn dfs<I, D>(dfs: &mut D, start: usize)
where
I: Iterator<Item = usize>,
D: DepthFirstSearch<ChildIter = I>,
{
let mut seen = BitSet::new();
dfs_impl(dfs, &mut seen, start);
}

View file

@ -6,6 +6,7 @@ pub mod bindings;
pub mod bitset;
pub mod cfg;
pub mod dataflow;
pub mod depth_first_search;
pub mod memstream;
pub mod nir;
pub mod nir_instr_printer;

View file

@ -6,6 +6,7 @@ _compiler_rs_sources = [
'bitset.rs',
'cfg.rs',
'dataflow.rs',
'depth_first_search.rs',
'memstream.rs',
'nir_instr_printer.rs',
'nir.rs',