From e2ef8438a24f7c56a0744eb579a6e293ee2fbf8e Mon Sep 17 00:00:00 2001 From: Chunseok Lee Date: Thu, 23 Apr 2020 14:45:49 +0900 Subject: Imported Upstream version 1.4.0 --- compiler/loco/include/loco/IR/NodeMixins.h | 133 +++++++++++++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 compiler/loco/include/loco/IR/NodeMixins.h (limited to 'compiler/loco/include/loco/IR/NodeMixins.h') diff --git a/compiler/loco/include/loco/IR/NodeMixins.h b/compiler/loco/include/loco/IR/NodeMixins.h new file mode 100644 index 000000000..f0e34b0ba --- /dev/null +++ b/compiler/loco/include/loco/IR/NodeMixins.h @@ -0,0 +1,133 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __LOCO_IR_NODE_MIXINS_H__ +#define __LOCO_IR_NODE_MIXINS_H__ + +#include "loco/IR/Node.h" +#include "loco/IR/DataType.h" +#include "loco/IR/Dimension.h" + +#include +#include + +namespace loco +{ + +enum class NodeTrait +{ + DataType, + // Nodes with TensorShape trait will provide the following methods: + // - rank() + // - rank(value) + // - dim() + // - dim(value) + // - shape({...}) + TensorShape, +}; + +template class NodeMixin; + +template <> class NodeMixin +{ +public: + NodeMixin() = default; + +public: + const DataType &dtype(void) const { return _dtype; } + void dtype(const DataType &dtype) { _dtype = dtype; } + +private: + /// @brief Data type + DataType _dtype{DataType::Unknown}; +}; + +template <> class NodeMixin +{ +public: + NodeMixin() = default; + +public: + uint32_t rank(void) const { return _dims.size(); } + void rank(uint32_t value) { _dims.resize(value); } + + const Dimension &dim(uint32_t axis) const { return _dims.at(axis); } + Dimension &dim(uint32_t axis) { return _dims.at(axis); } + + void shape(std::initializer_list dims) + { + rank(dims.size()); + + uint32_t axis = 0; + for (auto d : dims) + { + dim(axis++) = d; + } + } + +private: + /// @brief Data shape (as tensor) + std::vector _dims; +}; + +template struct FixedArity +{ + template class Mixin : public virtual Base + { + public: + Mixin() + { + for (uint32_t n = 0; n < N; ++n) + { + _args[n] = std::unique_ptr{new Use{this}}; + } + } + + virtual ~Mixin() = default; + + public: + unsigned arity(void) const final { return N; } + + Node *arg(uint32_t n) const final { return _args.at(n)->node(); } + + void drop(void) final + { + for (uint32_t n = 0; n < N; ++n) + { + _args.at(n)->node(nullptr); + } + } + + protected: + // This API allows inherited classes to access "_args" field. + Use *at(unsigned n) const { return _args.at(n).get(); } + + private: + std::array, N> _args{}; + }; +}; + +template struct With +{ + template struct Mixin : public virtual Base, public NodeMixin + { + // DO NOTHING + }; +}; + +} // namespace loco + +#endif // __LOCO_IR_NODE_MIXINS_H__ -- cgit v1.2.3