From 5d25ae4db604464e4e9f57b081f5ea353e3a363e Mon Sep 17 00:00:00 2001 From: tondapusili Date: Fri, 13 Mar 2026 15:56:55 -0700 Subject: [PATCH] rtlil: add fast Cell accessors and SigSpec::const_ratio() --- kernel/rtlil.cc | 35 +++++++++++++++++++++++++++++++++-- kernel/rtlil.h | 12 ++++++++++-- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/kernel/rtlil.cc b/kernel/rtlil.cc index 9b3ee4cf3..9cf8715f4 100644 --- a/kernel/rtlil.cc +++ b/kernel/rtlil.cc @@ -4564,6 +4564,32 @@ const RTLIL::Const &RTLIL::Cell::getParam(RTLIL::IdString paramname) const throw std::out_of_range("Cell::getParam()"); } +// NOTE: as_int() silently truncates >32-bit values and reinterprets string-typed Const values +std::map RTLIL::Cell::getParamsAsInts() const +{ + std::map result; + for (auto ¶m : parameters) { + std::string key = param.first.str(); + if (key.size() > 0 && key[0] == '\\') + key = key.substr(1); + result[key] = param.second.as_int(); + } + return result; +} + +double RTLIL::Cell::maxInputConstRatio() const +{ + double max_ratio = 0.0; + for (auto &conn : connections_) { + if (input(conn.first)) { + double ratio = conn.second.const_ratio(); + if (ratio > max_ratio) + max_ratio = ratio; + } + } + return max_ratio; +} + void RTLIL::Cell::sort() { connections_.sort(sort_by_id_str()); @@ -5605,13 +5631,18 @@ bool RTLIL::SigSpec::is_chunk() const return ++it == cs.end(); } -bool RTLIL::SigSpec::is_mostly_const(double const_ratio_threshold) const +double RTLIL::SigSpec::const_ratio() const { int constbits = 0; for (auto &chunk : chunks()) if (chunk.width > 0 && chunk.wire == NULL) constbits += chunk.width; - return (constbits > size() * const_ratio_threshold); + return empty() ? 0.0 : static_cast(constbits) / size(); +} + +bool RTLIL::SigSpec::is_mostly_const(double const_ratio_threshold) const +{ + return (const_ratio() > const_ratio_threshold); } bool RTLIL::SigSpec::known_driver() const diff --git a/kernel/rtlil.h b/kernel/rtlil.h index 7044e609a..3d2e54a1a 100644 --- a/kernel/rtlil.h +++ b/kernel/rtlil.h @@ -1684,8 +1684,9 @@ public: bool known_driver() const; - // const_ratio_threshold is expected in [0.0, 1.0] - // boundary is exclusive, returns true only if const bit ratio > const_ratio_threshold + // Constant bit ratio helpers: const_ratio() returns [0.0, 1.0], + // is_mostly_const() returns true if const_ratio() > threshold + double const_ratio() const; bool is_mostly_const(double const_ratio_threshold = 0.5) const; bool is_fully_const() const; bool is_fully_zero() const; @@ -2531,6 +2532,13 @@ public: void setParam(RTLIL::IdString paramname, RTLIL::Const value); const RTLIL::Const &getParam(RTLIL::IdString paramname) const; + // Primitive-type parameter accessors for efficient Python interop. + // NOTE: silently truncates wide (>32-bit) parameters and reinterprets string-typed Const values + std::map getParamsAsInts() const; + + // Returns the maximum const_ratio() across all input ports, 0.0 if no input ports + double maxInputConstRatio() const; + void sort(); void check(); void fixup_parameters(bool set_a_signed = false, bool set_b_signed = false);