[coco] Introduce Eval instruction (#1772)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Mon, 8 Oct 2018 04:50:50 +0000 (13:50 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Mon, 8 Oct 2018 04:50:50 +0000 (13:50 +0900)
This commit introduces Eval instruction as a mean to integrate tree-like
Op structure into coco IR.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Eval.h [new file with mode: 0644]
contrib/coco/core/include/coco/IR/Instr.lst
contrib/coco/core/include/coco/IR/InstrManager.h
contrib/coco/core/src/IR/Eval.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/Eval.test.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/InstrManager.cpp

diff --git a/contrib/coco/core/include/coco/IR/Eval.h b/contrib/coco/core/include/coco/IR/Eval.h
new file mode 100644 (file)
index 0000000..47aa29a
--- /dev/null
@@ -0,0 +1,61 @@
+/*
+ * 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_EVAL_H__
+#define __COCO_IR_EVAL_H__
+
+#include "coco/IR/Instr.h"
+#include "coco/IR/Def.h"
+#include "coco/IR/Step.h"
+
+namespace coco
+{
+
+/**
+ * @brief Evaluate an Object from a given Op
+ */
+class Eval final : public Instr, public Object::Producer
+{
+public:
+  explicit Eval();
+
+public:
+  Eval *asEval(void) override { return this; }
+  const Eval *asEval(void) const override { return this; }
+
+public:
+  std::set<Bag *> reads(void) const override;
+  std::set<Bag *> updates(void) const override;
+
+public:
+  Instr *loc(void) override { return this; }
+
+public:
+  Object *out(void) const { return _out.value(); }
+  void out(Object *obj) { _out.value(obj); }
+
+public:
+  Op *op(void) const { return _step.op(); }
+  void op(Op *op) { _step.op(op); }
+
+private:
+  Def _out;
+  Step _step;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_EVAL_H__
index ca48814..c7316b6 100644 (file)
@@ -4,6 +4,7 @@
 
 // INSTR(Name)
 
+INSTR(Eval)
 INSTR(UnitF)
 INSTR(Shuffle)
 INSTR(Copy)
index fb84f2a..f006faa 100644 (file)
@@ -18,6 +18,7 @@
 #define __COCO_IR_INSTR_MANAGER_H__
 
 #include "coco/IR/Instr.h"
+#include "coco/IR/Eval.h"
 #include "coco/IR/UnitF.h"
 #include "coco/IR/Shuffle.h"
 #include "coco/IR/Copy.h"
diff --git a/contrib/coco/core/src/IR/Eval.cpp b/contrib/coco/core/src/IR/Eval.cpp
new file mode 100644 (file)
index 0000000..f6a51df
--- /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/Eval.h"
+#include "coco/IR/Op.h"
+
+#include <cassert>
+
+namespace
+{
+std::set<coco::Bag *> &operator+=(std::set<coco::Bag *> &res, const coco::Object *o)
+{
+  if (o != nullptr && o->bag() != nullptr)
+  {
+    res.insert(o->bag());
+  }
+  return res;
+}
+} // namespace
+
+namespace coco
+{
+
+Eval::Eval() : _out{this}
+{
+  // DO NOTHING
+}
+
+std::set<Bag *> Eval::reads(void) const
+{
+  std::set<Bag *> res;
+
+  if (auto e = op())
+  {
+    for (auto obj : e->uses())
+    {
+      res += obj;
+    }
+  }
+
+  return res;
+}
+
+std::set<Bag *> Eval::updates(void) const
+{
+  std::set<Bag *> res;
+
+  res += out();
+
+  return res;
+}
+
+} // namespace coco
diff --git a/contrib/coco/core/src/IR/Eval.test.cpp b/contrib/coco/core/src/IR/Eval.test.cpp
new file mode 100644 (file)
index 0000000..98dfa6d
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * 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/Eval.h"
+#include "coco/IR/ObjectManager.h"
+#include "coco/IR/OpManager.h"
+
+#include <gtest/gtest.h>
+
+namespace
+{
+class EvalTest : public ::testing::Test
+{
+public:
+  virtual ~EvalTest() = default;
+
+protected:
+  coco::Eval *allocate(void)
+  {
+    auto ins = new coco::Eval{};
+    _allocated.emplace_back(ins);
+    return ins;
+  }
+
+private:
+  std::vector<std::unique_ptr<coco::Instr>> _allocated;
+};
+} // namespace
+
+TEST_F(EvalTest, constructor)
+{
+  auto ins = allocate();
+
+  ASSERT_EQ(ins->out(), nullptr);
+  ASSERT_EQ(ins->op(), nullptr);
+  ASSERT_TRUE(ins->reads().empty());
+  ASSERT_TRUE(ins->updates().empty());
+}
+
+TEST_F(EvalTest, asEval)
+{
+  auto ins = allocate();
+
+  coco::Instr *mutable_ptr = ins;
+  const coco::Instr *immutable_ptr = ins;
+
+  ASSERT_NE(mutable_ptr->asEval(), nullptr);
+  ASSERT_EQ(mutable_ptr->asEval(), immutable_ptr->asEval());
+}
index b702a1e..cbd2aae 100644 (file)
 namespace coco
 {
 
+template <> Eval *InstrManager::create(void)
+{
+  auto ins = nncc::foundation::make_unique<Eval>();
+  modulize(ins.get());
+  return take(std::move(ins));
+}
+
 template <> UnitF *InstrManager::create(void)
 {
   auto ins = nncc::foundation::make_unique<UnitF>();