[loco] Introduce CanonicalNode interface (#3743)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 12 Jun 2019 04:48:13 +0000 (13:48 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 12 Jun 2019 04:48:13 +0000 (13:48 +0900)
This commit introduces CanonicalNode interface, and rewrites Forward
node as an example.

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

diff --git a/contrib/loco/include/loco/IR/CanonicalNode.h b/contrib/loco/include/loco/IR/CanonicalNode.h
new file mode 100644 (file)
index 0000000..f1dd169
--- /dev/null
@@ -0,0 +1,44 @@
+/*
+ * 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_CANONICAL_NODE_H__
+#define __LOCO_IR_CANONICAL_NODE_H__
+
+#include "loco/IR/Node.h"
+#include "loco/IR/Dialect.h"
+#include "loco/IR/CanonicalOpcode.h"
+
+namespace loco
+{
+
+struct CanonicalNode : public Node
+{
+  virtual ~CanonicalNode() = default;
+
+  Dialect *dialect(void) const final;
+  virtual CanonicalOpcode opcode(void) const = 0;
+};
+
+template <CanonicalOpcode Code> struct CanonicalNodeImpl : public CanonicalNode
+{
+  virtual ~CanonicalNodeImpl() = default;
+
+  CanonicalOpcode opcode(void) const final { return Code; }
+};
+
+} // namespace loco
+
+#endif // __LOCO_IR_CANONICAL_NODE_H__
index 6094035..a332ea2 100644 (file)
@@ -28,6 +28,7 @@
 #include "loco/IR/FeatureCodec.h"
 #include "loco/IR/FilterCodec.h"
 #include "loco/IR/NodeMixins.h"
+#include "loco/IR/CanonicalNode.h"
 
 namespace loco
 {
@@ -62,7 +63,7 @@ public:
  *
  * This node may encode memory transfer (such as CPU -> GPU or GPU -> CPU)
  */
-class Forward final : public FixedArityNode<1, Node>
+class Forward final : public FixedArityNode<1, CanonicalNodeImpl<CanonicalOpcode::Forward>>
 {
 public:
   Forward() = default;
diff --git a/contrib/loco/src/IR/CanonicalNode.cpp b/contrib/loco/src/IR/CanonicalNode.cpp
new file mode 100644 (file)
index 0000000..42e9cd1
--- /dev/null
@@ -0,0 +1,25 @@
+/*
+ * 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/CanonicalNode.h"
+#include "loco/IR/CanonicalDialect.h"
+
+namespace loco
+{
+
+Dialect *CanonicalNode::dialect(void) const { return CanonicalDialect::get(); }
+
+} // namespace loco
index 72d2486..5a04fbc 100644 (file)
@@ -15,6 +15,7 @@
  */
 
 #include "loco/IR/Nodes.h"
+#include "loco/IR/CanonicalDialect.h"
 
 #include <gtest/gtest.h>
 
@@ -67,6 +68,9 @@ TEST(ForwardTest, constructor)
 {
   loco::Forward forward_node;
 
+  ASSERT_EQ(forward_node.dialect(), loco::CanonicalDialect::get());
+  ASSERT_EQ(forward_node.opcode(), loco::CanonicalOpcode::Forward);
+
   ASSERT_EQ(forward_node.input(), nullptr);
 }