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

Simplify constant_coalesce pass #182

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
53 changes: 19 additions & 34 deletions crates/redpiler/src/passes/constant_coalesce.rs
Original file line number Diff line number Diff line change
@@ -1,59 +1,44 @@
use std::collections::hash_map::Entry;

use super::Pass;
use crate::compile_graph::{CompileGraph, CompileNode, NodeIdx, NodeState, NodeType};
use crate::compile_graph::{CompileGraph, CompileLink, CompileNode, NodeIdx, NodeState, NodeType};
use crate::{CompilerInput, CompilerOptions};
use mchprs_world::World;
use petgraph::unionfind::UnionFind;
use petgraph::visit::{EdgeRef, IntoEdgeReferences, NodeIndexable};
use petgraph::visit::NodeIndexable;
use petgraph::Direction;
use rustc_hash::FxHashMap;

pub struct ConstantCoalesce;

impl<W: World> Pass<W> for ConstantCoalesce {
fn run_pass(&self, graph: &mut CompileGraph, _: &CompilerOptions, _: &CompilerInput<'_, W>) {
let mut vertex_sets = UnionFind::new(graph.node_bound());
for edge in graph.edge_references() {
let (src, dest) = (edge.source(), edge.target());
let node = &graph[src];
if node.ty != NodeType::Constant || !node.is_removable() {
vertex_sets.union(graph.to_index(src), graph.to_index(dest));
}
}
let constant = graph.add_node(CompileNode {
ty: NodeType::Constant,
block: None,
state: NodeState::ss(15),
is_input: false,
is_output: false,
annotations: Default::default(),
});

let mut constant_nodes = FxHashMap::default();
for i in 0..graph.node_bound() {
let idx = NodeIdx::new(i);
if !graph.contains_node(idx) {
continue;
}
if idx == constant {
continue;
}
let node = &graph[idx];
if node.ty != NodeType::Constant || !node.is_removable() {
continue;
}
let ss = node.state.output_strength;
let output_strength = node.state.output_strength;

let mut neighbors = graph.neighbors_directed(idx, Direction::Outgoing).detach();
while let Some((edge, dest)) = neighbors.next(graph) {
let weight = graph.remove_edge(edge).unwrap();
let subgraph_component = vertex_sets.find(graph.to_index(dest));

let constant_idx = match constant_nodes.entry((subgraph_component, ss)) {
Entry::Occupied(entry) => *entry.get(),
Entry::Vacant(entry) => {
let constant_idx = graph.add_node(CompileNode {
ty: NodeType::Constant,
block: None,
state: NodeState::ss(ss),
is_input: false,
is_output: false,
annotations: Default::default(),
});
*entry.insert(constant_idx)
}
};
graph.add_edge(constant_idx, dest, weight);
let CompileLink { ty, ss } = graph[edge];
let ss = ss + 15 - output_strength;
if ss < 15 {
graph.add_edge(constant, dest, CompileLink::new(ty, ss));
}
}
graph.remove_node(idx);
}
Expand Down