From cb80ab5fa47dc067b624ca7d6525aa63810817d4 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 12 Jul 2019 09:37:14 +0900 Subject: [PATCH] [locop] Introduce CanonicalNodeSummaryBuilder (#4205) * [locop] Introduce CanonicalNodeSummaryBuilder This commit introduces CanonicalNodeSummaryBuilder, which allows users to embed CanonicalNodeSummryBuilder into their own NodeSummaryBuilder. Signed-off-by: Jonghyun Park * Update example --- contrib/locop/include/locop/FormattedGraph.h | 18 +++++++ contrib/locop/src/FormattedGraph.cpp | 19 ++++++++ contrib/locop/src/FormattedGraph.test.cpp | 71 ++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+) diff --git a/contrib/locop/include/locop/FormattedGraph.h b/contrib/locop/include/locop/FormattedGraph.h index 5838884..518bdd7 100644 --- a/contrib/locop/include/locop/FormattedGraph.h +++ b/contrib/locop/include/locop/FormattedGraph.h @@ -118,6 +118,24 @@ struct NodeSummaryBuilderFactory virtual std::unique_ptr create(const SymbolTable *) const = 0; }; +/** + * @brief Built-in Node Summary Builder for Canonical Dialect + */ +class CanonicalNodeSummaryBuilder final : public NodeSummaryBuilder +{ +public: + CanonicalNodeSummaryBuilder(const SymbolTable *tbl) : _tbl{tbl} + { + // DO NOTHING + } + +public: + bool build(const loco::Node *node, locop::NodeSummary &out) const final; + +private: + const SymbolTable *_tbl; +}; + struct FormattedGraph { virtual ~FormattedGraph() = default; diff --git a/contrib/locop/src/FormattedGraph.cpp b/contrib/locop/src/FormattedGraph.cpp index ac5a151..ff31301 100644 --- a/contrib/locop/src/FormattedGraph.cpp +++ b/contrib/locop/src/FormattedGraph.cpp @@ -331,6 +331,25 @@ private: namespace locop { +bool CanonicalNodeSummaryBuilder::build(const loco::Node *node, locop::NodeSummary &out) const +{ + // Skip if a given node does not belong to loco.canonical + if (node->dialect() != loco::CanonicalDialect::get()) + { + return false; + } + + auto canonical_node = dynamic_cast(node); + assert(canonical_node != nullptr); + out = canonical_node_desc(*_tbl, canonical_node); + return true; +} + +} // namespace locop + +namespace locop +{ + std::ostream &operator<<(std::ostream &os, const FormattedGraph &fmt) { fmt.dump(os); diff --git a/contrib/locop/src/FormattedGraph.test.cpp b/contrib/locop/src/FormattedGraph.test.cpp index 0f8c595..6136390 100644 --- a/contrib/locop/src/FormattedGraph.test.cpp +++ b/contrib/locop/src/FormattedGraph.test.cpp @@ -77,3 +77,74 @@ TEST(LinearV1FormatterTest, user_defined_node_summary_builder) // TODO Check whether MyBuilder actually sees all the nodes in a graph SUCCEED(); } + +// This test shows how to compose two node summary builders. +TEST(LinearV1FormatterTest, node_summary_builder_composition) +{ + struct MyNode : public loco::FixedArityNode<0, loco::Node> + { + uint32_t opnum(void) const final { return 0; } + const loco::Dialect *dialect(void) const final { return nullptr; }; + }; + + auto g = loco::make_graph(); + { + auto user = g->nodes()->create(); + + auto push = g->nodes()->create(); + + push->from(user); + } + + // TODO Reuse MyBuilder above + struct MyBuilder final : public locop::NodeSummaryBuilder + { + bool build(const loco::Node *node, locop::NodeSummary &s) const final + { + s.opname("my.op"); + s.state(locop::NodeSummary::State::PartiallyKnown); + return true; + } + }; + + class CompositeBuilder final : public locop::NodeSummaryBuilder + { + public: + CompositeBuilder(const locop::SymbolTable *tbl) : _tbl{tbl} + { + // DO NOTHING + } + + public: + bool build(const loco::Node *node, locop::NodeSummary &s) const final + { + if (locop::CanonicalNodeSummaryBuilder(_tbl).build(node, s)) + { + return true; + } + + if (MyBuilder().build(node, s)) + { + return true; + } + + return false; + } + + private: + const locop::SymbolTable *_tbl; + }; + + struct MyFactory final : public locop::NodeSummaryBuilderFactory + { + std::unique_ptr create(const locop::SymbolTable *tbl) const final + { + return stdex::make_unique(tbl); + } + }; + + std::cout << locop::fmt(g).with(stdex::make_unique()) << std::endl; + + // TODO Check whether MyBuilder actually sees all the nodes in a graph + SUCCEED(); +} -- 2.7.4