[coco] Allow Op to have a parent Op (#1767)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 8 Oct 2018 01:54:18 +0000 (10:54 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 8 Oct 2018 01:54:18 +0000 (10:54 +0900)
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 <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Op.h
contrib/coco/core/include/coco/IR/Part.forward.h [new file with mode: 0644]
contrib/coco/core/include/coco/IR/Part.h [new file with mode: 0644]
contrib/coco/core/src/IR/Op.cpp
contrib/coco/core/src/IR/Part.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/Part.test.cpp [new file with mode: 0644]

index 5f31206..300a15e 100644 (file)
@@ -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 (file)
index 0000000..642ea56
--- /dev/null
@@ -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 (file)
index 0000000..72af217
--- /dev/null
@@ -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__
index dc671bc..98c21d9 100644 (file)
@@ -16,6 +16,7 @@
 
 #include "coco/IR/Op.h"
 #include "coco/IR/Step.h"
+#include "coco/IR/Part.h"
 
 #include <cassert>
 
@@ -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 (file)
index 0000000..bf68c1f
--- /dev/null
@@ -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 <cassert>
+
+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 (file)
index 0000000..e328111
--- /dev/null
@@ -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 <nncc/foundation/Memory.h>
+
+#include <gtest/gtest.h>
+
+using nncc::foundation::make_unique;
+
+namespace
+{
+namespace mock
+{
+
+struct Op final : public coco::Op
+{
+public:
+  Op() : _arg{this}
+  {
+    // DO NOTHING
+  }
+
+public:
+  std::set<coco::Object *> 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);
+}