Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rustc_data_structures::graph mini refactor #123934

Merged
merged 6 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 4 additions & 11 deletions compiler/rustc_borrowck/src/constraints/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,23 +216,16 @@ impl<'s, 'tcx, D: ConstraintGraphDirection> Iterator for Successors<'s, 'tcx, D>

impl<'s, 'tcx, D: ConstraintGraphDirection> graph::DirectedGraph for RegionGraph<'s, 'tcx, D> {
type Node = RegionVid;
}

impl<'s, 'tcx, D: ConstraintGraphDirection> graph::WithNumNodes for RegionGraph<'s, 'tcx, D> {
fn num_nodes(&self) -> usize {
self.constraint_graph.first_constraints.len()
}
}

impl<'s, 'tcx, D: ConstraintGraphDirection> graph::WithSuccessors for RegionGraph<'s, 'tcx, D> {
fn successors(&self, node: Self::Node) -> <Self as graph::GraphSuccessors<'_>>::Iter {
impl<'s, 'tcx, D: ConstraintGraphDirection> graph::Successors for RegionGraph<'s, 'tcx, D> {
type Successors<'g> = Successors<'s, 'tcx, D> where Self: 'g;

fn successors(&self, node: Self::Node) -> Self::Successors<'_> {
self.outgoing_regions(node)
}
}

impl<'s, 'tcx, D: ConstraintGraphDirection> graph::GraphSuccessors<'_>
for RegionGraph<'s, 'tcx, D>
{
type Item = RegionVid;
type Iter = Successors<'s, 'tcx, D>;
}
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/dataflow.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_data_structures::fx::FxIndexMap;
use rustc_data_structures::graph::WithSuccessors;
use rustc_data_structures::graph::Successors;
use rustc_index::bit_set::BitSet;
use rustc_middle::mir::{
self, BasicBlock, Body, CallReturnPlaces, Location, Place, TerminatorEdges,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/region_infer/reverse_sccs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::constraints::ConstraintSccIndex;
use crate::RegionInferenceContext;
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::graph::vec_graph::VecGraph;
use rustc_data_structures::graph::WithSuccessors;
use rustc_data_structures::graph::Successors;
use rustc_middle::ty::RegionVid;
use std::ops::Range;

Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_borrowck/src/type_check/liveness/trace.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
use rustc_data_structures::graph::WithSuccessors;
use rustc_data_structures::graph::Successors;
use rustc_index::bit_set::BitSet;
use rustc_index::interval::IntervalSet;
use rustc_infer::infer::canonical::QueryRegionConstraints;
Expand Down
24 changes: 12 additions & 12 deletions compiler/rustc_data_structures/src/graph/iterate/mod.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use super::{DirectedGraph, WithNumNodes, WithStartNode, WithSuccessors};
use super::{DirectedGraph, Successors, WithStartNode};
use rustc_index::bit_set::BitSet;
use rustc_index::{IndexSlice, IndexVec};
use std::ops::ControlFlow;

#[cfg(test)]
mod tests;

pub fn post_order_from<G: DirectedGraph + WithSuccessors + WithNumNodes>(
pub fn post_order_from<G: DirectedGraph + Successors>(
graph: &G,
start_node: G::Node,
) -> Vec<G::Node> {
post_order_from_to(graph, start_node, None)
}

pub fn post_order_from_to<G: DirectedGraph + WithSuccessors + WithNumNodes>(
pub fn post_order_from_to<G: DirectedGraph + Successors>(
graph: &G,
start_node: G::Node,
end_node: Option<G::Node>,
Expand All @@ -27,7 +27,7 @@ pub fn post_order_from_to<G: DirectedGraph + WithSuccessors + WithNumNodes>(
result
}

fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
fn post_order_walk<G: DirectedGraph + Successors>(
graph: &G,
node: G::Node,
result: &mut Vec<G::Node>,
Expand Down Expand Up @@ -60,7 +60,7 @@ fn post_order_walk<G: DirectedGraph + WithSuccessors + WithNumNodes>(
}
}

pub fn reverse_post_order<G: DirectedGraph + WithSuccessors + WithNumNodes>(
pub fn reverse_post_order<G: DirectedGraph + Successors>(
graph: &G,
start_node: G::Node,
) -> Vec<G::Node> {
Expand All @@ -72,7 +72,7 @@ pub fn reverse_post_order<G: DirectedGraph + WithSuccessors + WithNumNodes>(
/// A "depth-first search" iterator for a directed graph.
pub struct DepthFirstSearch<'graph, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
graph: &'graph G,
stack: Vec<G::Node>,
Expand All @@ -81,7 +81,7 @@ where

impl<'graph, G> DepthFirstSearch<'graph, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
pub fn new(graph: &'graph G) -> Self {
Self { graph, stack: vec![], visited: BitSet::new_empty(graph.num_nodes()) }
Expand Down Expand Up @@ -127,7 +127,7 @@ where

impl<G> std::fmt::Debug for DepthFirstSearch<'_, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let mut f = fmt.debug_set();
Expand All @@ -140,7 +140,7 @@ where

impl<G> Iterator for DepthFirstSearch<'_, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
type Item = G::Node;

Expand Down Expand Up @@ -201,7 +201,7 @@ struct Event<N> {
/// [CLR]: https://en.wikipedia.org/wiki/Introduction_to_Algorithms
pub struct TriColorDepthFirstSearch<'graph, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
graph: &'graph G,
stack: Vec<Event<G::Node>>,
Expand All @@ -211,7 +211,7 @@ where

impl<'graph, G> TriColorDepthFirstSearch<'graph, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors,
G: ?Sized + DirectedGraph + Successors,
{
pub fn new(graph: &'graph G) -> Self {
TriColorDepthFirstSearch {
Expand Down Expand Up @@ -278,7 +278,7 @@ where

impl<G> TriColorDepthFirstSearch<'_, G>
where
G: ?Sized + DirectedGraph + WithNumNodes + WithSuccessors + WithStartNode,
G: ?Sized + DirectedGraph + Successors + WithStartNode,
{
/// Performs a depth-first search, starting from `G::start_node()`.
///
Expand Down
50 changes: 15 additions & 35 deletions compiler/rustc_data_structures/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,68 +12,48 @@ mod tests;

pub trait DirectedGraph {
type Node: Idx;
}

pub trait WithNumNodes: DirectedGraph {
fn num_nodes(&self) -> usize;
}

pub trait WithNumEdges: DirectedGraph {
fn num_edges(&self) -> usize;
}

pub trait WithSuccessors: DirectedGraph
where
Self: for<'graph> GraphSuccessors<'graph, Item = <Self as DirectedGraph>::Node>,
{
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter;

fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self>
pub trait Successors: DirectedGraph {
WaffleLapkin marked this conversation as resolved.
Show resolved Hide resolved
type Successors<'g>: Iterator<Item = Self::Node>
where
Self: WithNumNodes,
{
Self: 'g;

fn successors(&self, node: Self::Node) -> Self::Successors<'_>;

fn depth_first_search(&self, from: Self::Node) -> iterate::DepthFirstSearch<'_, Self> {
iterate::DepthFirstSearch::new(self).with_start_node(from)
}
}

#[allow(unused_lifetimes)]
pub trait GraphSuccessors<'graph> {
type Item;
type Iter: Iterator<Item = Self::Item>;
}

pub trait WithPredecessors: DirectedGraph
where
Self: for<'graph> GraphPredecessors<'graph, Item = <Self as DirectedGraph>::Node>,
{
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter;
}
pub trait Predecessors: DirectedGraph {
type Predecessors<'g>: Iterator<Item = Self::Node>
where
Self: 'g;

#[allow(unused_lifetimes)]
pub trait GraphPredecessors<'graph> {
type Item;
type Iter: Iterator<Item = Self::Item>;
fn predecessors(&self, node: Self::Node) -> Self::Predecessors<'_>;
}

pub trait WithStartNode: DirectedGraph {
fn start_node(&self) -> Self::Node;
}

pub trait ControlFlowGraph:
DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
{
pub trait ControlFlowGraph: DirectedGraph + WithStartNode + Predecessors + Successors {
// convenient trait
}

impl<T> ControlFlowGraph for T where
T: DirectedGraph + WithStartNode + WithPredecessors + WithSuccessors + WithNumNodes
{
}
impl<T> ControlFlowGraph for T where T: DirectedGraph + WithStartNode + Predecessors + Successors {}

/// Returns `true` if the graph has a cycle that is reachable from the start node.
pub fn is_cyclic<G>(graph: &G) -> bool
where
G: ?Sized + DirectedGraph + WithStartNode + WithSuccessors + WithNumNodes,
G: ?Sized + DirectedGraph + WithStartNode + Successors,
{
iterate::TriColorDepthFirstSearch::new(graph)
.run_from_start(&mut iterate::CycleDetector)
Expand Down
24 changes: 8 additions & 16 deletions compiler/rustc_data_structures/src/graph/reference.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ use super::*;

impl<'graph, G: DirectedGraph> DirectedGraph for &'graph G {
type Node = G::Node;
}

impl<'graph, G: WithNumNodes> WithNumNodes for &'graph G {
fn num_nodes(&self) -> usize {
(**self).num_nodes()
}
Expand All @@ -16,24 +14,18 @@ impl<'graph, G: WithStartNode> WithStartNode for &'graph G {
}
}

impl<'graph, G: WithSuccessors> WithSuccessors for &'graph G {
fn successors(&self, node: Self::Node) -> <Self as GraphSuccessors<'_>>::Iter {
impl<'graph, G: Successors> Successors for &'graph G {
type Successors<'g> = G::Successors<'g> where 'graph: 'g;

fn successors(&self, node: Self::Node) -> Self::Successors<'_> {
(**self).successors(node)
}
}

impl<'graph, G: WithPredecessors> WithPredecessors for &'graph G {
fn predecessors(&self, node: Self::Node) -> <Self as GraphPredecessors<'_>>::Iter {
impl<'graph, G: Predecessors> Predecessors for &'graph G {
type Predecessors<'g> = G::Predecessors<'g> where 'graph: 'g;

fn predecessors(&self, node: Self::Node) -> Self::Predecessors<'_> {
(**self).predecessors(node)
}
}

impl<'iter, 'graph, G: WithPredecessors> GraphPredecessors<'iter> for &'graph G {
type Item = G::Node;
type Iter = <G as GraphPredecessors<'iter>>::Iter;
}

impl<'iter, 'graph, G: WithSuccessors> GraphSuccessors<'iter> for &'graph G {
type Item = G::Node;
type Iter = <G as GraphSuccessors<'iter>>::Iter;
}
24 changes: 9 additions & 15 deletions compiler/rustc_data_structures/src/graph/scc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

use crate::fx::FxHashSet;
use crate::graph::vec_graph::VecGraph;
use crate::graph::{DirectedGraph, GraphSuccessors, WithNumEdges, WithNumNodes, WithSuccessors};
use crate::graph::{DirectedGraph, Successors, WithNumEdges};
use rustc_index::{Idx, IndexSlice, IndexVec};
use std::ops::Range;

Expand Down Expand Up @@ -39,7 +39,7 @@ pub struct SccData<S: Idx> {
}

impl<N: Idx, S: Idx + Ord> Sccs<N, S> {
pub fn new(graph: &(impl DirectedGraph<Node = N> + WithNumNodes + WithSuccessors)) -> Self {
pub fn new(graph: &(impl DirectedGraph<Node = N> + Successors)) -> Self {
SccsConstruction::construct(graph)
}

Expand Down Expand Up @@ -89,30 +89,24 @@ impl<N: Idx, S: Idx + Ord> Sccs<N, S> {
}
}

impl<N: Idx, S: Idx> DirectedGraph for Sccs<N, S> {
impl<N: Idx, S: Idx + Ord> DirectedGraph for Sccs<N, S> {
type Node = S;
}

impl<N: Idx, S: Idx + Ord> WithNumNodes for Sccs<N, S> {
fn num_nodes(&self) -> usize {
self.num_sccs()
}
}

impl<N: Idx, S: Idx> WithNumEdges for Sccs<N, S> {
impl<N: Idx, S: Idx + Ord> WithNumEdges for Sccs<N, S> {
fn num_edges(&self) -> usize {
self.scc_data.all_successors.len()
}
}

impl<'graph, N: Idx, S: Idx> GraphSuccessors<'graph> for Sccs<N, S> {
type Item = S;

type Iter = std::iter::Cloned<std::slice::Iter<'graph, S>>;
}
impl<N: Idx, S: Idx + Ord> Successors for Sccs<N, S> {
type Successors<'g> = std::iter::Cloned<std::slice::Iter<'g, S>>;

impl<N: Idx, S: Idx + Ord> WithSuccessors for Sccs<N, S> {
fn successors(&self, node: S) -> <Self as GraphSuccessors<'_>>::Iter {
fn successors(&self, node: S) -> Self::Successors<'_> {
self.successors(node).iter().cloned()
}
}
Expand Down Expand Up @@ -158,7 +152,7 @@ impl<S: Idx> SccData<S> {
}
}

struct SccsConstruction<'c, G: DirectedGraph + WithNumNodes + WithSuccessors, S: Idx> {
struct SccsConstruction<'c, G: DirectedGraph + Successors, S: Idx> {
graph: &'c G,

/// The state of each node; used during walk to record the stack
Expand Down Expand Up @@ -218,7 +212,7 @@ enum WalkReturn<S> {

impl<'c, G, S> SccsConstruction<'c, G, S>
where
G: DirectedGraph + WithNumNodes + WithSuccessors,
G: DirectedGraph + Successors,
S: Idx,
{
/// Identifies SCCs in the graph `G` and computes the resulting
Expand Down
Loading