[locop] NodeSummary with Comments (#7193)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 5 Sep 2019 05:22:34 +0000 (14:22 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 5 Sep 2019 05:22:34 +0000 (14:22 +0900)
This commit allows usersr to add multi-line comments to NodeSummary, and
revises LinearV1 Graph Formatter to show these comments properly.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
compiler/locop/include/locop/NodeSummary.h
compiler/locop/src/FormattedGraph.cpp
compiler/locop/src/FormattedGraph.test.cpp
compiler/locop/src/NodeSummary.cpp

index e10758a..59fe663 100644 (file)
@@ -49,6 +49,24 @@ private:
 struct NodeDesc
 {
 public:
+  /**
+   * @brief Multi-line comments
+   */
+  class Comments final
+  {
+  public:
+    Comments() = default;
+
+  public:
+    uint32_t count(void) const { return _lines.size(); }
+    const std::string &at(uint32_t n) const { return _lines.at(n); }
+    void append(const std::string &s);
+
+  private:
+    std::vector<std::string> _lines;
+  };
+
+public:
   enum class State
   {
     // All the node descriptions are "Invalid" at the beginning.
@@ -76,12 +94,16 @@ public:
   const ArgDesc &args(void) const { return _args; }
   ArgDesc &args(void) { return _args; }
 
+  const Comments &comments(void) const { return _comments; }
+  Comments &comments(void) { return _comments; }
+
   const State &state(void) const { return _state; }
   void state(const State &s) { _state = s; }
 
 private:
   std::unique_ptr<OpName> _name = nullptr;
   ArgDesc _args;
+  Comments _comments;
   State _state = State::Invalid;
 };
 
index 5b21729..4b69a52 100644 (file)
@@ -307,6 +307,11 @@ void FormattedGraphImpl<Formatter::LinearV1>::dump(std::ostream &os) const
         throw std::runtime_error{"Fail to build a node summary"};
       }
 
+      for (uint32_t n = 0; n < node_summary.comments().count(); ++n)
+      {
+        os << "; " << node_summary.comments().at(n) << std::endl;
+      }
+
       os << symbol(node) << " = " << node_summary << std::endl;
     }
     os << std::endl;
index b0d9589..13ca3a3 100644 (file)
@@ -41,6 +41,11 @@ TEST(LinearV1FormatterTest, simple)
 
 TEST(LinearV1FormatterTest, user_defined_node_summary_builder)
 {
+  struct MyAnnotation final : public loco::NodeAnnotation
+  {
+    // DO NOTHING
+  };
+
   auto g = loco::make_graph();
   {
     auto pull = g->nodes()->create<loco::Pull>();
@@ -51,14 +56,19 @@ TEST(LinearV1FormatterTest, user_defined_node_summary_builder)
 
     auto push = g->nodes()->create<loco::Push>();
 
+    push->annot(stdex::make_unique<MyAnnotation>());
     push->from(pull);
   }
 
   struct MyBuilder final : public locop::NodeSummaryBuilder
   {
-    bool build(const loco::Node *, locop::NodeSummary &s) const final
+    bool build(const loco::Node *node, locop::NodeSummary &s) const final
     {
       s.opname("my.op");
+      if (node->annot<MyAnnotation>())
+      {
+        s.comments().append("annotated");
+      }
       s.state(locop::NodeSummary::State::PartiallyKnown);
       return true;
     }
index 34afc80..3f88569 100644 (file)
 namespace locop
 {
 
+void NodeDesc::Comments::append(const std::string &s)
+{
+  // TODO Check whether s contains any newline character
+  _lines.emplace_back(s);
+}
+
 const std::string &NodeDesc::opname(void) const
 {
   // _name SHOULD BE set before use