masahi commented on a change in pull request #10663:
URL: https://github.com/apache/tvm/pull/10663#discussion_r829395007
##########
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:
I think it is better to sort and put the sorted key + count pair into a
vector before calling `SyntacticToSemanticComputations`. That way you can
remove this `table.find(key)` and `SyntacticToSemanticComputations` becomes
`vector -> vector` transform (which is cleaner imo).
--
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]