3
0
Fork 0
mirror of https://github.com/YosysHQ/yosys synced 2025-09-21 08:51:27 +00:00

Stop using mutable in Const.

Now that we only call `bitvectorize()` in non-const methods, we can move the casting-away-const to only happen
in `bitvectorize()`, which is deprecated so only some plugins (maybe) are using it.

This means `const` `Const` methods don't change the underlying data, which means
they'll be safe to use from multiple threads if/when we want to do that.
This commit is contained in:
Robert O'Callahan 2025-08-29 05:56:19 +00:00
parent 29810f1e7c
commit fee2b8992c
2 changed files with 36 additions and 22 deletions

View file

@ -240,12 +240,22 @@ const pool<IdString> &RTLIL::builtin_ff_cell_types() {
#define check(condition) log_assert(condition && "malformed Const union")
Const::bitvectype& Const::get_bits() const {
const Const::bitvectype& Const::get_bits() const {
check(is_bits());
return *get_if_bits();
}
std::string& Const::get_str() const {
const std::string& Const::get_str() const {
check(is_str());
return *get_if_str();
}
Const::bitvectype& Const::get_bits() {
check(is_bits());
return *get_if_bits();
}
std::string& Const::get_str() {
check(is_str());
return *get_if_str();
}
@ -431,7 +441,7 @@ bool RTLIL::Const::as_bool() const
return false;
}
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (size_t i = 0; i < bv.size(); i++)
if (bv[i] == State::S1)
return true;
@ -448,7 +458,7 @@ int RTLIL::Const::as_int(bool is_signed) const
ret |= static_cast<unsigned char>((*s)[size - i]) << ((i - 1) * 8);
// If is_signed and the string is shorter than 4 bytes then apply sign extension.
if (is_signed && size > 0 && size < 4 && ((*s)[0] & 0x80))
ret |= -1 << size*8;
ret |= UINT32_MAX << size*8;
return ret;
}
@ -458,7 +468,7 @@ int RTLIL::Const::as_int(bool is_signed) const
if (bv[i] == State::S1)
ret |= 1 << i;
if (is_signed && significant_bits > 0 && significant_bits < 32 && bv.back() == State::S1 )
ret |= -1 << significant_bits;
ret |= UINT32_MAX << significant_bits;
return ret;
}
@ -579,7 +589,7 @@ std::string RTLIL::Const::decode_string() const
if (auto str = get_if_str())
return *str;
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
const int n = GetSize(bv);
const int n_over_8 = n / 8;
std::string s;
@ -627,7 +637,7 @@ bool RTLIL::Const::empty() const {
}
}
void RTLIL::Const::bitvectorize_internal() const {
void RTLIL::Const::bitvectorize_internal() {
if (tag == backing_tag::bits)
return;
@ -678,7 +688,7 @@ bool RTLIL::Const::is_fully_zero() const
return true;
}
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (const auto &bit : bv)
if (bit != RTLIL::State::S0)
@ -698,7 +708,7 @@ bool RTLIL::Const::is_fully_ones() const
return true;
}
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (const auto &bit : bv)
if (bit != RTLIL::State::S1)
return false;
@ -713,7 +723,7 @@ bool RTLIL::Const::is_fully_def() const
if (is_str())
return true;
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (const auto &bit : bv)
if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1)
return false;
@ -728,7 +738,7 @@ bool RTLIL::Const::is_fully_undef() const
if (auto str = get_if_str())
return str->empty();
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (const auto &bit : bv)
if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz)
return false;
@ -743,7 +753,7 @@ bool RTLIL::Const::is_fully_undef_x_only() const
if (auto str = get_if_str())
return str->empty();
bitvectype& bv = get_bits();
const bitvectype& bv = get_bits();
for (const auto &bit : bv)
if (bit != RTLIL::State::Sx)
return false;
@ -779,7 +789,7 @@ Hasher RTLIL::Const::hash_into(Hasher h) const
// If the bits are all 0/1, hash packed bits using the string hash.
// Otherwise hash the leading packed bits with the rest of the bits individually.
bitvectype &bv = get_bits();
const bitvectype &bv = get_bits();
int size = GetSize(bv);
std::string packed;
int packed_size = (size + 7) >> 3;

View file

@ -822,23 +822,27 @@ private:
using bitvectype = std::vector<RTLIL::State>;
enum class backing_tag: bool { bits, string };
// Do not access the union or tag even in Const methods unless necessary
mutable backing_tag tag;
backing_tag tag;
union {
mutable bitvectype bits_;
mutable std::string str_;
bitvectype bits_;
std::string str_;
};
// Use these private utilities instead
bool is_bits() const { return tag == backing_tag::bits; }
bool is_str() const { return tag == backing_tag::string; }
bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
bitvectype* get_if_bits() { return is_bits() ? &bits_ : NULL; }
std::string* get_if_str() { return is_str() ? &str_ : NULL; }
const bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
const std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
bitvectype& get_bits() const;
std::string& get_str() const;
bitvectype& get_bits();
std::string& get_str();
const bitvectype& get_bits() const;
const std::string& get_str() const;
std::vector<RTLIL::State>& bits_internal();
void bitvectorize_internal() const;
void bitvectorize_internal();
public:
Const() : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::vector<RTLIL::State>()) {}
@ -871,7 +875,7 @@ public:
[[deprecated("Don't use direct access to the internal std::vector<State>, that's an implementation detail.")]]
std::vector<RTLIL::State>& bits() { return bits_internal(); }
[[deprecated("Don't call bitvectorize() directly, it's an implementation detail.")]]
void bitvectorize() const { bitvectorize_internal(); }
void bitvectorize() const { const_cast<Const*>(this)->bitvectorize_internal(); }
bool as_bool() const;