[locop] Introduce NodeDesc class (#3891)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 20 Jun 2019 10:20:11 +0000 (19:20 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 20 Jun 2019 10:20:11 +0000 (19:20 +0900)
* [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 <jh1302.park@samsung.com>
* Update per feedback

contrib/locop/src/FormattedGraph.cpp

index 3da74fa..1a07b7e 100644 (file)
@@ -62,6 +62,111 @@ std::string opname(loco::Node *node)
 
 } // namespace
 
+namespace
+{
+
+using ArgName = std::string;
+using ArgValue = std::string;
+using ArgDesc = std::pair<ArgName, ArgValue>;
+
+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<ArgDesc> _args;
+  State _state = State::Invalid;
+};
+
+std::ostream &operator<<(std::ostream &os, const NodeDesc &d)
+{
+  assert(d.state() != NodeDesc::State::Invalid);
+
+  std::vector<std::string> 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<Formatter::LinearV1>::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;
   }