Skip to content

Commit

Permalink
fix(schematic): correctly deduplicate SCIR cell names during export (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
rohanku authored Jul 13, 2024
1 parent 43a2b29 commit 48af6fc
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
23 changes: 23 additions & 0 deletions libs/scir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ use indexmap::IndexMap;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use tracing::{span, Level};
use uniquify::Names;

pub mod merge;
pub mod schema;
Expand Down Expand Up @@ -483,6 +484,9 @@ pub struct LibraryBuilder<S: Schema + ?Sized = NoSchema> {
/// Cell names are only guaranteed to be unique in a validated [`Library`].
name_map: HashMap<ArcStr, CellId>,

/// Assigned names for purposes of auto-assigning names to new cells.
names: Names<CellId>,

/// A map of the primitives in the library.
primitives: IndexMap<PrimitiveId, S::Primitive>,

Expand All @@ -498,6 +502,7 @@ impl<S: Schema + ?Sized> Default for LibraryBuilder<S> {
cells: IndexMap::new(),
primitives: IndexMap::new(),
name_map: HashMap::new(),
names: Names::new(),
top: None,
}
}
Expand All @@ -510,6 +515,7 @@ impl<S: Schema<Primitive = impl Clone> + ?Sized> Clone for LibraryBuilder<S> {
primitive_id: self.primitive_id,
cells: self.cells.clone(),
name_map: self.name_map.clone(),
names: self.names.clone(),
primitives: self.primitives.clone(),
top: self.top,
}
Expand All @@ -523,6 +529,7 @@ impl<S: Schema<Primitive = impl std::fmt::Debug> + ?Sized> std::fmt::Debug for L
let _ = builder.field("primitive_id", &self.primitive_id);
let _ = builder.field("cells", &self.cells);
let _ = builder.field("name_map", &self.name_map);
let _ = builder.field("names", &self.names);
let _ = builder.field("primitives", &self.primitives);
let _ = builder.field("top", &self.top);
builder.finish()
Expand Down Expand Up @@ -845,6 +852,19 @@ impl<S: Schema + ?Sized> LibraryBuilder<S> {
pub fn add_cell(&mut self, cell: Cell) -> CellId {
let id = self.alloc_cell_id();
self.name_map.insert(cell.name.clone(), id);
self.names.reserve_name(id, cell.name.clone());
self.cells.insert(id, cell);
id
}

/// Merges the given cell into the library.
///
/// Returns the ID of the newly added cell. May rename the cell if the name is already taken.
pub fn merge_cell(&mut self, mut cell: Cell) -> CellId {
let id = self.alloc_cell_id();
let n_name = self.names.assign_name(id, &cell.name);
cell.name = n_name;
self.name_map.insert(cell.name.clone(), id);
self.cells.insert(id, cell);
id
}
Expand Down Expand Up @@ -872,6 +892,7 @@ impl<S: Schema + ?Sized> LibraryBuilder<S> {
assert!(!self.cells.contains_key(&id));
self.cell_id = std::cmp::max(id.0, self.cell_id);
self.name_map.insert(cell.name.clone(), id);
self.names.reserve_name(id, cell.name.clone());
self.cells.insert(id, cell);
}

Expand Down Expand Up @@ -1361,6 +1382,7 @@ impl<S: Schema + ?Sized> LibraryBuilder<S> {
name_map,
primitives,
top,
names,
} = self;

for (_, cell) in cells.iter_mut() {
Expand All @@ -1378,6 +1400,7 @@ impl<S: Schema + ?Sized> LibraryBuilder<S> {
primitive_id,
cells,
name_map,
names,
primitives: primitives
.into_iter()
.map(|(k, v)| Ok((k, convert_primitive(v)?)))
Expand Down
2 changes: 1 addition & 1 deletion substrate/src/schematic/conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ impl<S: Schema + ?Sized> RawCell<S> {
cell: scir_cell, ..
} = cell_ctx;

let id = lib_ctx.lib.add_cell(scir_cell);
let id = lib_ctx.lib.merge_cell(scir_cell);
conv.cell_id = Some(id);
lib_ctx.conv.add_cell(self.id, conv);

Expand Down

0 comments on commit 48af6fc

Please sign in to comment.