04f4e59c0c23bb606bc4fb648553ba7af8c84254
[platform/core/ml/nnfw.git] / runtime / onert / core / src / ir / pass / ConstantLoweringPass.cc
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 "ConstantLoweringPass.h"
18
19 #include "backend/Backend.h"
20 #include <ir/Graph.h>
21 #include <ir/operand/PermuteFactor.h>
22 #include <util/Utils.h>
23
24 namespace onert
25 {
26 namespace ir
27 {
28 namespace pass
29 {
30
31 void ConstantLoweringPass::callback(const OperationIndex &node_index, Operation &node)
32 {
33   const auto &op_sequence_index = _lowered_graph.op_seqs().getOperation(node_index);
34   const auto op_seq_lower_info = _lowered_graph.getLowerInfo(op_sequence_index);
35   const auto backend = op_seq_lower_info->backend();
36   const auto layout = op_seq_lower_info->layout();
37   const auto factor = operand::PermuteFactor{backend, layout};
38
39   // Now this runtime does not support the node making output of operation as constant
40   for (const auto input : node.getInputs() | Remove::DUPLICATED | ir::Remove::UNDEFINED)
41   {
42     auto &object = _graph.operands().at(input);
43     if (object.isConstant())
44     {
45       // All constant operand are already assinged at each backend by ContantInsertionPass. So a
46       // constant has `def` and `use` as the same PermuteFactor
47       _lowered_graph.setLowerInfo(input, std::make_unique<operand::LowerInfo>());
48       _lowered_graph.getLowerInfo(input)->addDefPermuteFactor(factor);
49       _lowered_graph.getLowerInfo(input)->addUsePermuteFactor(factor);
50     }
51   }
52 }
53
54 } // namespace pass
55 } // namespace ir
56 } // namespace onert