Skip to content

Commit

Permalink
Port attributes (#486)
Browse files Browse the repository at this point in the history
* port attributes

* swap to indexstore for port attributes

* cleanup unused import
  • Loading branch information
UnsignedByte authored Jan 24, 2025
1 parent 3377070 commit e0703dd
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 11 deletions.
7 changes: 5 additions & 2 deletions crates/ast/src/control.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::{
Binding, Expr, Id, Implication, Loc, OrderConstraint, Range, Time,
};
use fil_utils::PortAttrs;
use struct_variant::struct_variant;

#[derive(Clone)]
Expand Down Expand Up @@ -365,11 +366,13 @@ pub struct Bundle {
pub name: Loc<Id>,
/// Type of the bundle
pub typ: BundleType,
/// Attributes associated with the bundle
pub attrs: PortAttrs,
}

impl Bundle {
pub fn new(name: Loc<Id>, typ: BundleType) -> Self {
Self { name, typ }
pub fn new(name: Loc<Id>, typ: BundleType, attrs: PortAttrs) -> Self {
Self { name, typ, attrs }
}

/// Resolve expressions in the Bundle
Expand Down
4 changes: 2 additions & 2 deletions crates/ast/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ impl FilamentParser {
fn bundle_def(input: Node) -> ParseResult<ast::Bundle> {
match_nodes!(
input.clone().into_children();
[identifier(name), expr(sizes).., bundle_typ((params, range, width))] => {
[attributes(attrs), identifier(name), expr(sizes).., bundle_typ((params, range, width))] => {
let sizes = sizes.collect_vec();
// If no size is specified, treat this is as one dimensional bundle with size 1.
let (sizes, s_len) = if sizes.is_empty() {
Expand All @@ -739,7 +739,7 @@ impl FilamentParser {
params.push(Loc::unknown(ast::Id::from(format!("_{i}"))));
});

Ok(ast::Bundle::new(name, ast::BundleType::new(params, sizes, range, width)))
Ok(ast::Bundle::new(name, ast::BundleType::new(params, sizes, range, width), attrs))
}
)
}
Expand Down
2 changes: 1 addition & 1 deletion crates/ast/src/syntax.pest
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ bundle_typ = {

// Bundle definition
bundle_def = {
identifier ~ ("[" ~ expr ~ "]")* ~ ":" ~ bundle_typ
attributes ~ identifier ~ ("[" ~ expr ~ "]")* ~ ":" ~ bundle_typ
}

// Ports
Expand Down
5 changes: 5 additions & 0 deletions crates/filament/src/ir_passes/bundle_elim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ impl BundleElim {
info,
} = comp.get(pidx).clone();

let attrs = comp.port_attrs.get(pidx).clone();

let Liveness { idxs, lens, range } = live;

let start = comp.get(range.start).clone();
Expand Down Expand Up @@ -154,6 +156,9 @@ impl BundleElim {
width,
});

// copy over the attributes
comp.port_attrs.push(pidx, attrs.clone());

// Fill in the live idxs with a new dummy index
let port = ir::Param {
owner: ir::ParamOwner::bundle(pidx),
Expand Down
3 changes: 3 additions & 0 deletions crates/filament/src/ir_passes/mono/monosig.rs
Original file line number Diff line number Diff line change
Expand Up @@ -801,6 +801,7 @@ impl MonoSig {
live,
info,
} = underlying.get(port);
let attrs = underlying.port_attrs().get(port.idx());

let inv = match owner {
ir::PortOwner::Sig { .. } | ir::PortOwner::Local => None,
Expand All @@ -822,6 +823,8 @@ impl MonoSig {
info: info.get(),
});

self.base.push_port_attrs(new_port, attrs.clone());

// Overwrite the value in the port map if any. This is okay because this
// method can be called on local ports defined in iterative scopes.
self.port_map.insert(port_map_k, new_port);
Expand Down
15 changes: 14 additions & 1 deletion crates/filament/src/ir_passes/mono/utils/comp.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use fil_ast as ast;
use fil_ir::{
self as ir, AddCtx, Ctx, DisplayCtx, Idx, IndexStore, InterfaceSrc, MutCtx,
self as ir, AddCtx, Ctx, DenseIndexInfo, DisplayCtx, Idx, IndexStore,
InterfaceSrc, MutCtx,
};
use fil_utils as utils;

use super::{Base, IntoBase, IntoUdl, Underlying};

Expand All @@ -25,6 +27,9 @@ impl<'a> UnderlyingComp<'a> {
pub fn ports(&self) -> &IndexStore<ir::Port> {
self.0.ports()
}
pub fn port_attrs(&self) -> &DenseIndexInfo<ir::Port, utils::PortAttrs> {
&self.0.port_attrs
}
pub fn src_info(&self) -> &Option<InterfaceSrc> {
&self.0.src_info
}
Expand Down Expand Up @@ -95,6 +100,14 @@ impl BaseComp {
self.0.src_info = other;
}

pub fn push_port_attrs(
&mut self,
key: Base<ir::Port>,
val: utils::PortAttrs,
) {
self.0.port_attrs.push(key.get(), val);
}

pub fn extend_cmds(
&mut self,
other: impl IntoIterator<Item = ir::Command>,
Expand Down
4 changes: 3 additions & 1 deletion crates/ir/src/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
InvIdx, Invoke, MutCtx, Param, ParamIdx, Port, PortIdx, Prop, PropIdx,
Time, TimeSub,
};
use crate::{utils::Idx, ParamOwner};
use crate::{utils::Idx, DenseIndexInfo, ParamOwner};
use fil_ast as ast;
use fil_derive::Ctx;
use fil_utils as utils;
Expand Down Expand Up @@ -59,6 +59,8 @@ pub struct Component {
// ============== Component signature ===============
/// Attributes of the component
pub attrs: utils::CompAttrs,
/// Attributes of ports in the component
pub port_attrs: DenseIndexInfo<Port, utils::PortAttrs>,
/// The input parameters to the component
pub(crate) param_args: Box<[ParamIdx]>,
/// The input events to the component
Expand Down
3 changes: 3 additions & 0 deletions crates/ir/src/from_ast/astconv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,7 @@ impl<'prog> BuildCtx<'prog> {
liveness,
bitwidth,
},
attrs,
} = pd;

let info = self.comp().add(ir::Info::port(
Expand Down Expand Up @@ -478,6 +479,8 @@ impl<'prog> BuildCtx<'prog> {
// Defines helper variable here due to lifetime issues
let is_sig_port = p.is_sig();
let idx = self.comp().add(p);
// Add the attributes to port attributes
self.comp().port_attrs.push(idx, attrs);
// Fixup the liveness index parameter's owner
let idxs = self.comp().get(idx).live.idxs.clone();
for p in idxs {
Expand Down
5 changes: 3 additions & 2 deletions crates/utils/src/attr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ mod types;
pub use attributes::Attributes;
pub use ctx::AttrCtx;
pub use store::AttrStore;
pub use types::comp_attrs::{
Attrs as CompAttrs, Bool as CompBool, Num as CompNum,
pub use types::{
comp_attrs::{Attrs as CompAttrs, Bool as CompBool, Num as CompNum},
port_attrs::{Attrs as PortAttrs, Bool as PortBool, Num as PortNum},
};
6 changes: 6 additions & 0 deletions crates/utils/src/attr/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,9 @@ attr_set! {
numeric {
};
}

attr_set! {
port_attrs;
flag {};
numeric {};
}
5 changes: 4 additions & 1 deletion crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ mod math;
mod position;
mod reporter;

pub use attr::{AttrCtx, AttrStore, Attributes, CompAttrs, CompBool, CompNum};
pub use attr::{
AttrCtx, AttrStore, Attributes, CompAttrs, CompBool, CompNum, PortAttrs,
PortBool, PortNum,
};
pub use errors::{Error, FilamentResult};
pub use gsym::GSym;
pub use id::Id;
Expand Down
2 changes: 1 addition & 1 deletion crates/utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ macro_rules! attr_enum {
)*
};
) => {
attr_enum! {
$crate::attr_enum! {
enum $name;
pub {};
priv {
Expand Down

0 comments on commit e0703dd

Please sign in to comment.