From 8e494ebfaaa7b752fa188d1e42dcaf460ee69294 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=D0=A2=D0=B8=D0=BC=D1=83=D1=80=20=D0=9E=D1=82=D0=B5=D0=BB?= =?utf8?q?=D0=BB=D0=BE=D0=B2=D0=B8=D1=87=20=D0=90=D0=B1=D0=BB=D1=8F=D0=B7?= =?utf8?q?=D0=B8=D0=BC=D0=BE=D0=B2/AI=20Tools=20Lab=20/SRR/Staff=20Enginee?= =?utf8?q?r/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 1 Oct 2018 14:39:10 +0300 Subject: [PATCH] Acl backend add dom (#1638) * Adding the ACL C++ soft backend DOM This commit adds two class hierarchies: one for the ACL C++ soft backend and another for the code prodecers ("backends of the backend"). Signed-off-by: Timur Ablyazimov --- .../acl_soft_backend/ArtifactGeneratorCppCode.h | 56 +++++ .../acl_soft_backend/ArtifactGeneratorCppDecl.h | 56 +++++ .../passes/acl_soft_backend/ArtifactModel.h | 258 +++++++++++++++++++++ .../passes/acl_soft_backend/IArtifactGenerator.h | 57 +++++ .../acl_soft_backend/ArtifactGeneratorCppCode.cpp | 71 ++++++ .../acl_soft_backend/ArtifactGeneratorCppDecl.cpp | 60 +++++ .../nnc/passes/acl_soft_backend/ArtifactModel.cpp | 20 ++ contrib/nnc/passes/acl_soft_backend/CMakeLists.txt | 2 +- 8 files changed, 579 insertions(+), 1 deletion(-) create mode 100644 contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppCode.h create mode 100644 contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppDecl.h create mode 100644 contrib/nnc/include/passes/acl_soft_backend/ArtifactModel.h create mode 100644 contrib/nnc/include/passes/acl_soft_backend/IArtifactGenerator.h create mode 100644 contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppCode.cpp create mode 100644 contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp create mode 100644 contrib/nnc/passes/acl_soft_backend/ArtifactModel.cpp diff --git a/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppCode.h b/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppCode.h new file mode 100644 index 0000000..f2f64b7 --- /dev/null +++ b/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppCode.h @@ -0,0 +1,56 @@ +/* + * 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 _NNC_ARTIFACT_GENERATOR_CPP_CODE_H_ +#define _NNC_ARTIFACT_GENERATOR_CPP_CODE_H_ + +#include "IArtifactGenerator.h" + +#include + +namespace nncc +{ +namespace contrib +{ +namespace backend +{ +namespace soft +{ + +/** + * @brief The ACL C++ artifact source code producer. + */ +class ArtifactGeneratorCppCode : public IArtifactGenerator +{ +public: + explicit ArtifactGeneratorCppCode(const std::string& name); + + void visit(const ArtifactLiteral* node) override; + void visit(const ArtifactFunctionCall* node) override; + void visit(const ArtifactBlock* node) override; + void visit(const ArtifactFunction* node) override; + void visit(const ArtifactModule* node) override; + +private: + std::ofstream _out; +}; + +} // namespace soft +} // namespace backend +} // namespace contrib +} // namespace nncc + +#endif //_NNC_ARTIFACT_GENERATOR_CPP_CODE_H_ diff --git a/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppDecl.h b/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppDecl.h new file mode 100644 index 0000000..3271dc4 --- /dev/null +++ b/contrib/nnc/include/passes/acl_soft_backend/ArtifactGeneratorCppDecl.h @@ -0,0 +1,56 @@ +/* + * 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 _NNC_ARTIFACT_GENERATOR_CPP_DECL_H_ +#define _NNC_ARTIFACT_GENERATOR_CPP_DECL_H_ + +#include "IArtifactGenerator.h" + +#include + +namespace nncc +{ +namespace contrib +{ +namespace backend +{ +namespace soft +{ + +/** + * @brief The ACL C++ artifact header file producer. + */ +class ArtifactGeneratorCppDecl : public IArtifactGenerator +{ +public: + explicit ArtifactGeneratorCppDecl(const std::string& name); + + void visit(const ArtifactLiteral* node) override; + void visit(const ArtifactFunctionCall* node) override; + void visit(const ArtifactBlock* node) override; + void visit(const ArtifactFunction* node) override; + void visit(const ArtifactModule* node) override; + +private: + std::ofstream _out; +}; + +} // namespace soft +} // namespace backend +} // namespace contrib +} // namespace nncc + +#endif //_NNC_ARTIFACT_GENERATOR_CPP_DECL_H_ diff --git a/contrib/nnc/include/passes/acl_soft_backend/ArtifactModel.h b/contrib/nnc/include/passes/acl_soft_backend/ArtifactModel.h new file mode 100644 index 0000000..e0a3e85 --- /dev/null +++ b/contrib/nnc/include/passes/acl_soft_backend/ArtifactModel.h @@ -0,0 +1,258 @@ +/* + * 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 _NNC_ARTIFACT_MODEL_H_ +#define _NNC_ARTIFACT_MODEL_H_ + +#include +#include +#include + +#include "IArtifactGenerator.h" + +namespace nncc +{ +namespace contrib +{ +namespace backend +{ +namespace soft +{ + +/** + * @todo FIXME: remove the identical accept() function implementations! + * + * @todo Get rid of the 'Artifact' prefix in the class names + * in this hierarchy, after anticipated namespace refactoring + * in the nnc project. + */ + +/** + * @brief The base class of the whole artifact entities hierarchy. + */ +class ArtifactEntity +{ +public: + virtual ~ArtifactEntity() = default; + /** + * @brief This is the core function of each artifact entity and + * is implemented by all concrete classes in the hierarchy. + */ + virtual void accept(IArtifactGenerator* g) const = 0; + +protected: + ArtifactEntity() = default; +}; + +/** + * @brief Represents literals which should go to the artifact source code as is. + */ +class ArtifactLiteral : public ArtifactEntity +{ +public: + explicit ArtifactLiteral(const std::string& value) : _value(value) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + + const std::string& getValue() const { return _value; } + +private: + std::string _value; +}; + +class ArtifactIdentifier : public ArtifactEntity +{ +public: + explicit ArtifactIdentifier(const std::string& id) : _id(id) {} + const std::string& getId() const { return _id; } + +private: + std::string _id; +}; + +/** + * @brief Represents an entity with semantics like C/C++ reference operator. + */ +class ArtifactRef : public ArtifactEntity +{ +public: + explicit ArtifactRef(const ArtifactEntity& ref) : _ref(ref) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + +private: + const ArtifactEntity& _ref; +}; + +/** + * @brief Represents an entity with semantics like C/C++ dereference operator. + */ +class ArtifactDeref : public ArtifactEntity +{ +public: + explicit ArtifactDeref(const ArtifactEntity& ref) : _ref(ref) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + +private: + const ArtifactEntity& _ref; +}; + +/** + * @brief Represents a variable. + */ +class ArtifactVariable : public ArtifactIdentifier +{ +public: + ArtifactVariable(const std::string& typeName, const std::string& varName) : _typeName(typeName), ArtifactIdentifier(varName) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + +private: + const std::string& _typeName; +}; + +/** + * @brief Represents a class. + */ +class ArtifactClass : public ArtifactIdentifier +{ +public: + explicit ArtifactClass(const std::string& className) : ArtifactIdentifier(className) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } +}; + +/** + * @brief Represents a function call. + */ +class ArtifactFunctionCall : public ArtifactEntity +{ +public: + ArtifactFunctionCall(const std::string& funcName, const std::list& paramList) : + _funcName(funcName), _paramList(paramList) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + + const std::string& getFuncName() const { return _funcName; } + const std::list getParamList() const { return _paramList; } + +private: + std::string _funcName; + std::list _paramList; +}; + +/** + * @brief Represents a block of instructions. + */ +class ArtifactBlock : public ArtifactEntity +{ +public: + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + + void addStatement(ArtifactEntity* statement) + { + _statements.push_back(statement); + } + + std::list getStatements() const + { + return _statements; + } + +private: + std::list _statements; +}; + +/** + * @brief Represents a function. + */ +class ArtifactFunction : public ArtifactIdentifier +{ +public: + ArtifactFunction(const std::string& funcName, const std::list& params, const std::string& retTypeName) : + ArtifactIdentifier(funcName), _params(params), _retTypeName(retTypeName) {} + + void accept(IArtifactGenerator* g) const override + { + g->visit(this); + } + + const std::list& getParameters() const + { + return _params; + } + + const std::string& getRetTypeName() const + { + return _retTypeName; + } + + void AddStatement(ArtifactEntity* statement) + { + _body.addStatement(statement); + } + +private: + std::list _params; + std::string _retTypeName; + ArtifactBlock _body; +}; + +/** + * @brief Class representing a module in the ACL C++ soft backend. + */ +class ArtifactModule +{ +public: + explicit ArtifactModule(const std::string& name) : _name(name) {} + + void accept(IArtifactGenerator* g) const + { + g->visit(this); + } + +private: + std::string _name; +}; + +} // namespace soft +} // namespace backend +} // namespace contrib +} // namespace nncc + +#endif //_NNC_ARTIFACT_MODEL_H_ diff --git a/contrib/nnc/include/passes/acl_soft_backend/IArtifactGenerator.h b/contrib/nnc/include/passes/acl_soft_backend/IArtifactGenerator.h new file mode 100644 index 0000000..e7e2e36 --- /dev/null +++ b/contrib/nnc/include/passes/acl_soft_backend/IArtifactGenerator.h @@ -0,0 +1,57 @@ +/* + * 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 _NNC_ARTIFACT_GENERATOR_INTERFACE_H_ +#define _NNC_ARTIFACT_GENERATOR_INTERFACE_H_ + +namespace nncc +{ +namespace contrib +{ +namespace backend +{ +namespace soft +{ + +class ArtifactEntity; +class ArtifactLiteral; +class ArtifactFunctionCall; +class ArtifactBlock; +class ArtifactFunction; +class ArtifactModule; + +/** + * @brief The interface of the artifact source code producer. + */ +class IArtifactGenerator +{ +public: + virtual ~IArtifactGenerator() = default; + + virtual void visit(const ArtifactEntity* node) = 0; + virtual void visit(const ArtifactLiteral* node) = 0; + virtual void visit(const ArtifactFunctionCall* node) = 0; + virtual void visit(const ArtifactBlock* node) = 0; + virtual void visit(const ArtifactFunction* node) = 0; + virtual void visit(const ArtifactModule* node) = 0; +}; + +} // namespace soft +} // namespace backend +} // namespace contrib +} // namespace nncc + +#endif //_NNC_ARTIFACT_GENERATOR_INTERFACE_H_ diff --git a/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppCode.cpp b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppCode.cpp new file mode 100644 index 0000000..f796d83 --- /dev/null +++ b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppCode.cpp @@ -0,0 +1,71 @@ +/* + * 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 "passes/acl_soft_backend/ArtifactGeneratorCppCode.h" +#include "passes/acl_soft_backend/ArtifactModel.h" + +using namespace std; +using namespace nncc::contrib::backend::soft; + +ArtifactGeneratorCppCode::ArtifactGeneratorCppCode(const string& name) +{ +} + +void ArtifactGeneratorCppCode::visit(const ArtifactLiteral* node) +{ + _out << node->getValue(); +} + +void ArtifactGeneratorCppCode::visit(const ArtifactFunctionCall* node) +{ + _out << node->getFuncName(); + _out << "("; + bool addComma = false; + + for (const auto* par : node->getParamList()) + { + if (addComma) + _out << ", "; + + par->accept(this); + addComma = true; + } + + _out << ")"; +} + +void ArtifactGeneratorCppCode::visit(const ArtifactBlock* node) +{ + _out << "{" << endl; + + for (const auto* st : node->getStatements()) + { + st->accept(this); + _out << ";"; + } + + _out << "}" << endl; +} + +void ArtifactGeneratorCppCode::visit(const ArtifactFunction* node) +{ + +} + +void ArtifactGeneratorCppCode::visit(const ArtifactModule* node) +{ + +} diff --git a/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp new file mode 100644 index 0000000..2d6635c --- /dev/null +++ b/contrib/nnc/passes/acl_soft_backend/ArtifactGeneratorCppDecl.cpp @@ -0,0 +1,60 @@ +/* + * 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 "passes/acl_soft_backend/ArtifactGeneratorCppDecl.h" +#include "passes/acl_soft_backend/ArtifactModel.h" + +using namespace std; +using namespace nncc::contrib::backend::soft; + +ArtifactGeneratorCppDecl::ArtifactGeneratorCppDecl(const string& name) +{ +} + +void ArtifactGeneratorCppDecl::visit(const ArtifactLiteral* node) +{ +} + +void ArtifactGeneratorCppDecl::visit(const ArtifactFunctionCall* node) +{ +} + +void ArtifactGeneratorCppDecl::visit(const ArtifactBlock* node) +{ +} + +void ArtifactGeneratorCppDecl::visit(const ArtifactFunction* node) +{ + _out << node->getRetTypeName() << " " << node->getId() << "("; + + bool addComma = false; + + for (const auto* par : node->getParameters()) + { + if (addComma) + _out << ", "; + + par->accept(this); + addComma = true; + } + + _out << ");"; +} + +void ArtifactGeneratorCppDecl::visit(const ArtifactModule* node) +{ + +} diff --git a/contrib/nnc/passes/acl_soft_backend/ArtifactModel.cpp b/contrib/nnc/passes/acl_soft_backend/ArtifactModel.cpp new file mode 100644 index 0000000..84f787e --- /dev/null +++ b/contrib/nnc/passes/acl_soft_backend/ArtifactModel.cpp @@ -0,0 +1,20 @@ +/* + * 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 "passes/acl_soft_backend/IArtifactGenerator.h" + +using namespace std; +using namespace nncc::contrib::backend::soft; diff --git a/contrib/nnc/passes/acl_soft_backend/CMakeLists.txt b/contrib/nnc/passes/acl_soft_backend/CMakeLists.txt index 1b31105..b66bc4a 100644 --- a/contrib/nnc/passes/acl_soft_backend/CMakeLists.txt +++ b/contrib/nnc/passes/acl_soft_backend/CMakeLists.txt @@ -1,4 +1,4 @@ -set(ACL_SOFT_BACKEND_CPP_SOURCES AclCPPGenerator.cpp) +set(ACL_SOFT_BACKEND_CPP_SOURCES AclCPPGenerator.cpp ArtifactGeneratorCppCode.cpp ArtifactGeneratorCppDecl.cpp ArtifactModel.cpp) add_nnc_library(acl_soft_backend_cpp SHARED ${ACL_SOFT_BACKEND_CPP_SOURCES}) target_include_directories(acl_soft_backend_cpp PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) -- 2.7.4