From 16e0d3bad1f0b8577a905038d27f8d51671b16a7 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: Wed, 12 Jun 2019 13:48:13 +0900 Subject: [PATCH] [loco] Introduce CanonicalNode interface (#3743) This commit introduces CanonicalNode interface, and rewrites Forward node as an example. Signed-off-by: Jonghyun Park --- contrib/loco/include/loco/IR/CanonicalNode.h | 44 ++++++++++++++++++++++++++++ contrib/loco/include/loco/IR/Nodes.h | 3 +- contrib/loco/src/IR/CanonicalNode.cpp | 25 ++++++++++++++++ contrib/loco/src/IR/Nodes.test.cpp | 4 +++ 4 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 contrib/loco/include/loco/IR/CanonicalNode.h create mode 100644 contrib/loco/src/IR/CanonicalNode.cpp diff --git a/contrib/loco/include/loco/IR/CanonicalNode.h b/contrib/loco/include/loco/IR/CanonicalNode.h new file mode 100644 index 0000000..f1dd169 --- /dev/null +++ b/contrib/loco/include/loco/IR/CanonicalNode.h @@ -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 struct CanonicalNodeImpl : public CanonicalNode +{ + virtual ~CanonicalNodeImpl() = default; + + CanonicalOpcode opcode(void) const final { return Code; } +}; + +} // namespace loco + +#endif // __LOCO_IR_CANONICAL_NODE_H__ diff --git a/contrib/loco/include/loco/IR/Nodes.h b/contrib/loco/include/loco/IR/Nodes.h index 6094035..a332ea2 100644 --- a/contrib/loco/include/loco/IR/Nodes.h +++ b/contrib/loco/include/loco/IR/Nodes.h @@ -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> { public: Forward() = default; diff --git a/contrib/loco/src/IR/CanonicalNode.cpp b/contrib/loco/src/IR/CanonicalNode.cpp new file mode 100644 index 0000000..42e9cd1 --- /dev/null +++ b/contrib/loco/src/IR/CanonicalNode.cpp @@ -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 diff --git a/contrib/loco/src/IR/Nodes.test.cpp b/contrib/loco/src/IR/Nodes.test.cpp index 72d2486..5a04fbc 100644 --- a/contrib/loco/src/IR/Nodes.test.cpp +++ b/contrib/loco/src/IR/Nodes.test.cpp @@ -15,6 +15,7 @@ */ #include "loco/IR/Nodes.h" +#include "loco/IR/CanonicalDialect.h" #include @@ -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); } -- 2.7.4