From bf9123b3fa8388578bb5cf4904c6f364016ae2b5 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: Thu, 20 Jun 2019 19:20:11 +0900 Subject: [PATCH] [locop] Introduce NodeDesc class (#3891) * [locop] Introduce NodeDesc class This commit introduces NodeDesc class and refactors the internal implementation of LinearV1 formatter using this class. Signed-off-by: Jonghyun Park * Update per feedback --- contrib/locop/src/FormattedGraph.cpp | 113 ++++++++++++++++++++++++++++++++--- 1 file changed, 106 insertions(+), 7 deletions(-) diff --git a/contrib/locop/src/FormattedGraph.cpp b/contrib/locop/src/FormattedGraph.cpp index 3da74fa..1a07b7e 100644 --- a/contrib/locop/src/FormattedGraph.cpp +++ b/contrib/locop/src/FormattedGraph.cpp @@ -62,6 +62,111 @@ std::string opname(loco::Node *node) } // namespace +namespace +{ + +using ArgName = std::string; +using ArgValue = std::string; +using ArgDesc = std::pair; + +struct NodeDesc +{ +public: + enum class State + { + // All the node descriptions are "Invalid" at the beginning. + // + // Any valid node description SHOULD NOT be at this state. + Invalid, + // This state means that the producer is **NOT** confident about the information that + // it generates. + // + // There may be some missing information. + PartiallyKnown, + // This state means that the producer is confident about the information that it + // generates. + Complete, + }; + +public: + NodeDesc(const std::string &name) : _name{name} + { + // DO NOTHING + } + +public: + const std::string &name(void) const { return _name; } + + uint32_t arg_size(void) const { return _args.size(); } + const ArgDesc &arg(uint32_t n) const { return _args.at(n); } + void arg(const ArgName &name, const ArgValue &value) { _args.emplace_back(name, value); } + + const State &state(void) const { return _state; } + void state(const State &s) { _state = s; } + +private: + std::string _name; + std::vector _args; + State _state = State::Invalid; +}; + +std::ostream &operator<<(std::ostream &os, const NodeDesc &d) +{ + assert(d.state() != NodeDesc::State::Invalid); + + std::vector values; + + for (uint32_t n = 0; n < d.arg_size(); ++n) + { + values.emplace_back(d.arg(n).first + ": " + d.arg(n).second); + } + + if (d.state() == NodeDesc::State::PartiallyKnown) + { + values.emplace_back("..."); + } + + os << d.name(); + os << "("; + if (values.size() > 0) + { + os << values.at(0); + for (uint32_t n = 1; n < values.size(); ++n) + { + os << ", " << values.at(n); + } + } + os << ")"; + + return os; +} + +NodeDesc default_node_desc(const SymbolTable &tbl, loco::Node *node) +{ + NodeDesc res{opname(node)}; + + for (uint32_t n = 0; n < node->arity(); ++n) + { + res.arg(std::string{"arg"} + std::to_string(n), symbol_lookup(tbl, node->arg(n))); + } + res.state(NodeDesc::State::PartiallyKnown); + + return res; +} + +NodeDesc node_desc(const SymbolTable &tbl, loco::Node *node) +{ + if (node->dialect() == loco::CanonicalDialect::get()) + { + // TODO Use dedicated pretty printer + return default_node_desc(tbl, node); + } + + return default_node_desc(tbl, node); +} + +} // namespace + namespace locop { @@ -152,15 +257,9 @@ void FormattedGraphImpl::dump(std::ostream &os) const } } - // TODO How to print arguments for (auto node : loco::postorder_traversal(cluster_outputs)) { - os << symbol(node) << " = " << opname(node) << "("; - for (uint32_t n = 0; n < node->arity(); ++n) - { - os << " " << symbol(node->arg(n)); - } - os << " )" << std::endl; + os << symbol(node) << " = " << node_desc(symbols, node) << std::endl; } os << std::endl; } -- 2.7.4