Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / 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 <compiler/PermuteFactor.h>
22 #include <util/Utils.h>
23 #include "util/logging.h"
24
25 namespace onert
26 {
27 namespace compiler
28 {
29 namespace pass
30 {
31
32 void ConstantLoweringPass::callback(const ir::OperationIndex &node_index, ir::IOperation &node)
33 {
34   const auto op_lower_info = _lowered_graph.lower_info().operation.getRawPtr(node_index);
35   const auto backend = op_lower_info->backend();
36   const auto layout = op_lower_info->layout();
37   const auto factor = 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() | ir::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       auto operand_li = std::make_unique<compiler::OperandLowerInfo>();
48       operand_li->addDefPermuteFactor(factor);
49       operand_li->addUsePermuteFactor(factor);
50       _lowered_graph.lower_info().operand.set(input, std::move(operand_li));
51     }
52   }
53 }
54
55 } // namespace pass
56 } // namespace compiler
57 } // namespace onert