You are viewing a plain text version of this content. The canonical link for it is here.
Posted to discuss-archive@tvm.apache.org by "junrushao (via GitHub)" <gi...@apache.org> on 2026/04/06 03:27:39 UTC

[PR] [do not merge] tvmscript printer v2 [tvm]

junrushao opened a new pull request, #19359:
URL: https://github.com/apache/tvm/pull/19359

   (no comment)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [do not merge] tvmscript printer v2 [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen closed pull request #19359: [do not merge] tvmscript printer v2
URL: https://github.com/apache/tvm/pull/19359


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org


Re: [PR] [do not merge] tvmscript printer v2 [tvm]

Posted by "gemini-code-assist[bot] (via GitHub)" <gi...@apache.org>.
gemini-code-assist[bot] commented on code in PR #19359:
URL: https://github.com/apache/tvm/pull/19359#discussion_r3037810822


##########
src/relax/ir/script_print_utils.h:
##########
@@ -0,0 +1,425 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+#ifndef TVM_RELAX_IR_SCRIPT_PRINT_UTILS_H_
+#define TVM_RELAX_IR_SCRIPT_PRINT_UTILS_H_
+
+#include <tvm/ir/expr.h>
+#include <tvm/ir/global_info.h>
+#include <tvm/relax/attrs/op.h>
+#include <tvm/relax/distributed/global_info.h>
+#include <tvm/relax/distributed/struct_info.h>
+#include <tvm/relax/expr.h>
+#include <tvm/relax/struct_info.h>
+#include <tvm/ffi/reflection/accessor.h>
+#include <tvm/tirx/expr.h>
+#include <tvm/tirx/stmt_functor.h>
+
+#include <cmath>
+#include <limits>
+#include <sstream>
+#include <unordered_set>
+
+#include "../../script_v2/printer_utils.h"
+
+namespace tvm {
+namespace script_v2 {
+
+using namespace relax;
+
+// Thread-local flag: true when printing function param/return annotations.
+// Used by PrintShapeValue to decide whether to stringify symbolic TIR vars.
+inline thread_local bool g_printing_func_annotation = false;

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   The use of a `thread_local` global variable `g_printing_func_annotation` introduces global state, which can make the code harder to reason about, test, and maintain. It creates an implicit dependency for `PrintShapeValue` on its callers.
   
   While this might be a pragmatic solution given the printer's architecture, consider if it's possible to pass a configuration/context object down the call stack instead. If not, it would be beneficial to add a comment explaining why this approach is necessary and the potential risks involved.



##########
src/relax/ir/expr.cc:
##########
@@ -754,5 +795,767 @@ TVM_FFI_STATIC_INIT_BLOCK() {
       });
 }
 
+// ---- __ffi_text_print__ overrides (migrated from script_v2/printer/relax/expr.cc) ----
+
+TVM_FFI_STATIC_INIT_BLOCK() {
+  using namespace script_v2;
+  namespace refl = ::tvm::ffi::reflection;
+  namespace text = ::tvm::ffi::ir::text;
+  // DataflowBlock: with R.dataflow(): bindings + R.output(...)
+  // Must use __ffi_text_print__ (not trait) to preserve output vars in parent scope.
+  // The With trait's FramePop removes all vars, causing the SeqExpr body return to fail.
+  refl::TypeAttrDef<DataflowBlockNode>().def(
+      "__ffi_text_print__",
+      [](DataflowBlock node, text::IRPrinter printer, text::AccessPath path) -> text::NodeAST {
+        text::DefaultFrame frame;
+        printer->FramePush(frame);
+        ffi::List<text::StmtAST> body;
+        for (int i = 0; i < static_cast<int>(node->bindings.size()); ++i) {
+          text::NodeAST s = printer->operator()(ffi::Any(node->bindings[i]),
+                                           path->Attr("bindings")->ArrayItem(i))
+                          .cast<text::NodeAST>();
+          if (auto* block = s.as<text::StmtBlockASTObj>()) {
+            for (const auto& st : block->stmts) body.push_back(st);
+          } else if (s.get()->IsInstance<text::StmtASTObj>()) {
+            body.push_back(ffi::GetRef<text::StmtAST>(static_cast<const text::StmtASTObj*>(s.get())));
+          } else if (s.get()->IsInstance<text::ExprASTObj>()) {
+            body.push_back(text::ExprStmtAST(ffi::GetRef<text::ExprAST>(static_cast<const text::ExprASTObj*>(s.get()))));
+          }
+        }
+        // R.output() for non-DataflowVar bindings
+        ffi::List<text::ExprAST> outputs;
+        for (const auto& b : node->bindings) {
+          if (!b->var->IsInstance<DataflowVarNode>()) {
+            if (auto var_doc = printer->VarGet(b->var)) {
+              outputs.push_back(var_doc.value());
+            }
+          }
+        }
+        if (!outputs.empty()) {
+          body.push_back(text::ExprStmtAST(text::ExprCall(Relax("output"), std::move(outputs))));
+        }
+        // Pop frame BUT re-register output vars in the PARENT frame so they're
+        // accessible to the enclosing SeqExpr's return statement.
+        ffi::List<ffi::Any> output_vars;
+        for (const auto& b : node->bindings) {
+          if (!b->var->IsInstance<DataflowVarNode>()) {
+            output_vars.push_back(b->var);
+          }
+        }
+        // Save the var info before popping
+        ffi::Map<ffi::Any, text::VarInfo> saved_info;
+        for (const auto& ov : output_vars) {
+          auto it = printer->obj2info.find(ov);
+          if (it != printer->obj2info.end()) {
+            saved_info.Set(ov, (*it).second);
+          }
+        }
+        printer->FramePop();
+        // Re-register output vars in parent frame
+        if (!printer->frames.empty()) {
+          ffi::ObjectRef parent_frame = printer->frames.back().cast<ffi::ObjectRef>();
+          for (const auto& kv : saved_info) {
+            printer->obj2info.Set(kv.first, kv.second);
+            // Add to parent frame's var list
+            auto frame_vars = printer->frame_vars[parent_frame].cast<ffi::List<ffi::Any>>();
+            frame_vars.push_back(kv.first);
+          }
+        }

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   This block of code manually manages variable scopes by saving variable info, popping a frame, and then re-registering variables in the parent frame. This is very fragile and tightly coupled to the internal implementation of the `IRPrinter`.
   
   While the comment explains why this is necessary (to make dataflow output vars available to the subsequent `return` statement), this workaround is a significant maintainability risk. A future change in the printer's frame management could easily break this. This might indicate a need for a more robust scope management mechanism in the core printer framework to handle such cases gracefully.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: discuss-archive-unsubscribe@tvm.apache.org
For additional commands, e-mail: discuss-archive-help@tvm.apache.org