Skip to content

Commit

Permalink
Unrolled build for rust-lang#136082
Browse files Browse the repository at this point in the history
Rollup merge of rust-lang#136082 - Zalathar:iter-nodes, r=oli-obk

Incorporate `iter_nodes` into `graph::DirectedGraph`

This helper method iterates over all node IDs in the dense range `0..num_nodes`.

In practice, we have a lot of graph-algorithm code that already assumes that nodes are densely numbered, by using `num_nodes` to allocate per-node indexed data structures. So I don't think this is actually a substantial change to the de-facto semantics of `graph::DirectedGraph`.

---

Resolves a FIXME from rust-lang#135481.
  • Loading branch information
rust-timer authored Jan 27, 2025
2 parents 0cffe5c + d36e2b8 commit 39f103e
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 23 deletions.
16 changes: 16 additions & 0 deletions compiler/rustc_data_structures/src/graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,23 @@ mod tests;
pub trait DirectedGraph {
type Node: Idx;

/// Returns the total number of nodes in this graph.
///
/// Several graph algorithm implementations assume that every node ID is
/// strictly less than the number of nodes, i.e. nodes are densely numbered.
/// That assumption allows them to use `num_nodes` to allocate per-node
/// data structures, indexed by node.
fn num_nodes(&self) -> usize;

/// Iterates over all nodes of a graph in ascending numeric order.
///
/// Assumes that nodes are densely numbered, i.e. every index in
/// `0..num_nodes` is a valid node.
fn iter_nodes(
&self,
) -> impl Iterator<Item = Self::Node> + DoubleEndedIterator + ExactSizeIterator {
(0..self.num_nodes()).map(<Self::Node as Idx>::new)
}
}

pub trait NumEdges: DirectedGraph {
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_data_structures/src/graph/scc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -333,8 +333,8 @@ where
to_annotation,
};

let scc_indices = (0..num_nodes)
.map(G::Node::new)
let scc_indices = graph
.iter_nodes()
.map(|node| match this.start_walk_from(node) {
WalkReturn::Complete { scc_index, .. } => scc_index,
WalkReturn::Cycle { min_depth, .. } => {
Expand Down
2 changes: 0 additions & 2 deletions compiler/rustc_mir_transform/src/coverage/counters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@ use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::coverage::{CounterId, CovTerm, Expression, ExpressionId, Op};

use crate::coverage::counters::balanced_flow::BalancedFlowGraph;
use crate::coverage::counters::iter_nodes::IterNodes;
use crate::coverage::counters::node_flow::{
CounterTerm, NodeCounters, make_node_counters, node_flow_data_for_balanced_graph,
};
use crate::coverage::graph::{BasicCoverageBlock, CoverageGraph};

mod balanced_flow;
mod iter_nodes;
mod node_flow;
mod union_find;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ use rustc_data_structures::graph::reversed::ReversedGraph;
use rustc_index::Idx;
use rustc_index::bit_set::DenseBitSet;

use crate::coverage::counters::iter_nodes::IterNodes;

/// A view of an underlying graph that has been augmented to have “balanced flow”.
/// This means that the flow (execution count) of each node is equal to the
/// sum of its in-edge flows, and also equal to the sum of its out-edge flows.
Expand Down
16 changes: 0 additions & 16 deletions compiler/rustc_mir_transform/src/coverage/counters/iter_nodes.rs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use rustc_index::bit_set::DenseBitSet;
use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::mir::coverage::Op;

use crate::coverage::counters::iter_nodes::IterNodes;
use crate::coverage::counters::union_find::UnionFind;

#[cfg(test)]
Expand Down

0 comments on commit 39f103e

Please sign in to comment.