You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tvm.apache.org by GitBox <gi...@apache.org> on 2020/11/10 13:29:03 UTC

[GitHub] [incubator-tvm] tqchen commented on a change in pull request #6887: Fix bug of generate-unmatched-brackets in CodeGenC::PrintSSAAssign

tqchen commented on a change in pull request #6887:
URL: https://github.com/apache/incubator-tvm/pull/6887#discussion_r520560320



##########
File path: src/target/source/codegen_c.cc
##########
@@ -139,11 +139,29 @@ void CodeGenC::PrintExpr(const PrimExpr& n, std::ostream& os) {  // NOLINT(*)
   }
 }
 
+static bool CheckOutmostBracketMatch(const std::string& s) {
+  if (s.front() == '(' && s.back() == ')') {
+    int len = s.size();

Review comment:
       size_t

##########
File path: src/target/source/codegen_c.cc
##########
@@ -139,11 +139,29 @@ void CodeGenC::PrintExpr(const PrimExpr& n, std::ostream& os) {  // NOLINT(*)
   }
 }
 
+static bool CheckOutmostBracketMatch(const std::string& s) {
+  if (s.front() == '(' && s.back() == ')') {

Review comment:
       need tyo first check the length to avoid overflow

##########
File path: src/target/source/codegen_c.cc
##########
@@ -139,11 +139,29 @@ void CodeGenC::PrintExpr(const PrimExpr& n, std::ostream& os) {  // NOLINT(*)
   }
 }
 
+static bool CheckOutmostBracketMatch(const std::string& s) {
+  if (s.front() == '(' && s.back() == ')') {
+    int len = s.size();
+    int n_unmatched = 0;
+    for (int i = 0; i < len; ++i) {
+      if (s[i] == '(') {
+        n_unmatched++;
+      } else if (s[i] == ')') {
+        n_unmatched--;
+      }
+      if (n_unmatched == 0) {
+        return i == len - 1;
+      }
+    }
+  }
+  return false;
+}
+
 void CodeGenC::PrintSSAAssign(const std::string& target, const std::string& src, DataType t) {
   PrintType(t, stream);
   stream << ' ' << target << " = ";
-  if (src.length() > 3 && src[0] == '(' && src[src.length() - 1] == ')') {
-    stream << src.substr(1, src.length() - 2);
+  if (CheckOutmostBracketMatch(src)) {
+    stream << src.substr(1, src.length() - 1);

Review comment:
       change this line back as we need to also remove the bracket in the end

##########
File path: src/target/source/codegen_c.cc
##########
@@ -139,11 +139,29 @@ void CodeGenC::PrintExpr(const PrimExpr& n, std::ostream& os) {  // NOLINT(*)
   }
 }
 
+static bool CheckOutmostBracketMatch(const std::string& s) {

Review comment:
       Move static after all impl of the non-static function

##########
File path: src/target/source/codegen_c.cc
##########
@@ -139,11 +139,29 @@ void CodeGenC::PrintExpr(const PrimExpr& n, std::ostream& os) {  // NOLINT(*)
   }
 }
 
+static bool CheckOutmostBracketMatch(const std::string& s) {

Review comment:
       CheckOutermostBracketMatch




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

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