From 7574ab4a61d522d7ff163c7bc0483f74bf44fbe5 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Mon, 8 Oct 2018 10:54:18 +0900 Subject: [PATCH] [coco] Allow Op to have a parent Op (#1767) This commit allows each Op to have a parent Op as the first step to support tree-like Op structure. Signed-off-by: Jonghyun Park --- contrib/coco/core/include/coco/IR/Op.h | 11 ++++ contrib/coco/core/include/coco/IR/Part.forward.h | 27 ++++++++++ contrib/coco/core/include/coco/IR/Part.h | 53 +++++++++++++++++++ contrib/coco/core/src/IR/Op.cpp | 11 ++++ contrib/coco/core/src/IR/Part.cpp | 45 ++++++++++++++++ contrib/coco/core/src/IR/Part.test.cpp | 66 ++++++++++++++++++++++++ 6 files changed, 213 insertions(+) create mode 100644 contrib/coco/core/include/coco/IR/Part.forward.h create mode 100644 contrib/coco/core/include/coco/IR/Part.h create mode 100644 contrib/coco/core/src/IR/Part.cpp create mode 100644 contrib/coco/core/src/IR/Part.test.cpp diff --git a/contrib/coco/core/include/coco/IR/Op.h b/contrib/coco/core/include/coco/IR/Op.h index 5f31206..300a15e 100644 --- a/contrib/coco/core/include/coco/IR/Op.h +++ b/contrib/coco/core/include/coco/IR/Op.h @@ -20,6 +20,7 @@ #include "coco/IR/Object.forward.h" #include "coco/IR/Instr.forward.h" #include "coco/IR/Step.forward.h" +#include "coco/IR/Part.forward.h" #include "coco/IR/Entity.h" #include "coco/ADT/PtrLink.h" @@ -41,6 +42,7 @@ namespace coco struct Op : public Entity { friend class Step; + friend class Part; virtual ~Op() = default; @@ -94,11 +96,20 @@ struct Op : public Entity public: Instr *parent(void) const; + // @brief Return a pointer to the parent Op + Op *up(void) const; + private: // @brief A link to Instr from Op // // WARN Update this field only through Step Step *_step = nullptr; + + // @brief A link to a parent Op + // + // WARN Update this field only through Part + // NOTE An "Op" CANNOT have a link to a parent Op if it is linked to an "Instr" + Part *_part = nullptr; }; } // namespace coco diff --git a/contrib/coco/core/include/coco/IR/Part.forward.h b/contrib/coco/core/include/coco/IR/Part.forward.h new file mode 100644 index 0000000..642ea56 --- /dev/null +++ b/contrib/coco/core/include/coco/IR/Part.forward.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2018 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 __COCO_IR_PART_FORWARD_H__ +#define __COCO_IR_PART_FORWARD_H__ + +namespace coco +{ + +class Part; + +} // namespace coco + +#endif // __COCO_IR_PART_FORWARD_H__ diff --git a/contrib/coco/core/include/coco/IR/Part.h b/contrib/coco/core/include/coco/IR/Part.h new file mode 100644 index 0000000..72af217 --- /dev/null +++ b/contrib/coco/core/include/coco/IR/Part.h @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2018 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 __COCO_IR_PART_H__ +#define __COCO_IR_PART_H__ + +#include "coco/IR/Op.forward.h" + +namespace coco +{ + +/** + * @brief A Part represents the edge between a child Op and its parent Op + */ +class Part final +{ +public: + Part(Op *parent) : _parent{parent} + { + // DO NOTHING + } + +public: + ~Part() { child(nullptr); } + +public: + Op *child(void) const { return _child; } + void child(Op *c); + +public: + Op *parent(void) const { return _parent; } + +private: + Op *_parent = nullptr; + Op *_child = nullptr; +}; + +} // namespace coco + +#endif // __COCO_IR_PART_H__ diff --git a/contrib/coco/core/src/IR/Op.cpp b/contrib/coco/core/src/IR/Op.cpp index dc671bc..98c21d9 100644 --- a/contrib/coco/core/src/IR/Op.cpp +++ b/contrib/coco/core/src/IR/Op.cpp @@ -16,6 +16,7 @@ #include "coco/IR/Op.h" #include "coco/IR/Step.h" +#include "coco/IR/Part.h" #include @@ -30,4 +31,14 @@ Instr *Op::parent(void) const } return nullptr; } + +Op *Op::up(void) const +{ + if (_part) + { + assert(_part->parent() != nullptr); + return _part->parent(); + } + return nullptr; +} } // namespace coco diff --git a/contrib/coco/core/src/IR/Part.cpp b/contrib/coco/core/src/IR/Part.cpp new file mode 100644 index 0000000..bf68c1f --- /dev/null +++ b/contrib/coco/core/src/IR/Part.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2018 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 "coco/IR/Part.h" +#include "coco/IR/Op.h" + +#include + +namespace coco +{ + +void Part::child(Op *c) +{ + if (_child != nullptr) + { + assert(_child->_part == this); + _child->_part = nullptr; + _child = nullptr; + } + + assert(_child == nullptr); + + if (c != nullptr) + { + assert(c->_part == nullptr); + assert(c->_step == nullptr); + _child = c; + _child->_part = this; + } +} + +} // namespace coco diff --git a/contrib/coco/core/src/IR/Part.test.cpp b/contrib/coco/core/src/IR/Part.test.cpp new file mode 100644 index 0000000..e328111 --- /dev/null +++ b/contrib/coco/core/src/IR/Part.test.cpp @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2018 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 "coco/IR/Part.h" +#include "coco/IR/Op.h" + +#include + +#include + +using nncc::foundation::make_unique; + +namespace +{ +namespace mock +{ + +struct Op final : public coco::Op +{ +public: + Op() : _arg{this} + { + // DO NOTHING + } + +public: + std::set uses() const override { throw std::runtime_error{"Not supported"}; } + +public: + ::coco::Op *arg(void) const { return _arg.child(); } + void arg(::coco::Op *child) { _arg.child(child); } + +private: + coco::Part _arg; +}; + +} // namespace mock +} // namespace + +TEST(PartTest, destructor) +{ + auto parent = make_unique<::mock::Op>(); + auto child = make_unique<::mock::Op>(); + + parent->arg(child.get()); + ASSERT_EQ(parent->arg(), child.get()); + ASSERT_EQ(child->up(), parent.get()); + + parent.reset(); + + // NOTE parent SHOULD unlink itself from child on destruction + ASSERT_EQ(child->up(), nullptr); +} -- 2.7.4