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 String DocToPythonScript(Doc doc, const PrinterConfig& cfg);
46 
58 class DocNode : public Object {
59  public:
67  mutable 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 const char* _type_key = "script.printer.Doc";
75  static constexpr bool _type_mutable = true;
76 
78 
79  public:
80  virtual ~DocNode() = default;
81 };
82 
88 class Doc : public ObjectRef {
89  protected:
90  Doc() = default;
91 
92  public:
94 };
95 
96 class ExprDoc;
97 
103 class ExprDocNode : public DocNode {
104  public:
109  ExprDoc Attr(String attr) const;
110 
115  ExprDoc operator[](Array<Doc> indices) const;
116 
121  ExprDoc Call(Array<ExprDoc, void> args) const;
122 
129  ExprDoc Call(Array<ExprDoc, void> args, //
130  Array<String> kwargs_keys, //
131  Array<ExprDoc, void> kwargs_values) const;
132 
133  static void RegisterReflection() {
134  namespace refl = tvm::ffi::reflection;
135  refl::ObjectDef<ExprDocNode>();
136  }
137 
138  static constexpr const char* _type_key = "script.printer.ExprDoc";
139 
141 };
142 
148 class ExprDoc : public Doc {
149  protected:
150  ExprDoc() = default;
151 
152  public:
157  ExprDoc operator[](Array<Doc> indices) const;
158 
160 };
161 
167 class StmtDocNode : public DocNode {
168  public:
177  mutable Optional<String> comment{std::nullopt};
178 
179  static void RegisterReflection() {
180  namespace refl = tvm::ffi::reflection;
181  refl::ObjectDef<StmtDocNode>().def_rw("comment", &StmtDocNode::comment);
182  }
183 
184  static constexpr const char* _type_key = "script.printer.StmtDoc";
185 
187 };
188 
194 class StmtDoc : public Doc {
195  protected:
196  StmtDoc() = default;
197 
198  public:
200 };
201 
208 class StmtBlockDocNode : public DocNode {
209  public:
211  Array<StmtDoc> stmts;
212 
213  static void RegisterReflection() {
214  namespace refl = tvm::ffi::reflection;
215  refl::ObjectDef<StmtBlockDocNode>().def_ro("stmts", &StmtBlockDocNode::stmts);
216  }
217 
218  static constexpr const char* _type_key = "script.printer.StmtBlockDoc";
219 
221 };
222 
227 class StmtBlockDoc : public Doc {
228  public:
233  explicit StmtBlockDoc(Array<StmtDoc> stmts);
235 };
236 
242 class LiteralDocNode : public ExprDocNode {
243  public:
253  ffi::Any value;
254 
255  static void RegisterReflection() {
256  namespace refl = tvm::ffi::reflection;
257  refl::ObjectDef<LiteralDocNode>().def_ro("value", &LiteralDocNode::value);
258  }
259 
260  static constexpr const char* _type_key = "script.printer.LiteralDoc";
261 
263 };
264 
270 class LiteralDoc : public ExprDoc {
271  protected:
272  explicit LiteralDoc(ffi::Any value, const Optional<AccessPath>& object_path);
273 
274  public:
279  static LiteralDoc None(const Optional<AccessPath>& p) { return LiteralDoc(ffi::Any(nullptr), p); }
285  static LiteralDoc Int(int64_t v, const Optional<AccessPath>& p) {
286  return LiteralDoc(IntImm(DataType::Int(64), v), p);
287  }
293  static LiteralDoc Boolean(bool v, const Optional<AccessPath>& p) {
294  return LiteralDoc(IntImm(DataType::Bool(), v), p);
295  }
301  static LiteralDoc Float(double v, const Optional<AccessPath>& p) {
302  return LiteralDoc(FloatImm(DataType::Float(64), v), p);
303  }
309  static LiteralDoc Str(const String& v, const Optional<AccessPath>& p) { return LiteralDoc(v, p); }
315  static LiteralDoc DataType(const runtime::DataType& v, const Optional<AccessPath>& p) {
316  std::string dtype = v.is_void() ? "void" : runtime::DLDataTypeToString(v);
317  return LiteralDoc::Str(dtype, p);
318  }
324  static LiteralDoc Device(const DLDevice& v, const Optional<AccessPath>& p) {
325  std::ostringstream os;
326  runtime::operator<<(os, v);
327  return LiteralDoc::Str(os.str(), p);
328  }
329 
331 };
332 
338 class IdDocNode : public ExprDocNode {
339  public:
341  String name;
342 
343  static void RegisterReflection() {
344  namespace refl = tvm::ffi::reflection;
345  refl::ObjectDef<IdDocNode>().def_ro("name", &IdDocNode::name);
346  }
347 
348  static constexpr const char* _type_key = "script.printer.IdDoc";
349 
351 };
352 
358 class IdDoc : public ExprDoc {
359  public:
364  explicit IdDoc(String name);
365  explicit IdDoc(std::nullptr_t) : ExprDoc(nullptr) {}
367 };
368 
375  public:
377  ExprDoc value{nullptr};
379  String name;
380 
381  static void RegisterReflection() {
382  namespace refl = tvm::ffi::reflection;
383  refl::ObjectDef<AttrAccessDocNode>()
384  .def_ro("value", &AttrAccessDocNode::value)
385  .def_ro("name", &AttrAccessDocNode::name);
386  }
387 
388  static constexpr const char* _type_key = "script.printer.AttrAccessDoc";
389 
391 };
392 
398 class AttrAccessDoc : public ExprDoc {
399  public:
405  explicit AttrAccessDoc(ExprDoc value, String name);
407 };
408 
414 class IndexDocNode : public ExprDocNode {
415  public:
417  ExprDoc value{nullptr};
425  Array<Doc> indices; // Each element is union of: Slice / ExprDoc
426 
427  static void RegisterReflection() {
428  namespace refl = tvm::ffi::reflection;
429  refl::ObjectDef<IndexDocNode>()
430  .def_ro("value", &IndexDocNode::value)
431  .def_ro("indices", &IndexDocNode::indices);
432  }
433 
434  static constexpr const char* _type_key = "script.printer.IndexDoc";
435 
437 };
438 
444 class IndexDoc : public ExprDoc {
445  public:
451  explicit IndexDoc(ExprDoc value, Array<Doc> indices);
453 };
454 
460 class CallDocNode : public ExprDocNode {
461  public:
463  ExprDoc callee{nullptr};
465  Array<ExprDoc> args;
467  Array<String> kwargs_keys;
474  Array<ExprDoc> kwargs_values;
475 
476  static void RegisterReflection() {
477  namespace refl = tvm::ffi::reflection;
478  refl::ObjectDef<CallDocNode>()
479  .def_ro("callee", &CallDocNode::callee)
480  .def_ro("args", &CallDocNode::args)
481  .def_ro("kwargs_keys", &CallDocNode::kwargs_keys)
482  .def_ro("kwargs_values", &CallDocNode::kwargs_values);
483  }
484 
485  static constexpr const char* _type_key = "script.printer.CallDoc";
486 
488 };
489 
495 class CallDoc : public ExprDoc {
496  public:
504  CallDoc(ExprDoc callee, Array<ExprDoc> args, Array<String> kwargs_keys,
505  Array<ExprDoc> kwargs_values);
507 };
508 
518  public:
519  enum class Kind : int32_t {
520  // Unary operators
521  kUnaryStart = 0,
522  kUSub = 1, // -x
523  kInvert = 2, // ~x
524  kNot = 3, // not x
525  kUnaryEnd = 4,
526 
527  // Binary operators
528  kBinaryStart = 5,
529  kAdd = 6, // +
530  kSub = 7, // -
531  kMult = 8, // *
532  kDiv = 9, // /
533  kFloorDiv = 10, // // in Python
534  kMod = 11, // % in Python
535  kPow = 12, // ** in Python
536  kLShift = 13, // <<
537  kRShift = 14, // >>
538  kBitAnd = 15, // &
539  kBitOr = 16, // |
540  kBitXor = 17, // ^
541  kLt = 18, // <
542  kLtE = 19, // <=
543  kEq = 20, // ==
544  kNotEq = 21, // !=
545  kGt = 22, // >
546  kGtE = 23, // >=
547  kAnd = 24, // and
548  kOr = 25, // or
549  kBinaryEnd = 26,
550 
551  // Special
552  kSpecialStart = 27,
553  kIfThenElse = 28, // <operands[1]> if <operands[0]> else <operands[2]>
554  kSpecialEnd = 29
555  };
556 
560  Array<ExprDoc> operands;
561 
562  static void RegisterReflection() {
563  namespace refl = tvm::ffi::reflection;
564  refl::ObjectDef<OperationDocNode>()
565  .def_ro("kind", &OperationDocNode::kind)
566  .def_ro("operands", &OperationDocNode::operands);
567  }
568 
569  static constexpr const char* _type_key = "script.printer.OperationDoc";
570 
572 };
573 
579 class OperationDoc : public ExprDoc {
580  public:
586  explicit OperationDoc(OperationDocNode::Kind kind, Array<ExprDoc> operands);
588 };
589 
598 class LambdaDocNode : public ExprDocNode {
599  public:
601  Array<IdDoc> args;
603  ExprDoc body{nullptr};
604 
605  static void RegisterReflection() {
606  namespace refl = tvm::ffi::reflection;
607  refl::ObjectDef<LambdaDocNode>()
608  .def_ro("args", &LambdaDocNode::args)
609  .def_ro("body", &LambdaDocNode::body);
610  }
611 
612  static constexpr const char* _type_key = "script.printer.LambdaDoc";
613 
615 };
616 
622 class LambdaDoc : public ExprDoc {
623  public:
629  explicit LambdaDoc(Array<IdDoc> args, ExprDoc body);
631 };
632 
638 class TupleDocNode : public ExprDocNode {
639  public:
641  Array<ExprDoc> elements;
642 
643  static void RegisterReflection() {
644  namespace refl = tvm::ffi::reflection;
645  refl::ObjectDef<TupleDocNode>().def_ro("elements", &TupleDocNode::elements);
646  }
647 
648  static constexpr const char* _type_key = "script.printer.TupleDoc";
649 
651 };
652 
658 class TupleDoc : public ExprDoc {
659  public:
663  TupleDoc() : TupleDoc(ffi::make_object<TupleDocNode>()) {}
668  explicit TupleDoc(Array<ExprDoc> elements);
670 };
671 
677 class ListDocNode : public ExprDocNode {
678  public:
680  Array<ExprDoc> elements;
681 
682  static void RegisterReflection() {
683  namespace refl = tvm::ffi::reflection;
684  refl::ObjectDef<ListDocNode>().def_ro("elements", &ListDocNode::elements);
685  }
686 
687  static constexpr const char* _type_key = "script.printer.ListDoc";
688 
690 };
691 
697 class ListDoc : public ExprDoc {
698  public:
702  ListDoc() : ListDoc(ffi::make_object<ListDocNode>()) {}
707  explicit ListDoc(Array<ExprDoc> elements);
709 };
710 
716 class DictDocNode : public ExprDocNode {
717  public:
719  Array<ExprDoc> keys;
726  Array<ExprDoc> values;
727 
728  static void RegisterReflection() {
729  namespace refl = tvm::ffi::reflection;
730  refl::ObjectDef<DictDocNode>()
731  .def_ro("keys", &DictDocNode::keys)
732  .def_ro("values", &DictDocNode::values);
733  }
734 
735  static constexpr const char* _type_key = "script.printer.DictDoc";
736 
738 };
739 
745 class DictDoc : public ExprDoc {
746  public:
750  DictDoc() : DictDoc(ffi::make_object<DictDocNode>()) {}
756  explicit DictDoc(Array<ExprDoc> keys, Array<ExprDoc> values);
758 };
759 
767 class SliceDocNode : public DocNode {
768  public:
770  Optional<ExprDoc> start;
772  Optional<ExprDoc> stop;
774  Optional<ExprDoc> step;
775 
776  static void RegisterReflection() {
777  namespace refl = tvm::ffi::reflection;
778  refl::ObjectDef<SliceDocNode>()
779  .def_ro("start", &SliceDocNode::start)
780  .def_ro("stop", &SliceDocNode::stop)
781  .def_ro("step", &SliceDocNode::step);
782  }
783 
784  static constexpr const char* _type_key = "script.printer.SliceDoc";
785 
787 };
788 
794 class SliceDoc : public Doc {
795  public:
802  explicit SliceDoc(Optional<ExprDoc> start, Optional<ExprDoc> stop, Optional<ExprDoc> step);
804 };
805 
811 class AssignDocNode : public StmtDocNode {
812  public:
814  ExprDoc lhs{nullptr};
820  Optional<ExprDoc> rhs;
822  Optional<ExprDoc> annotation;
823 
824  static void RegisterReflection() {
825  namespace refl = tvm::ffi::reflection;
826  refl::ObjectDef<AssignDocNode>()
827  .def_ro("lhs", &AssignDocNode::lhs)
828  .def_ro("rhs", &AssignDocNode::rhs)
829  .def_ro("annotation", &AssignDocNode::annotation);
830  }
831 
832  static constexpr const char* _type_key = "script.printer.AssignDoc";
833 
835 };
836 
842 class AssignDoc : public StmtDoc {
843  public:
850  explicit AssignDoc(ExprDoc lhs, Optional<ExprDoc> rhs, Optional<ExprDoc> annotation);
852 };
853 
859 class IfDocNode : public StmtDocNode {
860  public:
862  ExprDoc predicate{nullptr};
864  Array<StmtDoc> then_branch;
866  Array<StmtDoc> else_branch;
867 
868  static void RegisterReflection() {
869  namespace refl = tvm::ffi::reflection;
870  refl::ObjectDef<IfDocNode>()
871  .def_ro("predicate", &IfDocNode::predicate)
872  .def_ro("then_branch", &IfDocNode::then_branch)
873  .def_ro("else_branch", &IfDocNode::else_branch);
874  }
875 
876  static constexpr const char* _type_key = "script.printer.IfDoc";
877 
879 };
880 
886 class IfDoc : public StmtDoc {
887  public:
894  explicit IfDoc(ExprDoc predicate, Array<StmtDoc> then_branch, Array<StmtDoc> else_branch);
896 };
897 
903 class WhileDocNode : public StmtDocNode {
904  public:
906  ExprDoc predicate{nullptr};
908  Array<StmtDoc> body;
909 
910  static void RegisterReflection() {
911  namespace refl = tvm::ffi::reflection;
912  refl::ObjectDef<WhileDocNode>()
913  .def_ro("predicate", &WhileDocNode::predicate)
914  .def_ro("body", &WhileDocNode::body);
915  }
916 
917  static constexpr const char* _type_key = "script.printer.WhileDoc";
918 
920 };
921 
927 class WhileDoc : public StmtDoc {
928  public:
934  explicit WhileDoc(ExprDoc predicate, Array<StmtDoc> body);
936 };
937 
947 class ForDocNode : public StmtDocNode {
948  public:
950  ExprDoc lhs{nullptr};
952  ExprDoc rhs{nullptr};
954  Array<StmtDoc> body;
955 
956  static void RegisterReflection() {
957  namespace refl = tvm::ffi::reflection;
958  refl::ObjectDef<ForDocNode>()
959  .def_ro("lhs", &ForDocNode::lhs)
960  .def_ro("rhs", &ForDocNode::rhs)
961  .def_ro("body", &ForDocNode::body);
962  }
963 
964  static constexpr const char* _type_key = "script.printer.ForDoc";
965 
967 };
968 
974 class ForDoc : public StmtDoc {
975  public:
982  explicit ForDoc(ExprDoc lhs, ExprDoc rhs, Array<StmtDoc> body);
984 };
985 
996 class ScopeDocNode : public StmtDocNode {
997  public:
999  Optional<ExprDoc> lhs{std::nullopt};
1001  ExprDoc rhs{nullptr};
1003  Array<StmtDoc> body;
1004 
1005  static void RegisterReflection() {
1006  namespace refl = tvm::ffi::reflection;
1007  refl::ObjectDef<ScopeDocNode>()
1008  .def_ro("lhs", &ScopeDocNode::lhs)
1009  .def_ro("rhs", &ScopeDocNode::rhs)
1010  .def_ro("body", &ScopeDocNode::body);
1011  }
1012 
1013  static constexpr const char* _type_key = "script.printer.ScopeDoc";
1014 
1016 };
1017 
1023 class ScopeDoc : public StmtDoc {
1024  public:
1031  explicit ScopeDoc(Optional<ExprDoc> lhs, ExprDoc rhs, Array<StmtDoc> body);
1032 
1038  explicit ScopeDoc(ExprDoc rhs, Array<StmtDoc> body);
1039 
1041 };
1042 
1049  public:
1051  ExprDoc expr{nullptr};
1052 
1053  static void RegisterReflection() {
1054  namespace refl = tvm::ffi::reflection;
1055  refl::ObjectDef<ExprStmtDocNode>().def_ro("expr", &ExprStmtDocNode::expr);
1056  }
1057 
1058  static constexpr const char* _type_key = "script.printer.ExprStmtDoc";
1059 
1061 };
1062 
1068 class ExprStmtDoc : public StmtDoc {
1069  public:
1074  explicit ExprStmtDoc(ExprDoc expr);
1076 };
1077 
1083 class AssertDocNode : public StmtDocNode {
1084  public:
1086  ExprDoc test{nullptr};
1088  Optional<ExprDoc> msg{std::nullopt};
1089 
1090  static void RegisterReflection() {
1091  namespace refl = tvm::ffi::reflection;
1092  refl::ObjectDef<AssertDocNode>()
1093  .def_ro("test", &AssertDocNode::test)
1094  .def_ro("msg", &AssertDocNode::msg);
1095  }
1096 
1097  static constexpr const char* _type_key = "script.printer.AssertDoc";
1098 
1100 };
1101 
1107 class AssertDoc : public StmtDoc {
1108  public:
1114  explicit AssertDoc(ExprDoc test, Optional<ExprDoc> msg = std::nullopt);
1116 };
1117 
1123 class ReturnDocNode : public StmtDocNode {
1124  public:
1126  ExprDoc value{nullptr};
1127 
1128  static void RegisterReflection() {
1129  namespace refl = tvm::ffi::reflection;
1130  refl::ObjectDef<ReturnDocNode>().def_ro("value", &ReturnDocNode::value);
1131  }
1132 
1133  static constexpr const char* _type_key = "script.printer.ReturnDoc";
1134 
1136 };
1137 
1143 class ReturnDoc : public StmtDoc {
1144  public:
1149  explicit ReturnDoc(ExprDoc value);
1151 };
1152 
1159  public:
1161  IdDoc name{nullptr};
1169  Array<AssignDoc> args;
1171  Array<ExprDoc> decorators;
1173  Optional<ExprDoc> return_type{std::nullopt};
1175  Array<StmtDoc> body;
1176 
1177  static void RegisterReflection() {
1178  namespace refl = tvm::ffi::reflection;
1179  refl::ObjectDef<FunctionDocNode>()
1180  .def_ro("name", &FunctionDocNode::name)
1181  .def_ro("args", &FunctionDocNode::args)
1182  .def_ro("decorators", &FunctionDocNode::decorators)
1183  .def_ro("return_type", &FunctionDocNode::return_type)
1184  .def_ro("body", &FunctionDocNode::body);
1185  }
1186 
1187  static constexpr const char* _type_key = "script.printer.FunctionDoc";
1188 
1190 };
1191 
1197 class FunctionDoc : public StmtDoc {
1198  public:
1207  explicit FunctionDoc(IdDoc name, Array<AssignDoc> args, Array<ExprDoc> decorators,
1208  Optional<ExprDoc> return_type, Array<StmtDoc> body);
1210 };
1211 
1217 class ClassDocNode : public StmtDocNode {
1218  public:
1220  IdDoc name{nullptr};
1222  Array<ExprDoc> decorators;
1224  Array<StmtDoc> body;
1225 
1226  static void RegisterReflection() {
1227  namespace refl = tvm::ffi::reflection;
1228  refl::ObjectDef<ClassDocNode>()
1229  .def_ro("name", &ClassDocNode::name)
1230  .def_ro("decorators", &ClassDocNode::decorators)
1231  .def_ro("body", &ClassDocNode::body);
1232  }
1233 
1234  static constexpr const char* _type_key = "script.printer.ClassDoc";
1235 
1237 };
1238 
1244 class ClassDoc : public StmtDoc {
1245  public:
1252  explicit ClassDoc(IdDoc name, Array<ExprDoc> decorators, Array<StmtDoc> body);
1254 };
1255 
1261 class CommentDocNode : public StmtDocNode {
1262  public:
1263  static void RegisterReflection() {
1264  namespace refl = tvm::ffi::reflection;
1265  refl::ObjectDef<CommentDocNode>();
1266  }
1267 
1268  static constexpr const char* _type_key = "script.printer.CommentDoc";
1270 };
1271 
1277 class CommentDoc : public StmtDoc {
1278  public:
1279  explicit CommentDoc(String comment);
1281 };
1282 
1289  public:
1290  static void RegisterReflection() {
1291  namespace refl = tvm::ffi::reflection;
1292  refl::ObjectDef<DocStringDocNode>();
1293  }
1294 
1295  static constexpr const char* _type_key = "script.printer.DocStringDoc";
1297 };
1298 
1304 class DocStringDoc : public StmtDoc {
1305  public:
1306  explicit DocStringDoc(String docs);
1308 };
1309 
1310 } // namespace printer
1311 } // namespace script
1312 } // namespace tvm
1313 
1314 #endif // TVM_SCRIPT_PRINTER_DOC_H_
Managed reference class to FloatImmNode.
Definition: expr.h:557
Managed reference class to IntImmNode.
Definition: expr.h:520
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:1083
static void RegisterReflection()
Definition: doc.h:1090
ExprDoc test
The expression to test.
Definition: doc.h:1086
TVM_DECLARE_FINAL_OBJECT_INFO(AssertDocNode, StmtDocNode)
static constexpr const char * _type_key
Definition: doc.h:1097
Optional< ExprDoc > msg
The optional error message when assertion failed.
Definition: doc.h:1088
Reference type of AssertDocNode.
Definition: doc.h:1107
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AssertDoc, StmtDoc, AssertDocNode)
AssertDoc(ExprDoc test, Optional< ExprDoc > msg=std::nullopt)
Constructor of AssertDoc.
Doc that represents assign statement.
Definition: doc.h:811
Optional< ExprDoc > annotation
The type annotation of this assignment.
Definition: doc.h:822
Optional< ExprDoc > rhs
The right hand side of the assignment.
Definition: doc.h:820
ExprDoc lhs
The left hand side of the assignment.
Definition: doc.h:814
static constexpr const char * _type_key
Definition: doc.h:832
TVM_DECLARE_FINAL_OBJECT_INFO(AssignDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:824
Reference type of AssignDocNode.
Definition: doc.h:842
AssignDoc(ExprDoc lhs, Optional< ExprDoc > rhs, Optional< ExprDoc > annotation)
Constructor of AssignDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AssignDoc, StmtDoc, AssignDocNode)
Doc that represents attribute access on another expression.
Definition: doc.h:374
static constexpr const char * _type_key
Definition: doc.h:388
String name
The attribute to be accessed.
Definition: doc.h:379
ExprDoc value
The target expression to be accessed.
Definition: doc.h:377
static void RegisterReflection()
Definition: doc.h:381
TVM_DECLARE_FINAL_OBJECT_INFO(AttrAccessDocNode, ExprDocNode)
Reference type of AttrAccessDocNode.
Definition: doc.h:398
AttrAccessDoc(ExprDoc value, String name)
Constructor of AttrAccessDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AttrAccessDoc, ExprDoc, AttrAccessDocNode)
Doc that represents function call.
Definition: doc.h:460
Array< ExprDoc > kwargs_values
The values of keyword arguments.
Definition: doc.h:474
Array< ExprDoc > args
The positional arguments.
Definition: doc.h:465
TVM_DECLARE_FINAL_OBJECT_INFO(CallDocNode, ExprDocNode)
static constexpr const char * _type_key
Definition: doc.h:485
Array< String > kwargs_keys
The keys of keyword arguments.
Definition: doc.h:467
static void RegisterReflection()
Definition: doc.h:476
ExprDoc callee
The callee of this function call.
Definition: doc.h:463
Reference type of CallDocNode.
Definition: doc.h:495
CallDoc(ExprDoc callee, Array< ExprDoc > args, Array< String > kwargs_keys, Array< ExprDoc > kwargs_values)
Constructor of CallDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(CallDoc, ExprDoc, CallDocNode)
Doc that represents class definition.
Definition: doc.h:1217
static constexpr const char * _type_key
Definition: doc.h:1234
Array< ExprDoc > decorators
Decorators of class.
Definition: doc.h:1222
TVM_DECLARE_FINAL_OBJECT_INFO(ClassDocNode, StmtDocNode)
Array< StmtDoc > body
The body of class.
Definition: doc.h:1224
static void RegisterReflection()
Definition: doc.h:1226
IdDoc name
The name of class.
Definition: doc.h:1220
Reference type of ClassDocNode.
Definition: doc.h:1244
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ClassDoc, StmtDoc, ClassDocNode)
ClassDoc(IdDoc name, Array< ExprDoc > decorators, Array< StmtDoc > body)
Constructor of ClassDoc.
Doc that represents comment.
Definition: doc.h:1261
TVM_DECLARE_FINAL_OBJECT_INFO(CommentDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:1263
static constexpr const char * _type_key
Definition: doc.h:1268
Reference type of CommentDocNode.
Definition: doc.h:1277
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(CommentDoc, StmtDoc, CommentDocNode)
Doc that represents dictionary literal.
Definition: doc.h:716
Array< ExprDoc > values
Values of dictionary.
Definition: doc.h:726
static void RegisterReflection()
Definition: doc.h:728
Array< ExprDoc > keys
keys of dictionary
Definition: doc.h:719
static constexpr const char * _type_key
Definition: doc.h:735
TVM_DECLARE_FINAL_OBJECT_INFO(DictDocNode, ExprDocNode)
Reference type of DictDocNode.
Definition: doc.h:745
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DictDoc, ExprDoc, DictDocNode)
DictDoc(Array< ExprDoc > keys, Array< ExprDoc > values)
Constructor of DictDoc.
DictDoc()
Create an empty dictionary.
Definition: doc.h:750
The base class of all Doc.
Definition: doc.h:58
static constexpr bool _type_mutable
Definition: doc.h:75
Array< ffi::reflection::AccessPath > source_paths
The list of object paths of the source IR node.
Definition: doc.h:67
virtual ~DocNode()=default
TVM_DECLARE_BASE_OBJECT_INFO(DocNode, Object)
static void RegisterReflection()
Definition: doc.h:69
static constexpr const char * _type_key
Definition: doc.h:74
Doc that represents docstring.
Definition: doc.h:1288
static void RegisterReflection()
Definition: doc.h:1290
static constexpr const char * _type_key
Definition: doc.h:1295
TVM_DECLARE_FINAL_OBJECT_INFO(DocStringDocNode, StmtDocNode)
Reference type of DocStringDocNode.
Definition: doc.h:1304
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(DocStringDoc, StmtDoc, DocStringDocNode)
Reference type of DocNode.
Definition: doc.h:88
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(Doc, ObjectRef, DocNode)
The base class of expression doc.
Definition: doc.h:103
static constexpr const char * _type_key
Definition: doc.h:138
TVM_DECLARE_BASE_OBJECT_INFO(ExprDocNode, DocNode)
static void RegisterReflection()
Definition: doc.h:133
ExprDoc Attr(String attr) const
Create a doc representing attribute access on the current ExprDoc.
ExprDoc Call(Array< ExprDoc, void > args) const
Create a doc representing calling the current ExprDoc.
ExprDoc operator[](Array< Doc > indices) const
Create a doc representing index access on the current ExprDoc.
ExprDoc Call(Array< ExprDoc, void > args, Array< String > kwargs_keys, Array< ExprDoc, void > kwargs_values) const
Create a doc representing attribute access on the current ExprDoc.
Reference type of ExprDocNode.
Definition: doc.h:148
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ExprDoc, Doc, ExprDocNode)
ExprDoc operator[](Array< Doc > indices) const
Create a doc representing index access on the current ExprDoc.
Doc that represents an expression as statement.
Definition: doc.h:1048
static constexpr const char * _type_key
Definition: doc.h:1058
static void RegisterReflection()
Definition: doc.h:1053
TVM_DECLARE_FINAL_OBJECT_INFO(ExprStmtDocNode, StmtDocNode)
ExprDoc expr
The expression represented by this doc.
Definition: doc.h:1051
Reference type of ExprStmtDocNode.
Definition: doc.h:1068
ExprStmtDoc(ExprDoc expr)
Constructor of ExprStmtDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ExprStmtDoc, StmtDoc, ExprStmtDocNode)
Doc that represents for statement.
Definition: doc.h:947
ExprDoc lhs
The left hand side of the assignment of iterating variable.
Definition: doc.h:950
static void RegisterReflection()
Definition: doc.h:956
static constexpr const char * _type_key
Definition: doc.h:964
ExprDoc rhs
The right hand side of the assignment of iterating variable.
Definition: doc.h:952
Array< StmtDoc > body
The body of the for statement.
Definition: doc.h:954
TVM_DECLARE_FINAL_OBJECT_INFO(ForDocNode, StmtDocNode)
Reference type of ForDocNode.
Definition: doc.h:974
ForDoc(ExprDoc lhs, ExprDoc rhs, Array< StmtDoc > body)
Constructor of ForDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ForDoc, StmtDoc, ForDocNode)
Doc that represents function definition.
Definition: doc.h:1158
TVM_DECLARE_FINAL_OBJECT_INFO(FunctionDocNode, StmtDocNode)
Optional< ExprDoc > return_type
The return type of function.
Definition: doc.h:1173
Array< ExprDoc > decorators
Decorators of function.
Definition: doc.h:1171
Array< StmtDoc > body
The body of function.
Definition: doc.h:1175
Array< AssignDoc > args
The arguments of function.
Definition: doc.h:1169
IdDoc name
The name of function.
Definition: doc.h:1161
static constexpr const char * _type_key
Definition: doc.h:1187
static void RegisterReflection()
Definition: doc.h:1177
Reference type of FunctionDocNode.
Definition: doc.h:1197
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(FunctionDoc, StmtDoc, FunctionDocNode)
FunctionDoc(IdDoc name, Array< AssignDoc > args, Array< ExprDoc > decorators, Optional< ExprDoc > return_type, Array< StmtDoc > body)
Constructor of FunctionDoc.
Doc that represents identifier.
Definition: doc.h:338
TVM_DECLARE_FINAL_OBJECT_INFO(IdDocNode, ExprDocNode)
String name
The name of the identifier.
Definition: doc.h:341
static void RegisterReflection()
Definition: doc.h:343
static constexpr const char * _type_key
Definition: doc.h:348
Reference type of IdDocNode.
Definition: doc.h:358
IdDoc(std::nullptr_t)
Definition: doc.h:365
IdDoc(String name)
Constructor of IdDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(IdDoc, ExprDoc, IdDocNode)
Doc that represent if-then-else statement.
Definition: doc.h:859
static void RegisterReflection()
Definition: doc.h:868
TVM_DECLARE_FINAL_OBJECT_INFO(IfDocNode, StmtDocNode)
Array< StmtDoc > else_branch
The else branch of the if-then-else statement.
Definition: doc.h:866
static constexpr const char * _type_key
Definition: doc.h:876
Array< StmtDoc > then_branch
The then branch of the if-then-else statement.
Definition: doc.h:864
ExprDoc predicate
The predicate of the if-then-else statement.
Definition: doc.h:862
Reference type of IfDocNode.
Definition: doc.h:886
IfDoc(ExprDoc predicate, Array< StmtDoc > then_branch, Array< StmtDoc > else_branch)
Constructor of IfDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(IfDoc, StmtDoc, IfDocNode)
Doc that represents index access on another expression.
Definition: doc.h:414
static constexpr const char * _type_key
Definition: doc.h:434
static void RegisterReflection()
Definition: doc.h:427
TVM_DECLARE_FINAL_OBJECT_INFO(IndexDocNode, ExprDocNode)
ExprDoc value
The container value to be accessed.
Definition: doc.h:417
Array< Doc > indices
The indices to access.
Definition: doc.h:425
Reference type of IndexDocNode.
Definition: doc.h:444
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(IndexDoc, ExprDoc, IndexDocNode)
IndexDoc(ExprDoc value, Array< Doc > indices)
Constructor of IndexDoc.
Doc that represents anonymous function.
Definition: doc.h:598
Array< IdDoc > args
The arguments of this anonymous function.
Definition: doc.h:601
static void RegisterReflection()
Definition: doc.h:605
ExprDoc body
The body of this anonymous function.
Definition: doc.h:603
static constexpr const char * _type_key
Definition: doc.h:612
TVM_DECLARE_FINAL_OBJECT_INFO(LambdaDocNode, ExprDocNode)
Reference type of LambdaDocNode.
Definition: doc.h:622
LambdaDoc(Array< IdDoc > args, ExprDoc body)
Constructor of LambdaDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(LambdaDoc, ExprDoc, LambdaDocNode)
Doc that represents list literal.
Definition: doc.h:677
Array< ExprDoc > elements
Elements of list.
Definition: doc.h:680
TVM_DECLARE_FINAL_OBJECT_INFO(ListDocNode, ExprDocNode)
static constexpr const char * _type_key
Definition: doc.h:687
static void RegisterReflection()
Definition: doc.h:682
Reference type of ListDocNode.
Definition: doc.h:697
ListDoc()
Create an empty ListDoc.
Definition: doc.h:702
ListDoc(Array< ExprDoc > elements)
Constructor of ListDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ListDoc, ExprDoc, ListDocNode)
Doc that represents literal value.
Definition: doc.h:242
static constexpr const char * _type_key
Definition: doc.h:260
ffi::Any value
the internal representation of the literal value.
Definition: doc.h:253
static void RegisterReflection()
Definition: doc.h:255
TVM_DECLARE_FINAL_OBJECT_INFO(LiteralDocNode, ExprDocNode)
Reference type of LiteralDocNode.
Definition: doc.h:270
static LiteralDoc DataType(const runtime::DataType &v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition: doc.h:315
static LiteralDoc Boolean(bool v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent boolean.
Definition: doc.h:293
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(LiteralDoc, ExprDoc, LiteralDocNode)
static LiteralDoc Int(int64_t v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent integer.
Definition: doc.h:285
static LiteralDoc None(const Optional< AccessPath > &p)
Create a LiteralDoc to represent None/null/empty value.
Definition: doc.h:279
static LiteralDoc Float(double v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent float.
Definition: doc.h:301
static LiteralDoc Device(const DLDevice &v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent device.
Definition: doc.h:324
LiteralDoc(ffi::Any value, const Optional< AccessPath > &object_path)
static LiteralDoc Str(const String &v, const Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition: doc.h:309
Doc that represents operation.
Definition: doc.h:517
TVM_DECLARE_FINAL_OBJECT_INFO(OperationDocNode, ExprDocNode)
Array< ExprDoc > operands
Operands of this expression.
Definition: doc.h:560
static constexpr const char * _type_key
Definition: doc.h:569
Kind kind
The kind of operation (operator)
Definition: doc.h:558
static void RegisterReflection()
Definition: doc.h:562
Reference type of OperationDocNode.
Definition: doc.h:579
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(OperationDoc, ExprDoc, OperationDocNode)
OperationDoc(OperationDocNode::Kind kind, Array< ExprDoc > operands)
Constructor of OperationDoc.
Doc that represents return statement.
Definition: doc.h:1123
ExprDoc value
The value to return.
Definition: doc.h:1126
static void RegisterReflection()
Definition: doc.h:1128
TVM_DECLARE_FINAL_OBJECT_INFO(ReturnDocNode, StmtDocNode)
static constexpr const char * _type_key
Definition: doc.h:1133
Reference type of ReturnDocNode.
Definition: doc.h:1143
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ReturnDoc, StmtDoc, ReturnDocNode)
ReturnDoc(ExprDoc value)
Constructor of ReturnDoc.
Doc that represents special scopes.
Definition: doc.h:996
TVM_DECLARE_FINAL_OBJECT_INFO(ScopeDocNode, StmtDocNode)
Optional< ExprDoc > lhs
The name of the scoped variable.
Definition: doc.h:999
static constexpr const char * _type_key
Definition: doc.h:1013
ExprDoc rhs
The value of the scoped variable.
Definition: doc.h:1001
Array< StmtDoc > body
The body of the scope doc.
Definition: doc.h:1003
static void RegisterReflection()
Definition: doc.h:1005
Reference type of ScopeDocNode.
Definition: doc.h:1023
ScopeDoc(ExprDoc rhs, Array< StmtDoc > body)
Constructor of ScopeDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ScopeDoc, StmtDoc, ScopeDocNode)
ScopeDoc(Optional< ExprDoc > lhs, ExprDoc rhs, Array< StmtDoc > body)
Constructor of ScopeDoc.
Doc that represents slice in Index expression.
Definition: doc.h:767
Optional< ExprDoc > start
The start of slice.
Definition: doc.h:770
static constexpr const char * _type_key
Definition: doc.h:784
Optional< ExprDoc > step
The step of slice.
Definition: doc.h:774
Optional< ExprDoc > stop
The exclusive end of slice.
Definition: doc.h:772
TVM_DECLARE_FINAL_OBJECT_INFO(SliceDocNode, DocNode)
static void RegisterReflection()
Definition: doc.h:776
Reference type of SliceDocNode.
Definition: doc.h:794
SliceDoc(Optional< ExprDoc > start, Optional< ExprDoc > stop, Optional< ExprDoc > step)
Constructor of SliceDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(SliceDoc, Doc, SliceDocNode)
The container doc that holds a list of StmtDoc.
Definition: doc.h:208
TVM_DECLARE_FINAL_OBJECT_INFO(StmtBlockDocNode, DocNode)
Array< StmtDoc > stmts
The list of statements.
Definition: doc.h:211
static void RegisterReflection()
Definition: doc.h:213
static constexpr const char * _type_key
Definition: doc.h:218
Reference type of StmtBlockDocNode.
Definition: doc.h:227
StmtBlockDoc(Array< StmtDoc > stmts)
Constructor of StmtBlockDoc.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(StmtBlockDoc, Doc, StmtBlockDocNode)
The base class of statement doc.
Definition: doc.h:167
TVM_DECLARE_BASE_OBJECT_INFO(StmtDocNode, DocNode)
static constexpr const char * _type_key
Definition: doc.h:184
Optional< String > comment
The comment of this doc.
Definition: doc.h:177
static void RegisterReflection()
Definition: doc.h:179
Reference type of StmtDocNode.
Definition: doc.h:194
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(StmtDoc, Doc, StmtDocNode)
Doc that represents tuple literal.
Definition: doc.h:638
Array< ExprDoc > elements
Elements of tuple.
Definition: doc.h:641
static constexpr const char * _type_key
Definition: doc.h:648
static void RegisterReflection()
Definition: doc.h:643
TVM_DECLARE_FINAL_OBJECT_INFO(TupleDocNode, ExprDocNode)
Reference type of TupleDocNode.
Definition: doc.h:658
TupleDoc(Array< ExprDoc > elements)
Constructor of TupleDoc.
TupleDoc()
Create an empty TupleDoc.
Definition: doc.h:663
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TupleDoc, ExprDoc, TupleDocNode)
Doc that represents while statement.
Definition: doc.h:903
static void RegisterReflection()
Definition: doc.h:910
static constexpr const char * _type_key
Definition: doc.h:917
Array< StmtDoc > body
The body of the while statement.
Definition: doc.h:908
TVM_DECLARE_FINAL_OBJECT_INFO(WhileDocNode, StmtDocNode)
ExprDoc predicate
The predicate of the while statement.
Definition: doc.h:906
Reference type of WhileDocNode.
Definition: doc.h:927
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(WhileDoc, StmtDoc, WhileDocNode)
WhileDoc(ExprDoc predicate, 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::reflection::AccessPath AccessPath
Definition: doc.h:35
String DocToPythonScript(Doc doc, const PrinterConfig &cfg)
Convert Doc into Python script.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
Definitions and helper macros for IR/AST nodes.