[enco] Eliminate dead bags (#1473)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 12 Sep 2018 23:01:15 +0000 (08:01 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Wed, 12 Sep 2018 23:01:15 +0000 (08:01 +0900)
This commit implements dead bag elimination optimization pass in enco
NNAPI backend.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/enco/core/src/Backend.cpp
contrib/enco/core/src/Transforms/Optimizations.cpp
contrib/enco/core/src/Transforms/Optimizations.h

index 1d5728c..ae1a6b0 100644 (file)
@@ -64,6 +64,8 @@ void Backend::compile(coco::Module *m, coco::Data *d)
 
   generate_bypass_shuffle(&code);
 
+  eliminate_dead_bag(&code);
+
   // Split instructions into a set of phases (each block serves as a phase)
   SplitPass split;
   split.runOnCode(&code);
index ab5c4db..f48c343 100644 (file)
@@ -78,3 +78,61 @@ void generate_bypass_shuffle(enco::Code *code)
 }
 
 } // namespace enco
+
+//
+// Dead Bag Elimination
+//
+#include <set>
+
+namespace
+{
+
+// @brief Return true if a given bag is marked as either input or output
+bool is_public(const coco::Bag *b) { return b->isInput() || b->isOutput(); }
+
+// @brief Return the set of "dead" bags in a given module
+std::set<coco::Bag *> dead_bags(const coco::Module *m)
+{
+  std::set<coco::Bag *> res;
+
+  for (uint32_t n = 0; n < m->entity()->bag()->size(); ++n)
+  {
+    auto bag = m->entity()->bag()->at(n);
+
+    if (coco::readers(bag).empty() && !is_public(bag))
+    {
+      res.insert(bag);
+    }
+  }
+
+  return res;
+}
+
+} // namespace
+
+namespace enco
+{
+
+void eliminate_dead_bag(enco::Code *code)
+{
+  auto m = code->module();
+
+  // Destroy a dead bag and its updaters
+  for (auto bag : dead_bags(m))
+  {
+    for (auto updater : coco::updaters(bag))
+    {
+      auto ins = updater->loc();
+
+      assert(ins != nullptr);
+
+      ins->detach();
+      m->entity()->instr()->destroy(ins);
+    }
+
+    bag->replaceWith(nullptr);
+    m->entity()->bag()->destroy(bag);
+  }
+}
+
+} // namespace enco
index 249174e..2a9f1c8 100644 (file)
@@ -9,6 +9,16 @@ namespace enco
 // TODO Add a comment
 void generate_bypass_shuffle(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
+ * dead, it is unnecessary to updates its values as these values are never used.
+ *
+ * "eliminate_dead_bag" removes all the dead bags and its updaters from IR.
+ */
+void eliminate_dead_bag(enco::Code *code);
+
 } // namespace enco
 
 #endif // __ENCO_OPTIMIZATIONS_H__