From e0703dd72851f1d412ade4c6dc3b4b1ed791b159 Mon Sep 17 00:00:00 2001 From: Edmund Lam Date: Fri, 24 Jan 2025 16:06:17 -0500 Subject: [PATCH] Port attributes (#486) * port attributes * swap to indexstore for port attributes * cleanup unused import --- crates/ast/src/control.rs | 7 +++++-- crates/ast/src/parser.rs | 4 ++-- crates/ast/src/syntax.pest | 2 +- crates/filament/src/ir_passes/bundle_elim.rs | 5 +++++ crates/filament/src/ir_passes/mono/monosig.rs | 3 +++ crates/filament/src/ir_passes/mono/utils/comp.rs | 15 ++++++++++++++- crates/ir/src/comp.rs | 4 +++- crates/ir/src/from_ast/astconv.rs | 3 +++ crates/utils/src/attr/mod.rs | 5 +++-- crates/utils/src/attr/types.rs | 6 ++++++ crates/utils/src/lib.rs | 5 ++++- crates/utils/src/macros.rs | 2 +- 12 files changed, 50 insertions(+), 11 deletions(-) diff --git a/crates/ast/src/control.rs b/crates/ast/src/control.rs index 236fa462b..48489b50f 100644 --- a/crates/ast/src/control.rs +++ b/crates/ast/src/control.rs @@ -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)] @@ -365,11 +366,13 @@ pub struct Bundle { pub name: Loc, /// Type of the bundle pub typ: BundleType, + /// Attributes associated with the bundle + pub attrs: PortAttrs, } impl Bundle { - pub fn new(name: Loc, typ: BundleType) -> Self { - Self { name, typ } + pub fn new(name: Loc, typ: BundleType, attrs: PortAttrs) -> Self { + Self { name, typ, attrs } } /// Resolve expressions in the Bundle diff --git a/crates/ast/src/parser.rs b/crates/ast/src/parser.rs index b38c4c1ff..4dcd5aeae 100644 --- a/crates/ast/src/parser.rs +++ b/crates/ast/src/parser.rs @@ -719,7 +719,7 @@ impl FilamentParser { fn bundle_def(input: Node) -> ParseResult { 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() { @@ -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)) } ) } diff --git a/crates/ast/src/syntax.pest b/crates/ast/src/syntax.pest index b66029fa3..79231bee3 100644 --- a/crates/ast/src/syntax.pest +++ b/crates/ast/src/syntax.pest @@ -175,7 +175,7 @@ bundle_typ = { // Bundle definition bundle_def = { - identifier ~ ("[" ~ expr ~ "]")* ~ ":" ~ bundle_typ + attributes ~ identifier ~ ("[" ~ expr ~ "]")* ~ ":" ~ bundle_typ } // Ports diff --git a/crates/filament/src/ir_passes/bundle_elim.rs b/crates/filament/src/ir_passes/bundle_elim.rs index ac4f144bd..e3d13c77c 100644 --- a/crates/filament/src/ir_passes/bundle_elim.rs +++ b/crates/filament/src/ir_passes/bundle_elim.rs @@ -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(); @@ -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), diff --git a/crates/filament/src/ir_passes/mono/monosig.rs b/crates/filament/src/ir_passes/mono/monosig.rs index 6d42d124f..f56f1dcfd 100644 --- a/crates/filament/src/ir_passes/mono/monosig.rs +++ b/crates/filament/src/ir_passes/mono/monosig.rs @@ -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, @@ -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); diff --git a/crates/filament/src/ir_passes/mono/utils/comp.rs b/crates/filament/src/ir_passes/mono/utils/comp.rs index 73ae0242e..3e8fac990 100644 --- a/crates/filament/src/ir_passes/mono/utils/comp.rs +++ b/crates/filament/src/ir_passes/mono/utils/comp.rs @@ -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}; @@ -25,6 +27,9 @@ impl<'a> UnderlyingComp<'a> { pub fn ports(&self) -> &IndexStore { self.0.ports() } + pub fn port_attrs(&self) -> &DenseIndexInfo { + &self.0.port_attrs + } pub fn src_info(&self) -> &Option { &self.0.src_info } @@ -95,6 +100,14 @@ impl BaseComp { self.0.src_info = other; } + pub fn push_port_attrs( + &mut self, + key: Base, + val: utils::PortAttrs, + ) { + self.0.port_attrs.push(key.get(), val); + } + pub fn extend_cmds( &mut self, other: impl IntoIterator, diff --git a/crates/ir/src/comp.rs b/crates/ir/src/comp.rs index 4e258dfd1..fcbdddec6 100644 --- a/crates/ir/src/comp.rs +++ b/crates/ir/src/comp.rs @@ -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; @@ -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, /// The input parameters to the component pub(crate) param_args: Box<[ParamIdx]>, /// The input events to the component diff --git a/crates/ir/src/from_ast/astconv.rs b/crates/ir/src/from_ast/astconv.rs index 435a3c321..b95741784 100644 --- a/crates/ir/src/from_ast/astconv.rs +++ b/crates/ir/src/from_ast/astconv.rs @@ -440,6 +440,7 @@ impl<'prog> BuildCtx<'prog> { liveness, bitwidth, }, + attrs, } = pd; let info = self.comp().add(ir::Info::port( @@ -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 { diff --git a/crates/utils/src/attr/mod.rs b/crates/utils/src/attr/mod.rs index a21d6a3e1..675e52fbb 100644 --- a/crates/utils/src/attr/mod.rs +++ b/crates/utils/src/attr/mod.rs @@ -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}, }; diff --git a/crates/utils/src/attr/types.rs b/crates/utils/src/attr/types.rs index c239e7db0..a1a803f18 100644 --- a/crates/utils/src/attr/types.rs +++ b/crates/utils/src/attr/types.rs @@ -13,3 +13,9 @@ attr_set! { numeric { }; } + +attr_set! { + port_attrs; + flag {}; + numeric {}; +} diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index 80ef01b7c..164ab8409 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -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; diff --git a/crates/utils/src/macros.rs b/crates/utils/src/macros.rs index b831035cc..8a13d6ebe 100644 --- a/crates/utils/src/macros.rs +++ b/crates/utils/src/macros.rs @@ -83,7 +83,7 @@ macro_rules! attr_enum { )* }; ) => { - attr_enum! { + $crate::attr_enum! { enum $name; pub {}; priv {