Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / ir / Operand.h
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 #ifndef __ONERT_IR_OPERAND_H__
18 #define __ONERT_IR_OPERAND_H__
19
20 #include <cassert>
21 #include <cstdint>
22 #include <memory>
23 #include <algorithm>
24
25 #include "ir/Data.h"
26 #include "ir/DataType.h"
27 #include "ir/OperandInfo.h"
28 #include "ir/OperationIndexSet.h"
29
30 namespace onert
31 {
32 namespace ir
33 {
34
35 class Operand
36 {
37 public:
38   explicit Operand(const Shape &shape, const TypeInfo &type)
39       : _info{shape, type, MemAllocType::STATIC}
40   {
41     // DO NOTHING
42   }
43
44 public:
45   const Shape &shape(void) const { return _info.shape(); }
46   const TypeInfo &typeInfo(void) const { return _info.typeInfo(); }
47   const OperandInfo &info(void) const { return _info; }
48   OperandInfo &info(void) { return _info; }
49   size_t operandSize(void) const;
50
51   const OperationIndexSet &getUses() const { return _uses; }
52   OperationIndex getDef() const { return _def; }
53   void insertUse(const OperationIndex &idx);
54   void removeUse(const OperationIndex &idx);
55   void setDef(const OperationIndex &idx);
56   void unsetDef();
57
58 public:
59   void type(const DataType type) { _info.type(type); };
60
61 public:
62   void data(std::shared_ptr<Data> &&data)
63   {
64     _data = std::move(data);
65     _info.setAsConstant();
66   }
67   const Data *data(void) const { return _data.get(); }
68
69   void releaseData(void) { _data.reset(); }
70
71   std::shared_ptr<Data> shareData(void) const { return _data; }
72
73   /**
74    * @brief Get true if Operand is const, otherwise @c false
75    a @return @c true if Operand is const, otherwise @c false
76    */
77   bool isConstant(void) const { return _info.isConstant(); }
78
79 public:
80   template <typename T, typename... Args> void data(Args &&... args)
81   {
82     data(std::make_unique<T>(std::forward<Args>(args)...));
83   }
84
85 public:
86   template <typename T> T asScalar(void) const
87   {
88     assert((shape().rank() == 0) || ((shape().rank() == 1) && (shape().dim(0) == 1)));
89     assert(_data != nullptr);
90     assert((_data->base() != nullptr) && (_data->size() == sizeof(T)));
91
92     return *(reinterpret_cast<const T *>(_data->base()));
93   }
94
95   template <typename T> std::vector<T> asVector() const
96   {
97     assert(_data != nullptr);
98     assert(_data->size() % sizeof(T) == 0);
99
100     const auto *base = reinterpret_cast<const T *>(_data->base());
101     const std::size_t size = _data->size() / sizeof(T);
102     return std::vector<T>(base, base + size);
103   }
104
105 private:
106   OperandInfo _info;
107   std::shared_ptr<Data> _data;
108
109   OperationIndexSet _uses;
110   OperationIndex _def;
111 };
112
113 } // namespace ir
114 } // namespace onert
115
116 #endif // __ONERT_IR_OPERAND_H__