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.
17 #ifndef __ONERT_IR_GRAPH_H__
18 #define __ONERT_IR_GRAPH_H__
21 #include <unordered_map>
24 #include "ir/Operands.h"
25 #include "ir/Operations.h"
35 } // namespace backend
54 explicit Graph(const Graph &);
60 OperandIndex addOperand(const Shape &shape, const TypeInfo &type);
62 * @brief Add an operand to the graph with the given index and object
64 * If the given index is available, it succeeds. And @c operand is moved which invalidates the
65 * caller's pointer. If the given index is already taken, it fails. And @c operand will not be
66 * moved so the caller's pointer will be still valid.
68 * @param[in] index Index to be added
69 * @param[in] operand Operand to be added
70 * @return OperandIndex @c index if successful, Undefined otherwise
72 OperandIndex addOperand(OperandIndex index, std::unique_ptr<Operand> &&operand);
73 OperationIndex addOperation(std::unique_ptr<Operation> &&node);
75 * @brief Add an operation to the graph with the given index and object
77 * If the given index is available, it succeeds. And @c operation is moved which invalidates the
78 * caller's pointer. If the given index is already taken, it fails. And @c operation will not be
79 * moved so the caller's pointer will be still valid.
81 * @param index Index to be added
82 * @param operation Operation to be added
83 * @return OperandIndex @c index if successful, Undefined otherwise
85 OperationIndex addOperation(OperationIndex index, std::unique_ptr<Operation> &&operation);
86 void setOperandValue(const OperandIndex &ind, std::shared_ptr<Data> data);
87 void addInput(const OperandIndex &ind, const std::string &name = "");
88 void addOutput(const OperandIndex &ind, const std::string &name = "");
90 void removeOperand(const OperandIndex &ind) { _operands.remove(ind); }
91 void setLayout(Layout layout) { _layout = layout; }
94 bool checkOperandsForOperation(const Operation &operation);
95 void linkOperandToOperation(OperationIndex index, const Operation &operation);
96 void initializeUseDef();
97 // TODO Rename to `sweepUnusedOperands`
98 // TODO Make this public
99 void sweepGarbageOperands();
101 // Custom operations support
104 bindKernelBuilder(const std::shared_ptr<onert::backend::custom::IKernelBuilder> &kernel_builder)
106 _kernel_builder = kernel_builder;
109 const std::shared_ptr<backend::custom::IKernelBuilder> &getKernelBuilder() const
111 return _kernel_builder;
115 std::shared_ptr<backend::custom::IKernelBuilder> _kernel_builder;
119 const OperandIndexSequence &getInputs() const { return _inputs; }
120 OperandIndexSequence &getInputs() { return _inputs; }
121 const OperandIndexSequence &getOutputs() const { return _outputs; }
122 OperandIndexSequence &getOutputs() { return _outputs; }
123 IOIndex getInputIndex(const std::string &name) const;
124 IOIndex getOutputIndex(const std::string &name) const;
125 const Operands &operands() const { return _operands; }
126 Operands &operands() { return _operands; } // TODO Remove this non-const accessor
127 const Operations &operations() const { return _operations; }
128 Operations &operations() { return _operations; }
129 Layout layout() const { return _layout; }
133 std::vector<ir::OperationIndex> topolSortOperations() const;
136 Operations _operations;
138 OperandIndexSequence _inputs;
139 OperandIndexSequence _outputs;
140 std::unordered_map<std::string, IOIndex> _name_to_input;
141 std::unordered_map<std::string, IOIndex> _name_to_output;
142 // TFLite and circle's default layout is NHWC;
143 Layout _layout{Layout::NHWC};
149 #endif // __ONERT_IR_GRAPH_H__