Skip to content

Commit cd370bf

Browse files
committed
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.
1 parent a515055 commit cd370bf

File tree

2 files changed

+34
-20
lines changed

2 files changed

+34
-20
lines changed

kernel/rtlil.cc

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -204,12 +204,22 @@ const pool<IdString> &RTLIL::builtin_ff_cell_types() {
204204

205205
#define check(condition) log_assert(condition && "malformed Const union")
206206

207-
Const::bitvectype& Const::get_bits() const {
207+
const Const::bitvectype& Const::get_bits() const {
208208
check(is_bits());
209209
return *get_if_bits();
210210
}
211211

212-
std::string& Const::get_str() const {
212+
const std::string& Const::get_str() const {
213+
check(is_str());
214+
return *get_if_str();
215+
}
216+
217+
Const::bitvectype& Const::get_bits() {
218+
check(is_bits());
219+
return *get_if_bits();
220+
}
221+
222+
std::string& Const::get_str() {
213223
check(is_str());
214224
return *get_if_str();
215225
}
@@ -395,7 +405,7 @@ bool RTLIL::Const::as_bool() const
395405
return false;
396406
}
397407

398-
bitvectype& bv = get_bits();
408+
const bitvectype& bv = get_bits();
399409
for (size_t i = 0; i < bv.size(); i++)
400410
if (bv[i] == State::S1)
401411
return true;
@@ -542,7 +552,7 @@ std::string RTLIL::Const::decode_string() const
542552
if (auto str = get_if_str())
543553
return *str;
544554

545-
bitvectype& bv = get_bits();
555+
const bitvectype& bv = get_bits();
546556
const int n = GetSize(bv);
547557
const int n_over_8 = n / 8;
548558
std::string s;
@@ -590,7 +600,7 @@ bool RTLIL::Const::empty() const {
590600
}
591601
}
592602

593-
void RTLIL::Const::bitvectorize_internal() const {
603+
void RTLIL::Const::bitvectorize_internal() {
594604
if (tag == backing_tag::bits)
595605
return;
596606

@@ -641,7 +651,7 @@ bool RTLIL::Const::is_fully_zero() const
641651
return true;
642652
}
643653

644-
bitvectype& bv = get_bits();
654+
const bitvectype& bv = get_bits();
645655

646656
for (const auto &bit : bv)
647657
if (bit != RTLIL::State::S0)
@@ -661,7 +671,7 @@ bool RTLIL::Const::is_fully_ones() const
661671
return true;
662672
}
663673

664-
bitvectype& bv = get_bits();
674+
const bitvectype& bv = get_bits();
665675
for (const auto &bit : bv)
666676
if (bit != RTLIL::State::S1)
667677
return false;
@@ -676,7 +686,7 @@ bool RTLIL::Const::is_fully_def() const
676686
if (is_str())
677687
return true;
678688

679-
bitvectype& bv = get_bits();
689+
const bitvectype& bv = get_bits();
680690
for (const auto &bit : bv)
681691
if (bit != RTLIL::State::S0 && bit != RTLIL::State::S1)
682692
return false;
@@ -691,7 +701,7 @@ bool RTLIL::Const::is_fully_undef() const
691701
if (auto str = get_if_str())
692702
return str->empty();
693703

694-
bitvectype& bv = get_bits();
704+
const bitvectype& bv = get_bits();
695705
for (const auto &bit : bv)
696706
if (bit != RTLIL::State::Sx && bit != RTLIL::State::Sz)
697707
return false;
@@ -706,7 +716,7 @@ bool RTLIL::Const::is_fully_undef_x_only() const
706716
if (auto str = get_if_str())
707717
return str->empty();
708718

709-
bitvectype& bv = get_bits();
719+
const bitvectype& bv = get_bits();
710720
for (const auto &bit : bv)
711721
if (bit != RTLIL::State::Sx)
712722
return false;
@@ -742,7 +752,7 @@ Hasher RTLIL::Const::hash_into(Hasher h) const
742752

743753
// If the bits are all 0/1, hash packed bits using the string hash.
744754
// Otherwise hash the leading packed bits with the rest of the bits individually.
745-
bitvectype &bv = get_bits();
755+
const bitvectype &bv = get_bits();
746756
int size = GetSize(bv);
747757
std::string packed;
748758
int packed_size = (size + 7) >> 3;

kernel/rtlil.h

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -723,23 +723,27 @@ struct RTLIL::Const
723723
using bitvectype = std::vector<RTLIL::State>;
724724
enum class backing_tag: bool { bits, string };
725725
// Do not access the union or tag even in Const methods unless necessary
726-
mutable backing_tag tag;
726+
backing_tag tag;
727727
union {
728-
mutable bitvectype bits_;
729-
mutable std::string str_;
728+
bitvectype bits_;
729+
std::string str_;
730730
};
731731

732732
// Use these private utilities instead
733733
bool is_bits() const { return tag == backing_tag::bits; }
734734
bool is_str() const { return tag == backing_tag::string; }
735735

736-
bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
737-
std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
736+
bitvectype* get_if_bits() { return is_bits() ? &bits_ : NULL; }
737+
std::string* get_if_str() { return is_str() ? &str_ : NULL; }
738+
const bitvectype* get_if_bits() const { return is_bits() ? &bits_ : NULL; }
739+
const std::string* get_if_str() const { return is_str() ? &str_ : NULL; }
738740

739-
bitvectype& get_bits() const;
740-
std::string& get_str() const;
741+
bitvectype& get_bits();
742+
std::string& get_str();
743+
const bitvectype& get_bits() const;
744+
const std::string& get_str() const;
741745
std::vector<RTLIL::State>& bits_internal();
742-
void bitvectorize_internal() const;
746+
void bitvectorize_internal();
743747

744748
public:
745749
Const() : flags(RTLIL::CONST_FLAG_NONE), tag(backing_tag::bits), bits_(std::vector<RTLIL::State>()) {}
@@ -772,7 +776,7 @@ struct RTLIL::Const
772776
[[deprecated("Don't use direct access to the internal std::vector<State>, that's an implementation detail.")]]
773777
std::vector<RTLIL::State>& bits() { return bits_internal(); }
774778
[[deprecated("Don't call bitvectorize() directly, it's an implementation detail.")]]
775-
void bitvectorize() const { bitvectorize_internal(); }
779+
void bitvectorize() const { const_cast<Const*>(this)->bitvectorize_internal(); }
776780

777781
bool as_bool() const;
778782

0 commit comments

Comments
 (0)