tvm
doc.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_DOC_H_
20 #define TVM_SCRIPT_PRINTER_DOC_H_
21 
22 #include <tvm/ffi/reflection/access_path.h>
23 #include <tvm/ffi/reflection/registry.h>
24 #include <tvm/ir/expr.h>
25 #include <tvm/node/node.h>
26 #include <tvm/runtime/data_type.h>
27 #include <tvm/runtime/device_api.h>
28 
29 #include <string>
30 
31 namespace tvm {
32 namespace script {
33 namespace printer {
34 
36 
37 // Forward declaration
38 class Doc;
39 
45 ffi::String DocToPythonScript(Doc doc, const PrinterConfig& cfg);
46 
58 class DocNode : public Object {
59  public:
67  mutable ffi::Array<ffi::reflection::AccessPath> source_paths;
68 
69  static void RegisterReflection() {
70  namespace refl = tvm::ffi::reflection;
71  refl::ObjectDef<DocNode>().def_rw("source_paths", &DocNode::source_paths);
72  }
73 
74  static constexpr bool _type_mutable = true;
75 
76  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.Doc", DocNode, Object);
77 
78  public:
79  virtual ~DocNode() = default;
80 };
81 
87 class Doc : public ObjectRef {
88  protected:
89  Doc() = default;
90  explicit Doc(ObjectPtr<DocNode> data) : ObjectRef(data) {}
91 
92  public:
94 };
95 
96 class ExprDoc;
97 
103 class ExprDocNode : public DocNode {
104  public:
109  ExprDoc Attr(ffi::String attr) const;
110 
115  ExprDoc operator[](ffi::Array<Doc> indices) const;
116 
121  ExprDoc Call(ffi::Array<ExprDoc, void> args) const;
122 
129  ExprDoc Call(ffi::Array<ExprDoc, void> args, //
130  ffi::Array<ffi::String> kwargs_keys, //
131  ffi::Array<ExprDoc, void> kwargs_values) const;
132 
133  static void RegisterReflection() {
134  namespace refl = tvm::ffi::reflection;
135  refl::ObjectDef<ExprDocNode>();
136  }
137  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.ExprDoc", ExprDocNode, DocNode);
138 };
139 
145 class ExprDoc : public Doc {
146  protected:
147  ExprDoc() = default;
148 
149  public:
154  ExprDoc operator[](ffi::Array<Doc> indices) const;
155 
156  explicit ExprDoc(ObjectPtr<ExprDocNode> data) : Doc(data) { TVM_FFI_ICHECK(data != nullptr); }
157 
159 };
160 
166 class StmtDocNode : public DocNode {
167  public:
176  mutable ffi::Optional<ffi::String> comment{std::nullopt};
177 
178  static void RegisterReflection() {
179  namespace refl = tvm::ffi::reflection;
180  refl::ObjectDef<StmtDocNode>().def_rw("comment", &StmtDocNode::comment);
181  }
182  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.StmtDoc", StmtDocNode, DocNode);
183 };
184 
190 class StmtDoc : public Doc {
191  protected:
192  StmtDoc() = default;
193 
194  public:
196 };
197 
204 class StmtBlockDocNode : public DocNode {
205  public:
207  ffi::Array<StmtDoc> stmts;
208 
209  static void RegisterReflection() {
210  namespace refl = tvm::ffi::reflection;
211  refl::ObjectDef<StmtBlockDocNode>().def_ro("stmts", &StmtBlockDocNode::stmts);
212  }
214 };
215 
220 class StmtBlockDoc : public Doc {
221  public:
226  explicit StmtBlockDoc(ffi::Array<StmtDoc> stmts);
228 };
229 
235 class LiteralDocNode : public ExprDocNode {
236  public:
246  ffi::Any value;
247 
248  static void RegisterReflection() {
249  namespace refl = tvm::ffi::reflection;
250  refl::ObjectDef<LiteralDocNode>().def_ro("value", &LiteralDocNode::value);
251  }
253 };
254 
260 class LiteralDoc : public ExprDoc {
261  protected:
262  explicit LiteralDoc(ffi::Any value, const ffi::Optional<AccessPath>& object_path);
263 
264  public:
269  static LiteralDoc None(const ffi::Optional<AccessPath>& p) {
270  return LiteralDoc(ffi::Any(nullptr), p);
271  }
277  static LiteralDoc Int(int64_t v, const ffi::Optional<AccessPath>& p) {
278  return LiteralDoc(IntImm(DataType::Int(64), v), p);
279  }
285  static LiteralDoc Boolean(bool v, const ffi::Optional<AccessPath>& p) {
286  return LiteralDoc(IntImm(DataType::Bool(), v), p);
287  }
293  static LiteralDoc Float(double v, const ffi::Optional<AccessPath>& p) {
294  return LiteralDoc(FloatImm(DataType::Float(64), v), p);
295  }
301  static LiteralDoc Str(const ffi::String& v, const ffi::Optional<AccessPath>& p) {
302  return LiteralDoc(v, p);
303  }
309  static LiteralDoc DataType(const runtime::DataType& v, const ffi::Optional<AccessPath>& p) {
310  std::string dtype = v.is_void() ? "void" : runtime::DLDataTypeToString(v);
311  return LiteralDoc::Str(dtype, p);
312  }
318  static LiteralDoc Device(const DLDevice& v, const ffi::Optional<AccessPath>& p) {
319  std::ostringstream os;
320  runtime::operator<<(os, v);
321  return LiteralDoc::Str(os.str(), p);
322  }
323 
325 };
326 
332 class IdDocNode : public ExprDocNode {
333  public:
335  ffi::String name;
336 
337  static void RegisterReflection() {
338  namespace refl = tvm::ffi::reflection;
339  refl::ObjectDef<IdDocNode>().def_ro("name", &IdDocNode::name);
340  }
342 };
343 
349 class IdDoc : public ExprDoc {
350  public:
355  explicit IdDoc(ffi::String name);
356  explicit IdDoc(std::nullptr_t) : ExprDoc(nullptr) {}
358 };
359 
366  public:
368  ExprDoc value{ffi::UnsafeInit()};
370  ffi::String name;
371 
372  static void RegisterReflection() {
373  namespace refl = tvm::ffi::reflection;
374  refl::ObjectDef<AttrAccessDocNode>()
375  .def_ro("value", &AttrAccessDocNode::value)
376  .def_ro("name", &AttrAccessDocNode::name);
377  }
379 };
380 
386 class AttrAccessDoc : public ExprDoc {
387  public:
393  explicit AttrAccessDoc(ExprDoc value, ffi::String name);
395 };
396 
402 class IndexDocNode : public ExprDocNode {
403  public:
405  ExprDoc value{ffi::UnsafeInit()};
413  ffi::Array<Doc> indices; // Each element is union of: Slice / ExprDoc
414 
415  static void RegisterReflection() {
416  namespace refl = tvm::ffi::reflection;
417  refl::ObjectDef<IndexDocNode>()
418  .def_ro("value", &IndexDocNode::value)
419  .def_ro("indices", &IndexDocNode::indices);
420  }
422 };
423 
429 class IndexDoc : public ExprDoc {
430  public:
436  explicit IndexDoc(ExprDoc value, ffi::Array<Doc> indices);
438 };
439 
445 class CallDocNode : public ExprDocNode {
446  public:
448  ExprDoc callee{ffi::UnsafeInit()};
450  ffi::Array<ExprDoc> args;
452  ffi::Array<ffi::String> kwargs_keys;
459  ffi::Array<ExprDoc> kwargs_values;
460 
461  static void RegisterReflection() {
462  namespace refl = tvm::ffi::reflection;
463  refl::ObjectDef<CallDocNode>()
464  .def_ro("callee", &CallDocNode::callee)
465  .def_ro("args", &CallDocNode::args)
466  .def_ro("kwargs_keys", &CallDocNode::kwargs_keys)
467  .def_ro("kwargs_values", &CallDocNode::kwargs_values);
468  }
470 };
471 
477 class CallDoc : public ExprDoc {
478  public:
486  CallDoc(ExprDoc callee, ffi::Array<ExprDoc> args, ffi::Array<ffi::String> kwargs_keys,
487  ffi::Array<ExprDoc> kwargs_values);
489 };
490 
500  public:
501  enum class Kind : int32_t {
502  // Unary operators
503  kUnaryStart = 0,
504  kUSub = 1, // -x
505  kInvert = 2, // ~x
506  kNot = 3, // not x
507  kUnaryEnd = 4,
508 
509  // Binary operators
510  kBinaryStart = 5,
511  kAdd = 6, // +
512  kSub = 7, // -
513  kMult = 8, // *
514  kDiv = 9, // /
515  kFloorDiv = 10, // // in Python
516  kMod = 11, // % in Python
517  kPow = 12, // ** in Python
518  kLShift = 13, // <<
519  kRShift = 14, // >>
520  kBitAnd = 15, // &
521  kBitOr = 16, // |
522  kBitXor = 17, // ^
523  kLt = 18, // <
524  kLtE = 19, // <=
525  kEq = 20, // ==
526  kNotEq = 21, // !=
527  kGt = 22, // >
528  kGtE = 23, // >=
529  kAnd = 24, // and
530  kOr = 25, // or
531  kBinaryEnd = 26,
532 
533  // Special
534  kSpecialStart = 27,
535  kIfThenElse = 28, // <operands[1]> if <operands[0]> else <operands[2]>
536  kSpecialEnd = 29
537  };
538 
542  ffi::Array<ExprDoc> operands;
543 
544  static void RegisterReflection() {
545  namespace refl = tvm::ffi::reflection;
546  refl::ObjectDef<OperationDocNode>()
547  .def_ro("kind", &OperationDocNode::kind)
548  .def_ro("operands", &OperationDocNode::operands);
549  }
551 };
552 
558 class OperationDoc : public ExprDoc {
559  public:
565  explicit OperationDoc(OperationDocNode::Kind kind, ffi::Array<ExprDoc> operands);
567 };
568 
577 class LambdaDocNode : public ExprDocNode {
578  public:
580  ffi::Array<IdDoc> args;
582  ExprDoc body{ffi::UnsafeInit()};
583 
584  static void RegisterReflection() {
585  namespace refl = tvm::ffi::reflection;
586  refl::ObjectDef<LambdaDocNode>()
587  .def_ro("args", &LambdaDocNode::args)
588  .def_ro("body", &LambdaDocNode::body);
589  }
591 };
592 
598 class LambdaDoc : public ExprDoc {
599  public:
605  explicit LambdaDoc(ffi::Array<IdDoc> args, ExprDoc body);
607 };
608 
614 class TupleDocNode : public ExprDocNode {
615  public:
617  ffi::Array<ExprDoc> elements;
618 
619  static void RegisterReflection() {
620  namespace refl = tvm::ffi::reflection;
621  refl::ObjectDef<TupleDocNode>().def_ro("elements", &TupleDocNode::elements);
622  }
624 };
625 
631 class TupleDoc : public ExprDoc {
632  public:
636  TupleDoc() : ExprDoc(ffi::make_object<TupleDocNode>()) {}
641  explicit TupleDoc(ffi::Array<ExprDoc> elements);
643 };
644 
650 class ListDocNode : public ExprDocNode {
651  public:
653  ffi::Array<ExprDoc> elements;
654 
655  static void RegisterReflection() {
656  namespace refl = tvm::ffi::reflection;
657  refl::ObjectDef<ListDocNode>().def_ro("elements", &ListDocNode::elements);
658  }
660 };
661 
667 class ListDoc : public ExprDoc {
668  public:
672  ListDoc() : ExprDoc(ffi::make_object<ListDocNode>()) {}
677  explicit ListDoc(ffi::Array<ExprDoc> elements);
679 };
680 
686 class DictDocNode : public ExprDocNode {
687  public:
689  ffi::Array<ExprDoc> keys;
696  ffi::Array<ExprDoc> values;
697 
698  static void RegisterReflection() {
699  namespace refl = tvm::ffi::reflection;
700  refl::ObjectDef<DictDocNode>()
701  .def_ro("keys", &DictDocNode::keys)
702  .def_ro("values", &DictDocNode::values);
703  }
705 };
706 
712 class DictDoc : public ExprDoc {
713  public:
717  DictDoc() : ExprDoc(ffi::make_object<DictDocNode>()) {}
723  explicit DictDoc(ffi::Array<ExprDoc> keys, ffi::Array<ExprDoc> values);
725 };
726 
734 class SliceDocNode : public DocNode {
735  public:
737  ffi::Optional<ExprDoc> start;
739  ffi::Optional<ExprDoc> stop;
741  ffi::Optional<ExprDoc> step;
742 
743  static void RegisterReflection() {
744  namespace refl = tvm::ffi::reflection;
745  refl::ObjectDef<SliceDocNode>()
746  .def_ro("start", &SliceDocNode::start)
747  .def_ro("stop", &SliceDocNode::stop)
748  .def_ro("step", &SliceDocNode::step);
749  }
751 };
752 
758 class SliceDoc : public Doc {
759  public:
766  explicit SliceDoc(ffi::Optional<ExprDoc> start, ffi::Optional<ExprDoc> stop,
767  ffi::Optional<ExprDoc> step);
769 };
770 
776 class AssignDocNode : public StmtDocNode {
777  public:
779  ExprDoc lhs{ffi::UnsafeInit()};
785  ffi::Optional<ExprDoc> rhs;
787  ffi::Optional<ExprDoc> annotation;
788 
789  static void RegisterReflection() {
790  namespace refl = tvm::ffi::reflection;
791  refl::ObjectDef<AssignDocNode>()
792  .def_ro("lhs", &AssignDocNode::lhs)
793  .def_ro("rhs", &AssignDocNode::rhs)
794  .def_ro("annotation", &AssignDocNode::annotation);
795  }
797 };
798 
804 class AssignDoc : public StmtDoc {
805  public:
812  explicit AssignDoc(ExprDoc lhs, ffi::Optional<ExprDoc> rhs, ffi::Optional<ExprDoc> annotation);
814 };
815 
821 class IfDocNode : public StmtDocNode {
822  public:
824  ExprDoc predicate{ffi::UnsafeInit()};
826  ffi::Array<StmtDoc> then_branch;
828  ffi::Array<StmtDoc> else_branch;
829 
830  static void RegisterReflection() {
831  namespace refl = tvm::ffi::reflection;
832  refl::ObjectDef<IfDocNode>()
833  .def_ro("predicate", &IfDocNode::predicate)
834  .def_ro("then_branch", &IfDocNode::then_branch)
835  .def_ro("else_branch", &IfDocNode::else_branch);
836  }
838 };
839 
845 class IfDoc : public StmtDoc {
846  public:
853  explicit IfDoc(ExprDoc predicate, ffi::Array<StmtDoc> then_branch,
854  ffi::Array<StmtDoc> else_branch);
856 };
857 
863 class WhileDocNode : public StmtDocNode {
864  public:
866  ExprDoc predicate{ffi::UnsafeInit()};
868  ffi::Array<StmtDoc> body;
869 
870  static void RegisterReflection() {
871  namespace refl = tvm::ffi::reflection;
872  refl::ObjectDef<WhileDocNode>()
873  .def_ro("predicate", &WhileDocNode::predicate)
874  .def_ro("body", &WhileDocNode::body);
875  }
877 };
878 
884 class WhileDoc : public StmtDoc {
885  public:
891  explicit WhileDoc(ExprDoc predicate, ffi::Array<StmtDoc> body);
893 };
894 
904 class ForDocNode : public StmtDocNode {
905  public:
907  ExprDoc lhs{ffi::UnsafeInit()};
909  ExprDoc rhs{ffi::UnsafeInit()};
911  ffi::Array<StmtDoc> body;
912 
913  static void RegisterReflection() {
914  namespace refl = tvm::ffi::reflection;
915  refl::ObjectDef<ForDocNode>()
916  .def_ro("lhs", &ForDocNode::lhs)
917  .def_ro("rhs", &ForDocNode::rhs)
918  .def_ro("body", &ForDocNode::body);
919  }
921 };
922 
928 class ForDoc : public StmtDoc {
929  public:
936  explicit ForDoc(ExprDoc lhs, ExprDoc rhs, ffi::Array<StmtDoc> body);
938 };
939 
950 class ScopeDocNode : public StmtDocNode {
951  public:
953  ffi::Optional<ExprDoc> lhs{std::nullopt};
955  ExprDoc rhs{ffi::UnsafeInit()};
957  ffi::Array<StmtDoc> body;
958 
959  static void RegisterReflection() {
960  namespace refl = tvm::ffi::reflection;
961  refl::ObjectDef<ScopeDocNode>()
962  .def_ro("lhs", &ScopeDocNode::lhs)
963  .def_ro("rhs", &ScopeDocNode::rhs)
964  .def_ro("body", &ScopeDocNode::body);
965  }
967 };
968 
974 class ScopeDoc : public StmtDoc {
975  public:
982  explicit ScopeDoc(ffi::Optional<ExprDoc> lhs, ExprDoc rhs, ffi::Array<StmtDoc> body);
983 
989  explicit ScopeDoc(ExprDoc rhs, ffi::Array<StmtDoc> body);
990 
992 };
993 
999 class ExprStmtDocNode : public StmtDocNode {
1000  public:
1002  ExprDoc expr{ffi::UnsafeInit()};
1003 
1004  static void RegisterReflection() {
1005  namespace refl = tvm::ffi::reflection;
1006  refl::ObjectDef<ExprStmtDocNode>().def_ro("expr", &ExprStmtDocNode::expr);
1007  }
1009 };
1010 
1016 class ExprStmtDoc : public StmtDoc {
1017  public:
1022  explicit ExprStmtDoc(ExprDoc expr);
1024 };
1025 
1031 class AssertDocNode : public StmtDocNode {
1032  public:
1034  ExprDoc test{ffi::UnsafeInit()};
1036  ffi::Optional<ExprDoc> msg{std::nullopt};
1037 
1038  static void RegisterReflection() {
1039  namespace refl = tvm::ffi::reflection;
1040  refl::ObjectDef<AssertDocNode>()
1041  .def_ro("test", &AssertDocNode::test)
1042  .def_ro("msg", &AssertDocNode::msg);
1043  }
1045 };
1046 
1052 class AssertDoc : public StmtDoc {
1053  public:
1059  explicit AssertDoc(ExprDoc test, ffi::Optional<ExprDoc> msg = std::nullopt);
1061 };
1062 
1068 class ReturnDocNode : public StmtDocNode {
1069  public:
1071  ExprDoc value{ffi::UnsafeInit()};
1072 
1073  static void RegisterReflection() {
1074  namespace refl = tvm::ffi::reflection;
1075  refl::ObjectDef<ReturnDocNode>().def_ro("value", &ReturnDocNode::value);
1076  }
1078 };
1079 
1085 class ReturnDoc : public StmtDoc {
1086  public:
1091  explicit ReturnDoc(ExprDoc value);
1093 };
1094 
1101  public:
1103  IdDoc name{ffi::UnsafeInit{}};
1111  ffi::Array<AssignDoc> args;
1113  ffi::Array<ExprDoc> decorators;
1115  ffi::Optional<ExprDoc> return_type{std::nullopt};
1117  ffi::Array<StmtDoc> body;
1118 
1119  static void RegisterReflection() {
1120  namespace refl = tvm::ffi::reflection;
1121  refl::ObjectDef<FunctionDocNode>()
1122  .def_ro("name", &FunctionDocNode::name)
1123  .def_ro("args", &FunctionDocNode::args)
1124  .def_ro("decorators", &FunctionDocNode::decorators)
1125  .def_ro("return_type", &FunctionDocNode::return_type)
1126  .def_ro("body", &FunctionDocNode::body);
1127  }
1129 };
1130 
1136 class FunctionDoc : public StmtDoc {
1137  public:
1146  explicit FunctionDoc(IdDoc name, ffi::Array<AssignDoc> args, ffi::Array<ExprDoc> decorators,
1147  ffi::Optional<ExprDoc> return_type, ffi::Array<StmtDoc> body);
1149 };
1150 
1156 class ClassDocNode : public StmtDocNode {
1157  public:
1159  IdDoc name{ffi::UnsafeInit{}};
1161  ffi::Array<ExprDoc> decorators;
1163  ffi::Array<StmtDoc> body;
1164 
1165  static void RegisterReflection() {
1166  namespace refl = tvm::ffi::reflection;
1167  refl::ObjectDef<ClassDocNode>()
1168  .def_ro("name", &ClassDocNode::name)
1169  .def_ro("decorators", &ClassDocNode::decorators)
1170  .def_ro("body", &ClassDocNode::body);
1171  }
1173 };
1174 
1180 class ClassDoc : public StmtDoc {
1181  public:
1188  explicit ClassDoc(IdDoc name, ffi::Array<ExprDoc> decorators, ffi::Array<StmtDoc> body);
1190 };
1191 
1197 class CommentDocNode : public StmtDocNode {
1198  public:
1199  static void RegisterReflection() {
1200  namespace refl = tvm::ffi::reflection;
1201  refl::ObjectDef<CommentDocNode>();
1202  }
1204 };
1205 
1211 class CommentDoc : public StmtDoc {
1212  public:
1213  explicit CommentDoc(ffi::String comment);
1215 };
1216 
1223  public:
1224  static void RegisterReflection() {
1225  namespace refl = tvm::ffi::reflection;
1226  refl::ObjectDef<DocStringDocNode>();
1227  }
1229 };
1230 
1236 class DocStringDoc : public StmtDoc {
1237  public:
1238  explicit DocStringDoc(ffi::String docs);
1240 };
1241 
1242 } // namespace printer
1243 } // namespace script
1244 } // namespace tvm
1245 
1246 #endif // TVM_SCRIPT_PRINTER_DOC_H_
Managed reference class to FloatImmNode.
Definition: expr.h:545
Managed reference class to IntImmNode.
Definition: expr.h:510
Definition: script_printer.h:155
Runtime primitive data type.
Definition: data_type.h:47
static DataType Float(int bits, int lanes=1)
Construct an float type.
Definition: data_type.h:291
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:383
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:274
bool is_void() const
Definition: data_type.h:209
Doc that represents assert statement.
Definition: doc.h:1031
ffi::Optional< ExprDoc > msg
The optional error message when assertion failed.
Definition: doc.h:1036
static void RegisterReflection()
Definition: doc.h:1038
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AssertDoc", AssertDocNode, StmtDocNode)
ExprDoc test
The expression to test.
Definition: doc.h:1034
Reference type of AssertDocNode.
Definition: doc.h:1052
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AssertDoc, StmtDoc, AssertDocNode)
AssertDoc(ExprDoc test, ffi::Optional< ExprDoc > msg=std::nullopt)
Constructor of AssertDoc.
Doc that represents assign statement.
Definition: doc.h:776
ffi::Optional< ExprDoc > annotation
The type annotation of this assignment.
Definition: doc.h:787
ffi::Optional< ExprDoc > rhs
The right hand side of the assignment.
Definition: doc.h:785
ExprDoc lhs
The left hand side of the assignment.
Definition: doc.h:779
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AssignDoc", AssignDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:789
Reference type of AssignDocNode.
Definition: doc.h:804
AssignDoc(ExprDoc lhs, ffi::Optional< ExprDoc > rhs, ffi::Optional< ExprDoc > annotation)
Constructor of AssignDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AssignDoc, StmtDoc, AssignDocNode)
Doc that represents attribute access on another expression.
Definition: doc.h:365
ffi::String name
The attribute to be accessed.
Definition: doc.h:370
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AttrAccessDoc", AttrAccessDocNode, ExprDocNode)
ExprDoc value
The target expression to be accessed.
Definition: doc.h:368
static void RegisterReflection()
Definition: doc.h:372
Reference type of AttrAccessDocNode.
Definition: doc.h:386
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(AttrAccessDoc, ExprDoc, AttrAccessDocNode)
AttrAccessDoc(ExprDoc value, ffi::String name)
Constructor of AttrAccessDoc.
Doc that represents function call.
Definition: doc.h:445
ffi::Array< ffi::String > kwargs_keys
The keys of keyword arguments.
Definition: doc.h:452
ffi::Array< ExprDoc > kwargs_values
The values of keyword arguments.
Definition: doc.h:459
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.CallDoc", CallDocNode, ExprDocNode)
static void RegisterReflection()
Definition: doc.h:461
ffi::Array< ExprDoc > args
The positional arguments.
Definition: doc.h:450
ExprDoc callee
The callee of this function call.
Definition: doc.h:448
Reference type of CallDocNode.
Definition: doc.h:477
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(CallDoc, ExprDoc, CallDocNode)
CallDoc(ExprDoc callee, ffi::Array< ExprDoc > args, ffi::Array< ffi::String > kwargs_keys, ffi::Array< ExprDoc > kwargs_values)
Constructor of CallDoc.
Doc that represents class definition.
Definition: doc.h:1156
ffi::Array< ExprDoc > decorators
Decorators of class.
Definition: doc.h:1161
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ClassDoc", ClassDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:1165
IdDoc name
The name of class.
Definition: doc.h:1159
ffi::Array< StmtDoc > body
The body of class.
Definition: doc.h:1163
Reference type of ClassDocNode.
Definition: doc.h:1180
ClassDoc(IdDoc name, ffi::Array< ExprDoc > decorators, ffi::Array< StmtDoc > body)
Constructor of ClassDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ClassDoc, StmtDoc, ClassDocNode)
Doc that represents comment.
Definition: doc.h:1197
static void RegisterReflection()
Definition: doc.h:1199
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.CommentDoc", CommentDocNode, StmtDocNode)
Reference type of CommentDocNode.
Definition: doc.h:1211
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(CommentDoc, StmtDoc, CommentDocNode)
CommentDoc(ffi::String comment)
Doc that represents dictionary literal.
Definition: doc.h:686
ffi::Array< ExprDoc > keys
keys of dictionary
Definition: doc.h:689
static void RegisterReflection()
Definition: doc.h:698
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.DictDoc", DictDocNode, ExprDocNode)
ffi::Array< ExprDoc > values
Values of dictionary.
Definition: doc.h:696
Reference type of DictDocNode.
Definition: doc.h:712
DictDoc(ffi::Array< ExprDoc > keys, ffi::Array< ExprDoc > values)
Constructor of DictDoc.
DictDoc()
Create an empty dictionary.
Definition: doc.h:717
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DictDoc, ExprDoc, DictDocNode)
The base class of all Doc.
Definition: doc.h:58
TVM_FFI_DECLARE_OBJECT_INFO("script.printer.Doc", DocNode, Object)
static constexpr bool _type_mutable
Definition: doc.h:74
virtual ~DocNode()=default
static void RegisterReflection()
Definition: doc.h:69
ffi::Array< ffi::reflection::AccessPath > source_paths
The list of object paths of the source IR node.
Definition: doc.h:67
Doc that represents docstring.
Definition: doc.h:1222
static void RegisterReflection()
Definition: doc.h:1224
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.DocStringDoc", DocStringDocNode, StmtDocNode)
Reference type of DocStringDocNode.
Definition: doc.h:1236
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DocStringDoc, StmtDoc, DocStringDocNode)
Reference type of DocNode.
Definition: doc.h:87
Doc(ObjectPtr< DocNode > data)
Definition: doc.h:90
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Doc, ObjectRef, DocNode)
The base class of expression doc.
Definition: doc.h:103
ExprDoc Attr(ffi::String attr) const
Create a doc representing attribute access on the current ExprDoc.
static void RegisterReflection()
Definition: doc.h:133
TVM_FFI_DECLARE_OBJECT_INFO("script.printer.ExprDoc", ExprDocNode, DocNode)
ExprDoc Call(ffi::Array< ExprDoc, void > args) const
Create a doc representing calling the current ExprDoc.
ExprDoc operator[](ffi::Array< Doc > indices) const
Create a doc representing index access on the current ExprDoc.
ExprDoc Call(ffi::Array< ExprDoc, void > args, ffi::Array< ffi::String > kwargs_keys, ffi::Array< ExprDoc, void > kwargs_values) const
Create a doc representing attribute access on the current ExprDoc.
Reference type of ExprDocNode.
Definition: doc.h:145
ExprDoc(ObjectPtr< ExprDocNode > data)
Definition: doc.h:156
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExprDoc, Doc, ExprDocNode)
ExprDoc operator[](ffi::Array< Doc > indices) const
Create a doc representing index access on the current ExprDoc.
Doc that represents an expression as statement.
Definition: doc.h:999
static void RegisterReflection()
Definition: doc.h:1004
ExprDoc expr
The expression represented by this doc.
Definition: doc.h:1002
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ExprStmtDoc", ExprStmtDocNode, StmtDocNode)
Reference type of ExprStmtDocNode.
Definition: doc.h:1016
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExprStmtDoc, StmtDoc, ExprStmtDocNode)
ExprStmtDoc(ExprDoc expr)
Constructor of ExprStmtDoc.
Doc that represents for statement.
Definition: doc.h:904
ffi::Array< StmtDoc > body
The body of the for statement.
Definition: doc.h:911
ExprDoc lhs
The left hand side of the assignment of iterating variable.
Definition: doc.h:907
static void RegisterReflection()
Definition: doc.h:913
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ForDoc", ForDocNode, StmtDocNode)
ExprDoc rhs
The right hand side of the assignment of iterating variable.
Definition: doc.h:909
Reference type of ForDocNode.
Definition: doc.h:928
ForDoc(ExprDoc lhs, ExprDoc rhs, ffi::Array< StmtDoc > body)
Constructor of ForDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ForDoc, StmtDoc, ForDocNode)
Doc that represents function definition.
Definition: doc.h:1100
ffi::Array< AssignDoc > args
The arguments of function.
Definition: doc.h:1111
ffi::Array< StmtDoc > body
The body of function.
Definition: doc.h:1117
IdDoc name
The name of function.
Definition: doc.h:1103
static void RegisterReflection()
Definition: doc.h:1119
ffi::Array< ExprDoc > decorators
Decorators of function.
Definition: doc.h:1113
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.FunctionDoc", FunctionDocNode, StmtDocNode)
ffi::Optional< ExprDoc > return_type
The return type of function.
Definition: doc.h:1115
Reference type of FunctionDocNode.
Definition: doc.h:1136
FunctionDoc(IdDoc name, ffi::Array< AssignDoc > args, ffi::Array< ExprDoc > decorators, ffi::Optional< ExprDoc > return_type, ffi::Array< StmtDoc > body)
Constructor of FunctionDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(FunctionDoc, StmtDoc, FunctionDocNode)
Doc that represents identifier.
Definition: doc.h:332
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IdDoc", IdDocNode, ExprDocNode)
ffi::String name
The name of the identifier.
Definition: doc.h:335
static void RegisterReflection()
Definition: doc.h:337
Reference type of IdDocNode.
Definition: doc.h:349
IdDoc(std::nullptr_t)
Definition: doc.h:356
IdDoc(ffi::String name)
Constructor of IdDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IdDoc, ExprDoc, IdDocNode)
Doc that represent if-then-else statement.
Definition: doc.h:821
static void RegisterReflection()
Definition: doc.h:830
ffi::Array< StmtDoc > else_branch
The else branch of the if-then-else statement.
Definition: doc.h:828
ffi::Array< StmtDoc > then_branch
The then branch of the if-then-else statement.
Definition: doc.h:826
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IfDoc", IfDocNode, StmtDocNode)
ExprDoc predicate
The predicate of the if-then-else statement.
Definition: doc.h:824
Reference type of IfDocNode.
Definition: doc.h:845
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IfDoc, StmtDoc, IfDocNode)
IfDoc(ExprDoc predicate, ffi::Array< StmtDoc > then_branch, ffi::Array< StmtDoc > else_branch)
Constructor of IfDoc.
Doc that represents index access on another expression.
Definition: doc.h:402
static void RegisterReflection()
Definition: doc.h:415
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IndexDoc", IndexDocNode, ExprDocNode)
ffi::Array< Doc > indices
The indices to access.
Definition: doc.h:413
ExprDoc value
The container value to be accessed.
Definition: doc.h:405
Reference type of IndexDocNode.
Definition: doc.h:429
IndexDoc(ExprDoc value, ffi::Array< Doc > indices)
Constructor of IndexDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IndexDoc, ExprDoc, IndexDocNode)
Doc that represents anonymous function.
Definition: doc.h:577
static void RegisterReflection()
Definition: doc.h:584
ExprDoc body
The body of this anonymous function.
Definition: doc.h:582
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.LambdaDoc", LambdaDocNode, ExprDocNode)
ffi::Array< IdDoc > args
The arguments of this anonymous function.
Definition: doc.h:580
Reference type of LambdaDocNode.
Definition: doc.h:598
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(LambdaDoc, ExprDoc, LambdaDocNode)
LambdaDoc(ffi::Array< IdDoc > args, ExprDoc body)
Constructor of LambdaDoc.
Doc that represents list literal.
Definition: doc.h:650
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ListDoc", ListDocNode, ExprDocNode)
ffi::Array< ExprDoc > elements
Elements of list.
Definition: doc.h:653
static void RegisterReflection()
Definition: doc.h:655
Reference type of ListDocNode.
Definition: doc.h:667
ListDoc()
Create an empty ListDoc.
Definition: doc.h:672
ListDoc(ffi::Array< ExprDoc > elements)
Constructor of ListDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ListDoc, ExprDoc, ListDocNode)
Doc that represents literal value.
Definition: doc.h:235
ffi::Any value
the internal representation of the literal value.
Definition: doc.h:246
static void RegisterReflection()
Definition: doc.h:248
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.LiteralDoc", LiteralDocNode, ExprDocNode)
Reference type of LiteralDocNode.
Definition: doc.h:260
LiteralDoc(ffi::Any value, const ffi::Optional< AccessPath > &object_path)
static LiteralDoc Boolean(bool v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent boolean.
Definition: doc.h:285
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(LiteralDoc, ExprDoc, LiteralDocNode)
static LiteralDoc DataType(const runtime::DataType &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition: doc.h:309
static LiteralDoc Device(const DLDevice &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent device.
Definition: doc.h:318
static LiteralDoc Int(int64_t v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent integer.
Definition: doc.h:277
static LiteralDoc Float(double v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent float.
Definition: doc.h:293
static LiteralDoc None(const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent None/null/empty value.
Definition: doc.h:269
static LiteralDoc Str(const ffi::String &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition: doc.h:301
Doc that represents operation.
Definition: doc.h:499
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.OperationDoc", OperationDocNode, ExprDocNode)
ffi::Array< ExprDoc > operands
Operands of this expression.
Definition: doc.h:542
Kind kind
The kind of operation (operator)
Definition: doc.h:540
static void RegisterReflection()
Definition: doc.h:544
Reference type of OperationDocNode.
Definition: doc.h:558
OperationDoc(OperationDocNode::Kind kind, ffi::Array< ExprDoc > operands)
Constructor of OperationDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(OperationDoc, ExprDoc, OperationDocNode)
Doc that represents return statement.
Definition: doc.h:1068
ExprDoc value
The value to return.
Definition: doc.h:1071
static void RegisterReflection()
Definition: doc.h:1073
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ReturnDoc", ReturnDocNode, StmtDocNode)
Reference type of ReturnDocNode.
Definition: doc.h:1085
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ReturnDoc, StmtDoc, ReturnDocNode)
ReturnDoc(ExprDoc value)
Constructor of ReturnDoc.
Doc that represents special scopes.
Definition: doc.h:950
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ScopeDoc", ScopeDocNode, StmtDocNode)
ffi::Optional< ExprDoc > lhs
The name of the scoped variable.
Definition: doc.h:953
ExprDoc rhs
The value of the scoped variable.
Definition: doc.h:955
static void RegisterReflection()
Definition: doc.h:959
ffi::Array< StmtDoc > body
The body of the scope doc.
Definition: doc.h:957
Reference type of ScopeDocNode.
Definition: doc.h:974
ScopeDoc(ffi::Optional< ExprDoc > lhs, ExprDoc rhs, ffi::Array< StmtDoc > body)
Constructor of ScopeDoc.
ScopeDoc(ExprDoc rhs, ffi::Array< StmtDoc > body)
Constructor of ScopeDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ScopeDoc, StmtDoc, ScopeDocNode)
Doc that represents slice in Index expression.
Definition: doc.h:734
ffi::Optional< ExprDoc > start
The start of slice.
Definition: doc.h:737
ffi::Optional< ExprDoc > step
The step of slice.
Definition: doc.h:741
ffi::Optional< ExprDoc > stop
The exclusive end of slice.
Definition: doc.h:739
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.SliceDoc", SliceDocNode, DocNode)
static void RegisterReflection()
Definition: doc.h:743
Reference type of SliceDocNode.
Definition: doc.h:758
SliceDoc(ffi::Optional< ExprDoc > start, ffi::Optional< ExprDoc > stop, ffi::Optional< ExprDoc > step)
Constructor of SliceDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(SliceDoc, Doc, SliceDocNode)
The container doc that holds a list of StmtDoc.
Definition: doc.h:204
static void RegisterReflection()
Definition: doc.h:209
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.StmtBlockDoc", StmtBlockDocNode, DocNode)
ffi::Array< StmtDoc > stmts
The list of statements.
Definition: doc.h:207
Reference type of StmtBlockDocNode.
Definition: doc.h:220
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtBlockDoc, Doc, StmtBlockDocNode)
StmtBlockDoc(ffi::Array< StmtDoc > stmts)
Constructor of StmtBlockDoc.
The base class of statement doc.
Definition: doc.h:166
ffi::Optional< ffi::String > comment
The comment of this doc.
Definition: doc.h:176
static void RegisterReflection()
Definition: doc.h:178
TVM_FFI_DECLARE_OBJECT_INFO("script.printer.StmtDoc", StmtDocNode, DocNode)
Reference type of StmtDocNode.
Definition: doc.h:190
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtDoc, Doc, StmtDocNode)
Doc that represents tuple literal.
Definition: doc.h:614
ffi::Array< ExprDoc > elements
Elements of tuple.
Definition: doc.h:617
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.TupleDoc", TupleDocNode, ExprDocNode)
static void RegisterReflection()
Definition: doc.h:619
Reference type of TupleDocNode.
Definition: doc.h:631
TupleDoc(ffi::Array< ExprDoc > elements)
Constructor of TupleDoc.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TupleDoc, ExprDoc, TupleDocNode)
TupleDoc()
Create an empty TupleDoc.
Definition: doc.h:636
Doc that represents while statement.
Definition: doc.h:863
static void RegisterReflection()
Definition: doc.h:870
ffi::Array< StmtDoc > body
The body of the while statement.
Definition: doc.h:868
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.WhileDoc", WhileDocNode, StmtDocNode)
ExprDoc predicate
The predicate of the while statement.
Definition: doc.h:866
Reference type of WhileDocNode.
Definition: doc.h:884
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(WhileDoc, StmtDoc, WhileDocNode)
WhileDoc(ExprDoc predicate, ffi::Array< StmtDoc > body)
Constructor of WhileDoc.
Abstract device memory management API.
Base expr nodes in TVM.
Definition: repr_printer.h:91
std::ostream & operator<<(std::ostream &os, const DataType &dtype)
Definition: data_type.h:453
ffi::String DocToPythonScript(Doc doc, const PrinterConfig &cfg)
Convert Doc into Python script.
ffi::reflection::AccessPath AccessPath
Definition: doc.h:35
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
Definitions and helper macros for IR/AST nodes.