2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "ConstantOutputPass.h"
20 #include "ir/operation/Permute.h"
21 #include "util/logging.h"
30 void ConstantOutputPass::callback(const ir::OperandIndex &ind, ir::Operand &obj)
32 if (!_graph.getOutputs().contains(ind) || !obj.isConstant())
35 auto permute_input_ind = _graph.addOperand(obj.shape(), obj.typeInfo());
36 auto &permute_input_obj = _graph.operands().at(permute_input_ind);
38 // Move the const data
39 permute_input_obj.data(obj.shareData());
41 obj.info().setAsNonConst();
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));
47 permute_input_obj.insertUse(permute_ind);
48 obj.setDef(permute_ind);
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)
54 permute_input_obj.insertUse(use);
56 _graph.operations().at(use).replaceInputs(ind, permute_input_ind);
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
63 VERBOSE(ConstantOutputPass) << " - Output(original) Operand : " << ind << std::endl;
67 } // namespace compiler