[enco] Introduce Object Hoisting Optimization (#1479)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 13 Sep 2018 01:24:42 +0000 (10:24 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 13 Sep 2018 01:24:42 +0000 (10:24 +0900)
* [enco] Introduce Object Hoisting Optimization

This commit implments object hoisting opimization in enco NNAPI backend.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Remove unused candidates

contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/Optimizations.cpp
contrib/enco/core/src/Transforms/Optimizations.h

index ae1a6b0..bab3597 100644 (file)
@@ -63,6 +63,7 @@ void Backend::compile(coco::Module *m, coco::Data *d)
   normalize.runOnCode(&code);
 
   generate_bypass_shuffle(&code);
+  hoist_object(&code);
 
   eliminate_dead_bag(&code);
 
index f48c343..df09e11 100644 (file)
@@ -80,6 +80,63 @@ void generate_bypass_shuffle(enco::Code *code)
 } // namespace enco
 
 //
+// Hoist Object
+//
+namespace
+{
+
+bool hoistable(const coco::Shuffle *shuffle)
+{
+  auto range = shuffle->range();
+
+  if (range.size() != shuffle->into()->size())
+  {
+    return false;
+  }
+
+  for (const auto &dst : range)
+  {
+    if (shuffle->at(dst).value() != dst.value())
+    {
+      return false;
+    }
+  }
+
+  return true;
+}
+
+} // namespace
+
+namespace enco
+{
+
+void hoist_object(enco::Code *code)
+{
+  auto m = code->module();
+
+  for (uint32_t n = 0; n < m->entity()->instr()->size(); ++n)
+  {
+    if (auto shuffle = m->entity()->instr()->at(n)->asShuffle())
+    {
+      if (shuffle->parent() == nullptr)
+      {
+        continue;
+      }
+
+      if (hoistable(shuffle))
+      {
+        auto from = shuffle->from();
+        auto into = shuffle->into();
+
+        into->replaceAllDepsWith(from);
+      }
+    }
+  }
+}
+
+} // namespace enco
+
+//
 // Dead Bag Elimination
 //
 #include <set>
index 2a9f1c8..d606115 100644 (file)
@@ -10,6 +10,41 @@ namespace enco
 void generate_bypass_shuffle(enco::Code *code);
 
 /**
+ * @brief Update the base bag of each object if possible
+ *
+ * Let us consider the following code:
+ *
+ * %bag_1 = Bag(size: 4)
+ * %bag_2 = Bag(size: 1)
+ *
+ * %obj_1 = ... at %bag_1
+ * %obj_2 = ... at %bag_2
+ *
+ * ...
+ * Shuffle(from: %bag_1, into: %bag_2, [0 -> 0]) <- shuffle
+ * ...
+ *
+ * Note that the content of %bag_2 after shuffle is identical to a part of %bag_1, so
+ * the following code is identical to the above code
+ *
+ * %bag_1 = Bag(size: 4)
+ * %bag_2 = Bag(size: 1)
+ *
+ * %obj_1 = ... at %bag_1
+ * %obj_2 = ... at %bag_1
+ *
+ * ...
+ * Shuffle(from: %bag_1, into: %bag_2, [0 -> 0])
+ * ...
+ *
+ * "hoist_object" optimization rewrites the former code as the latter one.
+ *
+ * NOTE "hoist_object" DOES NOT change any instruction. It just updates the base bag of objects of
+ *      interest.
+ */
+void hoist_object(enco::Code *code);
+
+/**
  * @brief Eliminate dead bags
  *
  * A bag is referred to as dead if it is neither input nor output, and has no read. If a bag is