[coco] Update OpLink on UnitF::op update (#939)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 9 Aug 2018 22:59:39 +0000 (07:59 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 9 Aug 2018 22:59:39 +0000 (07:59 +0900)
This commit revises UnitF::op method to update OpLink properly.

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

index 9d5c93c..af9fcd5 100644 (file)
@@ -185,7 +185,8 @@ namespace coco
 class UnitF final : public FeatureInstr
 {
 public:
-  UnitF(const PtrLink<Instr, Block> *link) : _link{link}, _op{nullptr}
+  UnitF(PtrLink<Op, Instr> *op_link, const PtrLink<Instr, Block> *link)
+      : _op_link{op_link}, _link{link}, _op{nullptr}
   {
     // DO NOTHING
   }
@@ -199,6 +200,7 @@ public:
   std::set<Bag *> updates(void) const override;
 
 private:
+  PtrLink<Op, Instr> *const _op_link;
   const PtrLink<Instr, Block> *const _link;
 
 private:
@@ -211,11 +213,7 @@ public:
   Op *op(void) const { return _op; }
 
 public:
-  void op(Op *op)
-  {
-    // TODO Update Op-Instr relation
-    _op = op;
-  }
+  void op(Op *op);
 };
 
 } // namespace coco
index 056dc02..4bb1b60 100644 (file)
@@ -33,6 +33,8 @@ void FeatureInstr::ofm(FeatureObject *ofm)
 //
 #include "coco/IR/Op.h"
 
+#include <cassert>
+
 namespace
 {
 std::set<coco::Bag *> &operator+=(std::set<coco::Bag *> &res, const coco::Object *o)
@@ -74,4 +76,23 @@ std::set<Bag *> UnitF::updates(void) const
   return res;
 }
 
+void UnitF::op(Op *op)
+{
+  if (_op != nullptr)
+  {
+    assert(_op_link->find(_op) == this);
+    _op_link->unset(_op);
+    _op = nullptr;
+  }
+
+  assert(_op == nullptr);
+
+  if (op != nullptr)
+  {
+    assert(_op_link->find(op) == nullptr);
+    _op = op;
+    _op_link->set(_op, this);
+  }
+}
+
 } // namespace coco
index 9a20ec1..4de57d9 100644 (file)
@@ -88,8 +88,9 @@ struct UnitFMutator : public coco::Instr::DefaultMutator
 
 TEST(IR_UNIT_F, ctor)
 {
+  coco::PtrLink<coco::Op, coco::Instr> op_link;
   coco::PtrLink<coco::Instr, coco::Block> link;
-  coco::UnitF ins{&link};
+  coco::UnitF ins{&op_link, &link};
 
   ASSERT_EQ(ins.op(), nullptr);
   ASSERT_TRUE(ins.reads().empty());
@@ -98,8 +99,9 @@ TEST(IR_UNIT_F, ctor)
 
 TEST(IR_UNIT_F, asUnitF)
 {
+  coco::PtrLink<coco::Op, coco::Instr> op_link;
   coco::PtrLink<coco::Instr, coco::Block> link;
-  coco::UnitF ins{&link};
+  coco::UnitF ins{&op_link, &link};
 
   coco::Instr *mutable_ptr = &ins;
   const coco::Instr *immutable_ptr = &ins;
@@ -120,16 +122,22 @@ TEST(IR_UNIT_F, op_update)
   // Test UnitF
   coco::PtrLink<coco::Instr, coco::Block> link;
 
-  coco::UnitF ins{&link};
+  coco::UnitF ins{&op_link, &link};
 
   ins.op(op);
   ASSERT_EQ(ins.op(), op);
+  ASSERT_EQ(op_link.find(op), &ins);
+
+  ins.op(nullptr);
+  ASSERT_EQ(ins.op(), nullptr);
+  ASSERT_EQ(op_link.find(op), nullptr);
 }
 
 TEST(IR_UNIT_F, visitor)
 {
+  coco::PtrLink<coco::Op, coco::Instr> op_link;
   coco::PtrLink<coco::Instr, coco::Block> link;
-  coco::UnitF ins{&link};
+  coco::UnitF ins{&op_link, &link};
 
   coco::Instr *mutable_ptr = &ins;
   const coco::Instr *immutable_ptr = &ins;
@@ -140,8 +148,9 @@ TEST(IR_UNIT_F, visitor)
 
 TEST(IR_UNIT_F, mutator)
 {
+  coco::PtrLink<coco::Op, coco::Instr> op_link;
   coco::PtrLink<coco::Instr, coco::Block> link;
-  coco::UnitF ins{&link};
+  coco::UnitF ins{&op_link, &link};
 
   UnitFMutator m{};
 
index f4f4755..3e40366 100644 (file)
@@ -9,7 +9,7 @@ namespace coco
 
 template <> UnitF *InstrManager::create(void)
 {
-  return take(nncc::foundation::make_unique<UnitF>(_instr_link));
+  return take(nncc::foundation::make_unique<UnitF>(_op_link, _instr_link));
 }
 
 } // namespace coco