ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Operation.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 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 "mir/Operation.h"
18#include "mir/Visitor.h"
19#include "mir/OpDefs.h"
20
21#include <algorithm>
22
23namespace mir
24{
25
26void Operation::Output::removeUse(Operation::Use use)
27{
28 auto it = std::remove(_uses.begin(), _uses.end(), use);
29 _uses.erase(it);
30}
31
32void Operation::Output::replaceAllUsesWith(mir::Operation::Output *new_def)
33{
34 for (auto use : _uses)
35 {
36 use.getNode()->_inputs[use.getIndex()] = new_def;
37 new_def->addUse(use);
38 }
39 _uses.clear();
40}
41
42Operation::Operation(Type type, const std::vector<Output *> &inputs, std::size_t num_outputs)
43 : _type(type)
44{
45 for (std::size_t i = 0; i < inputs.size(); ++i)
46 {
47 inputs[i]->addUse(Use(this, i));
48 _inputs.push_back(inputs[i]);
49 }
50 for (std::size_t i = 0; i < num_outputs; ++i)
51 {
52 _outputs.emplace_back(this, i);
53 }
54}
55
57{
58 switch (getType())
59 {
60#define HANDLE_OP(OpType, OpClass) \
61 case Type::OpType: \
62 v->visit(dynamic_cast<ops::OpClass &>(*this)); \
63 break;
64#include "mir/Operations.inc"
65#undef HANDLE_OP
66 default:
67 assert(false && "OP not defined!");
68 }
69}
70
71const std::string &getTypeName(Operation::Type type)
72{
73 switch (type)
74 {
75#define HANDLE_OP(OpType, OpClass) \
76 case Operation::Type::OpType: \
77 { \
78 static const std::string name(#OpType); \
79 return name; \
80 }
81#include "mir/Operations.inc"
82#undef HANDLE_OP
83 }
84 throw std::runtime_error("unexpected opcode");
85}
86
87} // namespace mir
Interface for visitors Use in MIR component if you want to enforce to implement visits for all operat...
Definition Visitor.h:38
Represents an output of a node.
Definition Operation.h:60
void addUse(Use use)
Adds the specified use to the uses of this output.
Definition Operation.h:82
Type getType() const
Definition Operation.h:123
Operation(OperandConstraint input_constr, const OperandIndexSequence &inputs, const OperandIndexSequence &outputs, OperandConstraint output_constr=OperandConstraint::createAny())
Definition Operation.cc:26
const std::string & getTypeName(Operation::Type type)
Definition Operation.cpp:71
Represents a use of an operation output.
Definition Operation.h:44
virtual void accept(OperationVisitor &v) const =0