From cb3853fca0dd85639af790e4f2d7c565387511c7 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 09:51:45 -0800 Subject: [PATCH 1/7] improvements to autoscope --- kernel/fstdata.cc | 61 +++++++++-------------------------- kernel/fstdata.h | 2 +- passes/sat/sim.cc | 5 +-- passes/silimate/reg_rename.cc | 5 +-- 4 files changed, 22 insertions(+), 51 deletions(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index 899447773..dd88590b2 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -397,41 +397,11 @@ int FstData::getWidth(fstHandle signal) } // Auto-discover scope from FST by finding the top module -std::string FstData::autoScope(Module *topmod) { +std::vector FstData::autoScope(Module *topmod) { - log("Auto-discovering scope from file...\n"); + log("Auto-discovering scopes from file...\n"); std::string top = RTLIL::unescape_id(topmod->name); - log("Available scopes:\n"); - std::set unique_scopes; - for (const auto& var : vars) { - unique_scopes.insert(var.scope); - } - for (const auto& scope : unique_scopes) { - log(" %s\n", scope.c_str()); - } - - // Option 1 - Instance based scope matching - // Will fail if the DUT instance name != the top module name - log("Trying instance-based scope matching...\n"); - for (const auto& var : vars) { - // Check if this scope ends with our top module - log_debug("Checking scope: %s\n", var.scope.c_str()); - if (var.scope == top || - var.scope.find("." + top) != std::string::npos) { - // Extract the full path up to (and including) the top module - size_t pos = var.scope.find(top); - if (pos != std::string::npos) { - std::string scope = var.scope.substr(0, pos + top.length()); - return scope; - } - } - } - - // Option 2 - Port based scope matching - // Matches based on exact port name matching of the top module - log("Trying port-based scope matching...\n"); - // Map top module port name to their bit widths (RTL reference point) dict top2widths; for (auto wire : topmod->wires()) { @@ -456,7 +426,7 @@ std::string FstData::autoScope(Module *topmod) { if (last_dot != std::string::npos) { // no '.' means no scope/signal extraction is possible std::string scope = name.substr(0, last_dot); std::string signal_name = name.substr(last_dot + 1); - + // Check that signal is in the top module and width matches if (top2widths.count(signal_name)) { int signal_width = getWidth(handle); @@ -467,24 +437,23 @@ std::string FstData::autoScope(Module *topmod) { } } - // Find scopes with exact matches - // If there is a tie, return the longest scope - std::string result = ""; + // Find scopes with exact matches and add to array + std::vector results; for (const auto& entry : scopes2matches) { int num_matches = entry.second; if (num_matches == GetSize(top2widths)) { std::string scope = entry.first; - if (result.empty() || scope.length() > result.length()) { - result = scope; - } + results.push_back(scope); } } - if (!result.empty()) { - return result; + if (results.empty()) { + log_warning("Could not auto-discover scope for module '%s'...\n", + RTLIL::unescape_id(topmod->name).c_str()); + } else { + log("Found %d scopes for module '%s':\n", GetSize(results), RTLIL::unescape_id(topmod->name).c_str()); + for (const auto& scope : results) { + log(" %s\n", scope.c_str()); + } } - - // No match found - log_warning("Could not auto-discover scope for module '%s'...\n", - RTLIL::unescape_id(topmod->name).c_str()); - return ""; + return results; } diff --git a/kernel/fstdata.h b/kernel/fstdata.h index f95806cca..01e175691 100644 --- a/kernel/fstdata.h +++ b/kernel/fstdata.h @@ -58,7 +58,7 @@ class FstData double getTimescale() { return timescale; } const char *getTimescaleString() { return timescale_str.c_str(); } int getWidth(fstHandle signal); - std::string autoScope(Module *topmod); + std::vector autoScope(Module *topmod); private: void extractVarNames(); diff --git a/passes/sat/sim.cc b/passes/sat/sim.cc index 113a15bdd..eb3f39480 100644 --- a/passes/sat/sim.cc +++ b/passes/sat/sim.cc @@ -1475,11 +1475,12 @@ struct SimWorker : SimShared fst = new FstData(sim_filename); timescale = fst->getTimescaleString(); if (scope.empty()) { - scope = fst->autoScope(topmod); - if (scope.empty()) { + scopes = fst->autoScope(topmod); + if (scopes.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); } + scope = scopes[0]; // Use the first scope found by default } log("Using scope: \"%s\"\n", scope.c_str()); diff --git a/passes/silimate/reg_rename.cc b/passes/silimate/reg_rename.cc index 5635b6869..05e5200fb 100644 --- a/passes/silimate/reg_rename.cc +++ b/passes/silimate/reg_rename.cc @@ -200,11 +200,12 @@ struct RegRenamePass : public Pass { try { FstData fst(vcd_filename); if (scope.empty()) { - scope = fst.autoScope(topmod); - if (scope.empty()) { + scopes = fst->autoScope(topmod); + if (scopes.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); } + scope = scopes[0]; // Use the first scope found by default } log("Using scope: \"%s\"\n", scope.c_str()); for (auto &var : fst.getVars()) { From c3a9a6d90e62e972a5757be6645ddbc059a39ec5 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 09:59:51 -0800 Subject: [PATCH 2/7] fix --- passes/sat/sim.cc | 2 +- passes/silimate/reg_rename.cc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/passes/sat/sim.cc b/passes/sat/sim.cc index eb3f39480..00b144037 100644 --- a/passes/sat/sim.cc +++ b/passes/sat/sim.cc @@ -1475,7 +1475,7 @@ struct SimWorker : SimShared fst = new FstData(sim_filename); timescale = fst->getTimescaleString(); if (scope.empty()) { - scopes = fst->autoScope(topmod); + std::vector scopes = fst->autoScope(topmod); if (scopes.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); diff --git a/passes/silimate/reg_rename.cc b/passes/silimate/reg_rename.cc index 05e5200fb..e5c7ffca2 100644 --- a/passes/silimate/reg_rename.cc +++ b/passes/silimate/reg_rename.cc @@ -200,7 +200,7 @@ struct RegRenamePass : public Pass { try { FstData fst(vcd_filename); if (scope.empty()) { - scopes = fst->autoScope(topmod); + std::vector scopes = fst.autoScope(topmod); if (scopes.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); From 20c1f3212f15b6b7c4bc161f1ceffbfbe74ad051 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 10:23:31 -0800 Subject: [PATCH 3/7] cleaner with warning --- kernel/fstdata.cc | 10 ++++++++-- kernel/fstdata.h | 2 +- passes/sat/sim.cc | 5 ++--- passes/silimate/reg_rename.cc | 5 ++--- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index dd88590b2..f2e73e9ec 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -397,7 +397,7 @@ int FstData::getWidth(fstHandle signal) } // Auto-discover scope from FST by finding the top module -std::vector FstData::autoScope(Module *topmod) { +std::string FstData::autoScope(Module *topmod) { log("Auto-discovering scopes from file...\n"); std::string top = RTLIL::unescape_id(topmod->name); @@ -449,11 +449,17 @@ std::vector FstData::autoScope(Module *topmod) { if (results.empty()) { log_warning("Could not auto-discover scope for module '%s'...\n", RTLIL::unescape_id(topmod->name).c_str()); + return ""; } else { log("Found %d scopes for module '%s':\n", GetSize(results), RTLIL::unescape_id(topmod->name).c_str()); for (const auto& scope : results) { log(" %s\n", scope.c_str()); } + if (results.size() > 1) { + log_warning("Multiple scopes found for module '%s'. Using the first one.\n", + RTLIL::unescape_id(topmod->name).c_str()); + } + std::string scope = results[0]; } - return results; + return scope; } diff --git a/kernel/fstdata.h b/kernel/fstdata.h index 01e175691..f95806cca 100644 --- a/kernel/fstdata.h +++ b/kernel/fstdata.h @@ -58,7 +58,7 @@ class FstData double getTimescale() { return timescale; } const char *getTimescaleString() { return timescale_str.c_str(); } int getWidth(fstHandle signal); - std::vector autoScope(Module *topmod); + std::string autoScope(Module *topmod); private: void extractVarNames(); diff --git a/passes/sat/sim.cc b/passes/sat/sim.cc index 00b144037..113a15bdd 100644 --- a/passes/sat/sim.cc +++ b/passes/sat/sim.cc @@ -1475,12 +1475,11 @@ struct SimWorker : SimShared fst = new FstData(sim_filename); timescale = fst->getTimescaleString(); if (scope.empty()) { - std::vector scopes = fst->autoScope(topmod); - if (scopes.empty()) { + scope = fst->autoScope(topmod); + if (scope.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); } - scope = scopes[0]; // Use the first scope found by default } log("Using scope: \"%s\"\n", scope.c_str()); diff --git a/passes/silimate/reg_rename.cc b/passes/silimate/reg_rename.cc index e5c7ffca2..5635b6869 100644 --- a/passes/silimate/reg_rename.cc +++ b/passes/silimate/reg_rename.cc @@ -200,12 +200,11 @@ struct RegRenamePass : public Pass { try { FstData fst(vcd_filename); if (scope.empty()) { - std::vector scopes = fst.autoScope(topmod); - if (scopes.empty()) { + scope = fst.autoScope(topmod); + if (scope.empty()) { log_error("No scope found for module '%s'. Please specify -scope explicitly.\n", RTLIL::unescape_id(topmod->name).c_str()); } - scope = scopes[0]; // Use the first scope found by default } log("Using scope: \"%s\"\n", scope.c_str()); for (auto &var : fst.getVars()) { From c9330dc36f8de507ee471e8e4f42bc3341dd12ff Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 10:30:49 -0800 Subject: [PATCH 4/7] oop --- kernel/fstdata.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index f2e73e9ec..7edc03e83 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -401,6 +401,7 @@ std::string FstData::autoScope(Module *topmod) { log("Auto-discovering scopes from file...\n"); std::string top = RTLIL::unescape_id(topmod->name); + std::string scope = ""; // Map top module port name to their bit widths (RTL reference point) dict top2widths; From 69145403df415d1742b52e22ff1a8543badee6c7 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 10:32:28 -0800 Subject: [PATCH 5/7] more updates --- kernel/fstdata.cc | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index 7edc03e83..307c573e8 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -410,7 +410,7 @@ std::string FstData::autoScope(Module *topmod) { top2widths[RTLIL::unescape_id(wire->name)] = wire->width; } } - log("Extracted %d ports from top module\n", GetSize(top2widths)); + log("Extracted %d ports from module '%s'\n", GetSize(top2widths), top); // For each scope, track the number of matching ports dict scopes2matches; @@ -449,16 +449,16 @@ std::string FstData::autoScope(Module *topmod) { } if (results.empty()) { log_warning("Could not auto-discover scope for module '%s'...\n", - RTLIL::unescape_id(topmod->name).c_str()); + top); return ""; } else { - log("Found %d scopes for module '%s':\n", GetSize(results), RTLIL::unescape_id(topmod->name).c_str()); + log("Found %d scopes for module '%s':\n", GetSize(results), top); for (const auto& scope : results) { log(" %s\n", scope.c_str()); } if (results.size() > 1) { log_warning("Multiple scopes found for module '%s'. Using the first one.\n", - RTLIL::unescape_id(topmod->name).c_str()); + top); } std::string scope = results[0]; } From 1592125e7162eabe357ba477329deb30ba0670e6 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 10:36:29 -0800 Subject: [PATCH 6/7] fix err --- kernel/fstdata.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index 307c573e8..fd72ed4c5 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -460,7 +460,7 @@ std::string FstData::autoScope(Module *topmod) { log_warning("Multiple scopes found for module '%s'. Using the first one.\n", top); } - std::string scope = results[0]; + scope = results[0]; } return scope; } From b7984f12f8f812763ec4821c2a021c3d4b3d1357 Mon Sep 17 00:00:00 2001 From: Stan Lee Date: Fri, 6 Mar 2026 12:19:17 -0800 Subject: [PATCH 7/7] greptile --- kernel/fstdata.cc | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/kernel/fstdata.cc b/kernel/fstdata.cc index fd72ed4c5..c1d56e353 100644 --- a/kernel/fstdata.cc +++ b/kernel/fstdata.cc @@ -410,7 +410,7 @@ std::string FstData::autoScope(Module *topmod) { top2widths[RTLIL::unescape_id(wire->name)] = wire->width; } } - log("Extracted %d ports from module '%s'\n", GetSize(top2widths), top); + log("Extracted %d ports from module '%s'\n", GetSize(top2widths), top.c_str()); // For each scope, track the number of matching ports dict scopes2matches; @@ -449,18 +449,17 @@ std::string FstData::autoScope(Module *topmod) { } if (results.empty()) { log_warning("Could not auto-discover scope for module '%s'...\n", - top); + top.c_str()); return ""; } else { - log("Found %d scopes for module '%s':\n", GetSize(results), top); + log("Found %d scopes for module '%s':\n", GetSize(results), top.c_str()); for (const auto& scope : results) { log(" %s\n", scope.c_str()); } if (results.size() > 1) { log_warning("Multiple scopes found for module '%s'. Using the first one.\n", - top); + top.c_str()); } - scope = results[0]; + return results[0]; } - return scope; }