summaryrefslogtreecommitdiff
path: root/compiler/coco/core/include/coco/IR/Instr.h
blob: fc1cc332dd6278a479100114314192bc20de74b2 (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
/*
 * 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.
 */

#ifndef __COCO_IR_INSTR_H__
#define __COCO_IR_INSTR_H__

#include "coco/IR/Bag.h"
#include "coco/IR/Block.forward.h"
#include "coco/IR/Instr.forward.h"
#include "coco/IR/InstrIndex.h"
#include "coco/IR/Entity.h"

#include "coco/ADT/DLinkedList.h"

#include <cassert>
#include <stdexcept>

namespace coco
{

#define INSTR(Name) class Name;
#include "coco/IR/Instr.lst"
#undef INSTR

using InstrList = coco::DLinkedList<Instr, Block>::Head;

/**
 * @brief Base interface on explicit computation steps in coco IR
 *
 * NOTE Input/output is explicit in Instr, but implicit in Op
 * NOTE Instr is may (not always) be a combination of multiple NN operations
 *
 * One may find a set of supported instructions from "Instrs.h"
 *
 * >> How to add a new base instruction in coco IR <<
 *
 * To introduce a new instruction (whose name is INS),
 *   1. Append "INSTR(INS)" to "Instr.lst"
 *   2. Declare class INS which inherits Instr class in "Instrs.h"
 *      NOTE This class SHOULD be default constructible
 *
 */
class Instr : public coco::DLinkedList<Instr, Block>::Node, public Entity
{
public:
  friend void DLinkedList<Instr, Block>::joined(Block *, Instr *);
  friend void DLinkedList<Instr, Block>::leaving(Block *, Instr *);

public:
  Instr() = default;

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

public:
  virtual ~Instr()
  {
    if (parent())
    {
      // NOTE It is safe to invoke detach here (although "Instr" is not a final class)
      //      as "leaving" hook accesses only the internal of "Instr" class
      detach();
    }
  }

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

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

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

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

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

public:
  template <typename T> T accept(IVisitor<T> *v) const
  {
#define INSTR(Name)          \
  if (auto ins = as##Name()) \
  {                          \
    return v->visit(ins);    \
  }
#include "coco/IR/Instr.lst"
#undef INSTR
    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:
  const InstrIndex &index(void) const { return _index; }

private:
  InstrIndex _index;
};

/**
 * @brief Return true if a given instruction is of T type
 *
 * @note "ins" cannot be a null pointer
 */
template <typename T> bool isa(const Instr *ins)
{
  assert(ins != nullptr);
  return dynamic_cast<const T *>(ins) != nullptr;
}

/**
 * @brief Cast as a derived instruction
 *
 * @note "safe_cast<T>(ins)" returns a null pointer if "ins" is not of T type
 * @note "safe_cast<T>(ins)" returns a null pointer if "ins" is a null pointer
 */
template <typename T> T *safe_cast(Instr *ins)
{
  // NOTE dynamic_cast<T *>(nullptr) returns nullptr
  return dynamic_cast<T *>(ins);
}

} // namespace coco

#endif // __COCO_IR_INSTR_H__