Skip to content

Commit

Permalink
Auto merge of rust-lang#116454 - tmiasko:small-dominators, r=<try>
Browse files Browse the repository at this point in the history
Generalize small dominators optimization

* Use small dominators optimization from 640ede7 more generally.
* Merge `DefLocation` and `LocationExtended` since they serve the same purpose.
  • Loading branch information
bors committed Oct 5, 2023
2 parents cf9fd95 + 827dc2a commit 828aaab
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 109 deletions.
17 changes: 1 addition & 16 deletions compiler/rustc_codegen_ssa/src/mir/analyze.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_index::bit_set::BitSet;
use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::mir::traversal;
use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::{self, Location, TerminatorKind};
use rustc_middle::mir::{self, DefLocation, Location, TerminatorKind};
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};

pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
Expand Down Expand Up @@ -67,21 +67,6 @@ enum LocalKind {
SSA(DefLocation),
}

#[derive(Copy, Clone, PartialEq, Eq)]
enum DefLocation {
Argument,
Body(Location),
}

impl DefLocation {
fn dominates(self, location: Location, dominators: &Dominators<mir::BasicBlock>) -> bool {
match self {
DefLocation::Argument => true,
DefLocation::Body(def) => def.successor_within_block().dominates(location, dominators),
}
}
}

struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
dominators: &'mir Dominators<mir::BasicBlock>,
Expand Down
76 changes: 65 additions & 11 deletions compiler/rustc_data_structures/src/graph/dominators/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,42 @@ rustc_index::newtype_index! {
struct PreorderIndex {}
}

pub fn dominators<G: ControlFlowGraph>(graph: &G) -> Dominators<G::Node> {
#[derive(Clone, Debug)]
pub struct Dominators<Node: Idx> {
kind: Kind<Node>,
}

#[derive(Clone, Debug)]
enum Kind<Node: Idx> {
/// A representation optimized for a small path graphs.
Path(usize),
General(Inner<Node>),
}

pub fn dominators<G: ControlFlowGraph>(g: &G) -> Dominators<G::Node> {
// We often encounter MIR bodies with 1 or 2 basic blocks. Special case the dominators
// computation and representation for those cases.
if is_small_path_graph(g) {
Dominators { kind: Kind::Path(g.num_nodes()) }
} else {
Dominators { kind: Kind::General(dominators_impl(g)) }
}
}

fn is_small_path_graph<G: ControlFlowGraph>(g: &G) -> bool {
if g.start_node().index() != 0 {
return false;
}
if g.num_nodes() == 1 {
return true;
}
if g.num_nodes() == 2 {
return g.successors(g.start_node()).any(|n| n.index() == 1);
}
false
}

fn dominators_impl<G: ControlFlowGraph>(graph: &G) -> Inner<G::Node> {
// compute the post order index (rank) for each node
let mut post_order_rank = IndexVec::from_elem_n(0, graph.num_nodes());

Expand Down Expand Up @@ -245,7 +280,7 @@ pub fn dominators<G: ControlFlowGraph>(graph: &G) -> Dominators<G::Node> {

let time = compute_access_time(start_node, &immediate_dominators);

Dominators { start_node, post_order_rank, immediate_dominators, time }
Inner { post_order_rank, immediate_dominators, time }
}

/// Evaluate the link-eval virtual forest, providing the currently minimum semi
Expand Down Expand Up @@ -310,8 +345,7 @@ fn compress(

/// Tracks the list of dominators for each node.
#[derive(Clone, Debug)]
pub struct Dominators<N: Idx> {
start_node: N,
struct Inner<N: Idx> {
post_order_rank: IndexVec<N, usize>,
// Even though we track only the immediate dominator of each node, it's
// possible to get its full list of dominators by looking up the dominator
Expand All @@ -323,12 +357,24 @@ pub struct Dominators<N: Idx> {
impl<Node: Idx> Dominators<Node> {
/// Returns true if node is reachable from the start node.
pub fn is_reachable(&self, node: Node) -> bool {
node == self.start_node || self.immediate_dominators[node].is_some()
match &self.kind {
Kind::Path(_) => true,
Kind::General(g) => g.time[node].start != 0,
}
}

/// Returns the immediate dominator of node, if any.
pub fn immediate_dominator(&self, node: Node) -> Option<Node> {
self.immediate_dominators[node]
match &self.kind {
Kind::Path(n) => {
if 0 < node.index() && node.index() < *n {
Some(Node::new(node.index() - 1))
} else {
None
}
}
Kind::General(g) => g.immediate_dominators[node],
}
}

/// Provides an iterator over each dominator up the CFG, for the given Node.
Expand All @@ -343,7 +389,10 @@ impl<Node: Idx> Dominators<Node> {
/// of two unrelated nodes will also be consistent, but otherwise the order has no
/// meaning.) This method cannot be used to determine if either Node dominates the other.
pub fn cmp_in_dominator_order(&self, lhs: Node, rhs: Node) -> Ordering {
self.post_order_rank[rhs].cmp(&self.post_order_rank[lhs])
match &self.kind {
Kind::Path(_) => lhs.index().cmp(&rhs.index()),
Kind::General(g) => g.post_order_rank[rhs].cmp(&g.post_order_rank[lhs]),
}
}

/// Returns true if `a` dominates `b`.
Expand All @@ -352,10 +401,15 @@ impl<Node: Idx> Dominators<Node> {
///
/// Panics if `b` is unreachable.
pub fn dominates(&self, a: Node, b: Node) -> bool {
let a = self.time[a];
let b = self.time[b];
assert!(b.start != 0, "node {b:?} is not reachable");
a.start <= b.start && b.finish <= a.finish
match &self.kind {
Kind::Path(_) => a.index() <= b.index(),
Kind::General(g) => {
let a = g.time[a];
let b = g.time[b];
assert!(b.start != 0, "node {b:?} is not reachable");
a.start <= b.start && b.finish <= a.finish
}
}
}
}

Expand Down
45 changes: 21 additions & 24 deletions compiler/rustc_data_structures/src/graph/dominators/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ use super::super::tests::TestGraph;
fn diamond() {
let graph = TestGraph::new(0, &[(0, 1), (0, 2), (1, 3), (2, 3)]);

let dominators = dominators(&graph);
let immediate_dominators = &dominators.immediate_dominators;
assert_eq!(immediate_dominators[0], None);
assert_eq!(immediate_dominators[1], Some(0));
assert_eq!(immediate_dominators[2], Some(0));
assert_eq!(immediate_dominators[3], Some(0));
let d = dominators(&graph);
assert_eq!(d.immediate_dominator(0), None);
assert_eq!(d.immediate_dominator(1), Some(0));
assert_eq!(d.immediate_dominator(2), Some(0));
assert_eq!(d.immediate_dominator(3), Some(0));
}

#[test]
Expand All @@ -22,15 +21,14 @@ fn paper() {
&[(6, 5), (6, 4), (5, 1), (4, 2), (4, 3), (1, 2), (2, 3), (3, 2), (2, 1)],
);

let dominators = dominators(&graph);
let immediate_dominators = &dominators.immediate_dominators;
assert_eq!(immediate_dominators[0], None); // <-- note that 0 is not in graph
assert_eq!(immediate_dominators[1], Some(6));
assert_eq!(immediate_dominators[2], Some(6));
assert_eq!(immediate_dominators[3], Some(6));
assert_eq!(immediate_dominators[4], Some(6));
assert_eq!(immediate_dominators[5], Some(6));
assert_eq!(immediate_dominators[6], None);
let d = dominators(&graph);
assert_eq!(d.immediate_dominator(0), None); // <-- note that 0 is not in graph
assert_eq!(d.immediate_dominator(1), Some(6));
assert_eq!(d.immediate_dominator(2), Some(6));
assert_eq!(d.immediate_dominator(3), Some(6));
assert_eq!(d.immediate_dominator(4), Some(6));
assert_eq!(d.immediate_dominator(5), Some(6));
assert_eq!(d.immediate_dominator(6), None);
}

#[test]
Expand All @@ -47,11 +45,11 @@ fn paper_slt() {
#[test]
fn immediate_dominator() {
let graph = TestGraph::new(1, &[(1, 2), (2, 3)]);
let dominators = dominators(&graph);
assert_eq!(dominators.immediate_dominator(0), None);
assert_eq!(dominators.immediate_dominator(1), None);
assert_eq!(dominators.immediate_dominator(2), Some(1));
assert_eq!(dominators.immediate_dominator(3), Some(2));
let d = dominators(&graph);
assert_eq!(d.immediate_dominator(0), None);
assert_eq!(d.immediate_dominator(1), None);
assert_eq!(d.immediate_dominator(2), Some(1));
assert_eq!(d.immediate_dominator(3), Some(2));
}

#[test]
Expand All @@ -75,8 +73,7 @@ fn transitive_dominator() {
],
);

let dom_tree = dominators(&graph);
let immediate_dominators = &dom_tree.immediate_dominators;
assert_eq!(immediate_dominators[2], Some(0));
assert_eq!(immediate_dominators[3], Some(0)); // This used to return Some(1).
let d = dominators(&graph);
assert_eq!(d.immediate_dominator(2), Some(0));
assert_eq!(d.immediate_dominator(3), Some(0)); // This used to return Some(1).
}
17 changes: 17 additions & 0 deletions compiler/rustc_middle/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,23 @@ impl Location {
}
}

/// `DefLocation` represents the location of a definition - either an argument or an assignment
/// within MIR body.
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum DefLocation {
Argument,
Body(Location),
}

impl DefLocation {
pub fn dominates(self, location: Location, dominators: &Dominators<BasicBlock>) -> bool {
match self {
DefLocation::Argument => true,
DefLocation::Body(def) => def.successor_within_block().dominates(location, dominators),
}
}
}

// Some nodes are used a lot. Make sure they don't unintentionally get bigger.
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
mod size_asserts {
Expand Down
Loading

0 comments on commit 828aaab

Please sign in to comment.