From 41b6c0cb9fefb5dfab77930652c29939f9b89edd Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Mon, 1 Sep 2025 03:26:40 +0000 Subject: [PATCH 1/5] Make CellTypes methods take IdString by reference to avoid refcount churn --- kernel/celltypes.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kernel/celltypes.h b/kernel/celltypes.h index 11640c25f..f08a695e9 100644 --- a/kernel/celltypes.h +++ b/kernel/celltypes.h @@ -303,24 +303,24 @@ struct CellTypes cell_types.clear(); } - bool cell_known(RTLIL::IdString type) const + bool cell_known(const RTLIL::IdString &type) const { return cell_types.count(type) != 0; } - bool cell_output(RTLIL::IdString type, RTLIL::IdString port) const + bool cell_output(const RTLIL::IdString &type, const RTLIL::IdString &port) const { auto it = cell_types.find(type); return it != cell_types.end() && it->second.outputs.count(port) != 0; } - bool cell_input(RTLIL::IdString type, RTLIL::IdString port) const + bool cell_input(const RTLIL::IdString &type, const RTLIL::IdString &port) const { auto it = cell_types.find(type); return it != cell_types.end() && it->second.inputs.count(port) != 0; } - bool cell_evaluable(RTLIL::IdString type) const + bool cell_evaluable(const RTLIL::IdString &type) const { auto it = cell_types.find(type); return it != cell_types.end() && it->second.is_evaluable; From 54a258f8546bbc999fd7ab8a53aeecbdc436fb25 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Mon, 1 Sep 2025 03:36:03 +0000 Subject: [PATCH 2/5] In hash_cell_inputs, avoid constructing an std::pair (which requires copying the port IdString) --- kernel/hashlib.h | 6 ++++++ passes/opt/opt_merge.cc | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 9c53e6687..39530baea 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -188,6 +188,12 @@ template struct hash_ops> { return h; } HASH_TOP_LOOP_FST (const std::pair &a) HASH_TOP_LOOP_SND + [[nodiscard]] static inline Hasher hash(const P &p, const Q &q) { + Hasher h; + h = hash_ops

::hash_into(p, h); + h = hash_ops::hash_into(q, h); + return h; + } }; template struct hash_ops> { diff --git a/passes/opt/opt_merge.cc b/passes/opt/opt_merge.cc index ba8168e74..6c81ee241 100644 --- a/passes/opt/opt_merge.cc +++ b/passes/opt/opt_merge.cc @@ -107,7 +107,7 @@ struct OptMergeWorker for (const auto& [port, sig] : cell->connections()) { if (cell->output(port)) continue; - comm.eat(hash_ops>::hash({port, assign_map(sig)})); + comm.eat(hash_ops>::hash(port, assign_map(sig))); } h = comm.hash_into(h); if (RTLIL::builtin_ff_cell_types().count(cell->type)) From b2ccfb2d0e5486e851ccaf70c107add77c3f5914 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Tue, 2 Sep 2025 02:43:03 +0000 Subject: [PATCH 3/5] Make in() variadic operator take parameters by reference so we don't copy IdStrings Template argument deduction strips references. --- kernel/rtlil.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/rtlil.h b/kernel/rtlil.h index e0de79ea9..ea358d83b 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -411,7 +411,7 @@ struct RTLIL::IdString // often one needs to check if a given IdString is part of a list (for example a list // of cell types). the following functions helps with that. template - bool in(Args... args) const { + bool in(const Args &... args) const { return (... || in(args)); } From ddf7ba5a34619eda291d9510f6da4f65bffbcf4f Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Tue, 2 Sep 2025 02:44:02 +0000 Subject: [PATCH 4/5] Make `ID()` macro return a reference to the underlying `IdString` instead of copying it Lambda return type deduction infers `IdString` here. --- kernel/yosys_common.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/yosys_common.h b/kernel/yosys_common.h index bc92e7869..943aa4f05 100644 --- a/kernel/yosys_common.h +++ b/kernel/yosys_common.h @@ -285,7 +285,7 @@ RTLIL::IdString new_id_suffix(std::string file, int line, std::string func, std: // // sed -i.orig -r 's/"\\\\([a-zA-Z0-9_]+)"/ID(\1)/g; s/"(\$[a-zA-Z0-9_]+)"/ID(\1)/g;' // -#define ID(_id) ([]() { const char *p = "\\" #_id, *q = p[1] == '$' ? p+1 : p; \ +#define ID(_id) ([]() -> const RTLIL::IdString & { const char *p = "\\" #_id, *q = p[1] == '$' ? p+1 : p; \ static const YOSYS_NAMESPACE_PREFIX RTLIL::IdString id(q); return id; })() namespace ID = RTLIL::ID; From 030e5ec43851569fc0e7991a6672cebdae295b48 Mon Sep 17 00:00:00 2001 From: Robert O'Callahan Date: Tue, 2 Sep 2025 03:01:11 +0000 Subject: [PATCH 5/5] Make IdString hashing take a reference to the IdString instead of copying it --- kernel/rtlil.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kernel/rtlil.h b/kernel/rtlil.h index ea358d83b..c81a0c00a 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -430,10 +430,10 @@ namespace hashlib { static inline bool cmp(const RTLIL::IdString &a, const RTLIL::IdString &b) { return a == b; } - [[nodiscard]] static inline Hasher hash(const RTLIL::IdString id) { + [[nodiscard]] static inline Hasher hash(const RTLIL::IdString &id) { return id.hash_top(); } - [[nodiscard]] static inline Hasher hash_into(const RTLIL::IdString id, Hasher h) { + [[nodiscard]] static inline Hasher hash_into(const RTLIL::IdString &id, Hasher h) { return id.hash_into(h); } };