[coco] Introduce Copy instruction (#1615)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 21 Sep 2018 04:08:43 +0000 (13:08 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 21 Sep 2018 04:08:43 +0000 (13:08 +0900)
This commit introduces Copy instruction as a mean to reduce compile
time.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/coco/core/include/coco/IR/Copy.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/Copy.cpp [new file with mode: 0644]
contrib/coco/core/src/IR/InstrManager.cpp

diff --git a/contrib/coco/core/include/coco/IR/Copy.h b/contrib/coco/core/include/coco/IR/Copy.h
new file mode 100644 (file)
index 0000000..6343fef
--- /dev/null
@@ -0,0 +1,64 @@
+#ifndef __COCO_IR_COPY_H__
+#define __COCO_IR_COPY_H__
+
+#include "coco/IR/Instr.h"
+
+#include "coco/IR/Object.h"
+#include "coco/IR/Def.h"
+#include "coco/IR/Use.h"
+
+namespace coco
+{
+
+/**
+ * @brief Index-wise element transfer between two objects
+ *
+ * Given two objects "src" and "dst" of the same kind/shape, "copy(src, dst)"
+ * denotes index-wise element transfers.
+ *
+ * For example, the following pseudo-code describes "copy(src, dat)"
+ * when both src and dst are a feature map of the shape B x C x H x W:
+ *
+ * for each valid index b, ch, row, col:
+ *   load the "src->at(b, ch, row, col)"-th element from bag(src)
+ *   store it as the "dst->at(b, ch, row, col)"-th element of bag(dst)
+ *
+ * In principle, "copy" is unnecessary as it is always possible to rewrite "copy"
+ * as a "shuffle". However, "shuffle"-based optimization is too heavy as it requires
+ * much of iterations.
+ */
+class Copy final : public Instr, public Object::Producer, public Object::Consumer
+{
+public:
+  Copy() : _from{this}, _into{this}
+  {
+    // DO NOTHING
+  }
+
+public:
+  Copy *asCopy(void) override { return this; }
+  const Copy *asCopy(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 *from(void) const { return _from.value(); }
+  void from(Object *o) { _from.value(o); }
+
+public:
+  Object *into(void) const { return _into.value(); }
+  void into(Object *o) { _into.value(o); }
+
+private:
+  Use _from;
+  Def _into;
+};
+
+} // namespace coco
+
+#endif // __COCO_IR_COPY_H__
index 67bbd03..1537174 100644 (file)
@@ -4,6 +4,7 @@
 #include "coco/IR/Instr.h"
 #include "coco/IR/UnitF.h"
 #include "coco/IR/Shuffle.h"
+#include "coco/IR/Copy.h"
 
 #include "coco/IR/Op.forward.h"
 
diff --git a/contrib/coco/core/src/IR/Copy.cpp b/contrib/coco/core/src/IR/Copy.cpp
new file mode 100644 (file)
index 0000000..84963d7
--- /dev/null
@@ -0,0 +1,36 @@
+#include "coco/IR/Copy.h"
+
+namespace coco
+{
+
+std::set<Bag *> Copy::reads(void) const
+{
+  std::set<Bag *> res;
+
+  if (auto o = _from.value())
+  {
+    if (auto b = o->bag())
+    {
+      res.insert(b);
+    }
+  }
+
+  return res;
+}
+
+std::set<Bag *> Copy::updates(void) const
+{
+  std::set<Bag *> res;
+
+  if (auto o = _into.value())
+  {
+    if (auto b = o->bag())
+    {
+      res.insert(b);
+    }
+  }
+
+  return res;
+}
+
+} // namespace coco
index 89386f9..46fd4b0 100644 (file)
@@ -23,6 +23,13 @@ template <> Shuffle *InstrManager::create(void)
   return take(std::move(ins));
 }
 
+template <> Copy *InstrManager::create(void)
+{
+  auto ins = nncc::foundation::make_unique<Copy>();
+  modulize(ins.get());
+  return take(std::move(ins));
+}
+
 void InstrManager::destroy(Instr *ins)
 {
   // ins SHOULD BE detached from any block before destroy call