Improve determinism of IdString DB for similar scripts
authorClifford Wolf <clifford@clifford.at>
Mon, 11 Mar 2019 19:12:28 +0000 (20:12 +0100)
committerClifford Wolf <clifford@clifford.at>
Mon, 11 Mar 2019 19:12:28 +0000 (20:12 +0100)
Signed-off-by: Clifford Wolf <clifford@clifford.at>
backends/verilog/verilog_backend.cc
kernel/log.cc
kernel/register.cc
kernel/rtlil.cc
kernel/rtlil.h

index 6818edb7a23ac45104683e6052025b101409ad4d..83d83f488090eb81e94726d14b354ae379820557 100644 (file)
@@ -1673,6 +1673,8 @@ struct VerilogBackend : public Backend {
                bool blackboxes = false;
                bool selected = false;
 
+               auto_name_map.clear();
+               reg_wires.clear();
                reg_ct.clear();
 
                reg_ct.insert("$dff");
@@ -1779,6 +1781,8 @@ struct VerilogBackend : public Backend {
                        dump_module(*f, "", it->second);
                }
 
+               auto_name_map.clear();
+               reg_wires.clear();
                reg_ct.clear();
        }
 } VerilogBackend;
index 0ee2170a062bef470211acdf6cecb23390f755b6..400a549dd90486e6dad9cb9c9876b864ae04c78f 100644 (file)
@@ -196,7 +196,11 @@ void logv_header(RTLIL::Design *design, const char *format, va_list ap)
        if (log_hdump.count(header_id) && design != nullptr)
                for (auto &filename : log_hdump.at(header_id)) {
                        log("Dumping current design to '%s'.\n", filename.c_str());
+                       if (yosys_xtrace)
+                               IdString::xtrace_db_dump();
                        Pass::call(design, {"dump", "-o", filename});
+                       if (yosys_xtrace)
+                               log("#X# -- end of dump --\n");
                }
 
        if (pop_errfile)
index 402a5b3ea5ceaf9bd6bee3e7edca22db64e0fcb1..64956401f2348bcb19a376d38e383c75bf78ab24 100644 (file)
@@ -86,6 +86,8 @@ Pass::pre_post_exec_state_t Pass::pre_execute()
 
 void Pass::post_execute(Pass::pre_post_exec_state_t state)
 {
+       IdString::checkpoint();
+
        int64_t time_ns = PerformanceTimer::query() - state.begin_ns;
        runtime_ns += time_ns;
        current_pass = state.parent_pass;
index d4aebcda9cabf6b9303af9fda34b83207359e84c..7f1816190a46737ab3d62f9ea4bec4f9dd767cdb 100644 (file)
@@ -33,6 +33,8 @@ std::vector<int> RTLIL::IdString::global_refcount_storage_;
 std::vector<char*> RTLIL::IdString::global_id_storage_;
 dict<char*, int, hash_cstr_ops> RTLIL::IdString::global_id_index_;
 std::vector<int> RTLIL::IdString::global_free_idx_list_;
+int RTLIL::IdString::last_created_idx_[8];
+int RTLIL::IdString::last_created_idx_ptr_;
 
 RTLIL::Const::Const()
 {
index f877622aa4f781e20cc21a6ef4ce36adb3a36f31..01323d11280b150e409dd85503882b8557b6b8a6 100644 (file)
@@ -76,6 +76,9 @@ namespace RTLIL
 
        struct IdString
        {
+               #undef YOSYS_XTRACE_GET_PUT
+               #undef YOSYS_SORT_ID_FREE_LIST
+
                // the global id string cache
 
                static struct destruct_guard_t {
@@ -89,9 +92,43 @@ namespace RTLIL
                static dict<char*, int, hash_cstr_ops> global_id_index_;
                static std::vector<int> global_free_idx_list_;
 
+               static int last_created_idx_ptr_;
+               static int last_created_idx_[8];
+
+               static inline void xtrace_db_dump()
+               {
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       for (int idx = 0; idx < GetSize(global_id_storage_); idx++)
+                       {
+                               if (global_id_storage_.at(idx) == nullptr)
+                                       log("#X# DB-DUMP index %d: FREE\n", idx);
+                               else
+                                       log("#X# DB-DUMP index %d: '%s' (ref %d)\n", idx, global_id_storage_.at(idx), global_refcount_storage_.at(idx));
+                       }
+               #endif
+               }
+
+               static inline void checkpoint()
+               {
+                       last_created_idx_ptr_ = 0;
+                       for (int i = 0; i < 8; i++) {
+                               if (last_created_idx_[i])
+                                       put_reference(last_created_idx_[i]);
+                               last_created_idx_[i] = 0;
+                       }
+               #ifdef YOSYS_SORT_ID_FREE_LIST
+                       std::sort(global_free_idx_list_.begin(), global_free_idx_list_.end(), std::greater<int>());
+               #endif
+               }
+
                static inline int get_reference(int idx)
                {
                        global_refcount_storage_.at(idx)++;
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# GET-BY-INDEX '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
                        return idx;
                }
 
@@ -107,6 +144,11 @@ namespace RTLIL
                        auto it = global_id_index_.find((char*)p);
                        if (it != global_id_index_.end()) {
                                global_refcount_storage_.at(it->second)++;
+               #ifdef YOSYS_XTRACE_GET_PUT
+                               if (yosys_xtrace) {
+                                       log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(it->second), it->second, global_refcount_storage_.at(it->second));
+                               }
+               #endif
                                return it->second;
                        }
 
@@ -124,16 +166,22 @@ namespace RTLIL
                        global_refcount_storage_.at(idx)++;
 
                        // Avoid Create->Delete->Create pattern
-                       static IdString last_created_id;
-                       put_reference(last_created_id.index_);
-                       last_created_id.index_ = idx;
-                       get_reference(last_created_id.index_);
+                       if (last_created_idx_[last_created_idx_ptr_])
+                               put_reference(last_created_idx_[last_created_idx_ptr_]);
+                       last_created_idx_[last_created_idx_ptr_] = idx;
+                       get_reference(last_created_idx_[last_created_idx_ptr_]);
+                       last_created_idx_ptr_ = (last_created_idx_ptr_ + 1) & 7;
 
                        if (yosys_xtrace) {
                                log("#X# New IdString '%s' with index %d.\n", p, idx);
                                log_backtrace("-X- ", yosys_xtrace-1);
                        }
 
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# GET-BY-NAME '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
                        return idx;
                }
 
@@ -144,6 +192,12 @@ namespace RTLIL
                        if (!destruct_guard.ok)
                                return;
 
+               #ifdef YOSYS_XTRACE_GET_PUT
+                       if (yosys_xtrace) {
+                               log("#X# PUT '%s' (index %d, refcount %d)\n", global_id_storage_.at(idx), idx, global_refcount_storage_.at(idx));
+                       }
+               #endif
+
                        log_assert(global_refcount_storage_.at(idx) > 0);
 
                        if (--global_refcount_storage_.at(idx) != 0)
@@ -1282,7 +1336,7 @@ inline bool RTLIL::SigBit::operator<(const RTLIL::SigBit &other) const {
                return wire ? (offset < other.offset) : (data < other.data);
        if (wire != nullptr && other.wire != nullptr)
                return wire->name < other.wire->name;
-       return wire < other.wire;
+       return (wire != nullptr) < (other.wire != nullptr);
 }
 
 inline bool RTLIL::SigBit::operator==(const RTLIL::SigBit &other) const {