tvm
repr_printer.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_NODE_REPR_PRINTER_H_
24 #define TVM_NODE_REPR_PRINTER_H_
25 
26 #include <tvm/ffi/reflection/access_path.h>
27 #include <tvm/node/functor.h>
29 
30 #include <iostream>
31 #include <string>
32 
33 namespace tvm {
35 class ReprPrinter {
36  public:
38  std::ostream& stream;
40  int indent{0};
41 
42  explicit ReprPrinter(std::ostream& stream) // NOLINT(*)
43  : stream(stream) {}
44 
46  TVM_DLL void Print(const ObjectRef& node);
48  TVM_DLL void Print(const ffi::Any& node);
50  TVM_DLL void PrintIndent();
51  // Allow registration to be printer.
52  using FType = NodeFunctor<void(const ObjectRef&, ReprPrinter*)>;
53  TVM_DLL static FType& vtable();
54 };
55 
60 TVM_DLL void Dump(const runtime::ObjectRef& node);
61 
66 TVM_DLL void Dump(const runtime::Object* node);
67 
68 } // namespace tvm
69 
70 namespace tvm {
71 namespace ffi {
72 // default print function for all objects
73 // provide in the runtime namespace as this is where objectref originally comes from.
74 inline std::ostream& operator<<(std::ostream& os, const ObjectRef& n) { // NOLINT(*)
75  ReprPrinter(os).Print(n);
76  return os;
77 }
78 
79 // default print function for any
80 inline std::ostream& operator<<(std::ostream& os, const Any& n) { // NOLINT(*)
81  ReprPrinter(os).Print(n);
82  return os;
83 }
84 
85 template <typename... V>
86 inline std::ostream& operator<<(std::ostream& os, const Variant<V...>& n) { // NOLINT(*)
87  ReprPrinter(os).Print(Any(n));
88  return os;
89 }
90 
91 namespace reflection {
92 
93 inline std::ostream& operator<<(std::ostream& os, const AccessStep& step) {
94  namespace refl = ffi::reflection;
95  switch (step->kind) {
96  case refl::AccessKind::kAttr: {
97  os << '.' << step->key.cast<String>();
98  return os;
99  }
100  case refl::AccessKind::kArrayItem: {
101  os << "[" << step->key.cast<int64_t>() << "]";
102  return os;
103  }
104  case refl::AccessKind::kMapItem: {
105  os << "[" << step->key << "]";
106  return os;
107  }
108  case refl::AccessKind::kAttrMissing: {
109  os << ".<missing attr " << step->key.cast<String>() << "`>";
110  return os;
111  }
112  case refl::AccessKind::kArrayItemMissing: {
113  os << "[<missing item at " << step->key.cast<int64_t>() << ">]";
114  return os;
115  }
116  case refl::AccessKind::kMapItemMissing: {
117  os << "[<missing item at " << step->key << ">]";
118  return os;
119  }
120  default: {
121  LOG(FATAL) << "Unknown access step kind: " << static_cast<int>(step->kind);
122  }
123  }
124  return os;
125 }
126 
127 inline std::ostream& operator<<(std::ostream& os, const AccessPath& path) {
128  Array<AccessStep> steps = path->ToSteps();
129  os << "<root>";
130  for (const auto& step : steps) {
131  os << step;
132  }
133  return os;
134 }
135 } // namespace reflection
136 } // namespace ffi
137 } // namespace tvm
138 #endif // TVM_NODE_REPR_PRINTER_H_
A dynamically dispatched functor on the type of the first argument.
Definition: functor.h:65
A printer class to print the AST/IR nodes.
Definition: repr_printer.h:35
void Print(const ObjectRef &node)
The node to be printed.
std::ostream & stream
The output stream.
Definition: repr_printer.h:38
ReprPrinter(std::ostream &stream)
Definition: repr_printer.h:42
void Print(const ffi::Any &node)
The node to be printed.
void PrintIndent()
Print indent to the stream.
static FType & vtable()
int indent
The indentation level.
Definition: repr_printer.h:40
Defines the Functor data structures.
std::ostream & operator<<(std::ostream &os, const AccessStep &step)
Definition: repr_printer.h:93
std::ostream & operator<<(std::ostream &os, const ObjectRef &n)
Definition: repr_printer.h:74
ffi::reflection::AccessPath AccessPath
Definition: doc.h:35
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
void Dump(const runtime::ObjectRef &node)
Dump the node to stderr, used for debug purposes.