From 5cb167aaa300b05ef2096626be5119fdcb798c8e Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 2 Oct 2018 18:26:18 +0900 Subject: [PATCH] [enco] Update Global only from CppCode (#1723) With this commit, Global is updated only inside CppCode. - SubnetStructBuilder no longer updates Global. Signed-off-by: Jonghyun Park --- contrib/enco/core/src/CppCode.cpp | 19 ++++++++++++++++++- contrib/enco/core/src/CppGen/Subnet.cpp | 10 +++------- contrib/enco/core/src/CppGen/Subnet.h | 18 +++++++++++------- 3 files changed, 32 insertions(+), 15 deletions(-) diff --git a/contrib/enco/core/src/CppCode.cpp b/contrib/enco/core/src/CppCode.cpp index 985614d..bf6accb 100644 --- a/contrib/enco/core/src/CppCode.cpp +++ b/contrib/enco/core/src/CppCode.cpp @@ -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(&", 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 diff --git a/contrib/enco/core/src/CppGen/Subnet.cpp b/contrib/enco/core/src/CppGen/Subnet.cpp index 65f71d2..4657f8f 100644 --- a/contrib/enco/core/src/CppGen/Subnet.cpp +++ b/contrib/enco/core/src/CppGen/Subnet.cpp @@ -353,14 +353,10 @@ std::unique_ptr 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(&", 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}; } diff --git a/contrib/enco/core/src/CppGen/Subnet.h b/contrib/enco/core/src/CppGen/Subnet.h index 9341778..0c9b1cb 100644 --- a/contrib/enco/core/src/CppGen/Subnet.h +++ b/contrib/enco/core/src/CppGen/Subnet.h @@ -18,11 +18,11 @@ #define __ENCO_CPP_GEN_SUBNET_H__ #include "ANN/Binder.h" -#include "CppGen/Global.h" #include "CppGen/MemoryContext.h" #include #include +#include namespace enco { @@ -47,16 +47,20 @@ struct SubnetStruct class SubnetStructBuilder { public: - SubnetStructBuilder(enco::Global *global) : _global{global} - { - // DO NOTHING - } + std::unique_ptr build(const ANNBinder *binder) const; public: - std::unique_ptr 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 _weighted; + std::map _base_exprs; + std::map _size_exprs; }; /** -- 2.7.4