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 "tqchen (via GitHub)" <gi...@apache.org> on 2026/07/04 16:18:17 UTC

[PR] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

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

   Replace the nested `DocToPythonScript` invocation in Relax dependent-shape printing with an expression-string Doc rendered during the active `PythonDocPrinter` traversal.
   
   This keeps recursive IR-to-doc conversion inside the active docsifier, preserves naming, precedence, escaping, source paths, and printer configuration, and avoids a nested top-level renderer or a new public rendering entry point.


-- 
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] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19940:
URL: https://github.com/apache/tvm/pull/19940#discussion_r3523797802


##########
src/script/printer/doc_printer/python_doc_printer.cc:
##########
@@ -394,6 +396,19 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc) {
   }
 }
 
+void PythonDocPrinter::PrintTypedDoc(const ExprStringDoc& doc) {
+  this->output_ << '"';
+  size_t start_pos = this->output_.tellp();
+  this->PrintDoc(doc->value);
+  std::string output = this->output_.str();
+  std::string value = output.substr(start_pos);
+  TVM_FFI_ICHECK(value.find('\n') == std::string::npos)
+      << "An expression rendered inside a Python string literal must be one line";
+  this->output_.str(output.substr(0, start_pos));
+  this->output_.seekp(start_pos);
+  this->output_ << support::StrEscape(value) << '"';
+}

Review Comment:
   Follow-up correction `a41b121336` now streams the existing byte-wise Python escaping directly into the final output only while `this->PrintDoc(doc->value)` runs. The scoped adapter delegates `tellp()` to the destination, so child spans are recorded at absolute escaped offsets; it also restores the original buffer and stream state on every exit. A focused `T.Cast("int64", n)` regression verifies the 22-byte wrapper, 9-byte escaped dtype, and 1-byte value spans at a nonzero prefix, repeated configuration use, and no diagnostic leakage. The build passed; the focused/adjacent suites passed 402 tests with 2 expected failures, and the broader printer/type suite passed 1,022 tests with 1 expected failure and 2 no-LLVM deselections. Both private and public PR heads resolve exactly to this commit; no merge was performed.



-- 
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] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19940:
URL: https://github.com/apache/tvm/pull/19940#discussion_r3523729367


##########
src/script/printer/doc_printer/python_doc_printer.cc:
##########
@@ -394,6 +396,19 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc) {
   }
 }
 
+void PythonDocPrinter::PrintTypedDoc(const ExprStringDoc& doc) {
+  this->output_ << '"';
+  size_t start_pos = this->output_.tellp();
+  this->PrintDoc(doc->value);
+  std::string output = this->output_.str();
+  std::string value = output.substr(start_pos);
+  TVM_FFI_ICHECK(value.find('\n') == std::string::npos)
+      << "An expression rendered inside a Python string literal must be one line";
+  this->output_.str(output.substr(0, start_pos));
+  this->output_.seekp(start_pos);
+  this->output_ << support::StrEscape(value) << '"';
+}

Review Comment:
   Addressed in `3546a2ad84`. The active `PythonDocPrinter` now redirects only its existing output stream buffer to an isolated child sink, so the printer instance, original configuration, locale, and formatting state are preserved. It copies and escapes only the child bytes, eliminating the accumulated-prefix copies and restoring linear output behavior. Child source-path registration is suppressed while offsets are child-local and pre-escape; after restoration, the enclosing `ExprStringDoc` records the complete final escaped literal span. This matches the earlier `LiteralDoc` diagnostic granularity and prevents raw child offsets from leaking. Existing focused/adjacent/broad suites pass (49, 354, and 1,164 tests respectively), and a disposable `T.Cast("int64", n)` probe verified the escaped wrapper underline, nearest-visible-parent behavior, config reuse, and absence of stale child carets or duplicated diagnostic text. No new test was checked in.



-- 
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] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

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


##########
src/script/printer/doc_printer/python_doc_printer.cc:
##########
@@ -394,6 +396,19 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc) {
   }
 }
 
+void PythonDocPrinter::PrintTypedDoc(const ExprStringDoc& doc) {
+  this->output_ << '"';
+  size_t start_pos = this->output_.tellp();
+  this->PrintDoc(doc->value);
+  std::string output = this->output_.str();
+  std::string value = output.substr(start_pos);
+  TVM_FFI_ICHECK(value.find('\n') == std::string::npos)
+      << "An expression rendered inside a Python string literal must be one line";
+  this->output_.str(output.substr(0, start_pos));
+  this->output_.seekp(start_pos);
+  this->output_ << support::StrEscape(value) << '"';
+}

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   ### Performance Bottleneck and Potential Underline Bug in `ExprStringDoc` Printing\n\nCalling `this->output_.str()` copies the **entire** accumulated output string so far. In large TVMScript files (e.g., large Relax or TIR models with many shape variables), calling this repeatedly for every `ExprStringDoc` results in quadratic $O(N^2)$ time and memory complexity, where $N$ is the size of the generated script.\n\nAdditionally, calling `PrintDoc(doc->value)` on the main printer can register incorrect source path underline spans (relative to the un-escaped positions) in `current_underline_candidates_`, which are never cleared or adjusted, potentially leading to misaligned or misplaced diagnostic underlines.\n\n### Solution\nWe can instantiate a temporary, isolated `PythonDocPrinter` with a default `PrinterConfig` to print the sub-expression. This completely avoids copying the main output buffer and ensures any recorded source path spans are safely discarded when the temporary printer
  goes out of scope.
   
   ```c
   void PythonDocPrinter::PrintTypedDoc(const ExprStringDoc& doc) {
     PythonDocPrinter sub_printer(PrinterConfig());
     sub_printer.PrintDoc(doc->value);
     std::string value = sub_printer.output_.str();
     TVM_FFI_ICHECK(value.find('\\n') == std::string::npos)
         << "An expression rendered inside a Python string literal must be one line";
     this->output_ << '"' << support::StrEscape(value) << '"';
   }
   ```



-- 
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] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen commented on code in PR #19940:
URL: https://github.com/apache/tvm/pull/19940#discussion_r3523830453


##########
src/script/printer/doc_printer/python_doc_printer.cc:
##########
@@ -394,6 +396,19 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc) {
   }
 }
 
+void PythonDocPrinter::PrintTypedDoc(const ExprStringDoc& doc) {
+  this->output_ << '"';
+  size_t start_pos = this->output_.tellp();
+  this->PrintDoc(doc->value);
+  std::string output = this->output_.str();
+  std::string value = output.substr(start_pos);
+  TVM_FFI_ICHECK(value.find('\n') == std::string::npos)
+      << "An expression rendered inside a Python string literal must be one line";
+  this->output_.str(output.substr(0, start_pos));
+  this->output_.seekp(start_pos);
+  this->output_ << support::StrEscape(value) << '"';
+}

Review Comment:
   Follow-up `fe7571044c` adds the requested reviewer-facing documentation at `ScopedExprStringEscapeBuf`: why the active printer must transform writes into the final buffer for precise source spans, plus buffer ownership/lifetime, byte-wise escaping, absolute `tellp()`, newline/write failures, and exception-safe restoration. Behavior is unchanged. The incremental build and focused Relax/Python-doc/underlining/invisible-path suite passed (286 passed, 1 expected failure); private and public heads both match this commit, with fresh CI running.



-- 
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] [REFACTOR][SCRIPT] Keep dependent shape recursion in docsifier [tvm]

Posted by "tqchen (via GitHub)" <gi...@apache.org>.
tqchen merged PR #19940:
URL: https://github.com/apache/tvm/pull/19940


-- 
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