2 * Copyright (c) 2018 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.
23 #include "util/logging.h"
24 #include "verifier/Verifier.h"
25 #include "ir/operation/LowerInfo.h"
26 #include "ir/operand/LowerInfo.h"
27 #include "ir/operand/PermuteFactor.h"
28 #include "ir/OperandIndexMap.h"
29 #include "ir/GraphIterator.h"
30 #include "backend/IConfig.h"
37 Graph::Graph() = default;
39 Graph::~Graph(void) = default;
41 OperandIndex Graph::addOperand(const Shape &shape, const TypeInfo &type)
43 return _operands.emplace(shape, type);
46 OperationIndex Graph::addOperation(std::unique_ptr<Operation> &&node)
48 assert(isBuildingPhase());
49 return _operations.push(std::move(node));
52 void Graph::setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data)
54 assert(isBuildingPhase());
55 assert(_operands.exist(ind));
56 _operands.at(ind).data(std::move(data));
59 void Graph::addInput(const OperandIndex &ind)
61 assert(isBuildingPhase());
65 void Graph::addOutput(const OperandIndex &ind)
67 assert(isBuildingPhase());
71 void Graph::finishBuilding(void)
73 assert(isBuildingPhase());
74 _phase = Phase::MODEL;
77 sweepGarbageOperands();
79 // Call graph verifications for the MODEL phase
81 assert(verifier::DAGChecker().verify(*this));
82 assert(verifier::EdgeConsistencyChecker().verify(*this));
86 void Graph::initializeUseDef()
88 operations().iterate([&](const OperationIndex &index, const Operation &node) -> void {
89 auto outputs = node.getOutputs();
90 for (auto output : outputs)
92 operands().at(output).setDef(index);
95 for (auto input : node.getInputs() | ir::Remove::UNDEFINED)
97 operands().at(input).insertUse(index);
102 void Graph::sweepGarbageOperands()
104 // Remove operands that are not used by any operations, except Graph inputs/outputs
105 ir::OperandIndexMap<bool> visited;
107 operations().iterate([&](const OperationIndex &, const Operation &node) {
108 for (auto ind : node.getInputs() + node.getOutputs())
114 // Graph's inputs/outputs are always reachable
115 for (auto ind : getInputs() + getOutputs())
120 operands().iterate([&](const OperandIndex &ind, const Operand &) {
123 VERBOSE(Graph::sweepGarbageOperands) << "Sweep garbage operand " << ind.value() << std::endl;
124 operands().remove(ind);