diff options
author | 박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com> | 2019-09-16 15:08:42 +0900 |
---|---|---|
committer | GitHub Enterprise <noreply-CODE@samsung.com> | 2019-09-16 15:08:42 +0900 |
commit | c8ab26b98efc60ad9191252cba88abc2f41b5398 (patch) | |
tree | acd3ce58fb32473f75a6e004e35ec3ea72e26aae | |
parent | 7e70f729cf850dd78ed1a0b7241f54b211ab7f75 (diff) | |
download | nnfw-c8ab26b98efc60ad9191252cba88abc2f41b5398.tar.gz nnfw-c8ab26b98efc60ad9191252cba88abc2f41b5398.tar.bz2 nnfw-c8ab26b98efc60ad9191252cba88abc2f41b5398.zip |
[loco] Introduce DialectService (#7439)
* [loco] Introduce DialectService
This commit introduces DialectServices class and extends Dialect class
to support service registration.
Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
* Update per feedback
-rw-r--r-- | compiler/loco/include/loco/IR/Dialect.h | 32 | ||||
-rw-r--r-- | compiler/loco/include/loco/IR/DialectService.h | 35 | ||||
-rw-r--r-- | compiler/loco/src/IR/Dialect.test.cpp | 41 | ||||
-rw-r--r-- | compiler/loco/src/IR/DialectService.cpp | 19 |
4 files changed, 126 insertions, 1 deletions
diff --git a/compiler/loco/include/loco/IR/Dialect.h b/compiler/loco/include/loco/IR/Dialect.h index d1d571517..b8942bfb4 100644 --- a/compiler/loco/include/loco/IR/Dialect.h +++ b/compiler/loco/include/loco/IR/Dialect.h @@ -17,6 +17,13 @@ #ifndef __LOCO_IR_DIALECT_H__ #define __LOCO_IR_DIALECT_H__ +#include "loco/IR/DialectService.h" + +#include <map> +#include <memory> +#include <typeindex> +#include <typeinfo> + namespace loco { @@ -26,9 +33,32 @@ namespace loco * Each dialect implementation is expected to have static "get" method * which returns "const Dialect *" value. */ -struct Dialect +class Dialect { +public: virtual ~Dialect() = default; + +protected: + template <typename ConcreteService> void service(std::unique_ptr<ConcreteService> &&s) + { + _services[typeid(ConcreteService)] = std::move(s); + } + +public: + template <typename ConcreteService> ConcreteService *service(void) const + { + auto it = _services.find(typeid(ConcreteService)); + + if (it == _services.end()) + { + return nullptr; + } + + return dynamic_cast<ConcreteService *>(it->second.get()); + } + +private: + std::map<std::type_index, std::unique_ptr<DialectService>> _services; }; } // namespace loco diff --git a/compiler/loco/include/loco/IR/DialectService.h b/compiler/loco/include/loco/IR/DialectService.h new file mode 100644 index 000000000..54a3fac74 --- /dev/null +++ b/compiler/loco/include/loco/IR/DialectService.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2019 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 __LOCO_IR_DIALECT_SERVICE_H__ +#define __LOCO_IR_DIALECT_SERVICE_H__ + +namespace loco +{ + +/** + * @brief Dialect Service interface + * + * Every service that each dialect exposes should inherit this interface. + */ +struct DialectService +{ + virtual ~DialectService() = default; +}; + +} // namespace loco + +#endif // __LOCO_IR_DIALECT_SERVICE_H__ diff --git a/compiler/loco/src/IR/Dialect.test.cpp b/compiler/loco/src/IR/Dialect.test.cpp new file mode 100644 index 000000000..312bb52ef --- /dev/null +++ b/compiler/loco/src/IR/Dialect.test.cpp @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 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 "loco/IR/Dialect.h" + +#include <stdex/Memory.h> + +#include <gtest/gtest.h> + +TEST(DialectTest, service) +{ + struct S0 final : public loco::DialectService + { + }; + struct S1 final : public loco::DialectService + { + }; + + struct MockDialect final : public loco::Dialect + { + MockDialect() { service<S1>(stdex::make_unique<S1>()); } + }; + + MockDialect dialect; + + ASSERT_EQ(dialect.service<S0>(), nullptr); + ASSERT_NE(dialect.service<S1>(), nullptr); +} diff --git a/compiler/loco/src/IR/DialectService.cpp b/compiler/loco/src/IR/DialectService.cpp new file mode 100644 index 000000000..fb8041e47 --- /dev/null +++ b/compiler/loco/src/IR/DialectService.cpp @@ -0,0 +1,19 @@ +/* + * Copyright (c) 2019 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 "loco/IR/DialectService.h" + +// NOTE This file validates "DialectService.h". Please DO NOT remove this file. |