From 4122f3fbcbb68af0d513cca429b9131816da036f Mon Sep 17 00:00:00 2001 From: Alexander McCord Date: Tue, 28 May 2024 08:31:25 -0700 Subject: [PATCH] Implement a basic version of Analysis. Will expand it later. --- EqSat/include/Luau/EGraph.h | 21 +++++++++++++++++---- tests/EqSat.propositional.test.cpp | 18 ++++++++++++++---- 2 files changed, 31 insertions(+), 8 deletions(-) diff --git a/EqSat/include/Luau/EGraph.h b/EqSat/include/Luau/EGraph.h index 6edcc6a7..903a0110 100644 --- a/EqSat/include/Luau/EGraph.h +++ b/EqSat/include/Luau/EGraph.h @@ -13,6 +13,20 @@ namespace Luau::EqSat { +template +class EGraph; + +template +struct Analysis final +{ + N analysis; + + typename N::Data make(const EGraph& egraph, const L& enode) const + { + return analysis.make(egraph, enode); + } +}; + /// Each e-class is a set of e-nodes representing equivalent terms from a given language, /// and an e-node is a function symbol paired with a list of children e-classes. template @@ -28,6 +42,8 @@ struct EClass final template class EGraph final { + Analysis analysis; + /// A union-find data structure 𝑈 stores an equivalence relation over e-class ids. UnionFind unionfind; @@ -55,16 +71,13 @@ private: classes.insert_or_assign(id, EClass{ id, {enode}, - {}, // TODO: analysis make + analysis.make(*this, enode), {}, }); return id; } public: - // TODO: static_assert L <: Language - // TODO: static_assert N <: Analysis - Id find(Id id) const { return unionfind.find(id); diff --git a/tests/EqSat.propositional.test.cpp b/tests/EqSat.propositional.test.cpp index 940918a2..4988822a 100644 --- a/tests/EqSat.propositional.test.cpp +++ b/tests/EqSat.propositional.test.cpp @@ -26,12 +26,22 @@ using namespace Luau; using PropositionalLogic = EqSat::Language; +using EGraph = EqSat::EGraph; + struct ConstantFold { using Data = std::optional; -}; -using EGraph = EqSat::EGraph; + Data make(const EGraph& egraph, const PropositionalLogic& enode) const + { + if (enode.get()) + return std::nullopt; + else if (auto b = enode.get()) + return b->value; + + return std::nullopt; + } +}; TEST_SUITE_BEGIN("EqSatPropositionalLogic"); @@ -54,8 +64,8 @@ TEST_CASE("egraph_data") EqSat::Id id1 = egraph.add(Bool{true}); EqSat::Id id2 = egraph.add(Bool{false}); - CHECK(egraph[id1].data == std::nullopt); // TODO: true - CHECK(egraph[id2].data == std::nullopt); // TODO: false + CHECK(egraph[id1].data == true); + CHECK(egraph[id2].data == false); } TEST_SUITE_END();