[enco] Update Global only from CppCode (#1723)
author박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Tue, 2 Oct 2018 09:26:18 +0000 (18:26 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Tue, 2 Oct 2018 09:26:18 +0000 (18:26 +0900)
With this commit, Global is updated only inside CppCode.
 - SubnetStructBuilder no longer updates Global.

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

index 985614d..bf6accb 100644 (file)
@@ -121,11 +121,28 @@ void CppCode::dump(std::ostream &os) const
    */
   for (uint32_t n = 0; n < _code->ann()->count(); ++n)
   {
-    const SubnetStructBuilder builder{&global};
+    SubnetStructBuilder builder;
 
     auto subnet_binder = _code->ann()->nth(n);
     auto subnet_struct_name = pp::fmt("Subnet_", subnet_ctx.size());
     auto subnet_field_name = pp::fmt("_subnet_", subnet_ctx.size());
+
+    // Create global data variable
+    auto emit_weight = [&global, &builder](const ann::OperandID &id, const ann::Operand *info) {
+      if (info->weight())
+      {
+        auto base = info->weight()->base();
+        auto size = info->weight()->size();
+        auto gvar = global.constant(base, size);
+
+        auto base_exp = pp::fmt("reinterpret_cast<const void *>(&", gvar, ")");
+        auto size_exp = pp::fmt(size);
+
+        builder.expr(info, base_exp, size_exp);
+      }
+    };
+    subnet_binder->module()->operand()->each(emit_weight);
+
     auto subnet_struct_content = builder.build(subnet_binder);
 
     // Emit C++ declaration
index 65f71d2..4657f8f 100644 (file)
@@ -353,14 +353,10 @@ std::unique_ptr<SubnetStruct> SubnetStructBuilder::build(const ANNBinder *binder
       throw std::runtime_error{"Unsupported"};
     }
 
-    if (info->weight())
+    if (_weighted.find(info) != _weighted.end())
     {
-      auto base = info->weight()->base();
-      auto size = info->weight()->size();
-      auto gvar = _global->constant(base, size);
-
-      auto base_exp = pp::fmt("reinterpret_cast<const void *>(&", gvar, ")");
-      auto size_exp = pp::fmt(size);
+      const auto &base_exp = _base_exprs.at(info);
+      const auto &size_exp = _size_exprs.at(info);
 
       res->ctor() << WeightDecl{mname, id, base_exp, size_exp};
     }
index 9341778..0c9b1cb 100644 (file)
 #define __ENCO_CPP_GEN_SUBNET_H__
 
 #include "ANN/Binder.h"
-#include "CppGen/Global.h"
 #include "CppGen/MemoryContext.h"
 
 #include <pp/MultiLineText.h>
 #include <map>
+#include <set>
 
 namespace enco
 {
@@ -47,16 +47,20 @@ struct SubnetStruct
 class SubnetStructBuilder
 {
 public:
-  SubnetStructBuilder(enco::Global *global) : _global{global}
-  {
-    // DO NOTHING
-  }
+  std::unique_ptr<SubnetStruct> build(const ANNBinder *binder) const;
 
 public:
-  std::unique_ptr<SubnetStruct> build(const ANNBinder *binder) const;
+  void expr(const ann::Operand *oper, const std::string &base, const std::string &size)
+  {
+    _weighted.insert(oper);
+    _base_exprs[oper] = base;
+    _size_exprs[oper] = size;
+  }
 
 private:
-  enco::Global *_global;
+  std::set<const ann::Operand *> _weighted;
+  std::map<const ann::Operand *, std::string> _base_exprs;
+  std::map<const ann::Operand *, std::string> _size_exprs;
 };
 
 /**