Skip to content

Commit

Permalink
[cider/flat] Instantiate the beginning flat environment (#1578)
Browse files Browse the repository at this point in the history
* get a better definition for the bases

* silly type shennanigans

* store the entrypoint

* can allocated the hopefully correct initial sizes

* consolidate the imports

* the environment exists now!

* unused import

* erroneous import

* add a note about the sanity checks

* clear out the warnings

* add a basic stats printout for sanity checks
  • Loading branch information
EclecticGriffin authored Jun 26, 2023
1 parent 488117c commit de9fdd1
Show file tree
Hide file tree
Showing 13 changed files with 514 additions and 79 deletions.
107 changes: 100 additions & 7 deletions interp/src/flatten/flat_ir/base.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::num::NonZeroU32;
use std::{num::NonZeroU32, ops::Add};

use crate::flatten::structures::index_trait::{
impl_index, impl_index_nonzero, IndexRange,
impl_index, impl_index_nonzero, IndexRange, IndexRef,
};

use super::{cell_prototype::CellPrototype, prelude::Identifier};
Expand All @@ -10,7 +10,7 @@ use super::{cell_prototype::CellPrototype, prelude::Identifier};
// second arg to contract or expand as needed

/// The identifier for a component definition
#[derive(Debug, Eq, Copy, Clone, PartialEq)]
#[derive(Debug, Eq, Copy, Clone, PartialEq, Hash)]
pub struct ComponentIdx(u32);
impl_index!(ComponentIdx);

Expand Down Expand Up @@ -38,19 +38,24 @@ impl_index!(RefPortDefinitionIdx);

/// The index of a port instance in the global value map
#[derive(Debug, Eq, Copy, Clone, PartialEq, Hash, PartialOrd, Ord)]
pub struct GlobalPortId(u32);
impl_index!(GlobalPortId);
pub struct GlobalPortId(NonZeroU32);
impl_index_nonzero!(GlobalPortId);

/// The index of a cell instance in the global value map
#[derive(Debug, Eq, Copy, Clone, PartialEq, Hash, PartialOrd, Ord)]
pub struct GlobalCellId(u32);
impl_index!(GlobalCellId);
pub struct GlobalCellId(NonZeroU32);
impl_index_nonzero!(GlobalCellId);

/// The index of a ref cell instance in the global value map
#[derive(Debug, Eq, Copy, Clone, PartialEq, Hash, PartialOrd, Ord)]
pub struct GlobalRefCellId(u32);
impl_index!(GlobalRefCellId);

/// The index of a ref port instance in the global value map
#[derive(Debug, Eq, Copy, Clone, PartialEq, Hash, PartialOrd, Ord)]
pub struct GlobalRefPortId(u32);
impl_index!(GlobalRefPortId);

// Offset indices

/// A local port offset for a component. These are used in the definition of
Expand Down Expand Up @@ -286,3 +291,91 @@ mod sealed {
impl PortType for LocalPortOffset {}
impl PortType for LocalRefPortOffset {}
}

#[derive(Debug, Clone)]
pub struct BaseIndices {
pub port_base: GlobalPortId,
pub cell_base: GlobalCellId,
pub ref_cell_base: GlobalRefCellId,
pub ref_port_base: GlobalRefPortId,
}

impl BaseIndices {
pub fn new(
port_base: GlobalPortId,
cell_base: GlobalCellId,
ref_cell_base: GlobalRefCellId,
ref_port_base: GlobalRefPortId,
) -> Self {
Self {
port_base,
cell_base,
ref_cell_base,
ref_port_base,
}
}
}

impl Add<LocalPortOffset> for &BaseIndices {
type Output = GlobalPortId;

fn add(self, rhs: LocalPortOffset) -> Self::Output {
GlobalPortId::new(self.port_base.index() + rhs.index())
}
}

impl Add<LocalRefPortOffset> for &BaseIndices {
type Output = GlobalRefPortId;

fn add(self, rhs: LocalRefPortOffset) -> Self::Output {
GlobalRefPortId::new(self.ref_port_base.index() + rhs.index())
}
}

impl Add<LocalCellOffset> for &BaseIndices {
type Output = GlobalCellId;

fn add(self, rhs: LocalCellOffset) -> Self::Output {
GlobalCellId::new(self.cell_base.index() + rhs.index())
}
}

impl Add<LocalRefCellOffset> for &BaseIndices {
type Output = GlobalRefCellId;

fn add(self, rhs: LocalRefCellOffset) -> Self::Output {
GlobalRefCellId::new(self.ref_cell_base.index() + rhs.index())
}
}

impl Add<&LocalPortOffset> for &BaseIndices {
type Output = GlobalPortId;

fn add(self, rhs: &LocalPortOffset) -> Self::Output {
GlobalPortId::new(self.port_base.index() + rhs.index())
}
}

impl Add<&LocalRefPortOffset> for &BaseIndices {
type Output = GlobalRefPortId;

fn add(self, rhs: &LocalRefPortOffset) -> Self::Output {
GlobalRefPortId::new(self.ref_port_base.index() + rhs.index())
}
}

impl Add<&LocalCellOffset> for &BaseIndices {
type Output = GlobalCellId;

fn add(self, rhs: &LocalCellOffset) -> Self::Output {
GlobalCellId::new(self.cell_base.index() + rhs.index())
}
}

impl Add<&LocalRefCellOffset> for &BaseIndices {
type Output = GlobalRefCellId;

fn add(self, rhs: &LocalRefCellOffset) -> Self::Output {
GlobalRefCellId::new(self.ref_cell_base.index() + rhs.index())
}
}
33 changes: 28 additions & 5 deletions interp/src/flatten/flat_ir/cell_prototype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,23 @@ use crate::primitives::prim_utils::get_params;

use super::prelude::ComponentIdx;

#[derive(Debug, Clone)]
pub enum LiteralOrPrimitive {
Literal,
Primitive,
}

#[derive(Debug, Clone)]
pub enum CellPrototype {
Component(ComponentIdx),
ConstantLiteral { value: u64, width: u64 },
Register { width: u64 },
ConstantPrimitive { value: u64, width: u64 },
Constant {
value: u64,
width: u64,
c_type: LiteralOrPrimitive,
},
Register {
width: u64,
},
// TODO Griffin: lots more
Unknown(String, Box<cir::Binding>),
}
Expand Down Expand Up @@ -57,7 +68,11 @@ impl CellPrototype {
width: "WIDTH"
];

Self::ConstantPrimitive { value, width }
Self::Constant {
value,
width,
c_type: LiteralOrPrimitive::Primitive,
}
}

_ => CellPrototype::Unknown(
Expand All @@ -66,7 +81,15 @@ impl CellPrototype {
),
}
} else {
panic!("construct_primitive called on non-primitive cell");
unreachable!("construct_primitive called on non-primitive cell");
}
}

/// Returns `true` if the cell prototype is [`Component`].
///
/// [`Component`]: CellPrototype::Component
#[must_use]
pub fn is_component(&self) -> bool {
matches!(self, Self::Component(..))
}
}
32 changes: 17 additions & 15 deletions interp/src/flatten/flat_ir/control/translator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use calyx_ir::{self as cir, RRC};
use crate::{
flatten::{
flat_ir::{
cell_prototype::CellPrototype,
cell_prototype::{CellPrototype, LiteralOrPrimitive},
component::{AuxillaryComponentInfo, ComponentCore},
flatten_trait::{flatten_tree, FlattenTree, SingleHandle},
prelude::{
Expand Down Expand Up @@ -43,10 +43,13 @@ pub fn translate(orig_ctx: &cir::Context) -> Context {
// iteration over the components in a post-order so this is a hack instead

for comp in CompTraversal::new(&orig_ctx.components).iter() {
// TODO Griffin: capture the entry-point component and store that
// somewhere in the context
let _ = translate_component(comp, &mut ctx, &mut component_id_map);
translate_component(comp, &mut ctx, &mut component_id_map);
}

ctx.entry_point = *component_id_map
.get(&orig_ctx.entrypoint().name)
.expect("Unable to find entrypoint");

ctx
}

Expand Down Expand Up @@ -116,7 +119,6 @@ fn translate_guard(
flatten_tree(guard, None, &mut interp_ctx.guards, map)
}

#[must_use]
fn translate_component(
comp: &cir::Component,
ctx: &mut Context,
Expand Down Expand Up @@ -242,7 +244,8 @@ fn insert_port(
local_offset.into()
}
ContainmentType::Local => {
let idx_definition = secondary_ctx.push_local_port(id);
let idx_definition =
secondary_ctx.push_local_port(id, port.borrow().width as usize);
let local_offset = aux.port_offset_map.insert(idx_definition);
local_offset.into()
}
Expand Down Expand Up @@ -435,12 +438,11 @@ fn create_cell_prototype(
CellPrototype::Component(comp_id_map[name])
}

cir::CellType::Constant { val, width } => {
CellPrototype::ConstantLiteral {
value: *val,
width: *width,
}
}
cir::CellType::Constant { val, width } => CellPrototype::Constant {
value: *val,
width: *width,
c_type: LiteralOrPrimitive::Literal,
},
cir::CellType::ThisComponent => unreachable!(
"the flattening should not have this cell type, this is an error"
),
Expand Down Expand Up @@ -547,7 +549,7 @@ impl FlattenTree for cir::Control {
.find(|&candidate_offset| {
let candidate_def = comp_info
.port_offset_map[candidate_offset];
ctx.secondary[candidate_def] == id
ctx.secondary[candidate_def].name == id
})
.unwrap()
.into()
Expand All @@ -572,10 +574,10 @@ impl FlattenTree for cir::Control {

let ref_cells = inv.ref_cells.iter().map(|(ref_cell_id, realizing_cell)| {
let invoked_comp = invoked_comp.as_component().expect("cannot invoke a non-component with ref cells");
let target = &ctx.secondary[*invoked_comp].ref_cell_offset_map.iter().find(|(&_idx, &def_idx)| {
let target = &ctx.secondary[*invoked_comp].ref_cell_offset_map.iter().find(|(_idx, &def_idx)| {
let def = &ctx.secondary[def_idx];
def.name == resolve_id(ref_cell_id)
}).map(|(&t, _)| t).expect("Unable to find the given ref cell in the invoked component");
}).map(|(t, _)| t).expect("Unable to find the given ref cell in the invoked component");
(*target, layout.cell_map[&realizing_cell.as_raw()])
});

Expand Down
5 changes: 5 additions & 0 deletions interp/src/flatten/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ pub mod primitives;
mod structures;
pub(crate) mod text_utils;

use structures::environment::Environment;

pub fn flat_main(ctx: &calyx_ir::Context) {
let i_ctx = flat_ir::control::translator::translate(ctx);

i_ctx.printer().print_program();

let env = Environment::new(&i_ctx);
env.print_env_stats();
}
33 changes: 33 additions & 0 deletions interp/src/flatten/primitives/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use crate::flatten::{
flat_ir::prelude::CellInfo, structures::environment::Environment,
};

use super::{prim_trait::DummyPrimitive, Primitive};

pub fn build_primitive(
_prim: &CellInfo,
_env: &Environment,
) -> Box<dyn Primitive> {
return DummyPrimitive::new_dyn();

#[allow(unreachable_code)]
match &_prim.prototype {
crate::flatten::flat_ir::cell_prototype::CellPrototype::Constant {
value: _,
width: _,
c_type: _,
} => todo!(),
crate::flatten::flat_ir::cell_prototype::CellPrototype::Register {
width: _,
} => todo!(),
crate::flatten::flat_ir::cell_prototype::CellPrototype::Unknown(
_,
_,
) => todo!(),
crate::flatten::flat_ir::cell_prototype::CellPrototype::Component(
_,
) => unreachable!(
"Build primitive erroneously called on a calyx component"
),
}
}
2 changes: 2 additions & 0 deletions interp/src/flatten/primitives/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
mod builder;
pub mod prim_trait;

pub(crate) use builder::build_primitive;
pub use prim_trait::Primitive;
18 changes: 18 additions & 0 deletions interp/src/flatten/primitives/prim_trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,21 @@ pub trait Primitive {
fn exec_comb_paths(&self, portmap: &PortMap) -> PortResults;
fn exec_stateful_paths(&mut self, portmap: &PortMap) -> PortResults;
}

pub struct DummyPrimitive;

impl DummyPrimitive {
pub fn new_dyn() -> Box<dyn Primitive> {
Box::new(Self)
}
}

impl Primitive for DummyPrimitive {
fn exec_comb_paths(&self, _portmap: &PortMap) -> PortResults {
todo!()
}

fn exec_stateful_paths(&mut self, _portmap: &PortMap) -> PortResults {
todo!()
}
}
Loading

0 comments on commit de9fdd1

Please sign in to comment.