summaryrefslogtreecommitdiff
path: root/compiler/loco/include/loco/IR/NodeMixins.h
blob: f0e34b0bad47f3a4a411ba1a2d4b7c3148d18c7d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
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 <vector>
#include <initializer_list>

namespace loco
{

enum class NodeTrait
{
  DataType,
  // Nodes with TensorShape trait will provide the following methods:
  // - rank()
  // - rank(value)
  // - dim()
  // - dim(value)
  // - shape({...})
  TensorShape,
};

template <NodeTrait T> class NodeMixin;

template <> class NodeMixin<NodeTrait::DataType>
{
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<NodeTrait::TensorShape>
{
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<uint32_t> dims)
  {
    rank(dims.size());

    uint32_t axis = 0;
    for (auto d : dims)
    {
      dim(axis++) = d;
    }
  }

private:
  /// @brief Data shape (as tensor)
  std::vector<Dimension> _dims;
};

template <unsigned N> struct FixedArity
{
  template <typename Base> class Mixin : public virtual Base
  {
  public:
    Mixin()
    {
      for (uint32_t n = 0; n < N; ++n)
      {
        _args[n] = std::unique_ptr<Use>{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<std::unique_ptr<Use>, N> _args{};
  };
};

template <NodeTrait Trait> struct With
{
  template <typename Base> struct Mixin : public virtual Base, public NodeMixin<Trait>
  {
    // DO NOTHING
  };
};

} // namespace loco

#endif // __LOCO_IR_NODE_MIXINS_H__