summaryrefslogtreecommitdiff
path: root/compiler/encodump/src/Dump.cpp
blob: 7ec00e2e2312294f7dee31b95c4b26943575c8a5 (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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
 * 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 Dump.cpp
 * @brief Print coco IR produced from enco frontend
 *
 * @note  Some object inherits multiple parents.
 * For example, coco:Conv2D inherits coco::Consumer and more. Assume that op is an instance
 * of coco::Conv2D. In this case, the printing results of the following may be different:
 *     1) cout << op; // printing address of type coco::Conv2D
 *     2) cout << reinterpret_cast<const coco::Object::Consumer *>(op);
 *     3) cout << object->consumer(); // assume that this object->consumer() returns op
 *     4) cout << dynamic_cast<const coco::Object::Consumer *>(op);
 *  1) and 2) prints same address. 3) and 4) prints same address but different from 1) and 2).
 * For details, refer to
 * https://stackoverflow.com/questions/22256620/why-pointers-to-the-same-object-have-different-values
 * For dumping, we will use 3), 4)
 */
#include "Dump.h"

#include <functional>
#include <iostream>

std::string tab(int n) { return std::string(n * 2, ' '); }

struct OpPrinter final : public coco::Op::Visitor<void>
{
public:
  OpPrinter(std::ostream &os, int indent) : _os(os), _indent(indent) {}

public:
  void visit(const coco::Load *op) override
  {
    _os << tab(_indent) << "Load(" << dynamic_cast<const coco::Op *>(op)
        << ", obj: " << op->object() << ")" << std::endl;
  }

  void visit(const coco::PadF *op) override
  {
    op->arg()->accept(this);
    _os << tab(_indent) << "PadF" << std::endl;
  }

  void visit(const coco::Conv2D *op) override
  {
    op->arg()->accept(this);
    const coco::Padding2D *pad = op->pad();
    const coco::Stride2D *stride = op->stride();

    _os << tab(_indent) << "Conv2D(" << dynamic_cast<const coco::Op *>(op)
        << ", ker obj: " << op->ker() << ", padding [T/B/L/R=" << pad->top() << "," << pad->bottom()
        << "," << pad->left() << "," << pad->right() << "]"
        << ", stride [V/H = " << stride->vertical() << "," << stride->horizontal() << "]"
        << ")" << std::endl;
  }

  void visit(const coco::MaxPool2D *op) override
  {
    op->arg()->accept(this);
    _os << tab(_indent) << "MaxPool2D" << std::endl;
  }

  void visit(const coco::AvgPool2D *op) override
  {
    op->arg()->accept(this);
    _os << tab(_indent) << "AvgPool2D" << std::endl;
  }

  void visit(const coco::Add *op) override
  {
    op->left()->accept(this);
    op->right()->accept(this);
    _os << tab(_indent) << "Add" << std::endl;
  }

  void visit(const coco::Mul *op) override
  {
    op->left()->accept(this);
    op->right()->accept(this);
    _os << tab(_indent) << "Mul" << std::endl;
  }

  void visit(const coco::ReLU *op) override
  {
    op->arg()->accept(this);
    _os << tab(_indent) << "ReLU" << std::endl;
  }

  void visit(const coco::ReLU6 *op) override
  {
    op->arg()->accept(this);
    _os << tab(_indent) << "ReLU6" << std::endl;
  }

  void visit(const coco::Sub *op) override
  {
    op->left()->accept(this);
    op->right()->accept(this);
    _os << tab(_indent) << "Sub" << std::endl;
  }

  void visit(const coco::ConcatF *op) override
  {
    op->left()->accept(this);
    op->right()->accept(this);
    _os << tab(_indent) << "ConcatF" << std::endl;
  }

  void visit(const coco::Div *op) override
  {
    op->left()->accept(this);
    op->right()->accept(this);
    _os << tab(_indent) << "Div" << std::endl;
  }

private:
  std::ostream &_os;

private:
  int _indent;
};

struct InstrPrinter final : public coco::Instr::Visitor<void>
{
public:
  InstrPrinter() = delete;

  InstrPrinter(int indent) : _indent(indent) {}

  void visit(const coco::Eval *ins) override
  {
    std::cout << tab(_indent) << "Eval (" << dynamic_cast<const coco::Instr *>(ins) << ")"
              << std::endl;
    std::cout << tab(_indent + 1) << "out: " << ins->out() << std::endl;
    std::cout << tab(_indent + 1) << "<op>: " << std::endl;
    {
      OpPrinter prn(std::cout, _indent + 2);
      ins->op()->accept(prn);
    }
  }

  void visit(const coco::Copy *ins) override
  {
    // copy is Producer and also Customer. We will use address for Producer
    std::cout << tab(_indent) << "Copy (" << dynamic_cast<const coco::Instr *>(ins) << ")"
              << std::endl;
    std::cout << tab(_indent) << "  from: " << ins->from() << std::endl;
    std::cout << tab(_indent) << "  into: " << ins->into() << std::endl;
  }

  void visit(const coco::Shuffle *ins) override
  {
    std::cout << tab(_indent) << "Shuffle (" << dynamic_cast<const coco::Instr *>(ins) << ")"
              << std::endl;
    std::cout << tab(_indent) << "  from: " << ins->from() << std::endl;
    std::cout << tab(_indent) << "  into: " << ins->into() << std::endl;
  }

private:
  int _indent;
};

void dump(const coco::Op *op, int indent)
{
  OpPrinter prn(std::cout, indent);
  op->accept(prn);
}

void dump(const coco::Instr *ins, int indent)
{
  std::cout << tab(indent) << "<Inst>:" << std::endl;

  static InstrPrinter prn(indent + 1);

  ins->accept(prn);
}

void dump(const coco::Block *B, int indent)
{
  std::cout << tab(indent) << "<Block> (index: " << B->index().value() << ")" << std::endl;
  for (auto I = B->instr()->head(); I != nullptr; I = I->next())
  {
    dump(I, indent + 1);
  }
}

void dump(const coco::BlockList *L, int indent)
{
  for (auto B = L->head(); B != nullptr; B = B->next())
  {
    dump(B, indent);
  }
}

template <typename SetT, typename EntityF>
void dump(std::string header, SetT set, EntityF print_addr_f)
{
  std::cout << header << ": [";
  if (set->size() == 0)
    std::cout << "x";
  else
  {
    int idx = 0;
    for (auto entity : *set)
    {
      if (idx++ != 0)
        std::cout << ", ";
      print_addr_f(entity);
    }
  }
  std::cout << "]";
}

void dump(const coco::BagManager *l, int indent)
{
  std::cout << tab(indent) << "<Bag>:" << std::endl;

  for (auto n = 0; n < l->size(); ++n)
  {
    auto bag = l->at(n);

    std::cout << tab(indent + 1) << bag << ", ";

    // print objects in bag->deps()
    auto print_dep_object = [](coco::Dep *dep) { std::cout << dep->object(); };
    dump("obj", bag->deps(), print_dep_object);
    std::cout << ", ";

    std::cout << "size: " << bag->size() << ", ";

    if (bag->isInput())
      std::cout << "input, ";
    if (bag->isOutput())
      std::cout << "output, ";
    if ((!bag->isInput()) || (!bag->isOutput()))
      std::cout << "const, ";

    // print readers in bag->reads()
    auto print_read_reader = [](coco::Read *read) {
      if (coco::Op *op = dynamic_cast<coco::Op *>(read->reader()))
        std::cout << "op: " << op;
      else if (coco::Instr *instr = dynamic_cast<coco::Instr *>(read->reader()))
        std::cout << "instr: " << instr;
      else
        std::cout << "x";
    };
    dump("reader", bag->reads(), print_read_reader);
    std::cout << ", ";

    // print updaters in bag->updates()
    auto print_update_updater = [](coco::Update *update) {
      if (coco::Op *op = dynamic_cast<coco::Op *>(update->updater()))
        std::cout << "op: " << op;
      else if (coco::Instr *instr = dynamic_cast<coco::Instr *>(update->updater()))
        std::cout << "instr: " << instr;
      else
        std::cout << "x";
    };
    dump("updater", bag->updates(), print_update_updater);
    std::cout << ", ";

    std::cout << std::endl;
  }
}

void dump(coco::FeatureObject *feature_ob)
{
  auto shape = feature_ob->shape();
  std::cout << "kind: Feature, Shape [H/W/D=" << shape.height() << "," << shape.width() << ","
            << shape.depth() << "]";
}

void dump(coco::KernelObject *kernel_ob)
{
  auto shape = kernel_ob->shape();
  std::cout << "kind: Kernel, Shape [N/H/W/D=" << shape.count() << "," << shape.height() << ","
            << shape.width() << "," << shape.depth() << "]";
}

void dump(const coco::ObjectManager *l, int indent)
{
  std::cout << tab(indent) << "<Object>:" << std::endl;
  for (auto n = 0; n < l->size(); ++n)
  {
    auto obj = l->at(n);
    std::cout << tab(indent + 1) << obj << ", bag: " << obj->bag() << ", ";

    using ObDumpers = std::function<void(coco::Object * ob)>;

    std::map<coco::Object::Kind, ObDumpers> ob_dumpers;

    ob_dumpers[coco::Object::Kind::Feature] = [](coco::Object *ob) { dump(ob->asFeature()); };
    ob_dumpers[coco::Object::Kind::Kernel] = [](coco::Object *ob) { dump(ob->asKernel()); };
    ob_dumpers[coco::Object::Kind::Unknown] = [](coco::Object *ob) {
      std::cout << "kind: Unknown";
    };

    ob_dumpers[obj->kind()](obj);

    std::cout << ", producer: ";
    auto def = obj->def();
    if (def)
    {
      if (coco::Op *op = dynamic_cast<coco::Op *>(def->producer()))
        std::cout << "op: " << op;
      else if (coco::Instr *instr = dynamic_cast<coco::Instr *>(def->producer()))
        std::cout << "instr: " << instr;
      else
        std::cout << "x";
    }
    else
      std::cout << "x";
    std::cout << ", ";

    // print consumers in obj->uses()
    auto print_consumer = [](coco::Use *use) {
      if (coco::Op *op = dynamic_cast<coco::Op *>(use->consumer()))
        std::cout << "op: " << op;
      else if (coco::Instr *instr = dynamic_cast<coco::Instr *>(use->consumer()))
        std::cout << "inst: " << instr;
      else
        std::cout << "x";
    };
    dump("comsumer", obj->uses(), print_consumer);
    std::cout << std::endl;
  }
}

template <typename T> void head(int indent);

template <> void head<coco::Input>(int indent) { std::cout << tab(indent) << "<Input>: "; }

template <> void head<coco::Output>(int indent) { std::cout << tab(indent) << "<Output>: "; }

template <typename PtrItemT> void dump(const coco::PtrList<PtrItemT> *list, int indent)
{
  head<PtrItemT>(indent);
  for (int n = 0; n < list->size(); n++)
  {
    const PtrItemT *item = list->at(n);
    if (n != 0)
      std::cout << ", ";
    std::cout << "bag " << item->bag() << ", name=" << item->name();
  }
  std::cout << std::endl;
}

void dump(const coco::Module *module)
{
  std::cout << "<Module>" << std::endl;

  dump(module->block(), 1);
  dump(module->input(), 1);
  dump(module->output(), 1);
  dump(module->entity()->bag(), 1);
  dump(module->entity()->object(), 1);
}