Skip to content

Commit

Permalink
small change + another test case
Browse files Browse the repository at this point in the history
  • Loading branch information
calebmkim committed Feb 1, 2024
1 parent ffade0d commit b684d04
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 18 deletions.
19 changes: 8 additions & 11 deletions calyx-opt/src/passes/schedule_compaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use crate::{
};
use calyx_ir as ir;
use calyx_utils::CalyxResult;
use ir::GetAttributes;
use itertools::Itertools;
use petgraph::{algo, graph::NodeIndex};
use std::collections::HashMap;
Expand Down Expand Up @@ -230,17 +231,13 @@ impl ScheduleCompaction {
par_control_threads.iter().map(|c| c.get_latency()).max();
assert!(max.unwrap() == total_time, "The schedule expects latency {}. The static par that was built has latency {}", total_time, max.unwrap());

if par_control_threads.len() == 1 {
let c = Vec::pop(&mut par_control_threads).unwrap();
ir::Control::Static(c)
} else {
let s_par = ir::StaticControl::Par(ir::StaticPar {
stmts: par_control_threads,
attributes: ir::Attributes::default(),
latency: total_time,
});
ir::Control::Static(s_par)
}
let mut s_par = ir::StaticControl::Par(ir::StaticPar {
stmts: par_control_threads,
attributes: ir::Attributes::default(),
latency: total_time,
});
s_par.get_mut_attributes().insert(ir::BoolAttr::Promoted, 1);
ir::Control::Static(s_par)
} else {
panic!(
"Error when producing topo sort. Dependency graph has a cycle."
Expand Down
39 changes: 32 additions & 7 deletions calyx-opt/src/passes/static_promotion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::traversal::{
};
use calyx_ir::{self as ir, LibrarySignatures};
use calyx_utils::CalyxResult;
use ir::GetAttributes;
use itertools::Itertools;
use std::num::NonZeroU64;
use std::rc::Rc;
Expand Down Expand Up @@ -108,12 +109,41 @@ impl StaticPromotion {
diff <= self.if_diff_limit.unwrap()
}

fn approx_size_static(sc: &ir::StaticControl, promoted: bool) -> u64 {
if !(sc.get_attributes().has(ir::BoolAttr::Promoted) || promoted) {
return APPROX_ENABLE_SIZE;
}
match sc {
ir::StaticControl::Empty(_) => 0,
ir::StaticControl::Enable(_) | ir::StaticControl::Invoke(_) => {
APPROX_ENABLE_SIZE
}
ir::StaticControl::Repeat(ir::StaticRepeat { body, .. }) => {
Self::approx_size_static(body, true) + APPROX_WHILE_REPEAT_SIZE
}
ir::StaticControl::If(ir::StaticIf {
tbranch, fbranch, ..
}) => {
Self::approx_size_static(tbranch, true)
+ Self::approx_size_static(fbranch, true)
+ APPROX_IF_SIZE
}
ir::StaticControl::Par(ir::StaticPar { stmts, .. })
| ir::StaticControl::Seq(ir::StaticSeq { stmts, .. }) => stmts
.iter()
.map(|stmt| Self::approx_size_static(stmt, true))
.sum(),
}
}

/// Calculates the approximate "size" of the control statements.
/// Tries to approximate the number of dynamic FSM transitions that will occur
fn approx_size(c: &ir::Control) -> u64 {
match c {
ir::Control::Empty(_) => 0,
ir::Control::Enable(_) => APPROX_ENABLE_SIZE,
ir::Control::Enable(_) | ir::Control::Invoke(_) => {
APPROX_ENABLE_SIZE
}
ir::Control::Seq(ir::Seq { stmts, .. })
| ir::Control::Par(ir::Par { stmts, .. }) => {
stmts.iter().map(Self::approx_size).sum()
Expand All @@ -129,12 +159,7 @@ impl StaticPromotion {
+ Self::approx_size(fbranch)
+ APPROX_IF_SIZE
}
ir::Control::Static(_) => {
// static control appears as one big group to the dynamic FSM
1
}
// Invokes are same size as enables.
ir::Control::Invoke(_) => APPROX_ENABLE_SIZE,
ir::Control::Static(sc) => Self::approx_size_static(sc, false),
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/passes/static-passes/both-passes-promote.futil
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// -p well-formed -p static-inference -p schedule-compaction -p static-promotion -x static-promotion:threshold=5
// Compaction should promote the body, promotion should promote the loop.

import "primitives/core.futil";

component main() -> () {
cells {
a = std_reg(2);
b = std_reg(2);
c = std_reg(2);
}

wires {
group A {
a.in = 2'd0;
a.write_en = 1'b1;
A[done] = a.done;
}

group B {
b.in = 2'd1;
b.write_en = 1'b1;
B[done] = b.done;
}

group C {
c.in = 2'd2;
c.write_en = 1'b1;
C[done] = c.done;
}
}

control {
repeat 10 {
seq { A; B; C;}
}
}
}

0 comments on commit b684d04

Please sign in to comment.