Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / compiler / BackendManager.h
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_COMPILER_BACKEND_MANAGER_H__
18 #define __ONERT_COMPILER_BACKEND_MANAGER_H__
19
20 #include <memory>
21 #include <map>
22
23 #include "ir/Operands.h"
24 #include "backend/Backend.h"
25 #include "backend/controlflow/Backend.h"
26
27 namespace onert
28 {
29 namespace compiler
30 {
31
32 class BackendManager
33 {
34 public:
35   using backend_create_t = backend::Backend *(*)();
36   using backend_destroy_t = void (*)(backend::Backend *);
37   using dlhandle_destroy_t = std::function<void(void *)>;
38
39   static BackendManager &get();
40
41 public:
42   backend::Backend *get(const std::string &key);
43   const backend::Backend *get(const std::string &key) const;
44   const backend::controlflow::Backend *getControlflow() const;
45   const std::vector<const backend::Backend *> getAll() const
46   {
47     std::vector<const backend::Backend *> v;
48     for (const auto &p : _gen_map)
49       v.emplace_back(p.second.get());
50     return v;
51   }
52   size_t num_backends() const { return _gen_map.size(); }
53   /**
54    * @brief load backend plugin
55    *
56    * @param backend backend to be loaded
57    *
58    * @return
59    */
60   void loadBackend(const std::string &backend);
61
62 private:
63   BackendManager();
64
65 private:
66   std::map<std::string, std::unique_ptr<void, dlhandle_destroy_t>> _handle_map;
67   std::map<std::string, std::unique_ptr<backend::Backend, backend_destroy_t>> _gen_map;
68   backend::controlflow::Backend *_controlflow{nullptr};
69   /**
70    * @brief load controlflow backend
71    *
72    * @param backend backend to be loaded
73    *
74    * @return
75    */
76   void loadControlflowBackend();
77 };
78
79 } // namespace compiler
80 } // namespace onert
81
82 #endif // __ONERT_COMPILER_BACKEND_MANAGER_H__