Skip to content

Commit 17bf401

Browse files
Merge pull request #3366 from verilog-to-routing/add_capnp_hash
Add Schema ID to RR Graph Formats
2 parents 35a3385 + 623deab commit 17bf401

File tree

13 files changed

+133
-25
lines changed

13 files changed

+133
-25
lines changed

libs/librrgraph/src/io/gen/rr_graph_uxsdcxx.h

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* https://github.com/duck2/uxsdcxx
55
* Modify only if your build process doesn't involve regenerating this file.
66
*
7-
* Cmdline: uxsdcxx/uxsdcxx.py /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8-
* Input file: /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9-
* md5sum of input file: 040903603053940a1b24392c38663b59
7+
* Cmdline: uxsdcxx/uxsdcxx.py /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8+
* Input file: /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9+
* md5sum of input file: e14523c72a5db9cc83592d3baaf45780
1010
*/
1111

1212
#include <functional>
@@ -188,6 +188,8 @@ template <class T, typename Context>
188188
inline void write_rr_graph_xml(T &in, Context &context, std::ostream &os){
189189
in.start_write();
190190
os << "<rr_graph";
191+
if((bool)in.get_rr_graph_schema_file_id(context))
192+
os << " schema_file_id=\"" << in.get_rr_graph_schema_file_id(context) << "\"";
191193
if((bool)in.get_rr_graph_tool_comment(context))
192194
os << " tool_comment=\"" << in.get_rr_graph_tool_comment(context) << "\"";
193195
if((bool)in.get_rr_graph_tool_name(context))
@@ -308,8 +310,8 @@ enum class gtok_t_rr_edges {EDGE};
308310
constexpr const char *gtok_lookup_t_rr_edges[] = {"edge"};
309311
enum class gtok_t_rr_graph {CHANNELS, SWITCHES, SEGMENTS, BLOCK_TYPES, GRID, RR_NODES, RR_EDGES};
310312
constexpr const char *gtok_lookup_t_rr_graph[] = {"channels", "switches", "segments", "block_types", "grid", "rr_nodes", "rr_edges"};
311-
enum class atok_t_rr_graph {TOOL_COMMENT, TOOL_NAME, TOOL_VERSION};
312-
constexpr const char *atok_lookup_t_rr_graph[] = {"tool_comment", "tool_name", "tool_version"};
313+
enum class atok_t_rr_graph {SCHEMA_FILE_ID, TOOL_COMMENT, TOOL_NAME, TOOL_VERSION};
314+
constexpr const char *atok_lookup_t_rr_graph[] = {"schema_file_id", "tool_comment", "tool_name", "tool_version"};
313315

314316

315317
/* Internal lexers. These convert the PugiXML node names to input tokens. */
@@ -1607,6 +1609,29 @@ inline atok_t_rr_graph lex_attr_t_rr_graph(const char *in, const std::function<v
16071609
default: break;
16081610
}
16091611
break;
1612+
case 14:
1613+
switch(*((triehash_uu64*)&in[0])){
1614+
case onechar('s', 0, 64) | onechar('c', 8, 64) | onechar('h', 16, 64) | onechar('e', 24, 64) | onechar('m', 32, 64) | onechar('a', 40, 64) | onechar('_', 48, 64) | onechar('f', 56, 64):
1615+
switch(*((triehash_uu32*)&in[8])){
1616+
case onechar('i', 0, 32) | onechar('l', 8, 32) | onechar('e', 16, 32) | onechar('_', 24, 32):
1617+
switch(in[12]){
1618+
case onechar('i', 0, 8):
1619+
switch(in[13]){
1620+
case onechar('d', 0, 8):
1621+
return atok_t_rr_graph::SCHEMA_FILE_ID;
1622+
break;
1623+
default: break;
1624+
}
1625+
break;
1626+
default: break;
1627+
}
1628+
break;
1629+
default: break;
1630+
}
1631+
break;
1632+
default: break;
1633+
}
1634+
break;
16101635
default: break;
16111636
}
16121637
noreturn_report(report_error, ("Found unrecognized attribute " + std::string(in) + " of <rr_graph>.").c_str());
@@ -2251,6 +2276,14 @@ inline enum_loc_side lex_enum_loc_side(const char *in, bool throw_on_invalid, co
22512276

22522277

22532278
/* Internal loading functions, which validate and load a PugiXML DOM tree into memory. */
2279+
inline unsigned long load_unsigned_long(const char *in, const std::function<void(const char *)> * report_error){
2280+
unsigned long out;
2281+
out = std::strtoull(in, NULL, 10);
2282+
if(errno != 0)
2283+
noreturn_report(report_error, ("Invalid value `" + std::string(in) + "` when loading into a unsigned long.").c_str());
2284+
return out;
2285+
}
2286+
22542287
inline int load_int(const char *in, const std::function<void(const char *)> * report_error){
22552288
int out;
22562289
out = std::strtol(in, NULL, 10);
@@ -3879,6 +3912,9 @@ inline void load_rr_graph(const pugi::xml_node &root, T &out, Context &context,
38793912
for(pugi::xml_attribute attr = root.first_attribute(); attr; attr = attr.next_attribute()){
38803913
atok_t_rr_graph in = lex_attr_t_rr_graph(attr.name(), report_error);
38813914
switch(in){
3915+
case atok_t_rr_graph::SCHEMA_FILE_ID:
3916+
out.set_rr_graph_schema_file_id(load_unsigned_long(attr.value(), report_error), context);
3917+
break;
38823918
case atok_t_rr_graph::TOOL_COMMENT:
38833919
out.set_rr_graph_tool_comment(attr.value(), context);
38843920
break;

libs/librrgraph/src/io/gen/rr_graph_uxsdcxx_capnp.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* https://github.com/duck2/uxsdcxx
55
* Modify only if your build process doesn't involve regenerating this file.
66
*
7-
* Cmdline: uxsdcxx/uxsdcap.py /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8-
* Input file: /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9-
* md5sum of input file: 040903603053940a1b24392c38663b59
7+
* Cmdline: uxsdcxx/uxsdcap.py /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8+
* Input file: /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9+
* md5sum of input file: e14523c72a5db9cc83592d3baaf45780
1010
*/
1111

1212
#include <functional>
@@ -434,6 +434,8 @@ inline void load_rr_graph_capnp(T &out, kj::ArrayPtr<const ::capnp::word> data,
434434
template <class T, typename Context>
435435
inline void write_rr_graph_capnp(T &in, Context &context, ucap::RrGraph::Builder &root) {
436436
in.start_write();
437+
if((bool)in.get_rr_graph_schema_file_id(context))
438+
root.setSchemaFileId(in.get_rr_graph_schema_file_id(context));
437439
if((bool)in.get_rr_graph_tool_comment(context))
438440
root.setToolComment(in.get_rr_graph_tool_comment(context));
439441
if((bool)in.get_rr_graph_tool_name(context))
@@ -946,6 +948,7 @@ inline void load_rr_graph_capnp_type(const ucap::RrGraph::Reader &root, T &out,
946948
(void)report_error;
947949
(void)stack;
948950

951+
out.set_rr_graph_schema_file_id(root.getSchemaFileId(), context);
949952
out.set_rr_graph_tool_comment(root.getToolComment().cStr(), context);
950953
out.set_rr_graph_tool_name(root.getToolName().cStr(), context);
951954
out.set_rr_graph_tool_version(root.getToolVersion().cStr(), context);

libs/librrgraph/src/io/gen/rr_graph_uxsdcxx_interface.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
* https://github.com/duck2/uxsdcxx
55
* Modify only if your build process doesn't involve regenerating this file.
66
*
7-
* Cmdline: uxsdcxx/uxsdcxx.py /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8-
* Input file: /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9-
* md5sum of input file: 040903603053940a1b24392c38663b59
7+
* Cmdline: uxsdcxx/uxsdcxx.py /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
8+
* Input file: /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
9+
* md5sum of input file: e14523c72a5db9cc83592d3baaf45780
1010
*/
1111

1212
#include <functional>
@@ -536,8 +536,11 @@ class RrGraphBase {
536536
* <xs:attribute name="tool_name" type="xs:string" />
537537
* <xs:attribute name="tool_version" type="xs:string" />
538538
* <xs:attribute name="tool_comment" type="xs:string" />
539+
* <xs:attribute name="schema_file_id" type="xs:unsignedLong" />
539540
* </xs:complexType>
540541
*/
542+
virtual inline unsigned long get_rr_graph_schema_file_id(typename ContextTypes::RrGraphReadContext &ctx) = 0;
543+
virtual inline void set_rr_graph_schema_file_id(unsigned long schema_file_id, typename ContextTypes::RrGraphWriteContext &ctx) = 0;
541544
virtual inline const char * get_rr_graph_tool_comment(typename ContextTypes::RrGraphReadContext &ctx) = 0;
542545
virtual inline void set_rr_graph_tool_comment(const char * tool_comment, typename ContextTypes::RrGraphWriteContext &ctx) = 0;
543546
virtual inline const char * get_rr_graph_tool_name(typename ContextTypes::RrGraphReadContext &ctx) = 0;

libs/librrgraph/src/io/rr_graph.xsd

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@
375375
<xs:attribute name="tool_name" type="xs:string"/>
376376
<xs:attribute name="tool_version" type="xs:string"/>
377377
<xs:attribute name="tool_comment" type="xs:string"/>
378+
379+
<xs:attribute name="schema_file_id" type="xs:unsignedLong"/>
378380
</xs:complexType>
379381
</xs:element>
380382

libs/librrgraph/src/io/rr_graph_reader.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "pugixml_util.hpp"
2727

2828
#ifdef VTR_ENABLE_CAPNPROTO
29+
# include <capnp/schema.h>
2930
# include "rr_graph_uxsdcxx_capnp.h"
3031
# include "mmap_file.h"
3132
#endif
@@ -72,6 +73,7 @@ void load_rr_file(RRGraphBuilder* rr_graph_builder,
7273
bool do_check_rr_graph,
7374
bool echo_enabled,
7475
const char* echo_file_name,
76+
const int route_verbosity,
7577
bool is_flat) {
7678
vtr::ScopedStartFinishTimer timer("Loading routing resource graph");
7779

@@ -85,6 +87,20 @@ void load_rr_file(RRGraphBuilder* rr_graph_builder,
8587
rr_graph_builder->set_tileable(true);
8688
}
8789

90+
// If Cap'n Proto is enabled, a unique ID is assigned to the schema used to serialize the RR graph.
91+
// This ID is used to verify that the schema used to serialize the RR graph matches the
92+
// schema being used to deserialize it.
93+
// If Cap'n Proto is not enabled, the schema ID is 0 and no schema ID check is performed.
94+
unsigned long schema_file_id = 0;
95+
#ifdef VTR_ENABLE_CAPNPROTO
96+
::capnp::Schema schema = ::capnp::Schema::from<ucap::RrGraph>();
97+
schema_file_id = schema.getProto().getScopeId();
98+
VTR_LOGV(route_verbosity > 1, "Schema file ID: 0x%016lx\n", schema_file_id);
99+
#else
100+
// Suppress the warning about unused variable 'route_verbosity'
101+
(void)route_verbosity;
102+
#endif
103+
88104
RrGraphSerializer reader(
89105
graph_type,
90106
base_cost_type,
@@ -108,6 +124,7 @@ void load_rr_file(RRGraphBuilder* rr_graph_builder,
108124
&rr_graph_builder->rr_node_metadata(),
109125
&rr_graph_builder->rr_edge_metadata(),
110126
&arch->strings,
127+
schema_file_id,
111128
is_flat);
112129

113130
if (vtr::check_file_name_extension(read_rr_graph_name, ".xml")) {

libs/librrgraph/src/io/rr_graph_reader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ void load_rr_file(RRGraphBuilder* rr_graph_builder,
2929
bool do_check_rr_graph,
3030
bool echo_enabled,
3131
const char* echo_file_name,
32+
const int route_verbosity,
3233
bool is_flat);
3334

3435
/**

libs/librrgraph/src/io/rr_graph_uxsdcxx_serializer.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,7 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
294294
MetadataStorage<int>* rr_node_metadata,
295295
MetadataStorage<std::tuple<int, int, short>>* rr_edge_metadata,
296296
vtr::string_internment* strings,
297+
unsigned long schema_file_id,
297298
bool is_flat)
298299
: chan_width_(chan_width)
299300
, rr_nodes_(rr_nodes)
@@ -319,6 +320,7 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
319320
, strings_(strings)
320321
, empty_(strings_->intern_string(""))
321322
, report_error_(nullptr)
323+
, schema_file_id_(schema_file_id)
322324
, is_flat_(is_flat) {
323325
// Initialize internal data
324326
init_side_map();
@@ -1727,6 +1729,20 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
17271729
* <xs:attribute name="tool_comment" type="xs:string" />
17281730
* </xs:complexType>
17291731
*/
1732+
inline void set_rr_graph_schema_file_id(unsigned long schema_file_id, void*& /*ctx*/) final {
1733+
// Only check if schema_file_id_ (set when initializing the class) is not 0.
1734+
// If it is 0, it means Cap'n Proto is not enabled, so we cannot check for a schema file ID mismatch.
1735+
// This function is only called when the RR graph file being read contains a schema file ID.
1736+
// If it does not, this function is not called, and the RR graph can be read without performing
1737+
// the schema file ID check.
1738+
if (schema_file_id_ != 0) {
1739+
if (schema_file_id != schema_file_id_) {
1740+
report_error(
1741+
"Schema file ID mismatch: Expected ID 0x%016lx, but got ID 0x%016lx",
1742+
schema_file_id_, schema_file_id);
1743+
}
1744+
}
1745+
}
17301746
inline void set_rr_graph_tool_comment(const char* tool_comment, void*& /*ctx*/) final {
17311747
std::string correct_string = "Generated from arch file ";
17321748
correct_string += get_arch_file_name();
@@ -1748,6 +1764,10 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
17481764
}
17491765
}
17501766

1767+
inline unsigned long get_rr_graph_schema_file_id(void*& /*ctx*/) final {
1768+
return schema_file_id_;
1769+
}
1770+
17511771
inline const char* get_rr_graph_tool_comment(void*& /*ctx*/) final {
17521772
temp_string_.assign("Generated from arch file ");
17531773
temp_string_ += get_arch_file_name();
@@ -2181,6 +2201,7 @@ class RrGraphSerializer final : public uxsd::RrGraphBase<RrGraphContextTypes> {
21812201
vtr::string_internment* strings_;
21822202
vtr::interned_string empty_;
21832203
const std::function<void(const char*)>* report_error_;
2204+
unsigned long schema_file_id_;
21842205
bool is_flat_;
21852206

21862207
// Temporary data to check grid block types

libs/librrgraph/src/io/rr_graph_writer.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "rr_graph_uxsdcxx_serializer.h"
1414
#include "rr_graph_uxsdcxx.h"
1515
#ifdef VTR_ENABLE_CAPNPROTO
16+
# include <capnp/schema.h>
1617
# include "serdes_utils.h"
1718
# include "rr_graph_uxsdcxx_capnp.h"
1819
#endif
@@ -37,8 +38,23 @@ void write_rr_graph(RRGraphBuilder* rr_graph_builder,
3738
const char* file_name,
3839
bool echo_enabled,
3940
const char* echo_file_name,
41+
const int route_verbosity,
4042
bool is_flat) {
4143

44+
// If Cap'n Proto is enabled, a unique ID is assigned to the schema used to serialize the RR graph.
45+
// This ID is used to verify that the schema used to serialize the RR graph matches the
46+
// schema being used to deserialize it.
47+
// If Cap'n Proto is not enabled, the schema ID is 0 and no schema ID check is performed.
48+
unsigned long schema_file_id = 0;
49+
#ifdef VTR_ENABLE_CAPNPROTO
50+
::capnp::Schema schema = ::capnp::Schema::from<ucap::RrGraph>();
51+
schema_file_id = schema.getProto().getScopeId();
52+
VTR_LOGV(route_verbosity > 1, "Schema file ID: 0x%016lx\n", schema_file_id);
53+
#else
54+
// Suppress the warning about unused variable 'route_verbosity'
55+
(void)route_verbosity;
56+
#endif
57+
4258
RrGraphSerializer reader(
4359
/*graph_type=*/e_graph_type(),
4460
/*base_cost_type=*/e_base_cost_type(),
@@ -62,6 +78,7 @@ void write_rr_graph(RRGraphBuilder* rr_graph_builder,
6278
&rr_graph_builder->rr_node_metadata(),
6379
&rr_graph_builder->rr_edge_metadata(),
6480
&arch->strings,
81+
schema_file_id,
6582
is_flat);
6683

6784
if (vtr::check_file_name_extension(file_name, ".xml")) {

libs/librrgraph/src/io/rr_graph_writer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@ void write_rr_graph(RRGraphBuilder* rr_graph_builder,
2323
const char* file_name,
2424
bool echo_enabled,
2525
const char* echo_file_name,
26+
const int route_verbosity,
2627
bool is_flat);

libs/libvtrcapnproto/gen/rr_graph_uxsdcxx.capnp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# https://github.com/duck2/uxsdcxx
33
# Modify only if your build process doesn't involve regenerating this file.
44
#
5-
# Cmdline: uxsdcxx/uxsdcap.py /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
6-
# Input file: /home/soheil/vtr/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
7-
# md5sum of input file: 040903603053940a1b24392c38663b59
5+
# Cmdline: uxsdcxx/uxsdcap.py /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
6+
# Input file: /dsoft/amohaghegh/vtr-verilog-to-routing/libs/librrgraph/src/io/rr_graph.xsd
7+
# md5sum of input file: e14523c72a5db9cc83592d3baaf45780
88

9-
@0xe7650575a8718aa2;
9+
@0xef4b0a4204785218;
1010
using Cxx = import "/capnp/c++.capnp";
1111
$Cxx.namespace("ucap");
1212

@@ -235,14 +235,15 @@ struct RrEdges {
235235
}
236236

237237
struct RrGraph {
238-
toolComment @0 :Text;
239-
toolName @1 :Text;
240-
toolVersion @2 :Text;
241-
channels @3 :Channels;
242-
switches @4 :Switches;
243-
segments @5 :Segments;
244-
blockTypes @6 :BlockTypes;
245-
grid @7 :GridLocs;
246-
rrNodes @8 :RrNodes;
247-
rrEdges @9 :RrEdges;
238+
schemaFileId @0 :UInt64;
239+
toolComment @1 :Text;
240+
toolName @2 :Text;
241+
toolVersion @3 :Text;
242+
channels @4 :Channels;
243+
switches @5 :Switches;
244+
segments @6 :Segments;
245+
blockTypes @7 :BlockTypes;
246+
grid @8 :GridLocs;
247+
rrNodes @9 :RrNodes;
248+
rrEdges @10 :RrEdges;
248249
}

0 commit comments

Comments
 (0)