From 39c2d7aa60a35b1bd270ca44a1794092a7f5daf4 Mon Sep 17 00:00:00 2001 From: Alain Dargelas Date: Wed, 6 Nov 2024 15:48:24 -0800 Subject: [PATCH 1/3] RTLIL Module dump and hash --- kernel/rtlil.cc | 26 ++++++++++++++++++++++++++ kernel/rtlil.h | 3 +++ 2 files changed, 29 insertions(+) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 503640f40..b02511e40 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -27,6 +27,10 @@ #include "backends/rtlil/rtlil_backend.h" #include +#include +#include +#include +#include #include #include @@ -2508,6 +2512,28 @@ void RTLIL::Module::swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2) wires_[w2->name] = w2; } +// Returns the RTLIL dump of a module +std::string RTLIL::Module::rtlil_dump() { + std::stringstream stream; + // Sorting the module to have a canonical RTLIL + sort(); + // Dumping the RTLIL in an in-memory stringstream + RTLIL_BACKEND::dump_module(stream, " ", this, design, false, true, false); + std::string origstring = stream.str(); + //RTLIL contains non utf-8 characters, converting to utf-8 + std::wstring_convert, char32_t> converter; + std::u32string orig(origstring.begin(), origstring.end()); + std::string utf8String = converter.to_bytes(orig); + return utf8String; +} + +// Returns a hash of the RTLIL dump +std::string RTLIL::Module::rtlil_hash() { + std::hash hasher; + size_t hash = hasher(rtlil_dump()); + return std::to_string(hash); +} + void RTLIL::Module::swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2) { log_assert(cells_[c1->name] == c1); diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 6a8fab875..bd9d31879 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -1591,6 +1591,9 @@ public: RTLIL::SigSpec OriginalTag (RTLIL::IdString name, const std::string &tag, const RTLIL::SigSpec &sig_a, const std::string &src = ""); RTLIL::SigSpec FutureFF (RTLIL::IdString name, const RTLIL::SigSpec &sig_e, const std::string &src = ""); + std::string rtlil_dump(); + std::string rtlil_hash(); + #ifdef WITH_PYTHON static std::map *get_all_modules(void); #endif From 3ad51e06bcaa03d10ac9433197565ea82df415cd Mon Sep 17 00:00:00 2001 From: Alain Dargelas Date: Wed, 6 Nov 2024 16:32:18 -0800 Subject: [PATCH 2/3] Using Yosys hash --- kernel/rtlil.cc | 18 ++++-------------- 1 file changed, 4 insertions(+), 14 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index b02511e40..bfb177e9b 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -28,9 +28,6 @@ #include #include -#include -#include -#include #include #include @@ -2514,24 +2511,17 @@ void RTLIL::Module::swap_names(RTLIL::Wire *w1, RTLIL::Wire *w2) // Returns the RTLIL dump of a module std::string RTLIL::Module::rtlil_dump() { - std::stringstream stream; // Sorting the module to have a canonical RTLIL sort(); - // Dumping the RTLIL in an in-memory stringstream + // Dumping the RTLIL in an in-memory stringstream + std::stringstream stream; RTLIL_BACKEND::dump_module(stream, " ", this, design, false, true, false); - std::string origstring = stream.str(); - //RTLIL contains non utf-8 characters, converting to utf-8 - std::wstring_convert, char32_t> converter; - std::u32string orig(origstring.begin(), origstring.end()); - std::string utf8String = converter.to_bytes(orig); - return utf8String; + return stream.str(); } // Returns a hash of the RTLIL dump std::string RTLIL::Module::rtlil_hash() { - std::hash hasher; - size_t hash = hasher(rtlil_dump()); - return std::to_string(hash); + return std::to_string(hash_ops::hash(rtlil_dump())); } void RTLIL::Module::swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2) From 43186b2c7af26ab07e017243f596571516d89bc1 Mon Sep 17 00:00:00 2001 From: Alain Dargelas Date: Wed, 6 Nov 2024 16:39:58 -0800 Subject: [PATCH 3/3] Returning unsigned int --- kernel/rtlil.cc | 4 ++-- kernel/rtlil.h | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index bfb177e9b..2650042ff 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -2520,8 +2520,8 @@ std::string RTLIL::Module::rtlil_dump() { } // Returns a hash of the RTLIL dump -std::string RTLIL::Module::rtlil_hash() { - return std::to_string(hash_ops::hash(rtlil_dump())); +unsigned int RTLIL::Module::rtlil_hash() { + return hash_ops::hash(rtlil_dump()); } void RTLIL::Module::swap_names(RTLIL::Cell *c1, RTLIL::Cell *c2) diff --git a/kernel/rtlil.h b/kernel/rtlil.h index bd9d31879..350a6afe3 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -1592,7 +1592,7 @@ public: RTLIL::SigSpec FutureFF (RTLIL::IdString name, const RTLIL::SigSpec &sig_e, const std::string &src = ""); std::string rtlil_dump(); - std::string rtlil_hash(); + unsigned int rtlil_hash(); #ifdef WITH_PYTHON static std::map *get_all_modules(void);