[loco] Introduce IR verification infrastructure (#3526)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 20 May 2019 03:42:27 +0000 (12:42 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 20 May 2019 03:42:27 +0000 (12:42 +0900)
This commit introduces basic verifier infrastructure for loco.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/loco/include/loco/IR/Verifier.h [new file with mode: 0644]
contrib/loco/src/IR/Verifier.cpp [new file with mode: 0644]
contrib/loco/src/IR/Verifier.test.cpp [new file with mode: 0644]

diff --git a/contrib/loco/include/loco/IR/Verifier.h b/contrib/loco/include/loco/IR/Verifier.h
new file mode 100644 (file)
index 0000000..8ff85e1
--- /dev/null
@@ -0,0 +1,100 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LOCO_IR_VERIFIER_H__
+#define __LOCO_IR_VERIFIER_H__
+
+#include "loco/IR/Graph.h"
+
+#include <memory>
+
+namespace loco
+{
+
+/**
+ * @brief Possible error categories
+ *
+ * This enum class enumerates all the possible validation failure reasons.
+ *
+ * WARN DO NOT serialize this code. The tag value is subject to change.
+ */
+enum class ErrorCategory
+{
+  MissingArgument,
+  /* TO BE ADDED */
+};
+
+/**
+ * @brief The details of each error
+ */
+template <ErrorCategory Code> class ErrorDetail;
+
+/**
+ * @brief The details of MissingArgument error
+ */
+template <> class ErrorDetail<ErrorCategory::MissingArgument>
+{
+public:
+  ErrorDetail(loco::Node *node, uint32_t index) : _node{node}, _index{index}
+  {
+    // DO NOTHING
+  }
+
+public:
+  /// @brief The node with missing arguments
+  loco::Node *node(void) const { return _node; }
+  /// @brief The missing argument index
+  uint32_t index(void) const { return _index; }
+
+private:
+  loco::Node *_node;
+  uint32_t _index;
+};
+
+/**
+ * @brief Error listener interface
+ *
+ * DOo NOT inherit this interface. Use DefaultErrorListener instead.
+ */
+struct IErrorListener
+{
+  virtual ~IErrorListener() = default;
+
+  virtual void notify(const ErrorDetail<ErrorCategory::MissingArgument> &) = 0;
+};
+
+/**
+ * @brief Error listener (with default implementation)
+ */
+struct ErrorListener : public IErrorListener
+{
+  virtual ~ErrorListener() = default;
+
+  void notify(const ErrorDetail<ErrorCategory::MissingArgument> &) override { return; }
+};
+
+/**
+ * @brief Validate a loco graph
+ *
+ * "valid" returns true if a given graph has no error.
+ *
+ * NOTE Given a valid(non-null) listener, "valid" notifies error details to the listener.
+ */
+bool valid(Graph *g, std::unique_ptr<ErrorListener> &&l = nullptr);
+
+} // namespace loco
+
+#endif // __LOCO_IR_VERIFIER_H__
diff --git a/contrib/loco/src/IR/Verifier.cpp b/contrib/loco/src/IR/Verifier.cpp
new file mode 100644 (file)
index 0000000..42735a3
--- /dev/null
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "loco/IR/Verifier.h"
+
+#include <set>
+#include <cassert>
+
+namespace
+{
+
+using namespace loco;
+
+struct GraphVerifier final
+{
+public:
+  GraphVerifier(loco::Graph *graph) : _graph{graph}
+  {
+    // graph SHOULD NOT BE null
+    assert(_graph != nullptr);
+  }
+
+public:
+  // ErrorListener SHOULD outlive GraphVerifier
+  GraphVerifier &enroll(ErrorListener *l)
+  {
+    if (l != nullptr)
+    {
+      _listeners.insert(l);
+    }
+    return (*this);
+  }
+
+  GraphVerifier &enroll(std::unique_ptr<ErrorListener> &&l)
+  {
+    if (l != nullptr)
+    {
+      _listeners.insert(l.get());
+      // Take the ownership of a given listener
+      _owned_listeners.insert(std::move(l));
+    }
+    return (*this);
+  }
+
+public:
+  void run(void) const
+  {
+    for (auto node : loco::all_nodes(_graph))
+    {
+      // Verify nodes
+      for (uint32_t n = 0; n < node->arity(); ++n)
+      {
+        if (node->arg(n) == nullptr)
+        {
+          notify(ErrorDetail<ErrorCategory::MissingArgument>{node, n});
+        }
+      }
+    }
+  }
+
+private:
+  template <typename Error> void notify(const Error &error) const
+  {
+    for (const auto &listener : _listeners)
+    {
+      listener->notify(error);
+    }
+  }
+
+private:
+  loco::Graph *_graph = nullptr;
+
+  // All active error listeners
+  std::set<ErrorListener *> _listeners;
+
+  // Owned error listeners
+  std::set<std::unique_ptr<ErrorListener>> _owned_listeners;
+};
+
+inline GraphVerifier graph_verifier(loco::Graph *graph) { return GraphVerifier{graph}; }
+
+} // namespace
+
+namespace loco
+{
+
+bool valid(Graph *g, std::unique_ptr<ErrorListener> &&l)
+{
+  class ErrorCounter final : public ErrorListener
+  {
+  public:
+    uint32_t count(void) const { return _count; }
+
+  public:
+    void notify(const ErrorDetail<ErrorCategory::MissingArgument> &) { _count += 1; }
+
+  private:
+    uint32_t _count = 0;
+  };
+
+  ErrorCounter counter;
+  graph_verifier(g).enroll(&counter).enroll(std::move(l)).run();
+  return counter.count() == 0;
+}
+
+} // namespace loco
diff --git a/contrib/loco/src/IR/Verifier.test.cpp b/contrib/loco/src/IR/Verifier.test.cpp
new file mode 100644 (file)
index 0000000..3e04dd4
--- /dev/null
@@ -0,0 +1,73 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "loco/IR/Verifier.h"
+
+#include <gtest/gtest.h>
+
+#include <vector>
+
+// Use "stdex" later
+namespace
+{
+
+template <typename T, typename... Args> std::unique_ptr<T> make_unique(Args &&... args)
+{
+  // NOTE std::make_unique is missing in C++11 standard
+  return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
+}
+
+} // namespace
+
+TEST(VerifierTest, valid_minimal)
+{
+  auto g = loco::make_graph();
+  auto push = g->nodes()->create<loco::Push>();
+
+  ASSERT_FALSE(loco::valid(g.get()));
+}
+
+TEST(VerifierTest, valid_error_reporter)
+{
+  using namespace loco;
+
+  auto g = loco::make_graph();
+  auto push = g->nodes()->create<loco::Push>();
+
+  class Collector final : public loco::ErrorListener
+  {
+  public:
+    Collector(std::vector<ErrorDetail<ErrorCategory::MissingArgument>> *out) : _out{out}
+    {
+      // DO NOTHING
+    }
+
+  public:
+    void notify(const ErrorDetail<ErrorCategory::MissingArgument> &d) override
+    {
+      _out->emplace_back(d);
+    }
+
+  private:
+    std::vector<ErrorDetail<ErrorCategory::MissingArgument>> *_out;
+  };
+
+  std::vector<ErrorDetail<ErrorCategory::MissingArgument>> errors;
+  ASSERT_FALSE(loco::valid(g.get(), make_unique<Collector>(&errors)));
+  ASSERT_EQ(errors.size(), 1);
+  ASSERT_EQ(errors.at(0).node(), push);
+  ASSERT_EQ(errors.at(0).index(), 0);
+}