Skip to content

Commit 03e5f72

Browse files
Merge pull request The-OpenROAD-Project#7960 from osamahammad21/odb-gen-objtype
ODB: Hash dbObjectType and use that hash in serialization and deserialization.
2 parents 9a96c6b + 6bf131c commit 03e5f72

File tree

6 files changed

+285
-12
lines changed

6 files changed

+285
-12
lines changed

src/odb/include/odb/dbObject.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,7 @@ class dbObject
169169
~dbObject() = default;
170170
};
171171

172+
dbIStream& operator>>(dbIStream& stream, dbObjectType& type);
173+
dbOStream& operator<<(dbOStream& stream, dbObjectType type);
174+
172175
} // namespace odb

src/odb/include/odb/dbStream.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ class dbOStream
141141
return *this;
142142
}
143143

144-
dbOStream& operator<<(dbObjectType c)
145-
{
146-
writeValueAsBytes(c);
147-
return *this;
148-
}
149-
150144
template <class T1, class T2>
151145
dbOStream& operator<<(const std::pair<T1, T2>& p)
152146
{
@@ -375,12 +369,6 @@ class dbIStream
375369
return *this;
376370
}
377371

378-
dbIStream& operator>>(dbObjectType& c)
379-
{
380-
_f.read(reinterpret_cast<char*>(&c), sizeof(c));
381-
return *this;
382-
}
383-
384372
template <class T1, class T2>
385373
dbIStream& operator>>(std::pair<T1, T2>& p)
386374
{

src/odb/src/codeGenerator/gen.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
is_set_by_ref,
2828
is_ref,
2929
std,
30+
fnv1a_32,
3031
)
3132

3233
# map types to their header if it isn't equal to their name
@@ -291,6 +292,7 @@ def generate(schema, env, includeDir, srcDir, keep_empty):
291292
generate_relations(schema)
292293

293294
to_be_merged = []
295+
hash_dict = {}
294296
for klass in schema["classes"]:
295297
# Adding functional name to fields and extracting field components
296298
flags_struct = {
@@ -311,6 +313,19 @@ def generate(schema, env, includeDir, srcDir, keep_empty):
311313
if "odb/db.h" not in klass["h_includes"]:
312314
klass["h_includes"].append("odb/db.h")
313315
break
316+
# Add hash to class
317+
if "hash" not in klass:
318+
hash_value = fnv1a_32(klass["name"])
319+
else:
320+
hash_value = int(klass["hash"], 16)
321+
322+
if hash_value in hash_dict:
323+
# Collision detected, error out
324+
raise ValueError(
325+
f"Collision detected for {klass['name']} with {hash_dict[hash_value]}"
326+
)
327+
hash_dict[hash_value] = klass["name"]
328+
klass["hash"] = f"0x{hash_value:08X}"
314329

315330
# Generating files
316331
for template_file in ["impl.h", "impl.cpp"]:

src/odb/src/codeGenerator/helper.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,3 +177,13 @@ def get_ref_type(type_name):
177177
return None
178178

179179
return type_name[6:-1] + "*"
180+
181+
182+
def fnv1a_32(string):
183+
FNV_prime = 0x01000193
184+
hash_value = 0x811C9DC5
185+
for c in string:
186+
hash_value ^= ord(c)
187+
hash_value = (hash_value * FNV_prime) % (1 << 32)
188+
189+
return hash_value

src/odb/src/codeGenerator/templates/dbObject.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,15 @@
33
"{{klass.name}}",
44
{% endfor %}
55
//Generator Code End ObjectNames
6+
7+
//Generator Code Begin HashToObjectType
8+
{% for klass in schema.classes|sort(attribute='name') %}
9+
{ {{klass.hash}}, {{klass.name}}Obj},
10+
{% endfor %}
11+
//Generator Code End HashToObjectType
12+
13+
//Generator Code Begin ObjectTypeToHash
14+
{% for klass in schema.classes|sort(attribute='name') %}
15+
{ {{klass.name}}Obj, {{klass.hash}} },
16+
{% endfor %}
17+
//Generator Code End ObjectTypeToHash

src/odb/src/db/dbObject.cpp

Lines changed: 245 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Copyright (c) 2019-2025, The OpenROAD Authors
33

44
#include <cstring>
5+
#include <unordered_map>
56

67
#include "dbCore.h"
78
#include "dbDatabase.h"
@@ -139,6 +140,236 @@ static const char* name_tbl[] = {"dbDatabase",
139140
"dbProperty",
140141
"dbName"};
141142

143+
static const std::unordered_map<uint32_t, dbObjectType> hash_to_object_type
144+
= {{0x0, dbDatabaseObj},
145+
146+
// Design Objects
147+
{0x1, dbChipObj},
148+
{0x2, dbGdsLibObj},
149+
{0x3, dbBlockObj},
150+
{0x4, dbInstHdrObj},
151+
{0x5, dbInstObj},
152+
{0x6, dbNetObj},
153+
{0x7, dbBTermObj},
154+
{0x8, dbITermObj},
155+
{0x9, dbBoxObj},
156+
{0xA, dbViaObj},
157+
{0xB, dbTrackGridObj},
158+
{0xC, dbObstructionObj},
159+
{0xD, dbBlockageObj},
160+
{0xE, dbWireObj},
161+
{0xF, dbSWireObj},
162+
{0x10, dbSBoxObj},
163+
{0x11, dbCapNodeObj},
164+
{0x12, dbRSegObj},
165+
{0x13, dbCCSegObj},
166+
{0x14, dbRowObj},
167+
{0x15, dbFillObj},
168+
{0x16, dbRegionObj},
169+
{0x17, dbHierObj},
170+
{0x18, dbBPinObj},
171+
// Generator Code Begin HashToObjectType
172+
{0x663302D5, dbAccessPointObj},
173+
{0x12B22B2C, dbBusPortObj},
174+
{0xEE4BAB67, dbCellEdgeSpacingObj},
175+
{0x7C713BD7, dbDftObj},
176+
{0x645E6090, dbGCellGridObj},
177+
{0xCC5EB4AD, dbGDSARefObj},
178+
{0xE4D136A3, dbGDSBoundaryObj},
179+
{0x2A7E2FFE, dbGDSBoxObj},
180+
{0xE38B6A0A, dbGDSPathObj},
181+
{0x85F207E7, dbGDSSRefObj},
182+
{0x99297302, dbGDSStructureObj},
183+
{0x90DFB402, dbGDSTextObj},
184+
{0xA0D52ACE, dbGlobalConnectObj},
185+
{0x7A16F67A, dbGroupObj},
186+
{0xE76AA1E9, dbGuideObj},
187+
{0xB6738B19, dbIsolationObj},
188+
{0x861F1D0A, dbLevelShifterObj},
189+
{0x2B46CE60, dbLogicPortObj},
190+
{0xEB83E7A1, dbMarkerObj},
191+
{0x4D3FFF1D, dbMarkerCategoryObj},
192+
{0x05DBC7F0, dbMasterEdgeTypeObj},
193+
{0x0FD3C974, dbMetalWidthViaMapObj},
194+
{0x7EBE8F33, dbModBTermObj},
195+
{0xAFA31C35, dbModInstObj},
196+
{0xFAC1E1FA, dbModITermObj},
197+
{0xDFD34354, dbModNetObj},
198+
{0x9F568C6F, dbModuleObj},
199+
{0x6D3EE435, dbNetTrackObj},
200+
{0x110F3173, dbPolygonObj},
201+
{0x7352FE40, dbPowerDomainObj},
202+
{0x7D280800, dbPowerSwitchObj},
203+
{0xBF9698BF, dbScanChainObj},
204+
{0x05CD9D4A, dbScanInstObj},
205+
{0xD398101E, dbScanListObj},
206+
{0x534639A0, dbScanPartitionObj},
207+
{0x2BA03C39, dbScanPinObj},
208+
{0x4ED3B51C, dbTechLayerObj},
209+
{0x2C0432EB, dbTechLayerAreaRuleObj},
210+
{0x1904C344, dbTechLayerArraySpacingRuleObj},
211+
{0x598158F4, dbTechLayerCornerSpacingRuleObj},
212+
{0xE1F72282, dbTechLayerCutClassRuleObj},
213+
{0xFFC5F736, dbTechLayerCutEnclosureRuleObj},
214+
{0xB78E788B, dbTechLayerCutSpacingRuleObj},
215+
{0x79DFAE70, dbTechLayerCutSpacingTableDefRuleObj},
216+
{0xA5373824, dbTechLayerCutSpacingTableOrthRuleObj},
217+
{0x49B1981B, dbTechLayerEolExtensionRuleObj},
218+
{0x88B0E445, dbTechLayerEolKeepOutRuleObj},
219+
{0x139CF020, dbTechLayerForbiddenSpacingRuleObj},
220+
{0x0AD77253, dbTechLayerKeepOutZoneRuleObj},
221+
{0xA65D53B9, dbTechLayerMaxSpacingRuleObj},
222+
{0x4BA55CDE, dbTechLayerMinCutRuleObj},
223+
{0x6BE58714, dbTechLayerMinStepRuleObj},
224+
{0x49E7A7BD, dbTechLayerSpacingEolRuleObj},
225+
{0xA223F41D, dbTechLayerSpacingTablePrlRuleObj},
226+
{0x7C5FB405, dbTechLayerTwoWiresForbiddenSpcRuleObj},
227+
{0x7BF3D392, dbTechLayerWidthTableRuleObj},
228+
{0xF73FA7DF, dbTechLayerWrongDirSpacingRuleObj},
229+
// Generator Code End HashToObjectType
230+
231+
// Lib Objects
232+
{0x52, dbLibObj},
233+
{0x53, dbGDSLibObj},
234+
{0x54, dbSiteObj},
235+
{0x55, dbMasterObj},
236+
{0x56, dbMPinObj},
237+
{0x57, dbMTermObj},
238+
{0x58, dbTechAntennaPinModelObj},
239+
240+
// Tech Objects
241+
{0x59, dbTechObj},
242+
{0x5A, dbTechViaObj},
243+
{0x5B, dbTechNonDefaultRuleObj},
244+
{0x5C, dbTechLayerRuleObj},
245+
{0x5D, dbTechSameNetRuleObj},
246+
{0x5E, dbTechLayerSpacingRuleObj},
247+
{0x5F, dbTechMinCutRuleObj},
248+
{0x60, dbTechMinEncRuleObj},
249+
{0x61, dbTechV55InfluenceEntryObj},
250+
{0x62, dbTechLayerAntennaRuleObj},
251+
{0x63, dbTechViaRuleObj},
252+
{0x64, dbTechViaGenerateRuleObj},
253+
{0x65, dbTechViaLayerRuleObj},
254+
255+
{0x66, dbPropertyObj},
256+
{0x67, dbNameObj}};
257+
258+
static const std::unordered_map<dbObjectType, uint32_t> object_type_to_hash
259+
= {{dbDatabaseObj, 0x0},
260+
261+
// Design Objects
262+
{dbChipObj, 0x1},
263+
{dbGdsLibObj, 0x2},
264+
{dbBlockObj, 0x3},
265+
{dbInstHdrObj, 0x4},
266+
{dbInstObj, 0x5},
267+
{dbNetObj, 0x6},
268+
{dbBTermObj, 0x7},
269+
{dbITermObj, 0x8},
270+
{dbBoxObj, 0x9},
271+
{dbViaObj, 0xA},
272+
{dbTrackGridObj, 0xB},
273+
{dbObstructionObj, 0xC},
274+
{dbBlockageObj, 0xD},
275+
{dbWireObj, 0xE},
276+
{dbSWireObj, 0xF},
277+
{dbSBoxObj, 0x10},
278+
{dbCapNodeObj, 0x11},
279+
{dbRSegObj, 0x12},
280+
{dbCCSegObj, 0x13},
281+
{dbRowObj, 0x14},
282+
{dbFillObj, 0x15},
283+
{dbRegionObj, 0x16},
284+
{dbHierObj, 0x17},
285+
{dbBPinObj, 0x18},
286+
// Generator Code Begin ObjectTypeToHash
287+
{dbAccessPointObj, 0x663302D5},
288+
{dbBusPortObj, 0x12B22B2C},
289+
{dbCellEdgeSpacingObj, 0xEE4BAB67},
290+
{dbDftObj, 0x7C713BD7},
291+
{dbGCellGridObj, 0x645E6090},
292+
{dbGDSARefObj, 0xCC5EB4AD},
293+
{dbGDSBoundaryObj, 0xE4D136A3},
294+
{dbGDSBoxObj, 0x2A7E2FFE},
295+
{dbGDSPathObj, 0xE38B6A0A},
296+
{dbGDSSRefObj, 0x85F207E7},
297+
{dbGDSStructureObj, 0x99297302},
298+
{dbGDSTextObj, 0x90DFB402},
299+
{dbGlobalConnectObj, 0xA0D52ACE},
300+
{dbGroupObj, 0x7A16F67A},
301+
{dbGuideObj, 0xE76AA1E9},
302+
{dbIsolationObj, 0xB6738B19},
303+
{dbLevelShifterObj, 0x861F1D0A},
304+
{dbLogicPortObj, 0x2B46CE60},
305+
{dbMarkerObj, 0xEB83E7A1},
306+
{dbMarkerCategoryObj, 0x4D3FFF1D},
307+
{dbMasterEdgeTypeObj, 0x05DBC7F0},
308+
{dbMetalWidthViaMapObj, 0x0FD3C974},
309+
{dbModBTermObj, 0x7EBE8F33},
310+
{dbModInstObj, 0xAFA31C35},
311+
{dbModITermObj, 0xFAC1E1FA},
312+
{dbModNetObj, 0xDFD34354},
313+
{dbModuleObj, 0x9F568C6F},
314+
{dbNetTrackObj, 0x6D3EE435},
315+
{dbPolygonObj, 0x110F3173},
316+
{dbPowerDomainObj, 0x7352FE40},
317+
{dbPowerSwitchObj, 0x7D280800},
318+
{dbScanChainObj, 0xBF9698BF},
319+
{dbScanInstObj, 0x05CD9D4A},
320+
{dbScanListObj, 0xD398101E},
321+
{dbScanPartitionObj, 0x534639A0},
322+
{dbScanPinObj, 0x2BA03C39},
323+
{dbTechLayerObj, 0x4ED3B51C},
324+
{dbTechLayerAreaRuleObj, 0x2C0432EB},
325+
{dbTechLayerArraySpacingRuleObj, 0x1904C344},
326+
{dbTechLayerCornerSpacingRuleObj, 0x598158F4},
327+
{dbTechLayerCutClassRuleObj, 0xE1F72282},
328+
{dbTechLayerCutEnclosureRuleObj, 0xFFC5F736},
329+
{dbTechLayerCutSpacingRuleObj, 0xB78E788B},
330+
{dbTechLayerCutSpacingTableDefRuleObj, 0x79DFAE70},
331+
{dbTechLayerCutSpacingTableOrthRuleObj, 0xA5373824},
332+
{dbTechLayerEolExtensionRuleObj, 0x49B1981B},
333+
{dbTechLayerEolKeepOutRuleObj, 0x88B0E445},
334+
{dbTechLayerForbiddenSpacingRuleObj, 0x139CF020},
335+
{dbTechLayerKeepOutZoneRuleObj, 0x0AD77253},
336+
{dbTechLayerMaxSpacingRuleObj, 0xA65D53B9},
337+
{dbTechLayerMinCutRuleObj, 0x4BA55CDE},
338+
{dbTechLayerMinStepRuleObj, 0x6BE58714},
339+
{dbTechLayerSpacingEolRuleObj, 0x49E7A7BD},
340+
{dbTechLayerSpacingTablePrlRuleObj, 0xA223F41D},
341+
{dbTechLayerTwoWiresForbiddenSpcRuleObj, 0x7C5FB405},
342+
{dbTechLayerWidthTableRuleObj, 0x7BF3D392},
343+
{dbTechLayerWrongDirSpacingRuleObj, 0xF73FA7DF},
344+
// Generator Code End ObjectTypeToHash
345+
346+
// Lib Objects
347+
{dbLibObj, 0x52},
348+
{dbGDSLibObj, 0x53},
349+
{dbSiteObj, 0x54},
350+
{dbMasterObj, 0x55},
351+
{dbMPinObj, 0x56},
352+
{dbMTermObj, 0x57},
353+
{dbTechAntennaPinModelObj, 0x58},
354+
355+
// Tech Objects
356+
{dbTechObj, 0x59},
357+
{dbTechViaObj, 0x5A},
358+
{dbTechNonDefaultRuleObj, 0x5B},
359+
{dbTechLayerRuleObj, 0x5C},
360+
{dbTechSameNetRuleObj, 0x5D},
361+
{dbTechLayerSpacingRuleObj, 0x5E},
362+
{dbTechMinCutRuleObj, 0x5F},
363+
{dbTechMinEncRuleObj, 0x60},
364+
{dbTechV55InfluenceEntryObj, 0x61},
365+
{dbTechLayerAntennaRuleObj, 0x62},
366+
{dbTechViaRuleObj, 0x63},
367+
{dbTechViaGenerateRuleObj, 0x64},
368+
{dbTechViaLayerRuleObj, 0x65},
369+
370+
{dbPropertyObj, 0x66},
371+
{dbNameObj, 0x67}};
372+
142373
const char* dbObject::getTypeName() const
143374
{
144375
return name_tbl[getImpl()->getType()];
@@ -182,4 +413,18 @@ bool compare_by_id(const dbObject* lhs, const dbObject* rhs)
182413
return compare_by_id(lhs_owner, rhs_owner);
183414
}
184415

416+
dbIStream& operator>>(dbIStream& stream, dbObjectType& type)
417+
{
418+
uint32_t hash;
419+
stream >> hash;
420+
type = hash_to_object_type.at(hash);
421+
return stream;
422+
}
423+
424+
dbOStream& operator<<(dbOStream& stream, dbObjectType type)
425+
{
426+
stream << object_type_to_hash.at(type);
427+
return stream;
428+
}
429+
185430
} // namespace odb

0 commit comments

Comments
 (0)