From: 박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 Date: Mon, 8 Oct 2018 00:21:25 +0000 (+0900) Subject: [enco] Global as local helper class (#1766) X-Git-Tag: nncc_backup~1616 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=96c2c13bf212e32a99609354959084a2e03e8186;p=platform%2Fcore%2Fml%2Fnnfw.git [enco] Global as local helper class (#1766) Now, only Global Data Generation pass accesses the internal of 'Global' class. Signed-off-by: Jonghyun Park --- diff --git a/contrib/enco/core/src/CppCode.cpp b/contrib/enco/core/src/CppCode.cpp index 4eb98f9..db984b4 100644 --- a/contrib/enco/core/src/CppCode.cpp +++ b/contrib/enco/core/src/CppCode.cpp @@ -18,7 +18,6 @@ #include "Transforms/GlobalDataGeneration.h" -#include "CppGen/Global.h" #include "CppGen/MemoryContext.h" #include "CppGen/Host.h" diff --git a/contrib/enco/core/src/CppGen/Global.cpp b/contrib/enco/core/src/CppGen/Global.cpp deleted file mode 100644 index d8ee646..0000000 --- a/contrib/enco/core/src/CppGen/Global.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "Global.h" - -#include - -namespace enco -{ - -GlobalOffset Global::constant(const std::string &s) -{ - auto const base = reinterpret_cast(s.c_str()); - auto const size = s.size() + 1 /* NUL */; - return constant(base, size); -} - -template <> GlobalOffset Global::constant(const std::vector &values) -{ - auto const base = reinterpret_cast(values.data()); - auto const size = sizeof(uint32_t) * values.size(); - return constant(base, size); -} - -GlobalOffset Global::constant(const uint8_t *base, uint32_t size) -{ - auto pos = _os.tellp(); - assert(pos != -1); - - _os.write(reinterpret_cast(base), size); - - return static_cast(pos); -} - -} // namespace enco diff --git a/contrib/enco/core/src/CppGen/Global.h b/contrib/enco/core/src/CppGen/Global.h deleted file mode 100644 index c3b4fff..0000000 --- a/contrib/enco/core/src/CppGen/Global.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __ENCO_GLOBAL_H__ -#define __ENCO_GLOBAL_H__ - -#include -#include -#include -#include - -namespace enco -{ - -using GlobalOffset = uint32_t; - -/** - * @brief Manage global variable declarations - */ -class Global -{ -public: - Global(std::ostream &os) : _os(os) - { - // DO NOTHING - } - -public: - // @brief Create a global constant string (const char *) literal, and return variable name - GlobalOffset constant(const std::string &value); - - // @brief Create a global constant array variable of type T - template GlobalOffset constant(const std::vector &values); - - // @brief Create a global constant array variable of byte (uint8_t) type - GlobalOffset constant(const uint8_t *base, uint32_t size); - -private: - uint32_t _offset = 0; - std::ostream &_os; -}; - -} // namespace enco - -#endif // __ENCO_GLOBAL_H__ diff --git a/contrib/enco/core/src/CppGen/Global.test.cpp b/contrib/enco/core/src/CppGen/Global.test.cpp deleted file mode 100644 index bd4e444..0000000 --- a/contrib/enco/core/src/CppGen/Global.test.cpp +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "Global.h" - -#include - -#include - -// TODO Rewrite this test -#if 0 -TEST(GLOBAL, distinct_id) -{ - enco::Global global; - - std::set ids; - - ids.insert(global.constant("Hello")); - ids.insert(global.constant("Nice to meet you")); - - ASSERT_EQ(ids.size(), 2); -} -#endif diff --git a/contrib/enco/core/src/Transforms/GlobalDataGeneration.cpp b/contrib/enco/core/src/Transforms/GlobalDataGeneration.cpp index b244f8a..123f466 100644 --- a/contrib/enco/core/src/Transforms/GlobalDataGeneration.cpp +++ b/contrib/enco/core/src/Transforms/GlobalDataGeneration.cpp @@ -26,6 +26,61 @@ using nncc::foundation::make_unique; namespace { +/** + * @brief Manage global variable declarations + */ +class Global +{ +public: + Global(std::ostream &os) : _os(os) + { + // DO NOTHING + } + +public: + // @brief Create a global constant string (const char *) literal, and return variable name + enco::GlobalOffset constant(const std::string &value); + + // @brief Create a global constant array variable of type T + template enco::GlobalOffset constant(const std::vector &values); + + // @brief Create a global constant array variable of byte (uint8_t) type + enco::GlobalOffset constant(const uint8_t *base, uint32_t size); + +private: + uint32_t _offset = 0; + std::ostream &_os; +}; + +enco::GlobalOffset Global::constant(const std::string &s) +{ + auto const base = reinterpret_cast(s.c_str()); + auto const size = s.size() + 1 /* NUL */; + return constant(base, size); +} + +template <> enco::GlobalOffset Global::constant(const std::vector &values) +{ + auto const base = reinterpret_cast(values.data()); + auto const size = sizeof(uint32_t) * values.size(); + return constant(base, size); +} + +enco::GlobalOffset Global::constant(const uint8_t *base, uint32_t size) +{ + auto pos = _os.tellp(); + assert(pos != -1); + + _os.write(reinterpret_cast(base), size); + + return static_cast(pos); +} + +} // namespace + +namespace +{ + std::map data_offset_ctx; std::map name_offset_ctx; std::map dims_offset_ctx; diff --git a/contrib/enco/core/src/Transforms/GlobalDataGeneration.h b/contrib/enco/core/src/Transforms/GlobalDataGeneration.h index 6298e15..fd7b382 100644 --- a/contrib/enco/core/src/Transforms/GlobalDataGeneration.h +++ b/contrib/enco/core/src/Transforms/GlobalDataGeneration.h @@ -17,12 +17,15 @@ #ifndef __ENCO_TRANSFORM_GLOBAL_DATA_GENERATION_H__ #define __ENCO_TRANSFORM_GLOBAL_DATA_GENERATION_H__ -#include "CppGen/Global.h" #include "Code.h" +#include + namespace enco { +using GlobalOffset = uint32_t; + struct GlobalData { static GlobalOffset data_offset(const ann::Operand *);