From 201352eedfd6e91a4df23aabbb5aded420ada338 Mon Sep 17 00:00:00 2001 From: Alexander McCord Date: Mon, 15 Jul 2024 16:33:53 -0700 Subject: [PATCH] Consistently use `struct`. --- EqSat/include/Luau/EGraph.h | 148 +++++++++++++++++----------------- EqSat/include/Luau/Language.h | 15 ++-- EqSat/include/Luau/Slice.h | 10 +-- 3 files changed, 87 insertions(+), 86 deletions(-) diff --git a/EqSat/include/Luau/EGraph.h b/EqSat/include/Luau/EGraph.h index 3f686895..abccd70c 100644 --- a/EqSat/include/Luau/EGraph.h +++ b/EqSat/include/Luau/EGraph.h @@ -15,7 +15,7 @@ namespace Luau::EqSat { template -class EGraph; +struct EGraph; template struct Analysis final @@ -58,8 +58,80 @@ struct EClass final /// See . template -class EGraph final +struct EGraph final { + Id find(Id id) const + { + return unionfind.find(id); + } + + std::optional lookup(const L& enode) const + { + LUAU_ASSERT(isCanonical(enode)); + + if (auto it = hashcons.find(enode); it != hashcons.end()) + return it->second; + + return std::nullopt; + } + + Id add(L enode) + { + canonicalize(enode); + + if (auto id = lookup(enode)) + return *id; + + Id id = makeEClass(enode); + return id; + } + + void merge(Id id1, Id id2) + { + id1 = find(id1); + id2 = find(id2); + if (id1 == id2) + return; + + unionfind.merge(id1, id2); + + EClass& eclass1 = get(id1); + EClass eclass2 = std::move(get(id2)); + classes.erase(id2); + + worklist.reserve(worklist.size() + eclass2.parents.size()); + for (auto [enode, id] : eclass2.parents) + worklist.push_back({std::move(enode), id}); + + analysis.join(eclass1.data, eclass2.data); + } + + void rebuild() + { + while (!worklist.empty()) + { + auto [enode, id] = worklist.back(); + worklist.pop_back(); + repair(get(find(id))); + } + } + + size_t size() const + { + return classes.size(); + } + + EClass& operator[](Id id) + { + return get(find(id)); + } + + const EClass& operator[](Id id) const + { + return const_cast(this)->get(find(id)); + } + +private: Analysis analysis; /// A union-find data structure 𝑈 stores an equivalence relation over e-class ids. @@ -151,78 +223,6 @@ private: eclass.parents.emplace_back(std::move(node.key()), node.mapped()); } } - -public: - Id find(Id id) const - { - return unionfind.find(id); - } - - std::optional lookup(const L& enode) const - { - LUAU_ASSERT(isCanonical(enode)); - - if (auto it = hashcons.find(enode); it != hashcons.end()) - return it->second; - - return std::nullopt; - } - - Id add(L enode) - { - canonicalize(enode); - - if (auto id = lookup(enode)) - return *id; - - Id id = makeEClass(enode); - return id; - } - - void merge(Id id1, Id id2) - { - id1 = find(id1); - id2 = find(id2); - if (id1 == id2) - return; - - unionfind.merge(id1, id2); - - EClass& eclass1 = get(id1); - EClass eclass2 = std::move(get(id2)); - classes.erase(id2); - - worklist.reserve(worklist.size() + eclass2.parents.size()); - for (auto [enode, id] : eclass2.parents) - worklist.push_back({std::move(enode), id}); - - analysis.join(eclass1.data, eclass2.data); - } - - void rebuild() - { - while (!worklist.empty()) - { - auto [enode, id] = worklist.back(); - worklist.pop_back(); - repair(get(find(id))); - } - } - - size_t size() const - { - return classes.size(); - } - - EClass& operator[](Id id) - { - return get(find(id)); - } - - const EClass& operator[](Id id) const - { - return const_cast(this)->get(find(id)); - } }; } // namespace Luau::EqSat diff --git a/EqSat/include/Luau/Language.h b/EqSat/include/Luau/Language.h index 2a84a303..c17ac577 100644 --- a/EqSat/include/Luau/Language.h +++ b/EqSat/include/Luau/Language.h @@ -158,12 +158,10 @@ struct Field : FieldBase }; template -class NodeFields +struct NodeFields { static_assert(std::conjunction...>::value); - std::array array; - template static constexpr int getIndex() { @@ -218,17 +216,17 @@ public: return languageHash(value.array); } }; + +private: + std::array array; }; template -class Language final +struct Language final { - Variant v; - template using WithinDomain = std::disjunction, Ts>...>; -public: template Language(T&& t, std::enable_if_t::value>* = 0) noexcept : v(std::forward(t)) @@ -298,6 +296,9 @@ public: return seed; } }; + +private: + Variant v; }; } // namespace Luau::EqSat diff --git a/EqSat/include/Luau/Slice.h b/EqSat/include/Luau/Slice.h index 886f0c3e..c1c8f098 100644 --- a/EqSat/include/Luau/Slice.h +++ b/EqSat/include/Luau/Slice.h @@ -10,12 +10,8 @@ namespace Luau::EqSat { template -class Slice final +struct Slice final { - T* _data; - size_t _size; - -public: Slice() : _data(nullptr) , _size(0) @@ -63,6 +59,10 @@ public: return _data[i]; } +public: + T* _data; + size_t _size; + public: T* begin() const {