1 /**
2 * Copyright 2021 Huawei Technologies Co., Ltd
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 MINDSPORE_LITE_INCLUDE_REGISTRY_PASS_REGISTRY_H_
18 #define MINDSPORE_LITE_INCLUDE_REGISTRY_PASS_REGISTRY_H_
19
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include "include/api/types.h"
24 #include "include/api/dual_abi_helper.h"
25
26 namespace mindspore {
27 namespace registry {
28 class PassBase;
29 using PassBasePtr = std::shared_ptr<PassBase>;
30 /// \brief PassPosition defined where to place user's pass.
31 enum MS_API PassPosition { POSITION_BEGIN = 0, POSITION_END = 1 };
32
33 /// \brief PassRegistry defined registration of Pass.
34 class MS_API PassRegistry {
35 public:
36 /// \brief Constructor of PassRegistry to register pass.
37 ///
38 /// \param[in] pass_name Define the name of the pass, a string which should guarantee uniqueness.
39 /// \param[in] pass Define pass instance.
40 inline PassRegistry(const std::string &pass_name, const PassBasePtr &pass);
41
42 /// \brief Constructor of PassRegistry to assign which passes are required for external extension.
43 ///
44 /// \param[in] position Define the place where assigned passes will run.
45 /// \param[in] names Define the names of the passes.
46 inline PassRegistry(PassPosition position, const std::vector<std::string> &names);
47
48 /// \brief Destructor of PassRegistrar.
49 ~PassRegistry() = default;
50
51 /// \brief Static method to obtain external scheduling task assigned by user.
52 ///
53 /// \param[in] position Define the place where assigned passes will run.
54 ///
55 /// \return Passes' Name Vector.
56 inline static std::vector<std::string> GetOuterScheduleTask(PassPosition position);
57
58 /// \brief Static method to obtain pass instance according to passes' name.
59 ///
60 /// \param[in] pass_name Define the name of pass.
61 ///
62 /// \return Pass Instance Vector.
63 inline static PassBasePtr GetPassFromStoreRoom(const std::string &pass_name);
64
65 private:
66 PassRegistry(const std::vector<char> &pass_name, const PassBasePtr &pass);
67 PassRegistry(PassPosition position, const std::vector<std::vector<char>> &names);
68 static std::vector<std::vector<char>> GetOuterScheduleTaskInner(PassPosition position);
69 static PassBasePtr GetPassFromStoreRoom(const std::vector<char> &pass_name_char);
70 };
71
PassRegistry(const std::string & pass_name,const PassBasePtr & pass)72 PassRegistry::PassRegistry(const std::string &pass_name, const PassBasePtr &pass)
73 : PassRegistry(StringToChar(pass_name), pass) {}
74
PassRegistry(PassPosition position,const std::vector<std::string> & names)75 PassRegistry::PassRegistry(PassPosition position, const std::vector<std::string> &names)
76 : PassRegistry(position, VectorStringToChar(names)) {}
77
GetOuterScheduleTask(PassPosition position)78 std::vector<std::string> PassRegistry::GetOuterScheduleTask(PassPosition position) {
79 return VectorCharToString(GetOuterScheduleTaskInner(position));
80 }
81
GetPassFromStoreRoom(const std::string & pass_name)82 PassBasePtr PassRegistry::GetPassFromStoreRoom(const std::string &pass_name) {
83 return GetPassFromStoreRoom(StringToChar(pass_name));
84 }
85 /// \brief Defined registering macro to register Pass, which called by user directly.
86 ///
87 /// \param[in] name Define the name of the pass, a string which should guarantee uniqueness.
88 /// \param[in] pass Define pass instance.
89 #define REG_PASS(name, pass) \
90 static mindspore::registry::PassRegistry g_##name##PassReg(#name, std::make_shared<pass>());
91
92 /// \brief Defined assigning macro to assign Passes, which called by user directly.
93 ///
94 /// \param[in] position Define the place where assigned passes will run.
95 /// \param[in] names Define the names of the passes.
96 #define REG_SCHEDULED_PASS(position, names) static mindspore::registry::PassRegistry g_##position(position, names);
97 } // namespace registry
98 } // namespace mindspore
99
100 #endif // MINDSPORE_LITE_INCLUDE_REGISTRY_PASS_REGISTRY_H_
101