From fb4f567456b24c820942fe8545470433fe7eb2c1 Mon Sep 17 00:00:00 2001 From: "Emil J. Tywoniak" Date: Mon, 22 Jun 2026 00:29:11 +0200 Subject: [PATCH] twine: auto type WIP --- kernel/rtlil.cc | 23 ++++++++++++++++------- kernel/twine.h | 17 ++++++++++++++++- kernel/yosys_common.h | 8 ++++---- 3 files changed, 36 insertions(+), 12 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 0e99bd8f7..0c0bd6b32 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -3809,15 +3809,24 @@ RTLIL::Cell *RTLIL::Module::addCell(Twine name, Twine type) RTLIL::Cell *RTLIL::Module::addCell(TwineRef name, const RTLIL::Cell *other) { - RTLIL::Cell *cell = addCell(name, other->type_impl); - cell->connections_ = other->connections_; + const RTLIL::Design *src_design = other->module ? other->module->design : nullptr; + bool cross_pool = src_design && this->design && src_design != this->design; + + TwineRef type = other->type_impl; + if (cross_pool) + type = this->design->twines.copy_from(src_design->twines, other->type_impl); + + RTLIL::Cell *cell = addCell(name, type); + if (cross_pool) { + for (auto &c : other->connections_) + cell->connections_[this->design->twines.copy_from(src_design->twines, c.first)] = c.second; + } else { + cell->connections_ = other->connections_; + } cell->parameters = other->parameters; cell->attributes = other->attributes; - { - const RTLIL::Design *src_design = other->module ? other->module->design : nullptr; - if (src_design && this->design) - copy_src_into(other, src_design, cell, this->design); - } + if (src_design && this->design) + copy_src_into(other, src_design, cell, this->design); return cell; } diff --git a/kernel/twine.h b/kernel/twine.h index 8f9372061..8fb4cf2c4 100644 --- a/kernel/twine.h +++ b/kernel/twine.h @@ -120,12 +120,19 @@ struct Twine { auto operator<=>(const Suffix&) const = default; }; - std::variant, Suffix> data; + struct AutoPrefix { + const std::string *prefix; + std::string tail; + auto operator<=>(const AutoPrefix&) const = default; + }; + + std::variant, Suffix, AutoPrefix> data; bool is_dead() const { return std::holds_alternative(data); } bool is_leaf() const { return std::holds_alternative(data); } bool is_concat() const { return std::holds_alternative>(data); } bool is_suffix() const { return std::holds_alternative(data); } + bool is_auto_prefix() const { return std::holds_alternative(data); } bool is_flat() const { return is_leaf() || is_suffix(); } const std::string &leaf() const { return std::get(data); } const std::vector &children() const { return std::get>(data); } @@ -363,6 +370,10 @@ struct TwinePool { // and tag publicity themselves (or use the add(std::string) overload). // Suffix names inherit the prefix handle's publicity. TwineRef add(Twine t) { + if (auto *ap = std::get_if(&t.data)) { + TwineRef pref = add_inner(Twine{*ap->prefix}); + return add_inner(Twine{Twine::Suffix{pref, std::move(ap->tail)}}); + } bool is_public = false; if (auto *sfx = std::get_if(&t.data)) { is_public = twine_is_public(sfx->prefix); @@ -649,6 +660,10 @@ struct TwineChildPool { // Local analog of TwinePool::add; see there for the convention. TwineRef add(Twine t) { + if (auto *ap = std::get_if(&t.data)) { + TwineRef pref = add_inner(Twine{*ap->prefix}); + return add_inner(Twine{Twine::Suffix{pref, std::move(ap->tail)}}); + } bool is_public = false; if (auto *leaf = std::get_if(&t.data)) { assert(!leaf->empty()); diff --git a/kernel/yosys_common.h b/kernel/yosys_common.h index dc807a7da..ddfec742d 100644 --- a/kernel/yosys_common.h +++ b/kernel/yosys_common.h @@ -308,15 +308,15 @@ RTLIL::IdString new_id_suffix(std::string_view file, int line, std::string_view #define NEW_ID_SUFFIX(suffix) \ YOSYS_NAMESPACE_PREFIX new_id_suffix(__FILE__, __LINE__, __FUNCTION__, suffix) #define NEW_TWINE \ - YOSYS_NAMESPACE_PREFIX Twine{*[](std::string_view func) -> const std::string * { \ + YOSYS_NAMESPACE_PREFIX Twine{YOSYS_NAMESPACE_PREFIX Twine::AutoPrefix{[](std::string_view func) -> const std::string * { \ static std::unique_ptr prefix(YOSYS_NAMESPACE_PREFIX create_id_prefix(__FILE__, __LINE__, func)); \ return prefix.get(); \ - }(__FUNCTION__) + std::to_string(YOSYS_NAMESPACE_PREFIX autoidx++)} + }(__FUNCTION__), std::to_string(YOSYS_NAMESPACE_PREFIX autoidx++)}} #define NEW_TWINE_SUFFIX(suffix) \ - YOSYS_NAMESPACE_PREFIX Twine{*[](std::string_view func) -> const std::string * { \ + YOSYS_NAMESPACE_PREFIX Twine{YOSYS_NAMESPACE_PREFIX Twine::AutoPrefix{[](std::string_view func) -> const std::string * { \ static std::unique_ptr prefix(YOSYS_NAMESPACE_PREFIX create_id_prefix(__FILE__, __LINE__, func)); \ return prefix.get(); \ - }(__FUNCTION__) + std::string(suffix) + "$" + std::to_string(YOSYS_NAMESPACE_PREFIX autoidx++)} + }(__FUNCTION__), std::string(suffix) + "$" + std::to_string(YOSYS_NAMESPACE_PREFIX autoidx++)}} namespace ID = RTLIL::ID;