From 18635831ea86df4d3854f8732efe688417e4a3b7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=B2=9C=EA=B5=90/On-Device=20Lab=28SR=29/Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Wed, 6 Nov 2019 19:15:56 +0900 Subject: [PATCH] [exo] Introduce Circle dialect (#8802) This commit introduces structure for Circle dialect for loco ecosystem Signed-off-by: Cheongyo Bahk --- compiler/exo/src/Dialect/IR/CircleDialect.cpp | 28 +++++++ compiler/exo/src/Dialect/IR/CircleDialect.h | 40 ++++++++++ compiler/exo/src/Dialect/IR/CircleDialect.test.cpp | 31 ++++++++ compiler/exo/src/Dialect/IR/CircleNode.cpp | 26 +++++++ compiler/exo/src/Dialect/IR/CircleNode.h | 23 ++++++ compiler/exo/src/Dialect/IR/CircleNodeDecl.h | 50 +++++++++++++ compiler/exo/src/Dialect/IR/CircleNodeImpl.h | 69 +++++++++++++++++ .../exo/src/Dialect/IR/CircleNodeVisitor.forward.h | 30 ++++++++ compiler/exo/src/Dialect/IR/CircleNodeVisitor.h | 86 ++++++++++++++++++++++ compiler/exo/src/Dialect/IR/CircleNodes.h | 32 ++++++++ compiler/exo/src/Dialect/IR/CircleNodes.lst | 9 +++ compiler/exo/src/Dialect/IR/CircleOpcode.h | 32 ++++++++ 12 files changed, 456 insertions(+) create mode 100644 compiler/exo/src/Dialect/IR/CircleDialect.cpp create mode 100644 compiler/exo/src/Dialect/IR/CircleDialect.h create mode 100644 compiler/exo/src/Dialect/IR/CircleDialect.test.cpp create mode 100644 compiler/exo/src/Dialect/IR/CircleNode.cpp create mode 100644 compiler/exo/src/Dialect/IR/CircleNode.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodeDecl.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodeImpl.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodeVisitor.forward.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodeVisitor.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodes.h create mode 100644 compiler/exo/src/Dialect/IR/CircleNodes.lst create mode 100644 compiler/exo/src/Dialect/IR/CircleOpcode.h diff --git a/compiler/exo/src/Dialect/IR/CircleDialect.cpp b/compiler/exo/src/Dialect/IR/CircleDialect.cpp new file mode 100644 index 0000000..ecd43b0 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleDialect.cpp @@ -0,0 +1,28 @@ +/* + * 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 "CircleDialect.h" + +namespace locoex +{ + +loco::Dialect *CircleDialect::get(void) +{ + static CircleDialect d; + return &d; +} + +} // namespace locoex diff --git a/compiler/exo/src/Dialect/IR/CircleDialect.h b/compiler/exo/src/Dialect/IR/CircleDialect.h new file mode 100644 index 0000000..9857d9e --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleDialect.h @@ -0,0 +1,40 @@ +/* + * 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 __LOCOEX_IR_CIRCLEDIALECT_H__ +#define __LOCOEX_IR_CIRCLEDIALECT_H__ + +#include + +namespace locoex +{ + +class CircleDialect final : public loco::Dialect +{ +private: + CircleDialect() = default; + +public: + CircleDialect(const CircleDialect &) = delete; + CircleDialect(CircleDialect &&) = delete; + +public: + static loco::Dialect *get(void); +}; + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLEDIALECT_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleDialect.test.cpp b/compiler/exo/src/Dialect/IR/CircleDialect.test.cpp new file mode 100644 index 0000000..6132eb3 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleDialect.test.cpp @@ -0,0 +1,31 @@ +/* + * 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 "CircleDialect.h" + +#include + +TEST(CircleDialectTest, get) +{ + using locoex::CircleDialect; + + auto d = CircleDialect::get(); + + // get() SHOULD return a valid(non-null) pointer + ASSERT_NE(d, nullptr); + // The return value SHOULD be stable across multiple invocations + ASSERT_EQ(d, CircleDialect::get()); +} diff --git a/compiler/exo/src/Dialect/IR/CircleNode.cpp b/compiler/exo/src/Dialect/IR/CircleNode.cpp new file mode 100644 index 0000000..cdcd434 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNode.cpp @@ -0,0 +1,26 @@ +/* + * 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 "CircleNode.h" + +#include "CircleDialect.h" + +namespace locoex +{ + +const loco::Dialect *CircleNode::dialect(void) const { return CircleDialect::get(); } + +} // namespace locoex diff --git a/compiler/exo/src/Dialect/IR/CircleNode.h b/compiler/exo/src/Dialect/IR/CircleNode.h new file mode 100644 index 0000000..1ae9d38 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNode.h @@ -0,0 +1,23 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODE_H__ +#define __LOCOEX_IR_CIRCLENODE_H__ + +#include "CircleNodeDecl.h" +#include "CircleNodeImpl.h" + +#endif // __LOCOEX_IR_CIRCLENODE_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodeDecl.h b/compiler/exo/src/Dialect/IR/CircleNodeDecl.h new file mode 100644 index 0000000..358b1f0 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodeDecl.h @@ -0,0 +1,50 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODEDECL_H__ +#define __LOCOEX_IR_CIRCLENODEDECL_H__ + +#include +#include + +#include "CircleOpcode.h" +#include "CircleNodeVisitor.forward.h" + +namespace locoex +{ + +struct CircleNode : public loco::Node +{ + virtual ~CircleNode() = default; + + const loco::Dialect *dialect(void) const final; + virtual CircleOpcode opcode(void) const = 0; + + template T accept(CircleNodeVisitorBase *) const; + template T accept(CircleNodeMutableVisitorBase *); +}; + +template struct CircleNodeImpl : public CircleNode +{ + virtual ~CircleNodeImpl() = default; + + uint32_t opnum(void) const final { return static_cast(Code); } + CircleOpcode opcode(void) const final { return Code; } +}; + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLENODEDECL_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodeImpl.h b/compiler/exo/src/Dialect/IR/CircleNodeImpl.h new file mode 100644 index 0000000..d3d33b1 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodeImpl.h @@ -0,0 +1,69 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODEIMPL_H__ +#define __LOCOEX_IR_CIRCLENODEIMPL_H__ + +#include "CircleNode.h" +#include "CircleNodes.h" +#include "CircleNodeVisitor.h" + +#include + +namespace locoex +{ + +template T CircleNode::accept(CircleNodeVisitorBase *v) const +{ + switch (this->opcode()) + { +#define CIRCLE_NODE(OPCODE, CLASS) \ + \ + case CircleOpcode::OPCODE: \ + return v->visit(dynamic_cast(this)); + +#include "CircleNodes.lst" +#undef CIRCLE_NODE + + default: + break; + } + + assert(false); +} + +template T CircleNode::accept(CircleNodeMutableVisitorBase *v) +{ + switch (this->opcode()) + { +#define CIRCLE_NODE(OPCODE, CLASS) \ + \ + case CircleOpcode::OPCODE: \ + return v->visit(dynamic_cast(this)); + +#include "CircleNodes.lst" +#undef CIRCLE_NODE + + default: + break; + } + + assert(false); +} + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLENODEIMPL_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodeVisitor.forward.h b/compiler/exo/src/Dialect/IR/CircleNodeVisitor.forward.h new file mode 100644 index 0000000..8ae28ab --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodeVisitor.forward.h @@ -0,0 +1,30 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODE_VISITOR_FORWARD_H__ +#define __LOCOEX_IR_CIRCLENODE_VISITOR_FORWARD_H__ + +namespace locoex +{ + +// NOTE These forward declarations SHOULD BE aligned with Node delcarations in +// "CircleNodeVisitor.h" +template struct CircleNodeVisitorBase; +template struct CircleNodeMutableVisitorBase; + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLENODE_VISITOR_FORWARD_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodeVisitor.h b/compiler/exo/src/Dialect/IR/CircleNodeVisitor.h new file mode 100644 index 0000000..99dd366 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodeVisitor.h @@ -0,0 +1,86 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODE_VISITOR_H__ +#define __LOCOEX_IR_CIRCLENODE_VISITOR_H__ + +#include "CircleNode.h" +#include "CircleNodes.h" + +#include + +namespace locoex +{ + +/** + * DO NOT use this class. Use CircleNodeVisitor instead. + */ +template struct CircleNodeVisitorBase +{ + virtual ~CircleNodeVisitorBase() = default; + +#define CIRCLE_NODE(OPCODE, Circle_CLASS) virtual T visit(const Circle_CLASS *) = 0; + +#include "CircleNodes.lst" +#undef CIRCLE_NODE +}; + +template struct CircleNodeVisitor : public CircleNodeVisitorBase +{ + virtual ~CircleNodeVisitor() = default; + +#define CIRCLE_NODE(OPCODE, Circle_CLASS) \ + \ + virtual T visit(const Circle_CLASS *node) { return visit(static_cast(node)); } + +#include "CircleNodes.lst" +#undef CIRCLE_NODE + + /// @brief Default fallback + virtual T visit(const CircleNode *) { assert(false); } +}; + +/** + * DO NOT use this class. Use CircleNodeMutableVisitor instead. + */ +template struct CircleNodeMutableVisitorBase +{ + virtual ~CircleNodeMutableVisitorBase() = default; + +#define CIRCLE_NODE(OPCODE, Circle_CLASS) virtual T visit(Circle_CLASS *) = 0; + +#include "CircleNodes.lst" +#undef CIRCLE_NODE +}; + +template struct CircleNodeMutableVisitor : public CircleNodeMutableVisitorBase +{ + virtual ~CircleNodeMutableVisitor() = default; + +#define CIRCLE_NODE(OPCODE, Circle_CLASS) \ + \ + virtual T visit(Circle_CLASS *node) { return visit(static_cast(node)); } + +#include "CircleNodes.lst" +#undef CIRCLE_NODE + + /// @brief Default fallback + virtual T visit(CircleNode *) { assert(false); } +}; + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLENODE_VISITOR_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodes.h b/compiler/exo/src/Dialect/IR/CircleNodes.h new file mode 100644 index 0000000..d97ef31 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodes.h @@ -0,0 +1,32 @@ +/* + * 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 __LOCOEX_IR_CIRCLENODES_H__ +#define __LOCOEX_IR_CIRCLENODES_H__ + +#include "CircleNodeDecl.h" +#include "CircleOpcode.h" + +#include + +namespace locoex +{ + +// TODO Add CircleNodes + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLENODES_H__ diff --git a/compiler/exo/src/Dialect/IR/CircleNodes.lst b/compiler/exo/src/Dialect/IR/CircleNodes.lst new file mode 100644 index 0000000..cd74559 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleNodes.lst @@ -0,0 +1,9 @@ +#ifndef CIRCLE_NODE +#error "Define CIRCLE_NODE" +#endif // CIRCLE_NODE + +// +// PLEASE SORT NODE DECLS IN ALPHABETICAL ORDER +// +// e.g.) +// CIRCLE_NODE(INSTANCE_NORM, locoex::CircleInstanceNorm) diff --git a/compiler/exo/src/Dialect/IR/CircleOpcode.h b/compiler/exo/src/Dialect/IR/CircleOpcode.h new file mode 100644 index 0000000..2643040 --- /dev/null +++ b/compiler/exo/src/Dialect/IR/CircleOpcode.h @@ -0,0 +1,32 @@ +/* + * 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 __LOCOEX_IR_CIRCLEOPCODE_H__ +#define __LOCOEX_IR_CIRCLEOPCODE_H__ + +namespace locoex +{ + +enum class CircleOpcode +{ +#define CIRCLE_NODE(OPCODE, CLASS) OPCODE, +#include "CircleNodes.lst" +#undef CIRCLE_NODE +}; + +} // namespace locoex + +#endif // __LOCOEX_IR_CIRCLEOPCODE_H__ -- 2.7.4