[enco] Reduce host-side allocation (#1756)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Fri, 5 Oct 2018 06:25:45 +0000 (15:25 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Fri, 5 Oct 2018 06:25:45 +0000 (15:25 +0900)
This commit eliminates unnecessary memory allocation for host
(non-NNAPI) code.

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

index 5ca0195..4eb98f9 100644 (file)
@@ -73,13 +73,52 @@ std::set<coco::Bag *> intermediates(const enco::Code *code)
   // TODO Optimize this!
   std::set<coco::Bag *> res;
 
+  auto accessed_by_host = [code](const coco::Bag *b) {
+    auto block_of = [](coco::Locatable *loc) -> coco::Block * {
+      if (auto ins = loc->loc())
+      {
+        if (auto blk = ins->parent())
+        {
+          return blk;
+        }
+      }
+
+      return nullptr;
+    };
+
+    auto is_host_block = [code](const coco::Block *blk) {
+      return (blk) ? (code->ann()->find(blk) == nullptr) : false;
+    };
+
+    for (const auto &updater : coco::updaters(b))
+    {
+      if (is_host_block(block_of(updater)))
+      {
+        return true;
+      }
+    }
+
+    for (const auto &reader : coco::readers(b))
+    {
+      if (is_host_block(block_of(reader)))
+      {
+        return true;
+      }
+    }
+
+    return false;
+  };
+
   for (uint32_t n = 0; n < code->module()->entity()->bag()->size(); ++n)
   {
     auto bag = code->module()->entity()->bag()->at(n);
 
     if (!bag->isInput() && !bag->isOutput())
     {
-      res.insert(bag);
+      if (accessed_by_host(bag))
+      {
+        res.insert(bag);
+      }
     }
   }