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_OPERAND_H__
18 #define __ONERT_IR_OPERAND_H__
26 #include "ir/DataType.h"
27 #include "ir/OperandInfo.h"
28 #include "ir/OperationIndexSet.h"
38 explicit Operand(const Shape &shape, const TypeInfo &type)
39 : _info{shape, type, MemAllocType::STATIC}
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;
51 const OperationIndexSet &getUses() const { return _uses; }
52 const OperationIndexSet &getDef() const { return _def; }
53 void insertUse(const OperationIndex &idx);
54 void removeUse(const OperationIndex &idx);
55 void insertDef(const OperationIndex &idx);
56 void removeDef(const OperationIndex &idx);
59 void type(const DataType type) { _info.type(type); };
62 void data(std::shared_ptr<Data> &&data)
64 _data = std::move(data);
65 _info.setAsConstant();
67 const Data *data(void) const { return _data.get(); }
69 void releaseData(void) { _data.reset(); }
71 std::shared_ptr<Data> shareData(void) const { return _data; }
74 * @brief Get true if Operand is const, otherwise @c false
75 a @return @c true if Operand is const, otherwise @c false
77 bool isConstant(void) const { return _info.isConstant(); }
80 template <typename T, typename... Args> void data(Args &&... args)
82 data(std::make_unique<T>(std::forward<Args>(args)...));
86 template <typename T> T asScalar(void) const
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)));
92 return *(reinterpret_cast<const T *>(_data->base()));
95 template <typename T> std::vector<T> asVector() const
97 assert(_data != nullptr);
98 assert(_data->size() % sizeof(T) == 0);
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);
107 std::shared_ptr<Data> _data;
109 OperationIndexSet _uses;
110 OperationIndexSet _def; // size is 0 (constant) or 1 (from def operation)
116 #endif // __ONERT_IR_OPERAND_H__