Skip to content

Fix hashing of a use of a name without the context/target for it #3601

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions src/ir/ExpressionAnalyzer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,15 +331,28 @@ size_t ExpressionAnalyzer::hash(Expression* curr) {
}
}
void visitScopeName(Name curr) {
if (curr.is()) { // try's delegate target can be null
// Names are relative, we give the same hash for
// (block $x (br $x))
// (block $y (br $y))
static_assert(sizeof(Index) == sizeof(int32_t),
"wasm64 will need changes here");
assert(internalNames.find(curr) != internalNames.end());
rehash(digest, internalNames[curr]);
// We consider 3 cases here, and prefix a hash value of 0, 1, or 2 to
// maximally differentiate them.

// Try's delegate target can be null.
if (!curr.is()) {
rehash(digest, 0);
return;
}
// Names are relative, we give the same hash for
// (block $x (br $x))
// (block $y (br $y))
// But if the name is not known to us, hash the absolute one.
if (!internalNames.count(curr)) {
rehash(digest, 1);
// Perform the same hashing as a generic name.
visitNonScopeName(curr);
return;
}
rehash(digest, 2);
static_assert(sizeof(Index) == sizeof(int32_t),
"wasm64 will need changes here");
Comment on lines +353 to +354
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The data structure here is std::map<Name, Index> internalNames; so I think the idea in the assertion is that wasm64 might need something bigger than an Index if it has over 2^32 names? I agree it should be clarified.

rehash(digest, internalNames[curr]);
}
void visitNonScopeName(Name curr) { rehash(digest, uint64_t(curr.str)); }
void visitType(Type curr) { rehash(digest, curr.getID()); }
Expand Down
10 changes: 10 additions & 0 deletions test/example/hash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,15 @@ int main() {
y.index = 11;
assertNotEqual(x, y);
}
{
// It is ok to hash something that refers to an unknown name, like a break
// without the outside context that it branches to. And different names
// should have different hashes.
Break x;
x.name = "foo";
Break y;
y.name = "bar";
assertNotEqual(x, y);
}
std::cout << "success.\n";
}