gemini-code-assist[bot] commented on code in PR #18857:
URL: https://github.com/apache/tvm/pull/18857#discussion_r2868249242


##########
src/target/source/codegen_c_host.cc:
##########
@@ -323,9 +323,32 @@ void CodeGenCHost::VisitStmt_(const AssertStmtNode* op) {  
// NOLINT(*)
     PrintIndent();
     stream << "if (!(" << cond << ")) {\n";
     int assert_if_scope = this->BeginScope();
+    int num_parts = static_cast<int>(op->message_parts.size());
     PrintIndent();
-    stream << "TVMFFIErrorSetRaisedFromCStr(\"RuntimeError\", \""
-           << op->message.as<StringImmNode>()->value << "\", NULL);\n";
+    stream << "const char* __tvm_assert_parts[" << num_parts << "] = {";
+    for (int i = 0; i < num_parts; ++i) {
+      if (i > 0) stream << ", ";
+      stream << "\"";
+      // Escape special characters in the string
+      std::string part_str = op->message_parts[i]->value;
+      for (size_t j = 0; j < part_str.size(); ++j) {
+        char c = part_str[j];
+        if (c == '"') {
+          stream << "\\\"";
+        } else if (c == '\\') {
+          stream << "\\\\";
+        } else if (c == '\n') {
+          stream << "\\n";
+        } else {
+          stream << c;
+        }
+      }
+      stream << "\"";

Review Comment:
   ![medium](https://www.gstatic.com/codereviewagent/medium-priority.svg)
   
   This string escaping logic is duplicated in 
`src/target/source/codegen_c.cc`. Consider refactoring it into a shared helper 
function in `CodeGenC` to avoid code duplication and improve maintainability.



##########
python/tvm/script/parser/tir/parser.py:
##########
@@ -537,9 +537,23 @@ def visit_assert(self: Parser, node: doc.Assert) -> None:
 
     node : doc.Assert
         The doc AST assert node.
+
+    The assert message can be either:
+    - A plain string: ``assert cond, "message"``
+    - A tuple of (kind, [parts...]): ``assert cond, ("ValueError", ["part0", 
"part1"])``
     """
     cond = self.eval_expr(node.test)
     msg = self.eval_expr(node.msg)
+    if isinstance(msg, tuple) and len(msg) == 2:
+        kind, parts = msg
+        if isinstance(kind, tvm.tir.StringImm):
+            kind = kind.value
+        if isinstance(parts, list | tuple):
+            parts_str = [p.value if isinstance(p, tvm.tir.StringImm) else 
str(p) for p in parts]
+            frame = T.Assert(cond, "".join(parts_str))
+            frame.add_callback(partial(frame.__exit__, None, None, None))
+            frame.__enter__()
+            return

Review Comment:
   ![high](https://www.gstatic.com/codereviewagent/high-priority.svg)
   
   The new structured assert message `(kind, [parts...])` is not being handled 
correctly. The `kind` is ignored, and the `parts` are joined into a single 
string. This loses the structured error information that this PR aims to 
introduce.
   
   For example, `assert cond, ("ValueError", ["part0", "part1"])` will 
incorrectly produce an `AssertStmt` with `kind="RuntimeError"` and 
`message_parts=["part0part1"]`, instead of the expected `kind="ValueError"` and 
`message_parts=["part0", "part1"]`.
   
   To fix this, the `T.Assert` IR builder function should be overloaded to 
accept the structured format, or the parser needs to manually construct the 
`AssertFrame` with the correct fields. The current implementation is a bug.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to