masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829398822



##########
File path: src/tir/transforms/common_subexpr_elim_tools.cc
##########
@@ -748,8 +748,26 @@ std::vector<std::pair<PrimExpr, size_t>> 
SyntacticToSemanticComputations(
   // We do this reservation even if it might reserve slightly more space than 
is needed in the end
   result.reserve(table.size());
 
-  // For each element in the hashtable
+  // Traverse through keys in a sorted order to maintain deterministic behavior
+  // We do this by comparing the string repr of each PrimExpr to get a 
determinstic ordering
+  std::vector<PrimExpr> table_keys;
+  table_keys.reserve(table.size());
   for (auto elem : table) {
+    table_keys.push_back(elem.first);
+  }
+  sort(table_keys.begin(), table_keys.end(), [](PrimExpr a, PrimExpr b) {
+    std::stringstream a_stream;
+    std::stringstream b_stream;
+    a_stream << a;
+    b_stream << b;
+    return a_stream.str().compare(b_stream.str()) < 0;
+  });
+
+  // For each element in the hashtable
+  for (PrimExpr key : table_keys) {
+    size_t value = table.find(key)->second;

Review comment:
       Actually, if you have a sorted `std::vector<std::pair<PrimExpr, 
size_t>>` beforehand, this transform can be done in a one pass, rather than the 
current O(N^2) implementation. Just loop through each input pair and count 
until `EquivalentTerms` returns false etc.




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


Reply via email to