• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 #ifndef MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_PASS_MANAGER_H_
17 #define MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_PASS_MANAGER_H_
18 
19 #include <utility>
20 #include <vector>
21 #include <string>
22 #include <memory>
23 #include <map>
24 
25 #include "include/backend/optimizer/pass.h"
26 #include "include/backend/optimizer/node_pass.h"
27 #include "include/backend/visible.h"
28 
29 namespace mindspore {
30 namespace opt {
31 // @brief For optimization passes management
32 class BACKEND_EXPORT PassManager {
33  public:
34   explicit PassManager(const std::string &name = "pm", bool run_only_once = true);
35   virtual ~PassManager() = default;
36   // Get all the passes added by AddPass
Passes()37   const std::vector<PassPtr> &Passes() const { return passes_; }
38   // Add graph pass, the pass object will be freed when pass manager freed.
39   virtual void AddPass(const PassPtr &pass);
40   // Run passes added in pass manager on the input graph
41   // @param [in out] graph The graph to be optimized
42   // @return true, graph changed
43   // @return false, graph not changed
44   virtual bool Run(const FuncGraphPtr &func_graph) const;
45   // Run the given graph passes on the input graph
46   // @param [in out] graph The graph to be optimized
47   // @param [in] passes The given graph passes
48   // @return true, graph changed
49   // @return false, graph not changed
50   virtual bool Run(const FuncGraphPtr &func_graph, const std::vector<PassPtr> &passes) const;
name()51   std::string name() const { return name_; }
52 
53  protected:
54   virtual bool RunPass(const FuncGraphPtr &func_graph, size_t pass_id, const PassPtr &pass) const;
55   virtual std::string GetPassFullname(size_t pass_id, const PassPtr &pass) const;
56   virtual void DumpPassIR(const FuncGraphPtr &func_graph, const std::string &pass_fullname) const;
57 
58   const std::string name_;
59   std::vector<PassPtr> passes_;
60   bool run_only_once_;
61   CacheManagerPtr cache_manager_;
62 };
63 using PassManagerPtr = std::shared_ptr<PassManager>;
64 }  // namespace opt
65 }  // namespace mindspore
66 
67 #endif  // MINDSPORE_CCSRC_BACKEND_OPTIMIZER_COMMON_PASS_MANAGER_H_
68