Skip to content

Commit

Permalink
redpiler: rerun coalesce pass logic to help with repeating patterns
Browse files Browse the repository at this point in the history
  • Loading branch information
StackDoubleFlow committed Aug 12, 2024
1 parent df8dbe7 commit 21b62fc
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 28 deletions.
2 changes: 1 addition & 1 deletion crates/redpiler/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ impl Compiler {
}
}

pub fn has_pending_ticks(&mut self) -> bool{
pub fn has_pending_ticks(&mut self) -> bool {
self.backend().has_pending_ticks()
}
}
Expand Down
71 changes: 44 additions & 27 deletions crates/redpiler/src/passes/coalesce.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,61 @@ use itertools::Itertools;
use mchprs_world::World;
use petgraph::visit::{EdgeRef, NodeIndexable};
use petgraph::Direction;
use tracing::trace;

pub struct Coalesce;

impl<W: World> Pass<W> for Coalesce {
fn run_pass(&self, graph: &mut CompileGraph, _: &CompilerOptions, _: &CompilerInput<'_, W>) {
for i in 0..graph.node_bound() {
let idx = NodeIdx::new(i);
if !graph.contains_node(idx) {
continue;
loop {
let num_coalesced = run_iteration(graph);
trace!("Iteration combined {} nodes", num_coalesced);
if num_coalesced == 0 {
break;
}
}
}

let node = &graph[idx];
// Comparators depend on the link weight as well as the type,
// we could implement that later if it's beneficial enough.
if matches!(node.ty, NodeType::Comparator { .. }) || !node.is_removable() {
continue;
}
fn status_message(&self) -> &'static str {
"Combining duplicate logic"
}
}

let Ok(edge) = graph.edges_directed(idx, Direction::Incoming).exactly_one() else {
continue;
};
fn run_iteration(graph: &mut CompileGraph) -> usize {
let mut num_coalesced = 0;
for i in 0..graph.node_bound() {
let idx = NodeIdx::new(i);
if !graph.contains_node(idx) {
continue;
}

if edge.weight().ty != LinkType::Default {
continue;
}
let node = &graph[idx];
// Comparators depend on the link weight as well as the type,
// we could implement that later if it's beneficial enough.
if matches!(node.ty, NodeType::Comparator { .. }) || !node.is_removable() {
continue;
}

let source = edge.source();
// Comparators might output less than 15 ss
if matches!(graph[source].ty, NodeType::Comparator { .. }) {
continue;
}
coalesce_outgoing(graph, source, idx);
let Ok(edge) = graph.edges_directed(idx, Direction::Incoming).exactly_one() else {
continue;
};

if edge.weight().ty != LinkType::Default {
continue;
}
}

fn status_message(&self) -> &'static str {
"Combining duplicate logic"
let source = edge.source();
// Comparators might output less than 15 ss
if matches!(graph[source].ty, NodeType::Comparator { .. }) {
continue;
}
num_coalesced += coalesce_outgoing(graph, source, idx);
}
num_coalesced
}

fn coalesce_outgoing(graph: &mut CompileGraph, source_idx: NodeIdx, into_idx: NodeIdx) {
fn coalesce_outgoing(graph: &mut CompileGraph, source_idx: NodeIdx, into_idx: NodeIdx) -> usize {
let mut num_coalesced = 0;
let mut walk_outgoing = graph
.neighbors_directed(source_idx, Direction::Outgoing)
.detach();
Expand All @@ -66,12 +80,15 @@ fn coalesce_outgoing(graph: &mut CompileGraph, source_idx: NodeIdx, into_idx: No
== 1
{
coalesce(graph, dest_idx, into_idx);
num_coalesced += 1;
}
}
num_coalesced
}

fn coalesce(graph: &mut CompileGraph, node: NodeIdx, into: NodeIdx) {
let mut walk_outgoing = graph.neighbors_directed(node, Direction::Outgoing).detach();
let mut walk_outgoing: petgraph::stable_graph::WalkNeighbors<u32> =
graph.neighbors_directed(node, Direction::Outgoing).detach();
while let Some(edge_idx) = walk_outgoing.next_edge(graph) {
let dest = graph.edge_endpoints(edge_idx).unwrap().1;
let weight = graph.remove_edge(edge_idx).unwrap();
Expand Down

0 comments on commit 21b62fc

Please sign in to comment.