Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / compiler / luci / pass / src / ShapeSignatureInferencePass.cpp
1 /*
2  * Copyright (c) 2020 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 "luci/Pass/ShapeSignatureInferencePass.h"
18
19 #include <luci/IR/CircleShapeSignature.h>
20 #include <luci/Service/CircleShapeSignatureInference.h>
21
22 #include <loco.h>
23
24 namespace luci
25 {
26
27 bool ShapeSignatureInferencePass::run(luci::Module *m)
28 {
29   bool changed = false;
30
31   for (size_t g = 0; g < m->size(); ++g)
32   {
33     if (run(m->graph(g)))
34       changed = true;
35   }
36
37   return changed;
38 }
39
40 bool ShapeSignatureInferencePass::run(loco::Graph *g)
41 {
42   luci::ssinf::Rule signature_inference_rule;
43   bool changed = false;
44
45   for (auto node : loco::postorder_traversal(loco::output_nodes(g)))
46   {
47     luci::ShapeSignature shape_signature;
48
49     auto circle_node = loco::must_cast<luci::CircleNode *>(node);
50     if (signature_inference_rule.infer(circle_node, shape_signature))
51     {
52       if (!(circle_node->shape_signature() == shape_signature))
53       {
54         circle_node->shape_signature(shape_signature);
55         changed = true;
56       }
57     }
58   }
59
60   return changed;
61 }
62
63 } // namespace luci