c176f6ffb1ca009f8561a6b1b4904990ddee3af0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / compiler / pass / ConstantOutputPass.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 "ConstantOutputPass.h"
18
19 #include "ir/Graph.h"
20 #include "ir/operation/Permute.h"
21 #include "util/logging.h"
22
23 namespace onert
24 {
25 namespace compiler
26 {
27 namespace pass
28 {
29
30 void ConstantOutputPass::callback(const ir::OperandIndex &ind, ir::Operand &obj)
31 {
32   if (!_graph.getOutputs().contains(ind) || !obj.isConstant())
33     return;
34
35   auto permute_input_ind = _graph.addOperand(obj.shape(), obj.typeInfo());
36   auto &permute_input_obj = _graph.operands().at(permute_input_ind);
37
38   // Move the const data
39   permute_input_obj.data(obj.shareData());
40   obj.releaseData();
41   obj.info().setAsNonConst();
42
43   using ir::operation::Permute;
44   auto permute_obj = std::make_unique<Permute>(permute_input_ind, ind, Permute::Type::COPY);
45   auto permute_ind = _graph.operations().push(std::move(permute_obj));
46
47   permute_input_obj.insertUse(permute_ind);
48   obj.setDef(permute_ind);
49
50   // Make the operations that uses this operand to use the generated operand
51   auto orig_uses = obj.getUses();
52   for (auto use : orig_uses)
53   {
54     permute_input_obj.insertUse(use);
55     obj.removeUse(use);
56     _graph.operations().at(use).replaceInputs(ind, permute_input_ind);
57   }
58
59   VERBOSE(ConstantOutputPass) << "Permute Op inserted for a constant ouput, node index : "
60                               << permute_ind << std::endl;
61   VERBOSE(ConstantOutputPass) << "  - Input (inserted) Operand : " << permute_input_ind
62                               << std::endl;
63   VERBOSE(ConstantOutputPass) << "  - Output(original) Operand : " << ind << std::endl;
64 }
65
66 } // namespace pass
67 } // namespace compiler
68 } // namespace onert