1 /*
2 * Copyright (c) 2018-2019 Arm Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 #include "arm_compute/graph/PassManager.h"
25
26 #include "arm_compute/graph/Logger.h"
27
28 namespace arm_compute
29 {
30 namespace graph
31 {
PassManager()32 PassManager::PassManager()
33 : _passes()
34 {
35 }
36
passes() const37 const std::vector<std::unique_ptr<IGraphMutator>> &PassManager::passes() const
38 {
39 return _passes;
40 }
41
pass(size_t index)42 IGraphMutator *PassManager::pass(size_t index)
43 {
44 return (index >= _passes.size()) ? nullptr : _passes.at(index).get();
45 }
46
append(std::unique_ptr<IGraphMutator> pass,bool conditional)47 void PassManager::append(std::unique_ptr<IGraphMutator> pass, bool conditional)
48 {
49 if(pass && conditional)
50 {
51 ARM_COMPUTE_LOG_GRAPH_VERBOSE("Appending mutating pass : " << pass->name() << std::endl);
52 _passes.push_back(std::move(pass));
53 }
54 }
55
clear()56 void PassManager::clear()
57 {
58 _passes.clear();
59 }
60
run_all(Graph & g)61 void PassManager::run_all(Graph &g)
62 {
63 for(auto &pass : _passes)
64 {
65 if(pass)
66 {
67 ARM_COMPUTE_LOG_GRAPH_INFO("Running mutating pass : " << pass->name() << std::endl);
68 pass->mutate(g);
69 }
70 }
71 }
72
run_type(Graph & g,IGraphMutator::MutationType type)73 void PassManager::run_type(Graph &g, IGraphMutator::MutationType type)
74 {
75 for(auto &pass : _passes)
76 {
77 if(pass && (pass->type() == type))
78 {
79 ARM_COMPUTE_LOG_GRAPH_INFO("Running mutating pass : " << pass->name() << std::endl);
80 pass->mutate(g);
81 }
82 }
83 }
84
run_index(Graph & g,size_t index)85 void PassManager::run_index(Graph &g, size_t index)
86 {
87 if(index >= _passes.size())
88 {
89 return;
90 }
91
92 auto &pass = _passes.at(index);
93 if(pass != nullptr)
94 {
95 ARM_COMPUTE_LOG_GRAPH_INFO("Running mutating pass : " << pass->name() << std::endl);
96 pass->mutate(g);
97 }
98 }
99 } // namespace graph
100 } // namespace arm_compute