@@ -63,46 +63,49 @@ InstructionKindRegEntry& InstructionKindRegEntry::RegisterOrGet(const ffi::Strin
6363
6464/* *************** Repr ****************/
6565
66+ namespace {
67+ ffi::String InstructionAsPythonRepr (const InstructionNode* self) {
68+ ffi::Array<Any> inputs;
69+ inputs.reserve (self->inputs .size ());
70+ for (const Any& obj : self->inputs ) {
71+ if (obj == nullptr ) {
72+ inputs.push_back (ffi::String (" None" ));
73+ } else if (auto opt_str = obj.as <ffi::String>()) {
74+ inputs.push_back (ffi::String (' "' + (*opt_str).operator std::string () + ' "' ));
75+ } else if (obj.as <SBlockRVNode>() || obj.as <LoopRVNode>()) {
76+ inputs.push_back (ffi::String (" _" ));
77+ } else if (obj.type_index () < ffi::TypeIndex::kTVMFFISmallStr ) {
78+ inputs.push_back (obj);
79+ } else if (obj.as <IntImmNode>() || obj.as <FloatImmNode>()) {
80+ inputs.push_back (obj);
81+ } else if (const auto * expr = obj.as <PrimExprNode>()) {
82+ PrimExpr new_expr = Substitute (
83+ ffi::GetRef<PrimExpr>(expr), [](const Var& var) -> ffi::Optional<PrimExpr> {
84+ ObjectPtr<VarNode> new_var = ffi::make_object<VarNode>(*var.get ());
85+ new_var->name_hint = " _" ;
86+ return Var (new_var);
87+ });
88+ std::ostringstream os;
89+ os << new_expr;
90+ inputs.push_back (ffi::String (os.str ()));
91+ } else if (obj.as <IndexMapNode>()) {
92+ inputs.push_back (obj);
93+ } else {
94+ TVM_FFI_THROW (TypeError) << " Stringifying is not supported for type: " << obj.GetTypeKey ();
95+ throw ;
96+ }
97+ }
98+ return self->kind ->f_as_python (
99+ /* inputs=*/ inputs,
100+ /* attrs=*/ self->attrs ,
101+ /* decision=*/ Any (nullptr ),
102+ /* outputs=*/ ffi::Array<ffi::String>(self->outputs .size (), ffi::String (" _" )));
103+ }
104+ } // namespace
105+
66106TVM_STATIC_IR_FUNCTOR (ReprPrinter, vtable)
67107 .set_dispatch<InstructionNode>([](const ObjectRef& obj, ReprPrinter* p) {
68- const auto * self = obj.as <InstructionNode>();
69- TVM_FFI_ICHECK_NOTNULL (self);
70- ffi::Array<Any> inputs;
71- inputs.reserve (self->inputs .size ());
72- for (const Any& obj : self->inputs ) {
73- if (obj == nullptr ) {
74- inputs.push_back (ffi::String (" None" ));
75- } else if (auto opt_str = obj.as <ffi::String>()) {
76- inputs.push_back (ffi::String (' "' + (*opt_str).operator std::string () + ' "' ));
77- } else if (obj.as <SBlockRVNode>() || obj.as <LoopRVNode>()) {
78- inputs.push_back (ffi::String (" _" ));
79- } else if (obj.type_index () < ffi::TypeIndex::kTVMFFISmallStr ) {
80- inputs.push_back (obj);
81- } else if (obj.as <IntImmNode>() || obj.as <FloatImmNode>()) {
82- inputs.push_back (obj);
83- } else if (const auto * expr = obj.as <PrimExprNode>()) {
84- PrimExpr new_expr = Substitute (
85- ffi::GetRef<PrimExpr>(expr), [](const Var& var) -> ffi::Optional<PrimExpr> {
86- ObjectPtr<VarNode> new_var = ffi::make_object<VarNode>(*var.get ());
87- new_var->name_hint = " _" ;
88- return Var (new_var);
89- });
90- std::ostringstream os;
91- os << new_expr;
92- inputs.push_back (ffi::String (os.str ()));
93- } else if (obj.as <IndexMapNode>()) {
94- inputs.push_back (obj);
95- } else {
96- TVM_FFI_THROW (TypeError)
97- << " Stringifying is not supported for type: " << obj.GetTypeKey ();
98- throw ;
99- }
100- }
101- p->stream << self->kind ->f_as_python (
102- /* inputs=*/ inputs,
103- /* attrs=*/ self->attrs ,
104- /* decision=*/ Any (nullptr ),
105- /* outputs=*/ ffi::Array<ffi::String>(self->outputs .size (), ffi::String (" _" )));
108+ p->stream << InstructionAsPythonRepr (obj.as <InstructionNode>());
106109 });
107110
108111/* *************** FFI ****************/
@@ -116,6 +119,10 @@ TVM_FFI_STATIC_INIT_BLOCK() {
116119 ffi::Array<Any> outputs) -> Instruction {
117120 return Instruction (kind, inputs, attrs, outputs);
118121 });
122+ refl::TypeAttrDef<InstructionNode>().def (
123+ refl::type_attr::kRepr , [](Instruction inst, ffi::Function) -> ffi::String {
124+ return InstructionAsPythonRepr (inst.get ());
125+ });
119126}
120127
121128} // namespace s_tir
0 commit comments