[coco] Introduce root helper (#2489)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 4 Dec 2018 09:44:07 +0000 (18:44 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 4 Dec 2018 09:44:07 +0000 (18:44 +0900)
This commit introduces root helper which allows users to easily access the
root Op for a given Op.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Op.h
contrib/coco/core/src/IR/Op.cpp
contrib/coco/core/src/IR/Ops.test.cpp

index d21ece5..0b8fdd7 100644 (file)
@@ -250,6 +250,13 @@ private:
   Part _right;
 };
 
+/**
+ * @brief Return the root Op from a given Op node
+ *
+ * @note root(op) == op holds for a root op
+ */
+Op *root(Op *);
+
 } // namespace coco
 
 #endif // __COCO_IR_OP_H__
index c320608..bcc65cf 100644 (file)
@@ -138,4 +138,16 @@ std::set<Object *> BinaryOp::uses(void) const
   return res;
 }
 
+//
+// Additional Helpers
+//
+Op *root(Op *cur)
+{
+  while (cur->up())
+  {
+    cur = cur->up();
+  }
+  return cur;
+}
+
 } // namespace coco
index ebd1455..d5f4192 100644 (file)
@@ -75,3 +75,48 @@ TEST(MulTest, constructor)
   ASSERT_EQ(op->left(), nullptr);
   ASSERT_EQ(op->right(), nullptr);
 }
+
+/**
+ * Section: Op Helpers
+ */
+namespace
+{
+
+class OpHelperTest : public ::testing::Test
+{
+public:
+  OpHelperTest()
+  {
+    // DO NOTHING
+  }
+
+protected:
+  template <typename Op> Op *allocate(void)
+  {
+    auto op = new Op;
+    _allocated.emplace_back(op);
+    return op;
+  }
+
+protected:
+  coco::ObjectManager obj_mgr;
+
+private:
+  std::vector<std::unique_ptr<coco::Op>> _allocated;
+};
+
+} // namespace
+
+TEST_F(OpHelperTest, root)
+{
+  auto load = allocate<coco::Load>();
+
+  ASSERT_EQ(root(load), load);
+
+  auto avgpool = allocate<coco::AvgPool2D>();
+
+  avgpool->arg(load);
+
+  ASSERT_EQ(root(load), avgpool);
+  ASSERT_EQ(root(avgpool), avgpool);
+}