diff options
author | Alon Zakai <azakai@google.com> | 2022-09-28 15:20:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-28 15:20:14 -0700 |
commit | e8915d2a0f8ab592d5da2d572c971a65b753c87c (patch) | |
tree | f4447cb98d87b12297148e4899521566c413ac95 /src/passes/GlobalStructInference.cpp | |
parent | e70806cd724a732eb6e8f9d3a20e2493eabfb808 (diff) | |
download | binaryen-e8915d2a0f8ab592d5da2d572c971a65b753c87c.tar.gz binaryen-e8915d2a0f8ab592d5da2d572c971a65b753c87c.tar.bz2 binaryen-e8915d2a0f8ab592d5da2d572c971a65b753c87c.zip |
Fix nondeterminism in GlobalStructInference (#5092)
We append to vectors of globals in a nondeterministically-ordered loop, which can lead to
different orderings of the vectors. This happens quite frequently in very large J2Wasm
files it turns out. As a solution, simply sort them after the nondeterministic stage.
Diffstat (limited to 'src/passes/GlobalStructInference.cpp')
-rw-r--r-- | src/passes/GlobalStructInference.cpp | 9 |
1 files changed, 8 insertions, 1 deletions
diff --git a/src/passes/GlobalStructInference.cpp b/src/passes/GlobalStructInference.cpp index e2b3fa0c9..1cba976a6 100644 --- a/src/passes/GlobalStructInference.cpp +++ b/src/passes/GlobalStructInference.cpp @@ -173,6 +173,13 @@ struct GlobalStructInference : public Pass { return; } + // The above loop on typeGlobalsCopy is on an unsorted data structure, and + // that can lead to nondeterminism in typeGlobals. Sort the vectors there to + // ensure determinism. + for (auto& [type, globals] : typeGlobals) { + std::sort(globals.begin(), globals.end()); + } + // Optimize based on the above. struct FunctionOptimizer : public WalkerPass<PostWalker<FunctionOptimizer>> { @@ -219,7 +226,7 @@ struct GlobalStructInference : public Pass { // (i32.const 1337) // (i32.const 42) // (ref.eq (ref) $global2)) - auto& globals = iter->second; + const auto& globals = iter->second; if (globals.size() < 2) { return; } |