From e8915d2a0f8ab592d5da2d572c971a65b753c87c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 28 Sep 2022 15:20:14 -0700 Subject: 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. --- src/passes/GlobalStructInference.cpp | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'src') 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> { @@ -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; } -- cgit v1.2.3