Finish the job: the source CFG and the emitted CFG share the one dominator core
Correction to db01510. Its subject said "one dominator computation, not three",
which was wrong when I wrote it: `native::cfg::graph` had a fourth (string-keyed
CHK, reused by the loop forest for post-dominators) and `native::cfg::exit_check`
a fifth (label-keyed, over the emitted SPIR-V CFG). Three was the number I had
just touched, not the number that existed. This commit makes the claim true.
Both now go through `native::dominators`. `graph::named_dominators` is the
adapter for named blocks -- dense indices from a deterministic block order,
dominance run once, results mapped back -- and the loop forest's reverse-graph
post-dominators use it too, since post-dominators are dominators of the reverse
graph rooted at a virtual exit. `EmittedDominators` builds the same dense view
from the label list and keeps only the intervals.
`EmittedDominators::new` loses its `entry` parameter. Every caller passed
`labels.first()`, so the entry is now defined as `labels[0]`, which is also what
the shared computation assumes; passing it separately was a way for the two to
disagree.
exit_check's own module doc already recorded that its dominator *sets* had once
"drove large translations far beyond their resident-memory budget". The same
quadratic form was still live in the structurizer's shape layer, three
directories away, and cost 400 MiB on a 6301-block function. That is the case
for one implementation rather than a warning in each.
Verified: full test suite, clippy, rustdoc, and byte-identical output over a
2880-source corpus sample (0 of 2880 differ).
This commit is contained in:
1 parent
db01510465
commit
07c999ad65
7 files changed
+109
-320
No files matched your search
@@ -806,7 +806,7 @@ pub(in crate::native) fn infer_loop_merges(blocks: &[BodyBlock]) -> HashMap<Stri
|
||||
}
|
||||
}
|
||||
|
||||
// Real dominator tree (CHK) over the source CFG's real-block edges — the single
|
||||
// Real dominator tree over the source CFG's real-block edges — the single
|
||||
// dominance oracle, replacing the local path-DFS `dominates` this module used to carry.
|
||||
// `None` only when `blocks` is empty, in which case the loop below never runs.
|
||||
let doms = Cfg::from_blocks(blocks).map(|cfg| cfg.dominators());
|
||||
|
||||
+31
-125
@@ -1,9 +1,10 @@
|
||||
//! Compact dominance over an emitted SPIR-V function CFG.
|
||||
//!
|
||||
//! Shared by late native rewrites. The immediate-dominator tree uses O(V + E) storage and answers
|
||||
//! dominance with DFS intervals; the former
|
||||
//! `label -> HashSet<all dominators>` representation was O(V²) and drove large translations far
|
||||
//! beyond their resident-memory budget even after those temporary sets were freed.
|
||||
//! Shared by late native rewrites. The dominator tree comes from [`crate::native::dominators`]:
|
||||
//! O(V + E) storage, answered with DFS intervals. The former `label -> HashSet<all dominators>`
|
||||
//! representation here was O(V²) and drove large translations far beyond their resident-memory
|
||||
//! budget even after those temporary sets were freed -- which is exactly why there is now one
|
||||
//! dominator computation in the crate rather than one per caller.
|
||||
|
||||
#[cfg(test)]
|
||||
use crate::spirv_module::{Function, Module, Operand};
|
||||
@@ -18,13 +19,14 @@ pub(in crate::native) struct EmittedDominators {
|
||||
index: HashMap<Word, usize>,
|
||||
#[cfg(test)]
|
||||
immediate_dominator: Vec<Option<usize>>,
|
||||
preorder: Vec<usize>,
|
||||
postorder: Vec<usize>,
|
||||
/// Euler intervals of the dominator tree, indexed as `labels` was.
|
||||
intervals: Vec<Option<(usize, usize)>>,
|
||||
}
|
||||
|
||||
impl EmittedDominators {
|
||||
/// `labels` is the function's blocks in serialized order, so `labels[0]` is the entry -- which
|
||||
/// is what every caller has, and what the shared dominator computation expects.
|
||||
pub(in crate::native) fn new(
|
||||
entry: Word,
|
||||
labels: &[Word],
|
||||
successors_by_label: &HashMap<Word, Vec<Word>>,
|
||||
) -> Self {
|
||||
@@ -34,103 +36,26 @@ impl EmittedDominators {
|
||||
.map(|(idx, label)| (*label, idx))
|
||||
.collect::<HashMap<_, _>>();
|
||||
let mut successors = vec![Vec::new(); labels.len()];
|
||||
let mut predecessors = vec![Vec::new(); labels.len()];
|
||||
for (&label, targets) in successors_by_label {
|
||||
let Some(&from) = index.get(&label) else {
|
||||
continue;
|
||||
};
|
||||
for target in targets {
|
||||
let Some(&to) = index.get(target) else {
|
||||
continue;
|
||||
};
|
||||
successors[from].push(to);
|
||||
predecessors[to].push(from);
|
||||
}
|
||||
}
|
||||
|
||||
let mut rpo = Vec::new();
|
||||
if let Some(&entry) = index.get(&entry) {
|
||||
let mut seen = vec![false; labels.len()];
|
||||
let mut stack = vec![(entry, 0usize)];
|
||||
seen[entry] = true;
|
||||
while let Some((node, next)) = stack.last_mut() {
|
||||
if *next < successors[*node].len() {
|
||||
let successor = successors[*node][*next];
|
||||
*next += 1;
|
||||
if !seen[successor] {
|
||||
seen[successor] = true;
|
||||
stack.push((successor, 0));
|
||||
}
|
||||
} else {
|
||||
rpo.push(*node);
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
rpo.reverse();
|
||||
}
|
||||
let mut rpo_rank = vec![usize::MAX; labels.len()];
|
||||
for (rank, node) in rpo.iter().copied().enumerate() {
|
||||
rpo_rank[node] = rank;
|
||||
}
|
||||
let mut idom = vec![None; labels.len()];
|
||||
if let Some(&entry) = rpo.first() {
|
||||
idom[entry] = Some(entry);
|
||||
loop {
|
||||
let mut changed = false;
|
||||
for node in rpo.iter().copied().skip(1) {
|
||||
let mut defined = predecessors[node]
|
||||
.iter()
|
||||
.copied()
|
||||
.filter(|pred| idom[*pred].is_some());
|
||||
let Some(mut next_idom) = defined.next() else {
|
||||
continue;
|
||||
};
|
||||
for pred in defined {
|
||||
next_idom = intersect(pred, next_idom, &idom, &rpo_rank);
|
||||
}
|
||||
if idom[node] != Some(next_idom) {
|
||||
idom[node] = Some(next_idom);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
if !changed {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let mut children = vec![Vec::new(); labels.len()];
|
||||
for (node, parent) in idom.iter().copied().enumerate() {
|
||||
if let Some(parent) = parent.filter(|parent| *parent != node) {
|
||||
children[parent].push(node);
|
||||
}
|
||||
}
|
||||
let mut preorder = vec![usize::MAX; labels.len()];
|
||||
let mut postorder = vec![usize::MAX; labels.len()];
|
||||
if let Some(&entry) = rpo.first() {
|
||||
let mut clock = 0usize;
|
||||
let mut stack = vec![(entry, 0usize)];
|
||||
preorder[entry] = clock;
|
||||
clock += 1;
|
||||
while let Some((node, next)) = stack.last_mut() {
|
||||
if *next < children[*node].len() {
|
||||
let child = children[*node][*next];
|
||||
*next += 1;
|
||||
preorder[child] = clock;
|
||||
clock += 1;
|
||||
stack.push((child, 0));
|
||||
} else {
|
||||
postorder[*node] = clock;
|
||||
stack.pop();
|
||||
if let Some(&to) = index.get(target) {
|
||||
successors[from].push(to);
|
||||
}
|
||||
}
|
||||
}
|
||||
let predecessors = crate::native::dominators::build_predecessors(&successors);
|
||||
// Underscore-prefixed because the parent links are only read by the serialization-order
|
||||
// check, which is test-only; the intervals are what production queries.
|
||||
let (_, intervals, _parents) =
|
||||
crate::native::dominators::dominance(&successors, &predecessors);
|
||||
Self {
|
||||
index,
|
||||
#[cfg(test)]
|
||||
immediate_dominator: idom,
|
||||
preorder,
|
||||
postorder,
|
||||
immediate_dominator: _parents,
|
||||
intervals,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -139,9 +64,7 @@ impl EmittedDominators {
|
||||
else {
|
||||
return false;
|
||||
};
|
||||
self.preorder[dominator] != usize::MAX
|
||||
&& self.preorder[dominator] <= self.preorder[node]
|
||||
&& self.preorder[node] < self.postorder[dominator]
|
||||
crate::native::dominators::dominates_interval(&self.intervals, dominator, node)
|
||||
}
|
||||
|
||||
/// Whether every reachable block's immediate dominator precedes it in serialized block order.
|
||||
@@ -230,11 +153,11 @@ fn function_has_late_dominator(function: &Function) -> bool {
|
||||
.iter()
|
||||
.filter_map(|block| block.label.as_ref()?.result_id)
|
||||
.collect::<Vec<_>>();
|
||||
let Some(&entry) = labels.first() else {
|
||||
if labels.is_empty() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
let successors = super::graph::spirv_block_successors_by_label(&function.blocks);
|
||||
let dominators = EmittedDominators::new(entry, &labels, &successors);
|
||||
let dominators = EmittedDominators::new(&labels, &successors);
|
||||
!dominators.serialization_respects_dominance()
|
||||
}
|
||||
|
||||
@@ -248,11 +171,11 @@ fn function_has_non_dominating_value_use(function: &Function) -> bool {
|
||||
.iter()
|
||||
.filter_map(|block| block.label.as_ref()?.result_id)
|
||||
.collect::<Vec<_>>();
|
||||
let Some(&entry) = labels.first() else {
|
||||
if labels.is_empty() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
let successors = super::graph::spirv_block_successors_by_label(&function.blocks);
|
||||
let dominators = EmittedDominators::new(entry, &labels, &successors);
|
||||
let dominators = EmittedDominators::new(&labels, &successors);
|
||||
let definitions = function
|
||||
.blocks
|
||||
.iter()
|
||||
@@ -322,11 +245,11 @@ fn function_has_unmarked_back_edge(function: &Function) -> bool {
|
||||
.iter()
|
||||
.filter_map(|block| block.label.as_ref()?.result_id)
|
||||
.collect::<Vec<_>>();
|
||||
let Some(&entry) = labels.first() else {
|
||||
if labels.is_empty() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
let successors = super::graph::spirv_block_successors_by_label(&function.blocks);
|
||||
let dominators = EmittedDominators::new(entry, &labels, &successors);
|
||||
let dominators = EmittedDominators::new(&labels, &successors);
|
||||
let loop_headers = function
|
||||
.blocks
|
||||
.iter()
|
||||
@@ -364,11 +287,11 @@ fn function_has_invalid_selection_exit(function: &Function) -> bool {
|
||||
.iter()
|
||||
.filter_map(|block| block.label.as_ref()?.result_id)
|
||||
.collect::<Vec<_>>();
|
||||
let Some(&entry) = labels.first() else {
|
||||
if labels.is_empty() {
|
||||
return false;
|
||||
};
|
||||
}
|
||||
let successors = super::graph::spirv_block_successors_by_label(&function.blocks);
|
||||
let dominators = EmittedDominators::new(entry, &labels, &successors);
|
||||
let dominators = EmittedDominators::new(&labels, &successors);
|
||||
let terminal_targets = function
|
||||
.blocks
|
||||
.iter()
|
||||
@@ -514,23 +437,6 @@ fn function_has_invalid_selection_exit(function: &Function) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn intersect(
|
||||
mut left: usize,
|
||||
mut right: usize,
|
||||
idom: &[Option<usize>],
|
||||
rpo_rank: &[usize],
|
||||
) -> usize {
|
||||
while left != right {
|
||||
while rpo_rank[left] > rpo_rank[right] {
|
||||
left = idom[left].expect("defined dominator chain");
|
||||
}
|
||||
while rpo_rank[right] > rpo_rank[left] {
|
||||
right = idom[right].expect("defined dominator chain");
|
||||
}
|
||||
}
|
||||
left
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
+54
-162
@@ -1,14 +1,13 @@
|
||||
//! CFG graph primitives: successor/predecessor adjacency and a
|
||||
//! Cooper-Harvey-Kennedy dominator tree, over the `BodyBlock` source CFG, plus
|
||||
//! the native-side SPIR-V (`crate::spirv_module::Block`, `Word` label) equivalents
|
||||
//! used by the emitter's control-flow repair and structurizer (see the `spirv_*`
|
||||
//! CFG graph primitives: successor/predecessor adjacency and a dominator tree, over the
|
||||
//! `BodyBlock` source CFG, plus the native-side SPIR-V (`crate::spirv_module::Block`, `Word` label)
|
||||
//! equivalents used by the emitter's control-flow repair and structurizer (see the `spirv_*`
|
||||
//! fns near the end of this file).
|
||||
//!
|
||||
//! This is the single home for low-level CFG graph analysis. Higher-level passes
|
||||
//! (the loop-forest in [`super::loopforest`], the structurizer, merge inference)
|
||||
//! build a [`Cfg`] once and query dominance/adjacency through it, instead of each
|
||||
//! open-coding its own successor scan and dominator computation. Dominators use
|
||||
//! the Cooper-Harvey-Kennedy iterative algorithm over reverse-postorder.
|
||||
//! open-coding its own successor scan. The dominators themselves come from
|
||||
//! [`crate::native::dominators`], which every dominator question in the crate goes through.
|
||||
|
||||
use super::blocks::block_successors;
|
||||
use super::BodyBlock;
|
||||
@@ -31,6 +30,9 @@ pub(in crate::native) struct Cfg {
|
||||
pub(in crate::native) predecessors: HashMap<String, Vec<String>>,
|
||||
/// The set of real block names, for cheap membership tests.
|
||||
names: HashSet<String>,
|
||||
/// Block names in function order, entry first. The dominator computation speaks dense block
|
||||
/// indices, and this is the deterministic numbering it is handed.
|
||||
order: Vec<String>,
|
||||
}
|
||||
|
||||
impl Cfg {
|
||||
@@ -67,6 +69,7 @@ impl Cfg {
|
||||
successors,
|
||||
predecessors,
|
||||
names,
|
||||
order: blocks.iter().map(|b| b.name.clone()).collect(),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -90,21 +93,54 @@ impl Cfg {
|
||||
reachable
|
||||
}
|
||||
|
||||
/// The immediate-dominator tree of this CFG (Cooper-Harvey-Kennedy).
|
||||
/// The immediate-dominator tree of this CFG.
|
||||
pub(in crate::native) fn dominators(&self) -> Dominators {
|
||||
let names: HashSet<&str> = self.names.iter().map(String::as_str).collect();
|
||||
let rpo = reverse_postorder(&self.entry, &self.successors, &names);
|
||||
let rpo_num: HashMap<&str, usize> = rpo
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, n)| (n.as_str(), i))
|
||||
.collect();
|
||||
let idom = compute_idom(&self.entry, &self.predecessors, &rpo, &rpo_num);
|
||||
let intervals = dominance_intervals(&self.entry, &idom);
|
||||
let (idom, intervals) = named_dominators(&self.order, |name| {
|
||||
self.successors.get(name).map(Vec::as_slice).unwrap_or(&[])
|
||||
});
|
||||
Dominators { idom, intervals }
|
||||
}
|
||||
}
|
||||
|
||||
/// Immediate dominators and Euler intervals of a CFG whose blocks are named.
|
||||
///
|
||||
/// `order` is the dense numbering, entry first; `successors` names each block's targets. Both the
|
||||
/// forward dominators here and the reverse-graph post-dominators in [`super::loopforest`] are this
|
||||
/// same question over a different graph, so both come through here to reach the crate's one
|
||||
/// dominator computation in [`crate::native::dominators`].
|
||||
pub(super) fn named_dominators<'a>(
|
||||
order: &'a [String],
|
||||
successors: impl Fn(&str) -> &'a [String],
|
||||
) -> (HashMap<String, String>, HashMap<String, (usize, usize)>) {
|
||||
let index: HashMap<&str, usize> = order
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(position, name)| (name.as_str(), position))
|
||||
.collect();
|
||||
let dense: Vec<Vec<usize>> = order
|
||||
.iter()
|
||||
.map(|name| {
|
||||
successors(name)
|
||||
.iter()
|
||||
.filter_map(|target| index.get(target.as_str()).copied())
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
let predecessors = crate::native::dominators::build_predecessors(&dense);
|
||||
let (_, intervals, parents) = crate::native::dominators::dominance(&dense, &predecessors);
|
||||
let named_idom = parents
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(block, parent)| Some((order[block].clone(), order[(*parent)?].clone())))
|
||||
.collect();
|
||||
let named_intervals = intervals
|
||||
.iter()
|
||||
.enumerate()
|
||||
.filter_map(|(block, interval)| Some((order[block].clone(), (*interval)?)))
|
||||
.collect();
|
||||
(named_idom, named_intervals)
|
||||
}
|
||||
|
||||
/// Immediate-dominator relation of a source CFG (entry maps to itself).
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub(in crate::native) struct Dominators {
|
||||
@@ -138,151 +174,6 @@ impl Dominators {
|
||||
}
|
||||
}
|
||||
|
||||
fn dominance_intervals(
|
||||
entry: &str,
|
||||
idom: &HashMap<String, String>,
|
||||
) -> HashMap<String, (usize, usize)> {
|
||||
let mut children = HashMap::<&str, Vec<&str>>::new();
|
||||
for (node, parent) in idom {
|
||||
if node != parent {
|
||||
children.entry(parent).or_default().push(node);
|
||||
}
|
||||
}
|
||||
for descendants in children.values_mut() {
|
||||
descendants.sort_unstable();
|
||||
}
|
||||
let mut entered = HashMap::<String, usize>::new();
|
||||
let mut intervals = HashMap::new();
|
||||
let mut clock = 0usize;
|
||||
let mut stack = vec![(entry, false)];
|
||||
while let Some((node, exiting)) = stack.pop() {
|
||||
if exiting {
|
||||
let start = entered[node];
|
||||
intervals.insert(node.to_string(), (start, clock));
|
||||
clock += 1;
|
||||
continue;
|
||||
}
|
||||
entered.insert(node.to_string(), clock);
|
||||
clock += 1;
|
||||
stack.push((node, true));
|
||||
if let Some(descendants) = children.get(node) {
|
||||
stack.extend(descendants.iter().rev().map(|child| (*child, false)));
|
||||
}
|
||||
}
|
||||
intervals
|
||||
}
|
||||
|
||||
/// Reverse postorder of the blocks reachable from `entry`, restricted to `names`.
|
||||
///
|
||||
/// Exposed so the reverse-graph post-dominator computation in [`super::loopforest`]
|
||||
/// can reuse it (post-dominators = dominators of the reverse CFG).
|
||||
pub(super) fn reverse_postorder(
|
||||
entry: &str,
|
||||
successors: &HashMap<String, Vec<String>>,
|
||||
names: &HashSet<&str>,
|
||||
) -> Vec<String> {
|
||||
let mut visited = HashSet::new();
|
||||
let mut post = Vec::new();
|
||||
// Iterative DFS tracking child cursor per frame.
|
||||
let mut stack: Vec<(String, usize)> = vec![(entry.to_string(), 0)];
|
||||
visited.insert(entry.to_string());
|
||||
while let Some((node, idx)) = stack.last().cloned() {
|
||||
let succ = successors.get(&node);
|
||||
let next = succ.and_then(|s| s.iter().filter(|t| names.contains(t.as_str())).nth(idx));
|
||||
match next {
|
||||
Some(child) => {
|
||||
stack.last_mut().unwrap().1 += 1;
|
||||
if visited.insert(child.clone()) {
|
||||
stack.push((child.clone(), 0));
|
||||
}
|
||||
}
|
||||
None => {
|
||||
post.push(node);
|
||||
stack.pop();
|
||||
}
|
||||
}
|
||||
}
|
||||
post.reverse();
|
||||
post
|
||||
}
|
||||
|
||||
/// Cooper-Harvey-Kennedy iterative immediate-dominators over reverse-postorder.
|
||||
///
|
||||
/// Exposed alongside [`reverse_postorder`] so [`super::loopforest`] can run it on
|
||||
/// the reverse CFG to obtain post-dominators.
|
||||
pub(super) fn compute_idom(
|
||||
entry: &str,
|
||||
predecessors: &HashMap<String, Vec<String>>,
|
||||
rpo: &[String],
|
||||
rpo_num: &HashMap<&str, usize>,
|
||||
) -> HashMap<String, String> {
|
||||
let mut idom: HashMap<String, String> = HashMap::new();
|
||||
idom.insert(entry.to_string(), entry.to_string());
|
||||
let mut changed = true;
|
||||
while changed {
|
||||
changed = false;
|
||||
for node in rpo {
|
||||
if node == entry {
|
||||
continue;
|
||||
}
|
||||
let mut new_idom: Option<String> = None;
|
||||
for pred in predecessors.get(node).into_iter().flatten() {
|
||||
if !idom.contains_key(pred) {
|
||||
continue;
|
||||
}
|
||||
new_idom = Some(match new_idom.as_deref() {
|
||||
None => pred.clone(),
|
||||
Some(cur) => intersect(pred, cur, &idom, rpo_num),
|
||||
});
|
||||
}
|
||||
if let Some(new_idom) = new_idom {
|
||||
if idom.get(node) != Some(&new_idom) {
|
||||
idom.insert(node.clone(), new_idom);
|
||||
changed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
idom
|
||||
}
|
||||
|
||||
/// Walk two finger up the dominator tree until they meet (CHK `intersect`).
|
||||
fn intersect(
|
||||
a: &str,
|
||||
b: &str,
|
||||
idom: &HashMap<String, String>,
|
||||
rpo_num: &HashMap<&str, usize>,
|
||||
) -> String {
|
||||
let mut a = a;
|
||||
let mut b = b;
|
||||
let num = |s: &str| rpo_num.get(s).copied().unwrap_or(usize::MAX);
|
||||
while a != b {
|
||||
while num(a) > num(b) {
|
||||
let Some(next) = idom.get(a).map(String::as_str) else {
|
||||
break;
|
||||
};
|
||||
if next == a {
|
||||
break;
|
||||
}
|
||||
a = next;
|
||||
}
|
||||
while num(b) > num(a) {
|
||||
let Some(next) = idom.get(b).map(String::as_str) else {
|
||||
break;
|
||||
};
|
||||
if next == b {
|
||||
break;
|
||||
}
|
||||
b = next;
|
||||
}
|
||||
// Guard against a stuck pair (shouldn't happen for reducible idom).
|
||||
if a != b && num(a) == num(b) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
a.to_string()
|
||||
}
|
||||
|
||||
/// Blocks reachable from `start` (inclusive) over the given forward-edge adjacency.
|
||||
///
|
||||
/// Operates directly on a caller-built `successors` map rather than a [`Cfg`], for the
|
||||
@@ -395,7 +286,8 @@ mod tests {
|
||||
/// Definitional dominance oracle: `d` dominates `n` iff every path from the
|
||||
/// entry to `n` passes through `d`. Equivalently (for `d != n`, `n` reachable),
|
||||
/// `n` is NOT reachable from the entry once `d` is deleted from the graph. This
|
||||
/// is an independent check of the CHK implementation in [`Dominators`].
|
||||
/// is an independent check of [`Dominators`], reached through a different route than the
|
||||
/// dense-set oracle in [`crate::native::dominators`]: path reachability, not a fixpoint.
|
||||
fn oracle_dominates(cfg: &Cfg, dominator: &str, node: &str) -> bool {
|
||||
if dominator == node {
|
||||
return true;
|
||||
|
||||
@@ -9,11 +9,11 @@
|
||||
//! a previously observed module shape, where the inner loop's merge coincides with the outer loop's continue — see
|
||||
//! [[metal2vulkan-native-emitter]]).
|
||||
//!
|
||||
//! Dominators use the Cooper-Harvey-Kennedy iterative algorithm over reverse-postorder; natural
|
||||
//! Dominators come from [`crate::native::dominators`]; natural
|
||||
//! loops are the standard back-edge (`u -> v` where `v` dominates `u`) closure.
|
||||
|
||||
use super::blocks::block_successors;
|
||||
use super::graph::{compute_idom, reverse_postorder, Cfg, Dominators};
|
||||
use super::graph::{Cfg, Dominators};
|
||||
use super::BodyBlock;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
|
||||
@@ -502,28 +502,19 @@ fn post_idom_cut(blocks: &[BodyBlock], cut: &HashSet<(String, String)>) -> HashM
|
||||
.push(VIRTUAL_EXIT.to_string());
|
||||
}
|
||||
|
||||
// Reverse-postorder + predecessors of the reverse graph, then CHK dominators rooted at the
|
||||
// virtual exit = post-dominators of the forward graph.
|
||||
let rnames: HashSet<&str> = reachable
|
||||
.iter()
|
||||
.map(String::as_str)
|
||||
.chain(std::iter::once(VIRTUAL_EXIT))
|
||||
.collect();
|
||||
let rpo = reverse_postorder(VIRTUAL_EXIT, &rsucc, &rnames);
|
||||
let rpo_num: HashMap<&str, usize> = rpo
|
||||
.iter()
|
||||
.enumerate()
|
||||
.map(|(i, n)| (n.as_str(), i))
|
||||
.collect();
|
||||
let mut rpreds: HashMap<String, Vec<String>> = HashMap::new();
|
||||
for (n, ts) in &rsucc {
|
||||
for t in ts {
|
||||
if rnames.contains(t.as_str()) {
|
||||
rpreds.entry(t.clone()).or_default().push(n.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
let ridom = compute_idom(VIRTUAL_EXIT, &rpreds, &rpo, &rpo_num);
|
||||
// Dominators of the reverse graph rooted at the virtual exit ARE the post-dominators of the
|
||||
// forward graph, so this is the same computation the forward direction uses. Sorted rather than
|
||||
// in `reachable`'s hash order so the dense numbering handed to it is deterministic.
|
||||
let mut order = vec![VIRTUAL_EXIT.to_string()];
|
||||
order.extend(
|
||||
reachable
|
||||
.iter()
|
||||
.cloned()
|
||||
.collect::<std::collections::BTreeSet<_>>(),
|
||||
);
|
||||
let (ridom, _) = super::graph::named_dominators(&order, |name| {
|
||||
rsucc.get(name).map(Vec::as_slice).unwrap_or(&[])
|
||||
});
|
||||
|
||||
ridom
|
||||
.into_iter()
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// Dominator/loop-forest analysis backing the structured-by-construction path (`structured_plan`).
|
||||
// A rejected local plan is recorded as an owned construction fact so the module can select its raw
|
||||
// CFG representation before serialization.
|
||||
// Low-level source-CFG graph primitives (successor/predecessor adjacency + CHK
|
||||
// Low-level source-CFG graph primitives (successor/predecessor adjacency +
|
||||
// dominator tree) shared by the loop-forest and structurizer analyses.
|
||||
pub(in crate::native) mod graph;
|
||||
pub(super) mod loopforest;
|
||||
|
||||
@@ -2457,11 +2457,10 @@ impl Emitter {
|
||||
Some((label, emitted_block_successors(block)))
|
||||
})
|
||||
.collect::<HashMap<_, _>>();
|
||||
let entry = labels
|
||||
.first()
|
||||
.copied()
|
||||
.ok_or_else(|| "native emitter: split function has no entry block".to_string())?;
|
||||
let dominators = crate::native::cfg::EmittedDominators::new(entry, &labels, &successors);
|
||||
if labels.is_empty() {
|
||||
return Err("native emitter: split function has no entry block".to_string());
|
||||
}
|
||||
let dominators = crate::native::cfg::EmittedDominators::new(&labels, &successors);
|
||||
let redirected = blocks
|
||||
.iter()
|
||||
.filter_map(|block| {
|
||||
|
||||
@@ -1060,10 +1060,11 @@ pub(super) fn rewrite_cross_binding_pointer_merges_to_values(module: &mut Module
|
||||
.iter()
|
||||
.zip(&block_labels)
|
||||
.map(|(function, labels)| {
|
||||
let entry = labels.first().copied().flatten()?;
|
||||
// The flatten below drops unlabelled blocks, so the entry has to be the first
|
||||
// label for index 0 to still be it.
|
||||
labels.first().copied().flatten()?;
|
||||
let labels = labels.iter().copied().flatten().collect::<Vec<_>>();
|
||||
Some(EmittedDominators::new(
|
||||
entry,
|
||||
&labels,
|
||||
&spirv_block_successors_by_label(&function.blocks),
|
||||
))
|
||||
|
||||
Reference in new issue
Block a user