diff options
author | Ashley Nelson <nashley@google.com> | 2023-07-13 14:44:36 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-07-13 21:44:36 +0000 |
commit | 688d0beacf67697f4c73289c7eae57b8d973083f (patch) | |
tree | 41d5f066f8f6e7cacfc2cfafb5fc9643353e455e /src/passes/hash-stringify-walker.cpp | |
parent | 417792e90feb81b8b27863814cb0b38f5fe10b1f (diff) | |
download | binaryen-688d0beacf67697f4c73289c7eae57b8d973083f.tar.gz binaryen-688d0beacf67697f4c73289c7eae57b8d973083f.tar.bz2 binaryen-688d0beacf67697f4c73289c7eae57b8d973083f.zip |
[Outlining] HashStringifyWalker (#5810)
This PR is part of the effort to bring Outlining to Binaryen.
HashStringifyWalker is a subclass of StringifyWalker #5772, and used to encode a wasm module as a "string". Our "string" is a vector and each symbol is a uint64_t, providing us with a capacity of 2^64 symbols.
Diffstat (limited to 'src/passes/hash-stringify-walker.cpp')
-rw-r--r-- | src/passes/hash-stringify-walker.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/src/passes/hash-stringify-walker.cpp b/src/passes/hash-stringify-walker.cpp new file mode 100644 index 000000000..d5b6e0361 --- /dev/null +++ b/src/passes/hash-stringify-walker.cpp @@ -0,0 +1,54 @@ +#include "stringify-walker.h" + +namespace wasm { + +size_t StringifyHasher::operator()(Expression* curr) const { + if (Properties::isControlFlowStructure(curr)) { + if (auto* iff = curr->dynCast<If>()) { + size_t digest = wasm::hash(iff->_id); + rehash(digest, ExpressionAnalyzer::hash(iff->ifTrue)); + if (iff->ifFalse) { + rehash(digest, ExpressionAnalyzer::hash(iff->ifFalse)); + } + return digest; + } + + return ExpressionAnalyzer::hash(curr); + } + + return ExpressionAnalyzer::shallowHash(curr); +} + +bool StringifyEquator::operator()(Expression* lhs, Expression* rhs) const { + if (Properties::isControlFlowStructure(lhs) && + Properties::isControlFlowStructure(rhs)) { + auto* iffl = lhs->dynCast<If>(); + auto* iffr = rhs->dynCast<If>(); + + if (iffl && iffr) { + return ExpressionAnalyzer::equal(iffl->ifTrue, iffr->ifTrue) && + ExpressionAnalyzer::equal(iffl->ifFalse, iffr->ifFalse); + } + + return ExpressionAnalyzer::equal(lhs, rhs); + } + + return ExpressionAnalyzer::shallowEqual(lhs, rhs); +} + +void HashStringifyWalker::addUniqueSymbol() { + // Use a negative value to distinguish symbols for separators from symbols + // for Expressions + hashString.push_back((uint64_t)-nextVal); + nextVal++; +} + +void HashStringifyWalker::visitExpression(Expression* curr) { + auto [it, inserted] = exprToCounter.insert({curr, nextVal}); + hashString.push_back(it->second); + if (inserted) { + nextVal++; + } +} + +} // namespace wasm |