Skip to content

Commit

Permalink
feat(hydroflow_lang): Add initial structure for by-reference edge types
Browse files Browse the repository at this point in the history
  • Loading branch information
MingweiSamuel committed Jan 5, 2024
1 parent d23c229 commit 57e002a
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 77 deletions.
28 changes: 16 additions & 12 deletions hydroflow_lang/src/graph/flat_graph_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use syn::spanned::Spanned;
use syn::{Error, Ident, ItemUse};

use super::ops::find_op_op_constraints;
use super::{GraphNodeId, HydroflowGraph, Node, PortIndexValue};
use super::{GraphEdgeType, GraphNode, GraphNodeId, HydroflowGraph, PortIndexValue};
use crate::diagnostic::{Diagnostic, Level};
use crate::graph::ops::{PortListSpec, RangeTrait};
use crate::parse::{HfCode, HfStatement, Operator, Pipeline};
Expand Down Expand Up @@ -77,14 +77,14 @@ impl FlatGraphBuilder {
builder.invocating_file_path = root_path; // imports inside of modules should be relative to the importing file.
builder.module_boundary_nodes = Some((
builder.flat_graph.insert_node(
Node::ModuleBoundary {
GraphNode::ModuleBoundary {
input: true,
import_expr: Span::call_site(),
},
Some(Ident::new("input", Span::call_site())),
),
builder.flat_graph.insert_node(
Node::ModuleBoundary {
GraphNode::ModuleBoundary {
input: false,
import_expr: Span::call_site(),
},
Expand Down Expand Up @@ -219,7 +219,7 @@ impl FlatGraphBuilder {
let op_span = Some(operator.span());
let nid = self
.flat_graph
.insert_node(Node::Operator(operator), current_varname.cloned());
.insert_node(GraphNode::Operator(operator), current_varname.cloned());
Ends {
inn: Some((PortIndexValue::Elided(op_span), GraphDet::Determined(nid))),
out: Some((PortIndexValue::Elided(op_span), GraphDet::Determined(nid))),
Expand Down Expand Up @@ -292,14 +292,14 @@ impl FlatGraphBuilder {

for (nid, node) in other.nodes() {
match node {
Node::Operator(_) => {
GraphNode::Operator(_) => {
let varname = other.node_varname(nid);
let new_id = self.flat_graph.insert_node(node.clone(), varname);
node_mapping.insert(nid, new_id);
}
Node::ModuleBoundary { input, .. } => {
GraphNode::ModuleBoundary { input, .. } => {
let new_id = self.flat_graph.insert_node(
Node::ModuleBoundary {
GraphNode::ModuleBoundary {
input: *input,
import_expr: parent_span,
},
Expand All @@ -321,14 +321,17 @@ impl FlatGraphBuilder {
Some((PortIndexValue::Elided(None), GraphDet::Determined(new_id)));
}
}
Node::Handoff { .. } => panic!("Handoff in graph that is being merged into self"),
GraphNode::Handoff { .. } => {
panic!("Handoff in graph that is being merged into self")
}
}
}

for (eid, (src, dst)) in other.edges() {
let (src_port, dst_port) = other.edge_ports(eid);

self.flat_graph.insert_edge(
other.edge_type(eid),
*node_mapping.get(&src).unwrap(),
src_port.clone(),
*node_mapping.get(&dst).unwrap(),
Expand Down Expand Up @@ -490,7 +493,8 @@ impl FlatGraphBuilder {
}
}
}
self.flat_graph.insert_edge(src, src_port, dst, dst_port);
self.flat_graph
.insert_edge(GraphEdgeType::Value, src, src_port, dst, dst_port);
}

/// Process operators and emit operator errors.
Expand All @@ -510,7 +514,7 @@ impl FlatGraphBuilder {
fn check_operator_errors(&mut self) {
for (node_id, node) in self.flat_graph.nodes() {
match node {
Node::Operator(operator) => {
GraphNode::Operator(operator) => {
let Some(op_constraints) = find_op_op_constraints(operator) else {
continue;
};
Expand Down Expand Up @@ -693,8 +697,8 @@ impl FlatGraphBuilder {
&mut self.diagnostics,
);
}
Node::Handoff { .. } => todo!("Node::Handoff"),
Node::ModuleBoundary { .. } => {
GraphNode::Handoff { .. } => todo!("Node::Handoff"),
GraphNode::ModuleBoundary { .. } => {
// Module boundaries don't require any checking.
}
}
Expand Down
28 changes: 18 additions & 10 deletions hydroflow_lang/src/graph/flat_to_partitioned.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ use syn::parse_quote;

use super::hydroflow_graph::HydroflowGraph;
use super::ops::{find_node_op_constraints, DelayType};
use super::{graph_algorithms, node_color, Color, GraphEdgeId, GraphNodeId, GraphSubgraphId, Node};
use super::{
graph_algorithms, node_color, Color, GraphEdgeId, GraphNode, GraphNodeId, GraphSubgraphId,
};
use crate::diagnostic::{Diagnostic, Level};
use crate::union_find::UnionFind;

Expand Down Expand Up @@ -40,7 +42,11 @@ fn find_subgraph_unionfind(
.filter_map(|(node_id, node)| {
let inn_degree = partitioned_graph.node_degree_in(node_id);
let out_degree = partitioned_graph.node_degree_out(node_id);
let op_color = node_color(matches!(node, Node::Handoff { .. }), inn_degree, out_degree);
let op_color = node_color(
matches!(node, GraphNode::Handoff { .. }),
inn_degree,
out_degree,
);
op_color.map(|op_color| (node_id, op_color))
})
.collect();
Expand Down Expand Up @@ -110,14 +116,14 @@ fn make_subgraph_collect(
let topo_sort = graph_algorithms::topo_sort(
partitioned_graph
.nodes()
.filter(|&(_, node)| !matches!(node, Node::Handoff { .. }))
.filter(|&(_, node)| !matches!(node, GraphNode::Handoff { .. }))
.map(|(node_id, _)| node_id),
|v| {
partitioned_graph
.node_predecessor_nodes(v)
.filter(|&pred_id| {
let pred = partitioned_graph.node(pred_id);
!matches!(pred, Node::Handoff { .. })
!matches!(pred, GraphNode::Handoff { .. })
})
},
);
Expand Down Expand Up @@ -158,11 +164,13 @@ fn make_subgraphs(
// Already has a handoff, no need to insert one.
let src_node = partitioned_graph.node(src_id);
let dst_node = partitioned_graph.node(dst_id);
if matches!(src_node, Node::Handoff { .. }) || matches!(dst_node, Node::Handoff { .. }) {
if matches!(src_node, GraphNode::Handoff { .. })
|| matches!(dst_node, GraphNode::Handoff { .. })
{
continue;
}

let hoff = Node::Handoff {
let hoff = GraphNode::Handoff {
src_span: src_node.span(),
dst_span: dst_node.span(),
};
Expand Down Expand Up @@ -266,7 +274,7 @@ fn find_subgraph_strata(
Default::default();

for (node_id, node) in partitioned_graph.nodes() {
if matches!(node, Node::Handoff { .. }) {
if matches!(node, GraphNode::Handoff { .. }) {
assert_eq!(1, partitioned_graph.node_successors(node_id).count());
let (succ_edge, succ) = partitioned_graph.node_successors(node_id).next().unwrap();

Expand Down Expand Up @@ -348,10 +356,10 @@ fn find_subgraph_strata(
let (new_node_id, new_edge_id) = partitioned_graph.insert_intermediate_node(
edge_id,
// TODO(mingwei): Proper span w/ `parse_quote_spanned!`?
Node::Operator(parse_quote! { identity() }),
GraphNode::Operator(parse_quote! { identity() }),
);
// Intermediate: A (src) -> H -> ID -> B (dst)
let hoff = Node::Handoff {
let hoff = GraphNode::Handoff {
src_span: Span::call_site(), // TODO(mingwei): Proper spanning?
dst_span: Span::call_site(),
};
Expand Down Expand Up @@ -424,7 +432,7 @@ fn separate_external_inputs(partitioned_graph: &mut HydroflowGraph) {
.collect::<Vec<_>>()
{
let span = partitioned_graph.node(node_id).span();
let hoff = Node::Handoff {
let hoff = GraphNode::Handoff {
src_span: span,
dst_span: span,
};
Expand Down
Loading

0 comments on commit 57e002a

Please sign in to comment.