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/runtime/data_type.h>
26 #include <tvm/runtime/device_api.h>
27 
28 #include <string>
29 
30 namespace tvm {
31 namespace script {
32 namespace printer {
33 
35 
36 // Forward declaration
37 class Doc;
38 
44 ffi::String DocToPythonScript(Doc doc, const PrinterConfig& cfg);
45 
57 class DocNode : public Object {
58  public:
66  mutable ffi::Array<ffi::reflection::AccessPath> source_paths;
67 
68  static void RegisterReflection() {
69  namespace refl = tvm::ffi::reflection;
70  refl::ObjectDef<DocNode>().def_rw("source_paths", &DocNode::source_paths);
71  }
72 
73  static constexpr bool _type_mutable = true;
74 
75  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.Doc", DocNode, Object);
76 
77  public:
78  virtual ~DocNode() = default;
79 };
80 
86 class Doc : public ObjectRef {
87  protected:
88  Doc() = default;
89  explicit Doc(ObjectPtr<DocNode> data) : ObjectRef(data) {}
90 
91  public:
93 };
94 
95 class ExprDoc;
96 
102 class ExprDocNode : public DocNode {
103  public:
108  ExprDoc Attr(ffi::String attr) const;
109 
114  ExprDoc operator[](ffi::Array<Doc> indices) const;
115 
120  ExprDoc Call(ffi::Array<ExprDoc, void> args) const;
121 
128  ExprDoc Call(ffi::Array<ExprDoc, void> args, //
129  ffi::Array<ffi::String> kwargs_keys, //
130  ffi::Array<ExprDoc, void> kwargs_values) const;
131 
132  static void RegisterReflection() {
133  namespace refl = tvm::ffi::reflection;
134  refl::ObjectDef<ExprDocNode>();
135  }
136  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.ExprDoc", ExprDocNode, DocNode);
137 };
138 
144 class ExprDoc : public Doc {
145  protected:
146  ExprDoc() = default;
147 
148  public:
153  ExprDoc operator[](ffi::Array<Doc> indices) const;
154 
155  explicit ExprDoc(ObjectPtr<ExprDocNode> data) : Doc(data) { TVM_FFI_ICHECK(data != nullptr); }
156 
158 };
159 
165 class StmtDocNode : public DocNode {
166  public:
175  mutable ffi::Optional<ffi::String> comment{std::nullopt};
176 
177  static void RegisterReflection() {
178  namespace refl = tvm::ffi::reflection;
179  refl::ObjectDef<StmtDocNode>().def_rw("comment", &StmtDocNode::comment);
180  }
181  TVM_FFI_DECLARE_OBJECT_INFO("script.printer.StmtDoc", StmtDocNode, DocNode);
182 };
183 
189 class StmtDoc : public Doc {
190  protected:
191  StmtDoc() = default;
192 
193  public:
195 };
196 
203 class StmtBlockDocNode : public DocNode {
204  public:
206  ffi::Array<StmtDoc> stmts;
207 
208  static void RegisterReflection() {
209  namespace refl = tvm::ffi::reflection;
210  refl::ObjectDef<StmtBlockDocNode>().def_ro("stmts", &StmtBlockDocNode::stmts);
211  }
213 };
214 
219 class StmtBlockDoc : public Doc {
220  public:
225  explicit StmtBlockDoc(ffi::Array<StmtDoc> stmts);
227 };
228 
234 class LiteralDocNode : public ExprDocNode {
235  public:
245  ffi::Any value;
246 
247  static void RegisterReflection() {
248  namespace refl = tvm::ffi::reflection;
249  refl::ObjectDef<LiteralDocNode>().def_ro("value", &LiteralDocNode::value);
250  }
252 };
253 
259 class LiteralDoc : public ExprDoc {
260  protected:
261  explicit LiteralDoc(ffi::Any value, const ffi::Optional<AccessPath>& object_path);
262 
263  public:
268  static LiteralDoc None(const ffi::Optional<AccessPath>& p) {
269  return LiteralDoc(ffi::Any(nullptr), p);
270  }
276  static LiteralDoc Int(int64_t v, const ffi::Optional<AccessPath>& p) {
277  return LiteralDoc(IntImm(DataType::Int(64), v), p);
278  }
284  static LiteralDoc Boolean(bool v, const ffi::Optional<AccessPath>& p) {
285  return LiteralDoc(IntImm(DataType::Bool(), v), p);
286  }
292  static LiteralDoc Float(double v, const ffi::Optional<AccessPath>& p) {
293  return LiteralDoc(FloatImm(DataType::Float(64), v), p);
294  }
300  static LiteralDoc Str(const ffi::String& v, const ffi::Optional<AccessPath>& p) {
301  return LiteralDoc(v, p);
302  }
308  static LiteralDoc DataType(const runtime::DataType& v, const ffi::Optional<AccessPath>& p) {
309  std::string dtype = v.is_void() ? "void" : runtime::DLDataTypeToString(v);
310  return LiteralDoc::Str(dtype, p);
311  }
317  static LiteralDoc Device(const DLDevice& v, const ffi::Optional<AccessPath>& p) {
318  std::ostringstream os;
319  runtime::operator<<(os, v);
320  return LiteralDoc::Str(os.str(), p);
321  }
322 
324 };
325 
331 class IdDocNode : public ExprDocNode {
332  public:
334  ffi::String name;
335 
336  static void RegisterReflection() {
337  namespace refl = tvm::ffi::reflection;
338  refl::ObjectDef<IdDocNode>().def_ro("name", &IdDocNode::name);
339  }
341 };
342 
348 class IdDoc : public ExprDoc {
349  public:
354  explicit IdDoc(ffi::String name);
355  explicit IdDoc(std::nullptr_t) : ExprDoc(nullptr) {}
357 };
358 
365  public:
367  ExprDoc value{ffi::UnsafeInit()};
369  ffi::String name;
370 
371  static void RegisterReflection() {
372  namespace refl = tvm::ffi::reflection;
373  refl::ObjectDef<AttrAccessDocNode>()
374  .def_ro("value", &AttrAccessDocNode::value)
375  .def_ro("name", &AttrAccessDocNode::name);
376  }
378 };
379 
385 class AttrAccessDoc : public ExprDoc {
386  public:
392  explicit AttrAccessDoc(ExprDoc value, ffi::String name);
394 };
395 
401 class IndexDocNode : public ExprDocNode {
402  public:
404  ExprDoc value{ffi::UnsafeInit()};
412  ffi::Array<Doc> indices; // Each element is union of: Slice / ExprDoc
413 
414  static void RegisterReflection() {
415  namespace refl = tvm::ffi::reflection;
416  refl::ObjectDef<IndexDocNode>()
417  .def_ro("value", &IndexDocNode::value)
418  .def_ro("indices", &IndexDocNode::indices);
419  }
421 };
422 
428 class IndexDoc : public ExprDoc {
429  public:
435  explicit IndexDoc(ExprDoc value, ffi::Array<Doc> indices);
437 };
438 
444 class CallDocNode : public ExprDocNode {
445  public:
447  ExprDoc callee{ffi::UnsafeInit()};
449  ffi::Array<ExprDoc> args;
451  ffi::Array<ffi::String> kwargs_keys;
458  ffi::Array<ExprDoc> kwargs_values;
459 
460  static void RegisterReflection() {
461  namespace refl = tvm::ffi::reflection;
462  refl::ObjectDef<CallDocNode>()
463  .def_ro("callee", &CallDocNode::callee)
464  .def_ro("args", &CallDocNode::args)
465  .def_ro("kwargs_keys", &CallDocNode::kwargs_keys)
466  .def_ro("kwargs_values", &CallDocNode::kwargs_values);
467  }
469 };
470 
476 class CallDoc : public ExprDoc {
477  public:
485  CallDoc(ExprDoc callee, ffi::Array<ExprDoc> args, ffi::Array<ffi::String> kwargs_keys,
486  ffi::Array<ExprDoc> kwargs_values);
488 };
489 
499  public:
500  enum class Kind : int32_t {
501  // Unary operators
502  kUnaryStart = 0,
503  kUSub = 1, // -x
504  kInvert = 2, // ~x
505  kNot = 3, // not x
506  kUnaryEnd = 4,
507 
508  // Binary operators
509  kBinaryStart = 5,
510  kAdd = 6, // +
511  kSub = 7, // -
512  kMult = 8, // *
513  kDiv = 9, // /
514  kFloorDiv = 10, // // in Python
515  kMod = 11, // % in Python
516  kPow = 12, // ** in Python
517  kLShift = 13, // <<
518  kRShift = 14, // >>
519  kBitAnd = 15, // &
520  kBitOr = 16, // |
521  kBitXor = 17, // ^
522  kLt = 18, // <
523  kLtE = 19, // <=
524  kEq = 20, // ==
525  kNotEq = 21, // !=
526  kGt = 22, // >
527  kGtE = 23, // >=
528  kAnd = 24, // and
529  kOr = 25, // or
530  kBinaryEnd = 26,
531 
532  // Special
533  kSpecialStart = 27,
534  kIfThenElse = 28, // <operands[1]> if <operands[0]> else <operands[2]>
535  kSpecialEnd = 29
536  };
537 
541  ffi::Array<ExprDoc> operands;
542 
543  static void RegisterReflection() {
544  namespace refl = tvm::ffi::reflection;
545  refl::ObjectDef<OperationDocNode>()
546  .def_ro("kind", &OperationDocNode::kind)
547  .def_ro("operands", &OperationDocNode::operands);
548  }
550 };
551 
557 class OperationDoc : public ExprDoc {
558  public:
564  explicit OperationDoc(OperationDocNode::Kind kind, ffi::Array<ExprDoc> operands);
566 };
567 
576 class LambdaDocNode : public ExprDocNode {
577  public:
579  ffi::Array<IdDoc> args;
581  ExprDoc body{ffi::UnsafeInit()};
582 
583  static void RegisterReflection() {
584  namespace refl = tvm::ffi::reflection;
585  refl::ObjectDef<LambdaDocNode>()
586  .def_ro("args", &LambdaDocNode::args)
587  .def_ro("body", &LambdaDocNode::body);
588  }
590 };
591 
597 class LambdaDoc : public ExprDoc {
598  public:
604  explicit LambdaDoc(ffi::Array<IdDoc> args, ExprDoc body);
606 };
607 
613 class TupleDocNode : public ExprDocNode {
614  public:
616  ffi::Array<ExprDoc> elements;
617 
618  static void RegisterReflection() {
619  namespace refl = tvm::ffi::reflection;
620  refl::ObjectDef<TupleDocNode>().def_ro("elements", &TupleDocNode::elements);
621  }
623 };
624 
630 class TupleDoc : public ExprDoc {
631  public:
635  TupleDoc() : ExprDoc(ffi::make_object<TupleDocNode>()) {}
640  explicit TupleDoc(ffi::Array<ExprDoc> elements);
642 };
643 
649 class ListDocNode : public ExprDocNode {
650  public:
652  ffi::Array<ExprDoc> elements;
653 
654  static void RegisterReflection() {
655  namespace refl = tvm::ffi::reflection;
656  refl::ObjectDef<ListDocNode>().def_ro("elements", &ListDocNode::elements);
657  }
659 };
660 
666 class ListDoc : public ExprDoc {
667  public:
671  ListDoc() : ExprDoc(ffi::make_object<ListDocNode>()) {}
676  explicit ListDoc(ffi::Array<ExprDoc> elements);
678 };
679 
685 class DictDocNode : public ExprDocNode {
686  public:
688  ffi::Array<ExprDoc> keys;
695  ffi::Array<ExprDoc> values;
696 
697  static void RegisterReflection() {
698  namespace refl = tvm::ffi::reflection;
699  refl::ObjectDef<DictDocNode>()
700  .def_ro("keys", &DictDocNode::keys)
701  .def_ro("values", &DictDocNode::values);
702  }
704 };
705 
711 class DictDoc : public ExprDoc {
712  public:
716  DictDoc() : ExprDoc(ffi::make_object<DictDocNode>()) {}
722  explicit DictDoc(ffi::Array<ExprDoc> keys, ffi::Array<ExprDoc> values);
724 };
725 
733 class SliceDocNode : public DocNode {
734  public:
736  ffi::Optional<ExprDoc> start;
738  ffi::Optional<ExprDoc> stop;
740  ffi::Optional<ExprDoc> step;
741 
742  static void RegisterReflection() {
743  namespace refl = tvm::ffi::reflection;
744  refl::ObjectDef<SliceDocNode>()
745  .def_ro("start", &SliceDocNode::start)
746  .def_ro("stop", &SliceDocNode::stop)
747  .def_ro("step", &SliceDocNode::step);
748  }
750 };
751 
757 class SliceDoc : public Doc {
758  public:
765  explicit SliceDoc(ffi::Optional<ExprDoc> start, ffi::Optional<ExprDoc> stop,
766  ffi::Optional<ExprDoc> step);
768 };
769 
775 class AssignDocNode : public StmtDocNode {
776  public:
778  ExprDoc lhs{ffi::UnsafeInit()};
784  ffi::Optional<ExprDoc> rhs;
786  ffi::Optional<ExprDoc> annotation;
787 
788  static void RegisterReflection() {
789  namespace refl = tvm::ffi::reflection;
790  refl::ObjectDef<AssignDocNode>()
791  .def_ro("lhs", &AssignDocNode::lhs)
792  .def_ro("rhs", &AssignDocNode::rhs)
793  .def_ro("annotation", &AssignDocNode::annotation);
794  }
796 };
797 
803 class AssignDoc : public StmtDoc {
804  public:
811  explicit AssignDoc(ExprDoc lhs, ffi::Optional<ExprDoc> rhs, ffi::Optional<ExprDoc> annotation);
813 };
814 
820 class IfDocNode : public StmtDocNode {
821  public:
823  ExprDoc predicate{ffi::UnsafeInit()};
825  ffi::Array<StmtDoc> then_branch;
827  ffi::Array<StmtDoc> else_branch;
828 
829  static void RegisterReflection() {
830  namespace refl = tvm::ffi::reflection;
831  refl::ObjectDef<IfDocNode>()
832  .def_ro("predicate", &IfDocNode::predicate)
833  .def_ro("then_branch", &IfDocNode::then_branch)
834  .def_ro("else_branch", &IfDocNode::else_branch);
835  }
837 };
838 
844 class IfDoc : public StmtDoc {
845  public:
852  explicit IfDoc(ExprDoc predicate, ffi::Array<StmtDoc> then_branch,
853  ffi::Array<StmtDoc> else_branch);
855 };
856 
862 class WhileDocNode : public StmtDocNode {
863  public:
865  ExprDoc predicate{ffi::UnsafeInit()};
867  ffi::Array<StmtDoc> body;
868 
869  static void RegisterReflection() {
870  namespace refl = tvm::ffi::reflection;
871  refl::ObjectDef<WhileDocNode>()
872  .def_ro("predicate", &WhileDocNode::predicate)
873  .def_ro("body", &WhileDocNode::body);
874  }
876 };
877 
883 class WhileDoc : public StmtDoc {
884  public:
890  explicit WhileDoc(ExprDoc predicate, ffi::Array<StmtDoc> body);
892 };
893 
903 class ForDocNode : public StmtDocNode {
904  public:
906  ExprDoc lhs{ffi::UnsafeInit()};
908  ExprDoc rhs{ffi::UnsafeInit()};
910  ffi::Array<StmtDoc> body;
911 
912  static void RegisterReflection() {
913  namespace refl = tvm::ffi::reflection;
914  refl::ObjectDef<ForDocNode>()
915  .def_ro("lhs", &ForDocNode::lhs)
916  .def_ro("rhs", &ForDocNode::rhs)
917  .def_ro("body", &ForDocNode::body);
918  }
920 };
921 
927 class ForDoc : public StmtDoc {
928  public:
935  explicit ForDoc(ExprDoc lhs, ExprDoc rhs, ffi::Array<StmtDoc> body);
937 };
938 
949 class ScopeDocNode : public StmtDocNode {
950  public:
952  ffi::Optional<ExprDoc> lhs{std::nullopt};
954  ExprDoc rhs{ffi::UnsafeInit()};
956  ffi::Array<StmtDoc> body;
957 
958  static void RegisterReflection() {
959  namespace refl = tvm::ffi::reflection;
960  refl::ObjectDef<ScopeDocNode>()
961  .def_ro("lhs", &ScopeDocNode::lhs)
962  .def_ro("rhs", &ScopeDocNode::rhs)
963  .def_ro("body", &ScopeDocNode::body);
964  }
966 };
967 
973 class ScopeDoc : public StmtDoc {
974  public:
981  explicit ScopeDoc(ffi::Optional<ExprDoc> lhs, ExprDoc rhs, ffi::Array<StmtDoc> body);
982 
988  explicit ScopeDoc(ExprDoc rhs, ffi::Array<StmtDoc> body);
989 
991 };
992 
998 class ExprStmtDocNode : public StmtDocNode {
999  public:
1001  ExprDoc expr{ffi::UnsafeInit()};
1002 
1003  static void RegisterReflection() {
1004  namespace refl = tvm::ffi::reflection;
1005  refl::ObjectDef<ExprStmtDocNode>().def_ro("expr", &ExprStmtDocNode::expr);
1006  }
1008 };
1009 
1015 class ExprStmtDoc : public StmtDoc {
1016  public:
1021  explicit ExprStmtDoc(ExprDoc expr);
1023 };
1024 
1030 class AssertDocNode : public StmtDocNode {
1031  public:
1033  ExprDoc test{ffi::UnsafeInit()};
1035  ffi::Optional<ExprDoc> msg{std::nullopt};
1036 
1037  static void RegisterReflection() {
1038  namespace refl = tvm::ffi::reflection;
1039  refl::ObjectDef<AssertDocNode>()
1040  .def_ro("test", &AssertDocNode::test)
1041  .def_ro("msg", &AssertDocNode::msg);
1042  }
1044 };
1045 
1051 class AssertDoc : public StmtDoc {
1052  public:
1058  explicit AssertDoc(ExprDoc test, ffi::Optional<ExprDoc> msg = std::nullopt);
1060 };
1061 
1067 class ReturnDocNode : public StmtDocNode {
1068  public:
1070  ExprDoc value{ffi::UnsafeInit()};
1071 
1072  static void RegisterReflection() {
1073  namespace refl = tvm::ffi::reflection;
1074  refl::ObjectDef<ReturnDocNode>().def_ro("value", &ReturnDocNode::value);
1075  }
1077 };
1078 
1084 class ReturnDoc : public StmtDoc {
1085  public:
1090  explicit ReturnDoc(ExprDoc value);
1092 };
1093 
1100  public:
1102  IdDoc name{ffi::UnsafeInit{}};
1110  ffi::Array<AssignDoc> args;
1112  ffi::Array<ExprDoc> decorators;
1114  ffi::Optional<ExprDoc> return_type{std::nullopt};
1116  ffi::Array<StmtDoc> body;
1117 
1118  static void RegisterReflection() {
1119  namespace refl = tvm::ffi::reflection;
1120  refl::ObjectDef<FunctionDocNode>()
1121  .def_ro("name", &FunctionDocNode::name)
1122  .def_ro("args", &FunctionDocNode::args)
1123  .def_ro("decorators", &FunctionDocNode::decorators)
1124  .def_ro("return_type", &FunctionDocNode::return_type)
1125  .def_ro("body", &FunctionDocNode::body);
1126  }
1128 };
1129 
1135 class FunctionDoc : public StmtDoc {
1136  public:
1145  explicit FunctionDoc(IdDoc name, ffi::Array<AssignDoc> args, ffi::Array<ExprDoc> decorators,
1146  ffi::Optional<ExprDoc> return_type, ffi::Array<StmtDoc> body);
1148 };
1149 
1155 class ClassDocNode : public StmtDocNode {
1156  public:
1158  IdDoc name{ffi::UnsafeInit{}};
1160  ffi::Array<ExprDoc> decorators;
1162  ffi::Array<StmtDoc> body;
1163 
1164  static void RegisterReflection() {
1165  namespace refl = tvm::ffi::reflection;
1166  refl::ObjectDef<ClassDocNode>()
1167  .def_ro("name", &ClassDocNode::name)
1168  .def_ro("decorators", &ClassDocNode::decorators)
1169  .def_ro("body", &ClassDocNode::body);
1170  }
1172 };
1173 
1179 class ClassDoc : public StmtDoc {
1180  public:
1187  explicit ClassDoc(IdDoc name, ffi::Array<ExprDoc> decorators, ffi::Array<StmtDoc> body);
1189 };
1190 
1196 class CommentDocNode : public StmtDocNode {
1197  public:
1198  static void RegisterReflection() {
1199  namespace refl = tvm::ffi::reflection;
1200  refl::ObjectDef<CommentDocNode>();
1201  }
1203 };
1204 
1210 class CommentDoc : public StmtDoc {
1211  public:
1212  explicit CommentDoc(ffi::String comment);
1214 };
1215 
1222  public:
1223  static void RegisterReflection() {
1224  namespace refl = tvm::ffi::reflection;
1225  refl::ObjectDef<DocStringDocNode>();
1226  }
1228 };
1229 
1235 class DocStringDoc : public StmtDoc {
1236  public:
1237  explicit DocStringDoc(ffi::String docs);
1239 };
1240 
1241 } // namespace printer
1242 } // namespace script
1243 } // namespace tvm
1244 
1245 #endif // TVM_SCRIPT_PRINTER_DOC_H_
Managed reference class to FloatImmNode.
Definition: expr.h:546
Managed reference class to IntImmNode.
Definition: expr.h:511
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:295
static DataType Bool(int lanes=1, bool is_scalable=false)
Construct a bool type.
Definition: data_type.h:387
static DataType Int(int bits, int lanes=1)
Construct an int type.
Definition: data_type.h:278
bool is_void() const
Definition: data_type.h:213
Doc that represents assert statement.
Definition: doc.h:1030
ffi::Optional< ExprDoc > msg
The optional error message when assertion failed.
Definition: doc.h:1035
static void RegisterReflection()
Definition: doc.h:1037
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AssertDoc", AssertDocNode, StmtDocNode)
ExprDoc test
The expression to test.
Definition: doc.h:1033
Reference type of AssertDocNode.
Definition: doc.h:1051
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:775
ffi::Optional< ExprDoc > annotation
The type annotation of this assignment.
Definition: doc.h:786
ffi::Optional< ExprDoc > rhs
The right hand side of the assignment.
Definition: doc.h:784
ExprDoc lhs
The left hand side of the assignment.
Definition: doc.h:778
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AssignDoc", AssignDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:788
Reference type of AssignDocNode.
Definition: doc.h:803
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:364
ffi::String name
The attribute to be accessed.
Definition: doc.h:369
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.AttrAccessDoc", AttrAccessDocNode, ExprDocNode)
ExprDoc value
The target expression to be accessed.
Definition: doc.h:367
static void RegisterReflection()
Definition: doc.h:371
Reference type of AttrAccessDocNode.
Definition: doc.h:385
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:444
ffi::Array< ffi::String > kwargs_keys
The keys of keyword arguments.
Definition: doc.h:451
ffi::Array< ExprDoc > kwargs_values
The values of keyword arguments.
Definition: doc.h:458
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.CallDoc", CallDocNode, ExprDocNode)
static void RegisterReflection()
Definition: doc.h:460
ffi::Array< ExprDoc > args
The positional arguments.
Definition: doc.h:449
ExprDoc callee
The callee of this function call.
Definition: doc.h:447
Reference type of CallDocNode.
Definition: doc.h:476
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:1155
ffi::Array< ExprDoc > decorators
Decorators of class.
Definition: doc.h:1160
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ClassDoc", ClassDocNode, StmtDocNode)
static void RegisterReflection()
Definition: doc.h:1164
IdDoc name
The name of class.
Definition: doc.h:1158
ffi::Array< StmtDoc > body
The body of class.
Definition: doc.h:1162
Reference type of ClassDocNode.
Definition: doc.h:1179
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:1196
static void RegisterReflection()
Definition: doc.h:1198
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.CommentDoc", CommentDocNode, StmtDocNode)
Reference type of CommentDocNode.
Definition: doc.h:1210
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(CommentDoc, StmtDoc, CommentDocNode)
CommentDoc(ffi::String comment)
Doc that represents dictionary literal.
Definition: doc.h:685
ffi::Array< ExprDoc > keys
keys of dictionary
Definition: doc.h:688
static void RegisterReflection()
Definition: doc.h:697
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.DictDoc", DictDocNode, ExprDocNode)
ffi::Array< ExprDoc > values
Values of dictionary.
Definition: doc.h:695
Reference type of DictDocNode.
Definition: doc.h:711
DictDoc(ffi::Array< ExprDoc > keys, ffi::Array< ExprDoc > values)
Constructor of DictDoc.
DictDoc()
Create an empty dictionary.
Definition: doc.h:716
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DictDoc, ExprDoc, DictDocNode)
The base class of all Doc.
Definition: doc.h:57
TVM_FFI_DECLARE_OBJECT_INFO("script.printer.Doc", DocNode, Object)
static constexpr bool _type_mutable
Definition: doc.h:73
virtual ~DocNode()=default
static void RegisterReflection()
Definition: doc.h:68
ffi::Array< ffi::reflection::AccessPath > source_paths
The list of object paths of the source IR node.
Definition: doc.h:66
Doc that represents docstring.
Definition: doc.h:1221
static void RegisterReflection()
Definition: doc.h:1223
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.DocStringDoc", DocStringDocNode, StmtDocNode)
Reference type of DocStringDocNode.
Definition: doc.h:1235
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(DocStringDoc, StmtDoc, DocStringDocNode)
Reference type of DocNode.
Definition: doc.h:86
Doc(ObjectPtr< DocNode > data)
Definition: doc.h:89
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Doc, ObjectRef, DocNode)
The base class of expression doc.
Definition: doc.h:102
ExprDoc Attr(ffi::String attr) const
Create a doc representing attribute access on the current ExprDoc.
static void RegisterReflection()
Definition: doc.h:132
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:144
ExprDoc(ObjectPtr< ExprDocNode > data)
Definition: doc.h:155
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:998
static void RegisterReflection()
Definition: doc.h:1003
ExprDoc expr
The expression represented by this doc.
Definition: doc.h:1001
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ExprStmtDoc", ExprStmtDocNode, StmtDocNode)
Reference type of ExprStmtDocNode.
Definition: doc.h:1015
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ExprStmtDoc, StmtDoc, ExprStmtDocNode)
ExprStmtDoc(ExprDoc expr)
Constructor of ExprStmtDoc.
Doc that represents for statement.
Definition: doc.h:903
ffi::Array< StmtDoc > body
The body of the for statement.
Definition: doc.h:910
ExprDoc lhs
The left hand side of the assignment of iterating variable.
Definition: doc.h:906
static void RegisterReflection()
Definition: doc.h:912
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:908
Reference type of ForDocNode.
Definition: doc.h:927
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:1099
ffi::Array< AssignDoc > args
The arguments of function.
Definition: doc.h:1110
ffi::Array< StmtDoc > body
The body of function.
Definition: doc.h:1116
IdDoc name
The name of function.
Definition: doc.h:1102
static void RegisterReflection()
Definition: doc.h:1118
ffi::Array< ExprDoc > decorators
Decorators of function.
Definition: doc.h:1112
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.FunctionDoc", FunctionDocNode, StmtDocNode)
ffi::Optional< ExprDoc > return_type
The return type of function.
Definition: doc.h:1114
Reference type of FunctionDocNode.
Definition: doc.h:1135
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:331
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IdDoc", IdDocNode, ExprDocNode)
ffi::String name
The name of the identifier.
Definition: doc.h:334
static void RegisterReflection()
Definition: doc.h:336
Reference type of IdDocNode.
Definition: doc.h:348
IdDoc(std::nullptr_t)
Definition: doc.h:355
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:820
static void RegisterReflection()
Definition: doc.h:829
ffi::Array< StmtDoc > else_branch
The else branch of the if-then-else statement.
Definition: doc.h:827
ffi::Array< StmtDoc > then_branch
The then branch of the if-then-else statement.
Definition: doc.h:825
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IfDoc", IfDocNode, StmtDocNode)
ExprDoc predicate
The predicate of the if-then-else statement.
Definition: doc.h:823
Reference type of IfDocNode.
Definition: doc.h:844
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:401
static void RegisterReflection()
Definition: doc.h:414
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.IndexDoc", IndexDocNode, ExprDocNode)
ffi::Array< Doc > indices
The indices to access.
Definition: doc.h:412
ExprDoc value
The container value to be accessed.
Definition: doc.h:404
Reference type of IndexDocNode.
Definition: doc.h:428
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:576
static void RegisterReflection()
Definition: doc.h:583
ExprDoc body
The body of this anonymous function.
Definition: doc.h:581
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.LambdaDoc", LambdaDocNode, ExprDocNode)
ffi::Array< IdDoc > args
The arguments of this anonymous function.
Definition: doc.h:579
Reference type of LambdaDocNode.
Definition: doc.h:597
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:649
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ListDoc", ListDocNode, ExprDocNode)
ffi::Array< ExprDoc > elements
Elements of list.
Definition: doc.h:652
static void RegisterReflection()
Definition: doc.h:654
Reference type of ListDocNode.
Definition: doc.h:666
ListDoc()
Create an empty ListDoc.
Definition: doc.h:671
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:234
ffi::Any value
the internal representation of the literal value.
Definition: doc.h:245
static void RegisterReflection()
Definition: doc.h:247
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.LiteralDoc", LiteralDocNode, ExprDocNode)
Reference type of LiteralDocNode.
Definition: doc.h:259
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:284
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:308
static LiteralDoc Device(const DLDevice &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent device.
Definition: doc.h:317
static LiteralDoc Int(int64_t v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent integer.
Definition: doc.h:276
static LiteralDoc Float(double v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent float.
Definition: doc.h:292
static LiteralDoc None(const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent None/null/empty value.
Definition: doc.h:268
static LiteralDoc Str(const ffi::String &v, const ffi::Optional< AccessPath > &p)
Create a LiteralDoc to represent string.
Definition: doc.h:300
Doc that represents operation.
Definition: doc.h:498
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.OperationDoc", OperationDocNode, ExprDocNode)
ffi::Array< ExprDoc > operands
Operands of this expression.
Definition: doc.h:541
Kind kind
The kind of operation (operator)
Definition: doc.h:539
static void RegisterReflection()
Definition: doc.h:543
Reference type of OperationDocNode.
Definition: doc.h:557
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:1067
ExprDoc value
The value to return.
Definition: doc.h:1070
static void RegisterReflection()
Definition: doc.h:1072
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ReturnDoc", ReturnDocNode, StmtDocNode)
Reference type of ReturnDocNode.
Definition: doc.h:1084
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(ReturnDoc, StmtDoc, ReturnDocNode)
ReturnDoc(ExprDoc value)
Constructor of ReturnDoc.
Doc that represents special scopes.
Definition: doc.h:949
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.ScopeDoc", ScopeDocNode, StmtDocNode)
ffi::Optional< ExprDoc > lhs
The name of the scoped variable.
Definition: doc.h:952
ExprDoc rhs
The value of the scoped variable.
Definition: doc.h:954
static void RegisterReflection()
Definition: doc.h:958
ffi::Array< StmtDoc > body
The body of the scope doc.
Definition: doc.h:956
Reference type of ScopeDocNode.
Definition: doc.h:973
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:733
ffi::Optional< ExprDoc > start
The start of slice.
Definition: doc.h:736
ffi::Optional< ExprDoc > step
The step of slice.
Definition: doc.h:740
ffi::Optional< ExprDoc > stop
The exclusive end of slice.
Definition: doc.h:738
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.SliceDoc", SliceDocNode, DocNode)
static void RegisterReflection()
Definition: doc.h:742
Reference type of SliceDocNode.
Definition: doc.h:757
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:203
static void RegisterReflection()
Definition: doc.h:208
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.StmtBlockDoc", StmtBlockDocNode, DocNode)
ffi::Array< StmtDoc > stmts
The list of statements.
Definition: doc.h:206
Reference type of StmtBlockDocNode.
Definition: doc.h:219
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:165
ffi::Optional< ffi::String > comment
The comment of this doc.
Definition: doc.h:175
static void RegisterReflection()
Definition: doc.h:177
TVM_FFI_DECLARE_OBJECT_INFO("script.printer.StmtDoc", StmtDocNode, DocNode)
Reference type of StmtDocNode.
Definition: doc.h:189
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(StmtDoc, Doc, StmtDocNode)
Doc that represents tuple literal.
Definition: doc.h:613
ffi::Array< ExprDoc > elements
Elements of tuple.
Definition: doc.h:616
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.TupleDoc", TupleDocNode, ExprDocNode)
static void RegisterReflection()
Definition: doc.h:618
Reference type of TupleDocNode.
Definition: doc.h:630
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:635
Doc that represents while statement.
Definition: doc.h:862
static void RegisterReflection()
Definition: doc.h:869
ffi::Array< StmtDoc > body
The body of the while statement.
Definition: doc.h:867
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("script.printer.WhileDoc", WhileDocNode, StmtDocNode)
ExprDoc predicate
The predicate of the while statement.
Definition: doc.h:865
Reference type of WhileDocNode.
Definition: doc.h:883
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:457
ffi::String DocToPythonScript(Doc doc, const PrinterConfig &cfg)
Convert Doc into Python script.
ffi::reflection::AccessPath AccessPath
Definition: doc.h:34
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37