Skip to content

Commit bda29e4

Browse files
authored
Limit number of function parameters in MergeSimilarFunctions (#7341)
MergeSimilarFunctions can merge functions whose bodies differ only the constants they use. These different constants become parameters to the merged function. In extreme cases, the optimization could produce an arbitrary number of new parameters, exceeding implementation limits. Cap the total number of parameters to a merged function at 255. The Web limitation on the number of parameters is 1000, but other implementations, including those on the JVM, cannot handle more than 255. It would be possible to make this limit configurable in the future.
1 parent faaa985 commit bda29e4

File tree

3 files changed

+10131
-0
lines changed

3 files changed

+10131
-0
lines changed

src/passes/MergeSimilarFunctions.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
#include "pass.h"
8585
#include "support/hash.h"
8686
#include "support/utilities.h"
87+
#include "wasm-limits.h"
8788
#include "wasm.h"
8889
#include <algorithm>
8990
#include <cassert>
@@ -490,6 +491,15 @@ void EquivalentClass::merge(Module* module,
490491
// than the reduced size.
491492
bool EquivalentClass::hasMergeBenefit(Module* module,
492493
const std::vector<ParamInfo>& params) {
494+
if (params.size() + primaryFunction->getNumParams() >
495+
MaxSyntheticFunctionParams) {
496+
// It requires too many parameters to merge this equivalence class. In
497+
// principle, we could try splitting the class into smaller classes of
498+
// functions that share more constants with each other, but that could
499+
// be expensive. TODO: investigate splitting the class.
500+
return false;
501+
}
502+
493503
size_t funcCount = functions.size();
494504
Index exprSize = Measurer::measure(primaryFunction->body);
495505
size_t thunkCount = funcCount;

src/wasm-limits.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ enum WebLimitations : uint32_t {
3131
MaxFunctionParams = 1000
3232
};
3333

34+
// Web limits allow up to 1000 function parameters, but other implementations,
35+
// such as those on the JVM, only allow up to 255. Do not allow optimizations to
36+
// introduce functions with more than this many parameters.
37+
static constexpr int MaxSyntheticFunctionParams = 255;
38+
3439
} // namespace wasm
3540

3641
#endif // wasm_wasm_limits_h

0 commit comments

Comments
 (0)