tvm
ir_docsifier.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  */
19 #ifndef TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_
20 #define TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_
21 
22 #include <tvm/node/node.h>
23 #include <tvm/runtime/logging.h>
24 #include <tvm/script/printer/doc.h>
29 #include <tvm/support/with.h>
30 
31 namespace tvm {
32 namespace script {
33 namespace printer {
34 
36 
54 class IRDocsifierNode : public Object {
55  public:
77 
79  v->Visit("vars", &vars);
80  v->Visit("frames", &frames);
81  v->Visit("dispatch_tokens", &dispatch_tokens);
82  v->Visit("ir_prefix", &ir_prefix);
83  }
84 
85  static constexpr const char* _type_key = "script.printer.IRDocsifier";
87 
88  public:
95  template <class TDoc>
96  TDoc AsDoc(const TracedObject<ObjectRef>& obj) const {
97  auto result = Downcast<TDoc>(AsDocImpl(obj));
98  result->source_paths.push_back(obj.GetPath());
99  return result;
100  }
101 
108  ExprDoc AsExprDoc(const TracedObject<ObjectRef>& obj) { return AsDoc<ExprDoc>(obj); }
109 
121  this->dispatch_tokens.push_back(token);
122  return WithCtx(nullptr, [this]() { this->dispatch_tokens.pop_back(); });
123  }
124 
136  WithCtx WithFrame(const Frame& frame) {
137  frame->EnterWithScope();
138  this->frames.push_back(frame);
139  return WithCtx(nullptr, [this, pushed_frame = frame]() {
140  Frame last_frame = this->frames.back();
141  ICHECK_EQ(last_frame, pushed_frame);
142  this->frames.pop_back();
143  last_frame->ExitWithScope();
144  });
145  }
146 
151  template <typename FrameType>
153  for (auto it = frames.rbegin(); it != frames.rend(); ++it) {
154  if (const auto* f = (*it).as<typename FrameType::ContainerType>()) {
155  return GetRef<FrameType>(f);
156  }
157  }
158  return NullOpt;
159  }
160 
161  private:
162  Doc AsDocImpl(const TracedObject<ObjectRef>& obj) const;
163 };
164 
168 class IRDocsifier : public ObjectRef {
169  public:
175 
180  TVM_DLL static FType& vtable();
181 
183 };
184 
206  public:
209 
210  void VisitAttrs(tvm::AttrVisitor* v) { v->Visit("root_node", &root_node); }
211 
212  static constexpr const char* _type_key = "script.printer.RootNodeContainer";
214 };
215 
216 class RootNodeContainer : public ObjectRef {
217  public:
222  explicit RootNodeContainer(ObjectRef root_node);
224 };
225 
226 } // namespace printer
227 } // namespace script
228 } // namespace tvm
229 
230 #endif // TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_
WithCtx WithFrame(const Frame &frame)
Push a new frame the stack.
Definition: ir_docsifier.h:136
Traced wrapper for regular (non-container) TVM objects.
Definition: traced_object.h:39
Definition: ir_docsifier.h:216
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:758
Definitions and helper macros for IR/AST nodes.
const T back() const
Definition: array.h:443
void VisitAttrs(tvm::AttrVisitor *v)
Definition: ir_docsifier.h:210
void VisitAttrs(tvm::AttrVisitor *v)
Definition: ir_docsifier.h:78
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Reference type of IRDocsifierNode.
Definition: ir_docsifier.h:168
Reference type of DocNode.
Definition: doc.h:67
VarTable vars
The var table to use during the printing process.
Definition: ir_docsifier.h:60
Map< String, String > ir_prefix
This map connects IR dipatch token to the name of identifier.
Definition: ir_docsifier.h:76
base class of all object containers.
Definition: object.h:167
ObjectRef root_node
The root node to print.
Definition: ir_docsifier.h:208
TVM_DECLARE_FINAL_OBJECT_INFO(IRDocsifierNode, Object)
void push_back(const T &item)
push a new item to the back of the list
Definition: array.h:457
TDoc AsDoc(const TracedObject< ObjectRef > &obj) const
Transform the input object into TDoc.
Definition: ir_docsifier.h:96
Dynamic dispatch functor based on TracedObject.
Definition: traced_object_functor.h:82
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Reference type of FrameNode.
Definition: frame.h:76
Array< String > dispatch_tokens
The stack of dispatch tokens.
Definition: ir_docsifier.h:72
Reference to string objects.
Definition: string.h:97
reverse_iterator rbegin() const
Definition: array.h:393
const ObjectPath & GetPath() const
Get the path of the wrapped object.
Definition: traced_object.h:157
RAII wrapper function to enter and exit a context object similar to python&#39;s with syntax...
Definition: with.h:58
Base class of all object reference.
Definition: object.h:511
Array< Frame > frames
The stack of frames.
Definition: ir_docsifier.h:65
A wrapper object to provide injection point for printer of each IR.
Definition: ir_docsifier.h:205
IRDocsifier is the top-level interface in the IR->Doc process.
Definition: ir_docsifier.h:54
static constexpr const char * _type_key
Definition: ir_docsifier.h:85
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
void pop_back()
Remove the last item of the list.
Definition: array.h:503
Reference type of ExprDocNode.
Definition: doc.h:133
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
reverse_iterator rend() const
Definition: array.h:399
Reference type of VarTableNode.
Definition: var_table.h:142
WithCtx WithDispatchToken(const String &token)
Push a new dispatch token into the stack.
Definition: ir_docsifier.h:120
Optional< FrameType > GetFrame() const
Get the top frame with type FrameType.
Definition: ir_docsifier.h:152
With< ContextManager > WithCtx
Definition: ir_docsifier.h:35
ExprDoc AsExprDoc(const TracedObject< ObjectRef > &obj)
Helper method to transform object into ExprDoc.
Definition: ir_docsifier.h:108
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
RAII wrapper function to enter and exit a context object similar to python&#39;s with syntax...