Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / logo / src / Passes / RemoveForwardNodePass.cpp
1 /*
2  * Copyright (c) 2019 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 #include <logo/RemoveForwardNodePass.h>
18
19 #include <loco/IR/CanonicalDialect.h>
20 #include <loco/IR/CanonicalNode.h>
21
22 #include <set>
23
24 namespace logo
25 {
26
27 bool RemoveForwardNodePass::run(loco::Graph *g)
28 {
29   struct Collector final : public loco::CanonicalNodeMutableVisitor<void>
30   {
31     void visit(loco::Forward *node) final
32     {
33       if (node->input() != nullptr)
34       {
35         candidates.insert(node);
36       }
37     }
38
39     void visit(loco::Node *) final { return; }
40
41     std::set<loco::Forward *> candidates;
42   };
43
44   Collector collector;
45
46   for (auto node : loco::all_nodes(g))
47   {
48     if (node->dialect() == loco::CanonicalDialect::get())
49     {
50       auto canonical_node = loco::must_cast<loco::CanonicalNode *>(node);
51       canonical_node->accept(&collector);
52     }
53   }
54
55   for (auto node : collector.candidates)
56   {
57     replace(node).with(node->input());
58     node->input(nullptr);
59   }
60
61   return collector.candidates.size() > 0;
62 }
63
64 } // namespace logo