Skip to content

Commit c75494e

Browse files
committed
added potential infrastructure for dfg_passes
1 parent 6f0e148 commit c75494e

8 files changed

Lines changed: 121 additions & 2 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/dfg_passes/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ edition.workspace = true
55

66
[dependencies]
77
calyx-libm-dfg.workspace = true
8+
cranelift-entity.workspace = true
9+
malachite.workspace = true

crates/dfg_passes/src/builder.rs

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
use calyx_libm_dfg as dfg;
2+
use cranelift_entity::PrimaryMap;
3+
use dfg::*;
4+
use std::ops::{Index, IndexMut};
5+
/*
6+
#[derive(Copy, Clone, PartialEq, PartialOrd, Eq)]
7+
pub struct BuilderEdgeId(u32);
8+
entity_impl!(BuilderEdgeId, "be");
9+
*/
10+
11+
pub struct DfgBuilder {
12+
nodes: PrimaryMap<NodeId, Node>,
13+
edges: PrimaryMap<EdgeId, Edge>,
14+
}
15+
16+
impl DfgBuilder {
17+
pub fn new() -> Self {
18+
Self {
19+
nodes: PrimaryMap::new(),
20+
edges: PrimaryMap::new(),
21+
}
22+
}
23+
24+
pub fn finish(self) -> Dfg {
25+
let mut dfg: Dfg = Dfg::with_nodes(self.nodes);
26+
27+
for edge in self.edges.values() {
28+
let n_out = edge.output;
29+
let n_in = edge.input;
30+
dfg.add_edge(Edge {
31+
input: n_in,
32+
output: n_out,
33+
props: edge.props.clone(),
34+
edge_type: edge.edge_type.clone(),
35+
});
36+
}
37+
38+
dfg
39+
}
40+
41+
pub fn add_node(&mut self, node: Node) -> NodeId {
42+
self.nodes.push(node)
43+
}
44+
45+
pub fn connect_nodes(
46+
&mut self,
47+
input: NodeId,
48+
output: NodeId,
49+
props: Vec<String>,
50+
edge_type: Type,
51+
) -> EdgeId {
52+
self.edges.push(Edge {
53+
input,
54+
output,
55+
props,
56+
edge_type,
57+
})
58+
}
59+
}
60+
61+
macro_rules! dfg_index_impl {
62+
($field:ident, $idx:ty, $out:ty) => {
63+
impl Index<$idx> for DfgBuilder {
64+
type Output = $out;
65+
66+
#[inline]
67+
fn index(&self, index: $idx) -> &Self::Output {
68+
&self.$field[index]
69+
}
70+
}
71+
72+
impl IndexMut<$idx> for DfgBuilder {
73+
#[inline]
74+
fn index_mut(&mut self, index: $idx) -> &mut Self::Output {
75+
&mut self.$field[index]
76+
}
77+
}
78+
};
79+
}
80+
81+
dfg_index_impl!(nodes, NodeId, Node);

crates/dfg_passes/src/dfg_passes.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use calyx_libm_dfg as dfg;
22
use dfg::*;
33

4-
pub fn pass(dfg: &mut Dfg) -> Result<(), &'static str>{
4+
pub fn pass(dfg: &mut Dfg) -> Result<(), &'static str> {
55
for node in dfg.node_iter() {
66
let kind = node.node_type.clone();
77
match kind {
@@ -15,5 +15,8 @@ pub fn pass(dfg: &mut Dfg) -> Result<(), &'static str>{
1515
}
1616

1717
fn parse_operator(op: ArithOp) {
18-
18+
match op {
19+
ArithOp::Sin => {}
20+
_ => return,
21+
}
1922
}

crates/dfg_passes/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,7 @@
1+
mod builder;
12
mod dfg_passes;
3+
mod ops;
4+
5+
pub use builder::*;
6+
pub use dfg_passes::*;
7+
pub use ops::*;

crates/dfg_passes/src/ops/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod operator;
2+
pub mod table;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use calyx_libm_dfg as dfg;
2+
use dfg::*;
3+
4+
pub trait OpBuilder {
5+
fn build(&self) -> Dfg;
6+
}

crates/dfg_passes/src/ops/table.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use super::operator::OpBuilder;
2+
use calyx_libm_dfg as dfg;
3+
use dfg::*;
4+
use malachite::Rational;
5+
6+
pub struct Lut {
7+
pub left: Rational,
8+
pub right: Rational,
9+
pub size: u32,
10+
}
11+
12+
impl OpBuilder for Lut {
13+
fn build(&self) -> Dfg {
14+
Dfg::new()
15+
}
16+
}

0 commit comments

Comments
 (0)