summaryrefslogtreecommitdiff
path: root/compiler/coco/core/include/coco/IR/Op.h
blob: 090527e2fd8431e08b7a97538e021fe6d62106a0 (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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
 * Copyright (c) 2018 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.
 */

/**
 * @file  Op.h
 * @brief This header file declares "Op" class and several traits related with "Op"
 */
#ifndef __COCO_IR_OP_H__
#define __COCO_IR_OP_H__

#include "coco/IR/Object.forward.h"
#include "coco/IR/Instr.forward.h"
#include "coco/IR/Step.forward.h"
#include "coco/IR/Part.h"
#include "coco/IR/Entity.h"

#include <set>

#include <stdexcept>

namespace coco
{

#define OP(Name) class Name;
#include "coco/IR/Op.lst"
#undef OP

/**
 * @brief Base interface on all supported NN operations
 */
struct Op : public Entity
{
  friend class Step;
  friend class Part;

  virtual ~Op();

  /**
   * @brief Return the number of arguments (# of child Ops)
   */
  virtual uint32_t arity(void) const = 0;

  /**
   * @brief Return N-th argument
   *
   * @note The behavior of arg(n) is defined only when n < artiy()
   */
  virtual Op *arg(uint32_t n) const = 0;

  /**
   * @brief Return a set of object(s) used during execution
   *
   * NOTE There is no 'def' method as Op is not allowed to define a new object
   */
  virtual std::set<Object *> uses(void) const = 0;

#define OP(Name)                                   \
  virtual Name *as##Name(void) { return nullptr; } \
  virtual const Name *as##Name(void) const { return nullptr; }
#include "coco/IR/Op.lst"
#undef OP

  /**
   * @brief Op visitor interface
   *
   * WARN Use this interface only for coco-internal classes
   *      (to minimize changes upon Op extension)
   */
  template <typename T> struct IVisitor
  {
    virtual ~IVisitor() = default;

#define OP(Name) virtual T visit(const Name *) = 0;
#include "coco/IR/Op.lst"
#undef OP
  };

  template <typename T> struct Visitor : public IVisitor<T>
  {
    virtual ~Visitor() = default;

#define OP(Name) \
  T visit(const Name *) override { throw std::runtime_error{"NYI"}; }
#include "coco/IR/Op.lst"
#undef OP
  };

  template <typename T> T accept(IVisitor<T> *v) const
  {
#define OP(Name)            \
  if (auto op = as##Name()) \
  {                         \
    return v->visit(op);    \
  }
#include "coco/IR/Op.lst"
#undef OP
    throw std::runtime_error{"unreachable"};
  }

  template <typename T> T accept(IVisitor<T> &v) const { return accept(&v); }
  template <typename T> T accept(IVisitor<T> &&v) const { return accept(&v); }

public:
  /**
   * @brief Op mutator interface
   *
   * WARN Use this interface only for coco-internal classes
   *      (to minimize changes upon Instr extension)
   */
  struct IMutator
  {
    virtual ~IMutator() = default;

#define OP(Name) virtual void mutate(Name *) = 0;
#include "coco/IR/Op.lst"
#undef OP
  };

  struct Mutator : public IMutator
  {
    virtual ~Mutator() = default;

#define OP(Name) \
  void mutate(Name *) override { throw std::runtime_error{"NYI"}; }
#include "coco/IR/Op.lst"
#undef OP
  };

  void accept(IMutator *m)
  {
#define OP(Name)            \
  if (auto op = as##Name()) \
  {                         \
    return m->mutate(op);   \
  }
#include "coco/IR/Op.lst"
#undef OP
    throw std::runtime_error{"unreachable"};
  }

  void accept(IMutator &m) { return accept(&m); }
  void accept(IMutator &&m) { return accept(&m); }

public:
  Instr *parent(void) const;

  /// @brief Return a pointer to the parent Op
  Op *up(void) const;

private:
  /**
   * @brief A link to Instr from Op
   *
   * WARN Update this field only through Step
   */
  Step *_step = nullptr;

  /**
   * @brief A link to a parent Op
   *
   * WARN Update this field only through Part
   * NOTE An "Op" CANNOT have a link to a parent Op if it is linked to an "Instr"
   */
  Part *_part = nullptr;
};

/**
 * @brief Op with a single argument
 */
class UnaryOp : public Op
{
public:
  explicit UnaryOp();

public:
  UnaryOp(const UnaryOp &) = delete;
  UnaryOp(UnaryOp &&) = delete;

public:
  virtual ~UnaryOp() = default;

public:
  uint32_t arity(void) const final;
  Op *arg(uint32_t n) const final;

  std::set<Object *> uses(void) const final;

public:
  Op *arg(void) const { return _arg.child(); }
  void arg(Op *arg) { _arg.child(arg); }

private:
  /// @brief Link to Op's argument
  Part _arg;
};

/**
 * @brief Op with two arguments
 */
class BinaryOp : public Op
{
public:
  explicit BinaryOp();

public:
  BinaryOp(const BinaryOp &) = delete;
  BinaryOp(BinaryOp &&) = delete;

public:
  virtual ~BinaryOp() = default;

public:
  uint32_t arity(void) const final;
  Op *arg(uint32_t n) const final;

  std::set<Object *> uses(void) const final;

public:
  Op *left(void) const { return _left.child(); }
  void left(Op *op) { _left.child(op); }

public:
  Op *right(void) const { return _right.child(); }
  void right(Op *op) { _right.child(op); }

private:
  /// @brief Left-hand side (LHS) argument
  Part _left;
  /// @brief Right-hand side (RHS) argument
  Part _right;
};

/**
 * @brief Return the root Op from a given Op node
 *
 * @note root(op) == op holds for a root op
 */
Op *root(Op *);

} // namespace coco

#endif // __COCO_IR_OP_H__