Implementing replication in LStream, OASIS and GDS2

This commit is contained in:
Matthias Koefferlein 2026-03-22 21:21:11 +01:00
parent 56e84e7056
commit aa361277a0
25 changed files with 232 additions and 70 deletions

View File

@ -857,7 +857,7 @@ public:
void check_locked () const;
/**
* @brief Tell, if this cell is a proxy cell
* @brief Gets a value indicating if this cell is a proxy cell
*
* Proxy cells are such whose layout represents a snapshot of another entity.
* Such cells can be PCell variants or library references for example.
@ -867,6 +867,17 @@ public:
return false;
}
/**
* @brief Gets a value indicating that this cell is a replica that can be skipped
*
* This attribute is evaluated by file writers to skip cell replicas for
* library cells that do not want to replicated.
*/
virtual bool can_skip_replica () const
{
return false;
}
/**
* @brief Sets the cell name
*

View File

@ -692,6 +692,11 @@ public:
return false;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<db::CommonReaderOptions> ("common",

View File

@ -241,6 +241,13 @@ LibraryProxy::update (db::ImportLayerMapping *layer_mapping)
}
}
bool
LibraryProxy::can_skip_replica () const
{
const Library *lib = LibraryManager::instance ().lib (lib_id ());
return lib && ! lib->replicate ();
}
std::string
LibraryProxy::get_basic_name () const
{

View File

@ -93,6 +93,14 @@ public:
return true;
}
/**
* @brief Gets a value indicating that this cell is a replica that can be skipped
*
* This attribute is evaluated by file writers to skip cell replicas for
* library cells that do not want to replicated.
*/
virtual bool can_skip_replica () const;
/**
* @brief Gets the basic name
*

View File

@ -56,7 +56,7 @@ SaveLayoutOptions::operator= (const SaveLayoutOptions &d)
m_format = d.m_format;
m_layers = d.m_layers;
m_cells = d.m_cells;
m_implied_childred = d.m_implied_childred;
m_implied_children = d.m_implied_children;
m_all_layers = d.m_all_layers;
m_all_cells = d.m_all_cells;
m_dbu = d.m_dbu;
@ -189,7 +189,7 @@ SaveLayoutOptions::add_cell (db::cell_index_type cell_index)
{
m_all_cells = false;
m_cells.insert (cell_index);
m_implied_childred.insert (cell_index);
m_implied_children.insert (cell_index);
}
void
@ -204,7 +204,7 @@ SaveLayoutOptions::clear_cells ()
{
m_all_cells = false;
m_cells.clear ();
m_implied_childred.clear ();
m_implied_children.clear ();
}
void
@ -212,7 +212,7 @@ SaveLayoutOptions::select_all_cells ()
{
m_all_cells = true;
m_cells.clear ();
m_implied_childred.clear ();
m_implied_children.clear ();
}
void
@ -340,21 +340,75 @@ SaveLayoutOptions::get_valid_layers (const db::Layout &layout, std::vector <std:
}
}
static void
collect_called_cells_unskipped (db::cell_index_type ci, const db::Layout &layout, std::set<cell_index_type> &called)
{
const db::Cell &c = layout.cell (ci);
if (c.can_skip_replica ()) {
return;
}
for (auto cc = c.begin_child_cells (); ! cc.at_end (); ++cc) {
if (called.find (*cc) == called.end () && layout.is_valid_cell_index (*cc)) {
called.insert (*cc);
collect_called_cells_unskipped (*cc, layout, called);
}
}
}
void
SaveLayoutOptions::get_cells (const db::Layout &layout, std::set <db::cell_index_type> &cells, const std::vector <std::pair <unsigned int, db::LayerProperties> > &valid_layers, bool require_unique_names) const
{
bool has_context = m_write_context_info;
for (tl::Registrar<db::StreamFormatDeclaration>::iterator fmt = tl::Registrar<db::StreamFormatDeclaration>::begin (); fmt != tl::Registrar<db::StreamFormatDeclaration>::end (); ++fmt) {
if (fmt->format_name () == m_format) {
if (! fmt->supports_context ()) {
has_context = false;
}
break;
}
}
if (m_all_cells) {
for (db::Layout::const_iterator cell = layout.begin (); cell != layout.end (); ++cell) {
cells.insert (cell->cell_index ());
bool has_skipped_replica = false;
// check if we have skipped replicas
if (has_context) {
for (db::Layout::const_iterator cell = layout.begin (); cell != layout.end () && ! has_skipped_replica; ++cell) {
has_skipped_replica = cell->can_skip_replica ();
}
}
// with skipped replicas start again and skip child cells of such cells
// unless they are needed in other places.
if (has_skipped_replica) {
for (db::Layout::const_iterator cell = layout.begin (); cell != layout.end (); ++cell) {
if (cell->is_top ()) {
cells.insert (cell->cell_index ());
collect_called_cells_unskipped (cell->cell_index (), layout, cells);
}
}
} else {
for (db::Layout::const_iterator cell = layout.begin (); cell != layout.end () && ! has_skipped_replica; ++cell) {
cells.insert (cell->cell_index ());
}
}
} else {
for (std::set <db::cell_index_type>::const_iterator c = m_cells.begin (); c != m_cells.end (); ++c) {
cells.insert (*c);
if (m_implied_childred.find (*c) != m_implied_childred.end ()) {
layout.cell (*c).collect_called_cells (cells);
if (m_implied_children.find (*c) != m_implied_children.end ()) {
if (has_context) {
collect_called_cells_unskipped (*c, layout, cells);
} else {
layout.cell (*c).collect_called_cells (cells);
}
}
}

View File

@ -449,7 +449,7 @@ private:
std::string m_format;
std::map<unsigned int, db::LayerProperties> m_layers;
std::set<db::cell_index_type> m_cells;
std::set<db::cell_index_type> m_implied_childred;
std::set<db::cell_index_type> m_implied_children;
bool m_all_layers;
bool m_all_cells;
double m_dbu;

View File

@ -109,6 +109,11 @@ public:
*/
virtual bool can_write () const = 0;
/**
* @brief Returns true, when the format supports a context (e.g. PCell parameters, Library references)
*/
virtual bool supports_context () const = 0;
/**
* @brief Delivers the XMLElement object that represents the reader options within a technology XML tree
*

View File

@ -172,15 +172,22 @@ LibraryController::sync_files ()
for (auto lf = lib_files.begin (); lf != lib_files.end (); ++lf) {
tl::log << "Reading lib file '" << *lf << "'";
tl::log << "Reading lib file '" << lf->first << "'";
std::vector<LibFileInfo> libs;
read_lib_file (lf->first, lf->second, libs);
try {
read_libs (libs, new_lib_files);
std::vector<LibFileInfo> libs;
read_lib_file (lf->first, lf->second, libs);
if (m_file_watcher) {
m_file_watcher->add_file (tl::absolute_file_path (lf->first));
read_libs (libs, new_lib_files);
if (m_file_watcher) {
m_file_watcher->add_file (tl::absolute_file_path (lf->first));
}
} catch (tl::Exception &ex) {
tl::error << tl::to_string (tr ("Error reading lib file")) << " " << lf->first << ":" << tl::endl << ex.msg ();
} catch (...) {
}
}
@ -416,7 +423,7 @@ LibraryController::read_libs (const std::vector<LibraryController::LibFileInfo>
auto ll = m_lib_files.find (lib_path);
if (ll == m_lib_files.end ()) {
needs_load = true;
} else if (fi.lastModified () > ll->second.time) {
} else if (fi.lastModified () > ll->second.time || im->tech != ll->second.tech || im->replicate != ll->second.replicate) {
needs_load = true;
}
@ -479,6 +486,7 @@ LibraryController::read_libs (const std::vector<LibraryController::LibFileInfo>
li.name = libname;
li.time = fi.lastModified ();
li.tech = im->tech;
li.replicate = im->replicate;
new_lib_files.insert (std::make_pair (lib_path, li));
lib->set_name (libname);

View File

@ -135,10 +135,11 @@ private slots:
private:
struct LibInfo
{
LibInfo () : name (), time (), tech () { }
LibInfo () : name (), time (), tech (), replicate (true) { }
std::string name;
QDateTime time;
std::set<std::string> tech;
bool replicate;
};
tl::FileSystemWatcher *m_file_watcher;

View File

@ -173,6 +173,11 @@ public:
return true;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<db::CIFReaderOptions> ("cif",

View File

@ -140,6 +140,11 @@ public:
return true;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<db::DXFReaderOptions> ("dxf",

View File

@ -89,6 +89,11 @@ class GDS2TextFormatDeclaration
{
return true;
}
virtual bool supports_context () const
{
return true;
}
};
static tl::RegisteredClass<db::StreamFormatDeclaration> format_txt_decl (new GDS2TextFormatDeclaration(), 1, "GDS2Text");

View File

@ -66,6 +66,11 @@ class GDS2FormatDeclaration
return true;
}
virtual bool supports_context () const
{
return true;
}
virtual tl::XMLElementBase *xml_writer_options_element () const
{
return new db::WriterOptionsXMLElement<db::GDS2WriterOptions> ("gds2",

View File

@ -338,7 +338,7 @@ GDS2WriterBase::write_shape (const db::Layout &layout, int layer, int datatype,
}
void
GDS2WriterBase::write_cell (db::Layout &layout, const db::Cell &cref, const std::vector <std::pair <unsigned int, db::LayerProperties> > &layers, const std::set<db::cell_index_type> &cell_set, double sf, short *time_data)
GDS2WriterBase::write_cell (db::Layout &layout, const db::Cell &cref, const std::vector <std::pair <unsigned int, db::LayerProperties> > &layers, const std::set<db::cell_index_type> &cell_set, double sf, short *time_data, bool skip_body)
{
// cell header
@ -363,51 +363,56 @@ GDS2WriterBase::write_cell (db::Layout &layout, const db::Cell &cref, const std:
}
}
// instances
// skip instances and shapes for replicas
if (! skip_body) {
for (db::Cell::const_iterator inst = cref.begin (); ! inst.at_end (); ++inst) {
// instances
// write only instances to selected cells
if (m_keep_instances || cell_set.find (inst->cell_index ()) != cell_set.end ()) {
for (db::Cell::const_iterator inst = cref.begin (); ! inst.at_end (); ++inst) {
// write only instances to selected cells
if (m_keep_instances || cell_set.find (inst->cell_index ()) != cell_set.end ()) {
progress_checkpoint ();
try {
write_inst (sf, *inst, true /*normalize*/, m_resolve_skew_arrays, layout, inst->prop_id ());
} catch (tl::Exception &ex) {
throw tl::Exception (ex.msg () + tl::to_string (tr (", writing instances")));
}
progress_checkpoint ();
try {
write_inst (sf, *inst, true /*normalize*/, m_resolve_skew_arrays, layout, inst->prop_id ());
} catch (tl::Exception &ex) {
throw tl::Exception (ex.msg () + tl::to_string (tr (", writing instances")));
}
}
}
// shapes
// shapes
for (std::vector <std::pair <unsigned int, db::LayerProperties> >::const_iterator l = layers.begin (); l != layers.end (); ++l) {
for (std::vector <std::pair <unsigned int, db::LayerProperties> >::const_iterator l = layers.begin (); l != layers.end (); ++l) {
if (layout.is_valid_layer (l->first) && l->second.layer >= 0 && l->second.datatype >= 0) {
if (layout.is_valid_layer (l->first) && l->second.layer >= 0 && l->second.datatype >= 0) {
int layer = l->second.layer;
if (layer > std::numeric_limits<uint16_t>::max ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("Cannot write layer numbers larger than %d to GDS2 streams")), int (std::numeric_limits<uint16_t>::max ())));
}
int datatype = l->second.datatype;
if (datatype > std::numeric_limits<uint16_t>::max ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("Cannot write datatype numbers larger than %d to GDS2 streams")), int (std::numeric_limits<uint16_t>::max ())));
}
db::ShapeIterator shape (cref.shapes (l->first).begin (db::ShapeIterator::Boxes | db::ShapeIterator::Polygons | db::ShapeIterator::Edges | db::ShapeIterator::EdgePairs | db::ShapeIterator::Paths | db::ShapeIterator::Texts));
while (! shape.at_end ()) {
progress_checkpoint ();
try {
write_shape (layout, layer, datatype, *shape, sf);
} catch (tl::Exception &ex) {
throw tl::Exception (ex.msg () + tl::sprintf (tl::to_string (tr (", writing layer %d/%d")), layer, datatype));
int layer = l->second.layer;
if (layer > std::numeric_limits<uint16_t>::max ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("Cannot write layer numbers larger than %d to GDS2 streams")), int (std::numeric_limits<uint16_t>::max ())));
}
int datatype = l->second.datatype;
if (datatype > std::numeric_limits<uint16_t>::max ()) {
throw tl::Exception (tl::sprintf (tl::to_string (tr ("Cannot write datatype numbers larger than %d to GDS2 streams")), int (std::numeric_limits<uint16_t>::max ())));
}
++shape;
db::ShapeIterator shape (cref.shapes (l->first).begin (db::ShapeIterator::Boxes | db::ShapeIterator::Polygons | db::ShapeIterator::Edges | db::ShapeIterator::EdgePairs | db::ShapeIterator::Paths | db::ShapeIterator::Texts));
while (! shape.at_end ()) {
progress_checkpoint ();
try {
write_shape (layout, layer, datatype, *shape, sf);
} catch (tl::Exception &ex) {
throw tl::Exception (ex.msg () + tl::sprintf (tl::to_string (tr (", writing layer %d/%d")), layer, datatype));
}
++shape;
}
}
@ -580,7 +585,8 @@ GDS2WriterBase::write (db::Layout &layout, tl::OutputStream &stream, const db::S
if (! cref.is_real_ghost_cell () && (! cref.is_proxy () || ! cref.is_top ())) {
try {
write_cell (layout, cref, layers, cell_set, sf, time_data);
bool skip_body = options.write_context_info () && cref.can_skip_replica ();
write_cell (layout, cref, layers, cell_set, sf, time_data, skip_body);
} catch (tl::Exception &ex) {
throw tl::Exception (ex.msg () + tl::sprintf (tl::to_string (tr (", writing cell '%s'")), layout.cell_name (*cell)));
}

View File

@ -181,7 +181,7 @@ private:
void write_context_cell (db::Layout &layout, const short *time_data, const std::vector<cell_index_type> &cells);
void write_context_string (size_t n, const std::string &s);
void write_cell (db::Layout &layout, const db::Cell &cref, const std::vector <std::pair <unsigned int, db::LayerProperties> > &layers,
const std::set <db::cell_index_type> &cell_set, double sf, short *time_data);
const std::set <db::cell_index_type> &cell_set, double sf, short *time_data, bool skip_body);
void write_shape (const db::Layout &layout, int layer, int datatype, const db::Shape &shape, double sf);
};

View File

@ -310,6 +310,11 @@ class LEFDEFFormatDeclaration
return false;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<LEFDEFReaderOptions> ("lefdef",

View File

@ -105,6 +105,14 @@ class LStreamFormatDeclaration
return true;
}
/**
* @brief Returns a value indicating whether context information is supported
*/
virtual bool supports_context () const
{
return true;
}
virtual tl::XMLElementBase *xml_writer_options_element () const
{
return new db::WriterOptionsXMLElement<lstr::WriterOptions> ("lstream",

View File

@ -150,7 +150,8 @@ Writer::write (db::Layout &layout, tl::OutputStream &stream, const db::SaveLayou
for (auto c = mp_layout->begin_top_down (); c != mp_layout->end_top_down (); ++c) {
if (m_cells_to_write.find (*c) != m_cells_to_write.end ()) {
m_cellname = layout.cell_name (*c);
write_cell (*c, kj_stream);
bool skip_body = options.write_context_info () && mp_layout->cell (*c).can_skip_replica ();
write_cell (*c, kj_stream, skip_body);
m_cellname.clear ();
}
}
@ -911,9 +912,9 @@ Writer::make_meta_data (const db::Cell *cell, stream::metaData::MetaData::Builde
* This method generates a single-view cell message (the view is only a layout view).
*/
void
Writer::write_cell (db::cell_index_type ci, kj::BufferedOutputStream &os)
Writer::write_cell (db::cell_index_type ci, kj::BufferedOutputStream &os, bool skip_body)
{
bool needs_layout_view = ! mp_layout->cell (ci).is_real_ghost_cell ();
bool needs_layout_view = ! mp_layout->cell (ci).is_real_ghost_cell () && ! skip_body;
bool needs_meta_data_view = mp_layout->begin_meta (ci) != mp_layout->end_meta (ci);
capnp::MallocMessageBuilder message;

View File

@ -114,7 +114,7 @@ private:
void make_cell_hierarchy_tree (stream::library::CellHierarchyTree::Builder cell_tree);
void make_meta_data (const db::Cell *cell, stream::metaData::MetaData::Builder meta_data);
void write_cell (db::cell_index_type ci, kj::BufferedOutputStream &os);
void write_cell (db::cell_index_type ci, kj::BufferedOutputStream &os, bool skip_body);
void write_layout_view (db::cell_index_type ci, kj::BufferedOutputStream &os);
void write_meta_data_view (db::cell_index_type ci, kj::BufferedOutputStream &os);
void make_repetition (const RegularArray &array, stream::repetition::Repetition::Builder builder);

View File

@ -81,6 +81,11 @@ public:
return true;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<db::MAGReaderOptions> ("mag",

View File

@ -161,6 +161,11 @@ public:
return false;
}
virtual bool supports_context () const
{
return false;
}
virtual tl::XMLElementBase *xml_reader_options_element () const
{
return new db::ReaderOptionsXMLElement<db::MALYReaderOptions> ("maly",

View File

@ -464,6 +464,11 @@ public:
return true;
}
virtual bool supports_context () const
{
return true;
}
virtual tl::XMLElementBase *xml_writer_options_element () const
{
return new db::WriterOptionsXMLElement<db::OASISWriterOptions> ("oasis",

View File

@ -1727,18 +1727,23 @@ OASISWriter::write (db::Layout &layout, tl::OutputStream &stream, const db::Save
write_props (cref.prop_id ());
}
// instances
if (cref.cell_instances () > 0) {
write_insts (cell_set);
}
bool skip_body = options.write_context_info () && cref.can_skip_replica ();
if (! skip_body) {
// shapes
for (std::vector <std::pair <unsigned int, db::LayerProperties> >::const_iterator l = layers.begin (); l != layers.end (); ++l) {
const db::Shapes &shapes = cref.shapes (l->first);
if (! shapes.empty ()) {
write_shapes (l->second, shapes);
m_progress.set (mp_stream->pos ());
// instances
if (cref.cell_instances () > 0) {
write_insts (cell_set);
}
// shapes
for (std::vector <std::pair <unsigned int, db::LayerProperties> >::const_iterator l = layers.begin (); l != layers.end (); ++l) {
const db::Shapes &shapes = cref.shapes (l->first);
if (! shapes.empty ()) {
write_shapes (l->second, shapes);
m_progress.set (mp_stream->pos ());
}
}
}
// end CBLOCK if required

View File

@ -1206,6 +1206,11 @@ class GerberFormatDeclaration
{
return false;
}
virtual bool supports_context () const
{
return false;
}
};
static tl::RegisteredClass<db::StreamFormatDeclaration> format_decl (new GerberFormatDeclaration (), 1000, "GerberPCB");

View File

@ -4108,8 +4108,6 @@ Eval::parse (Expression &expr, const std::string &s, bool top)
void
Eval::parse (Expression &expr, tl::Extractor &ex, bool top)
{
ex.skip ();
expr = Expression (this, ex.get ());
tl::Extractor ex0 = ex;