From f25f8fe7c445e19a11835ccea87a16947b201c3e Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Mon, 21 Jul 2025 05:32:31 +0000 Subject: [PATCH] In the Verilog backend, only sort modules that we're going to emit. If you have a large design with a lot of modules and you use the Verilog backend to emit modules one at a time to separate files, performance is very low. The problem is that the Verilog backend calls `design->sort()` every time, which sorts the contents of all modules, and this is slow even when everything is already sorted. We can easily fix this by only sorting the contents of modules that we're actually going to emit. --- backends/verilog/verilog_backend.cc | 3 ++- kernel/rtlil.cc | 6 ++++++ kernel/rtlil.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/backends/verilog/verilog_backend.cc b/backends/verilog/verilog_backend.cc index 1cef7be60..144dad90c 100644 --- a/backends/verilog/verilog_backend.cc +++ b/backends/verilog/verilog_backend.cc @@ -2612,7 +2612,7 @@ struct VerilogBackend : public Backend { Pass::call(design, "clean_zerowidth"); log_pop(); - design->sort(); + design->sort_modules(); *f << stringf("/* Generated by %s */\n", yosys_maybe_version()); @@ -2625,6 +2625,7 @@ struct VerilogBackend : public Backend { continue; } log("Dumping module `%s'.\n", module->name.c_str()); + module->sort(); dump_module(*f, "", module); } diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 8a0080dbf..db954a7c3 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -1139,6 +1139,12 @@ void RTLIL::Design::sort() it.second->sort(); } +void RTLIL::Design::sort_modules() +{ + scratchpad.sort(); + modules_.sort(sort_by_id_str()); +} + void RTLIL::Design::check() { #ifndef NDEBUG diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 504fa0062..cf3f24c81 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -1368,6 +1368,7 @@ struct RTLIL::Design std::string scratchpad_get_string(const std::string &varname, const std::string &default_value = std::string()) const; void sort(); + void sort_modules(); void check(); void optimize();