19 #ifndef TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_ 20 #define TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_ 28 #include <unordered_map> 29 #include <unordered_set> 40 class IRDocsifierNode;
56 v->Visit(
"stmts", &stmts);
61 static constexpr
const char*
_type_key =
"script.printer.Frame";
71 template <
typename TCallback>
73 callbacks.emplace_back(std::forward<TCallback>(cb));
100 virtual ~
Frame() =
default;
145 std::unordered_map<ObjectRef, VariableInfo, ObjectPtrHash, ObjectPtrEqual>
obj2info;
147 std::unordered_map<String, Array<ObjectRef>>
metadata;
151 std::unordered_map<const Object*, std::vector<const Object*>>
common_prefix;
156 v->Visit(
"frames", &frames);
157 v->Visit(
"dispatch_tokens", &dispatch_tokens);
165 static constexpr
const char*
_type_key =
"script.printer.IRDocsifier";
215 bool IsVarDefined(
const ObjectRef& obj)
const;
231 template <
class TDoc = Doc>
244 TVM_DLL
static FType& vtable();
253 d->
frames.push_back(GetRef<Frame>(
this));
258 for (
const std::function<
void()>& callback :
callbacks) {
267 template <
class TDoc>
270 if (cfg->obj_to_annotate.count(obj)) {
272 if (stmt->comment.defined()) {
273 stmt->comment = stmt->comment.value() +
"\n" + cfg->obj_to_annotate.at(obj);
275 stmt->comment = cfg->obj_to_annotate.at(obj);
278 LOG(WARNING) <<
"Expect StmtDoc to be annotated for object " << obj <<
", but got " 279 << Downcast<TDoc>(
d)->_type_key;
282 for (
const ObjectRef& o : cfg->obj_to_underline) {
284 cfg->path_to_underline.push_back(path);
287 for (
const auto& pair : cfg->path_to_annotate) {
289 String attn = pair.second;
290 if (p->IsPrefixOf(path) && path->IsPrefixOf(p)) {
292 if (stmt->comment.defined()) {
293 stmt->comment = stmt->comment.value() +
"\n" + attn;
295 stmt->comment = attn;
298 LOG(WARNING) <<
"Expect StmtDoc to be annotated at object path " << p <<
", but got " 299 << Downcast<TDoc>(
d)->_type_key;
305 template <
class TDoc>
309 d->source_paths.push_back(path);
310 AddDocDecoration<TDoc>(
d, obj, path, cfg);
311 return Downcast<TDoc>(
d);
317 d->dispatch_tokens.push_back(token);
325 #endif // TVM_SCRIPT_PRINTER_IR_DOCSIFIER_H_ void VisitAttrs(tvm::AttrVisitor *v)
Definition: ir_docsifier.h:55
std::unordered_map< const Object *, std::vector< const Object * > > common_prefix
Common prefixes of variable usages.
Definition: ir_docsifier.h:151
Definition: ir_docsifier.h:46
virtual ~FrameNode()=default
std::vector< std::function< void()> > callbacks
Definition: ir_docsifier.h:53
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:758
Reference type of IdDocNode.
Definition: doc.h:333
Definitions and helper macros for IR/AST nodes.
void EnterWithScope()
Method that's called when Frame enters the scope.
Definition: ir_docsifier.h:103
Information about a variable, including its optional name and its doc creator.
Definition: ir_docsifier.h:124
IRModule that holds the functions and type definitions.
const T back() const
Definition: array.h:443
void VisitAttrs(tvm::AttrVisitor *v)
Definition: ir_docsifier.h:155
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Reference type of IRDocsifierNode.
Definition: ir_docsifier.h:238
Reference type of DocNode.
Definition: doc.h:78
The base class of statement doc.
Definition: doc.h:154
void ExitWithScope()
Method that's called when Frame exits the scope.
Definition: ir_docsifier.h:106
Array< StmtDoc > stmts
Definition: ir_docsifier.h:49
Dynamic dispatch functor based on ObjectPath.
Definition: ir_docsifier_functor.h:42
static FType & vtable()
The registration table for IRDocsifier.
base class of all object containers.
Definition: object.h:167
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
bool defined() const
Definition: object.h:544
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
IRDocsifierNode * d
Definition: ir_docsifier.h:51
void AddDispatchToken(const IRDocsifier &d, const String &token)
Add a dispatch token to the docsifier, and a callback that pops the token when this frame exits...
Definition: ir_docsifier.h:316
Reference type of FrameNode.
Definition: ir_docsifier.h:95
Array< String > dispatch_tokens
The stack of dispatch tokens.
Definition: ir_docsifier.h:143
static constexpr const char * _type_key
Definition: ir_docsifier.h:61
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
std::function< ExprDoc()> DocCreator
A function that creates the doc for a variable.
Definition: ir_docsifier.h:122
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:522
const Object * get() const
Definition: object.h:546
Base class of all object reference.
Definition: object.h:511
Array< Frame > frames
The stack of frames.
Definition: ir_docsifier.h:136
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
virtual void EnterWithScope()
Method that's called when Frame enters the scope.
Definition: ir_docsifier.h:251
IRDocsifier is the top-level interface in the IR->Doc process.
Definition: ir_docsifier.h:119
DocCreator creator
The creator.
Definition: ir_docsifier.h:126
TVM_DECLARE_BASE_OBJECT_INFO(FrameNode, Object)
Definition: object_path.h:122
std::unordered_map< String, Array< ObjectRef > > metadata
Metadata printing.
Definition: ir_docsifier.h:147
TDoc AsDoc(const ObjectRef &obj, const ObjectPath &path) const
Transform the input object into TDoc.
Definition: ir_docsifier.h:306
std::unordered_set< std::string > ir_usage
The IR usages for headers printing.
Definition: ir_docsifier.h:153
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference type of ExprDocNode.
Definition: doc.h:135
void AddExitCallback(TCallback &&cb)
Add a callback function to be called when this frame exits.
Definition: ir_docsifier.h:72
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:865
std::unordered_set< String > defined_names
The variable names used already.
Definition: ir_docsifier.h:149
Definition: script_printer.h:97
virtual void ExitWithScope()
Method that's called when Frame exits the scope.
Definition: ir_docsifier.h:257
Optional< String > name
The name of the variable.
Definition: ir_docsifier.h:128
static LiteralDoc None(const Optional< ObjectPath > &p)
Create a LiteralDoc to represent None/null/empty value.
Definition: doc.h:263
std::unordered_map< ObjectRef, VariableInfo, ObjectPtrHash, ObjectPtrEqual > obj2info
Mapping from a var to its info.
Definition: ir_docsifier.h:145