From 8968986b543a6d3b42408ec5d3eb5996210ad209 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Fri, 14 Feb 2025 18:57:36 +0100 Subject: [PATCH 1/4] share: add -pattern-limit to set analysis effort on branch-unbranch mux regions --- passes/opt/share.cc | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/passes/opt/share.cc b/passes/opt/share.cc index 6081d140e..bf583dd32 100644 --- a/passes/opt/share.cc +++ b/passes/opt/share.cc @@ -23,6 +23,7 @@ #include "kernel/modtools.h" #include "kernel/utils.h" #include "kernel/macc.h" +#include USING_YOSYS_NAMESPACE PRIVATE_NAMESPACE_BEGIN @@ -33,6 +34,7 @@ typedef std::pair ssc_pair_t; struct ShareWorkerConfig { int limit; + size_t pattern_limit; bool opt_force; bool opt_aggressive; bool opt_fast; @@ -853,6 +855,21 @@ struct ShareWorker optimize_activation_patterns(patterns); } + template + void insert_capped(pool& cache, Iterator begin_pattern, Iterator end_pattern) + { + if (cache.size() + std::distance(begin_pattern, end_pattern) > config.pattern_limit) { + cache.clear(); + cache.insert(ssc_pair_t()); + } else { + cache.insert(begin_pattern, end_pattern); + } + } + void insert_capped(pool& cache, ssc_pair_t pattern) + { + insert_capped(cache, &pattern, &pattern + 1); + } + const pool &find_cell_activation_patterns(RTLIL::Cell *cell, const char *indent) { if (recursion_state.count(cell)) { @@ -909,20 +926,20 @@ struct ShareWorker for (int i = 0; i < GetSize(sig_s); i++) p.first.append(sig_s[i]), p.second.bits().push_back(RTLIL::State::S0); if (sort_check_activation_pattern(p)) - activation_patterns_cache[cell].insert(p); + insert_capped(activation_patterns_cache[cell], p); } for (int idx : used_in_b_parts) for (auto p : c_patterns) { p.first.append(sig_s[idx]), p.second.bits().push_back(RTLIL::State::S1); if (sort_check_activation_pattern(p)) - activation_patterns_cache[cell].insert(p); + insert_capped(activation_patterns_cache[cell], p); } } for (auto c : driven_cells) { const pool &c_patterns = find_cell_activation_patterns(c, indent); - activation_patterns_cache[cell].insert(c_patterns.begin(), c_patterns.end()); + insert_capped(activation_patterns_cache[cell], c_patterns.begin(), c_patterns.end()); } log_assert(recursion_state.count(cell) != 0); @@ -1437,6 +1454,10 @@ struct SharePass : public Pass { log("\n"); log(" -limit N\n"); log(" Only perform the first N merges, then stop. This is useful for debugging.\n"); + log(" -pattern-limit N\n"); + log(" Only analyze up to N activation patterns per cell, otherwise assume active.\n"); + log(" N is 1000 by default. Higher values may merge more resources at the cost of\n"); + log(" more runtime and memory consumption.\n"); log("\n"); } void execute(std::vector args, RTLIL::Design *design) override @@ -1444,6 +1465,7 @@ struct SharePass : public Pass { ShareWorkerConfig config; config.limit = -1; + config.pattern_limit = 1000; config.opt_force = false; config.opt_aggressive = false; config.opt_fast = false; @@ -1508,6 +1530,10 @@ struct SharePass : public Pass { config.limit = atoi(args[++argidx].c_str()); continue; } + if (args[argidx] == "-pattern-limit" && argidx+1 < args.size()) { + config.pattern_limit = atoi(args[++argidx].c_str()); + continue; + } break; } extra_args(args, argidx, design); From 912c93ca0ba75e109c184aae32e10f2df4bc5b4d Mon Sep 17 00:00:00 2001 From: Emil J Date: Mon, 3 Mar 2025 18:52:05 +0100 Subject: [PATCH 2/4] share: help message formatting Co-authored-by: KrystalDelusion <93062060+KrystalDelusion@users.noreply.github.com> --- passes/opt/share.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/passes/opt/share.cc b/passes/opt/share.cc index bf583dd32..20ec8d358 100644 --- a/passes/opt/share.cc +++ b/passes/opt/share.cc @@ -1454,6 +1454,7 @@ struct SharePass : public Pass { log("\n"); log(" -limit N\n"); log(" Only perform the first N merges, then stop. This is useful for debugging.\n"); + log("\n"); log(" -pattern-limit N\n"); log(" Only analyze up to N activation patterns per cell, otherwise assume active.\n"); log(" N is 1000 by default. Higher values may merge more resources at the cost of\n"); From ca9176cd0f7268fabd5bc9f56a66b4cfc09936d7 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 10 Mar 2025 17:55:04 +0100 Subject: [PATCH 3/4] share: bail better on too many patterns --- passes/opt/share.cc | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/passes/opt/share.cc b/passes/opt/share.cc index 20ec8d358..7bb48eddc 100644 --- a/passes/opt/share.cc +++ b/passes/opt/share.cc @@ -856,18 +856,20 @@ struct ShareWorker } template - void insert_capped(pool& cache, Iterator begin_pattern, Iterator end_pattern) + bool insert_capped(pool& cache, Iterator begin_pattern, Iterator end_pattern) { if (cache.size() + std::distance(begin_pattern, end_pattern) > config.pattern_limit) { cache.clear(); cache.insert(ssc_pair_t()); + return false; } else { cache.insert(begin_pattern, end_pattern); } + return true; } - void insert_capped(pool& cache, ssc_pair_t pattern) + bool insert_capped(pool& cache, ssc_pair_t pattern) { - insert_capped(cache, &pattern, &pattern + 1); + return insert_capped(cache, &pattern, &pattern + 1); } const pool &find_cell_activation_patterns(RTLIL::Cell *cell, const char *indent) @@ -926,20 +928,29 @@ struct ShareWorker for (int i = 0; i < GetSize(sig_s); i++) p.first.append(sig_s[i]), p.second.bits().push_back(RTLIL::State::S0); if (sort_check_activation_pattern(p)) - insert_capped(activation_patterns_cache[cell], p); + if (!insert_capped(activation_patterns_cache[cell], p)) { + recursion_state.erase(cell); + return activation_patterns_cache[cell]; + } } for (int idx : used_in_b_parts) for (auto p : c_patterns) { p.first.append(sig_s[idx]), p.second.bits().push_back(RTLIL::State::S1); if (sort_check_activation_pattern(p)) - insert_capped(activation_patterns_cache[cell], p); + if (!insert_capped(activation_patterns_cache[cell], p)) { + recursion_state.erase(cell); + return activation_patterns_cache[cell]; + } } } for (auto c : driven_cells) { const pool &c_patterns = find_cell_activation_patterns(c, indent); - insert_capped(activation_patterns_cache[cell], c_patterns.begin(), c_patterns.end()); + if (!insert_capped(activation_patterns_cache[cell], c_patterns.begin(), c_patterns.end())) { + recursion_state.erase(cell); + return activation_patterns_cache[cell]; + } } log_assert(recursion_state.count(cell) != 0); From 6c9857403cb112fc36db73a9eece2fcf34637518 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Tue, 11 Mar 2025 14:57:00 +0100 Subject: [PATCH 4/4] share: use share.pattern_limit from scratchpad --- passes/opt/share.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/passes/opt/share.cc b/passes/opt/share.cc index 7bb48eddc..cb1377626 100644 --- a/passes/opt/share.cc +++ b/passes/opt/share.cc @@ -1477,7 +1477,7 @@ struct SharePass : public Pass { ShareWorkerConfig config; config.limit = -1; - config.pattern_limit = 1000; + config.pattern_limit = design->scratchpad_get_int("share.pattern_limit", 1000); config.opt_force = false; config.opt_aggressive = false; config.opt_fast = false;