Skip to content
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
8 changes: 8 additions & 0 deletions src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5754,6 +5754,14 @@ void BinaryenSetFlexibleInlineMaxSize(BinaryenIndex size) {
globalPassOptions.inlining.flexibleInlineMaxSize = size;
}

BinaryenIndex BinaryenGetMaxCombinedBinarySize(void) {
return globalPassOptions.inlining.maxCombinedBinarySize;
}

void BinaryenSetMaxCombinedBinarySize(BinaryenIndex size) {
globalPassOptions.inlining.maxCombinedBinarySize = size;
}

BinaryenIndex BinaryenGetOneCallerInlineMaxSize(void) {
return globalPassOptions.inlining.oneCallerInlineMaxSize;
}
Expand Down
8 changes: 8 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -3148,6 +3148,14 @@ BINARYEN_API BinaryenIndex BinaryenGetFlexibleInlineMaxSize(void);
// Applies to all modules, globally.
BINARYEN_API void BinaryenSetFlexibleInlineMaxSize(BinaryenIndex size);

// Gets the limit for the combined size of the code after inlining.
// Applies to all modules, globally.
BINARYEN_API BinaryenIndex BinaryenGetMaxCombinedBinarySize(void);

// Sets the limit for the combined size of the code after inlining.
// Applies to all modules, globally.
BINARYEN_API void BinaryenSetMaxCombinedBinarySize(BinaryenIndex size);

// Gets the function size which we inline when there is only one caller.
// Applies to all modules, globally.
BINARYEN_API BinaryenIndex BinaryenGetOneCallerInlineMaxSize(void);
Expand Down
11 changes: 11 additions & 0 deletions src/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ struct InliningOptions {
// This is checked after alwaysInlineMaxSize and oneCallerInlineMaxSize, but
// the order normally won't matter.
Index flexibleInlineMaxSize = 20;
// The limit for the combined size of the code after inlining.
// We have an absolute limit in order to avoid extremely-large sizes after
// inlining, as they may hit limits in VMs and/or slow down startup
// (measurements there indicate something like ~1 second to optimize a 100K
// function). See e.g.
// https://github.com/WebAssembly/binaryen/pull/3730#issuecomment-867939138
// https://github.com/emscripten-core/emscripten/issues/13899#issuecomment-825073344
// The limit is arbitrary, but based on the links above. It is a very high
// value that should appear very rarely in practice (for example, it does
// not occur on the Emscripten benchmark suite of real-world codebases).
Index maxCombinedBinarySize = 400 * 1024;
// Loops usually mean the function does heavy work, so the call overhead
// is not significant and we do not inline such functions by default.
bool allowFunctionsWithLoops = false;
Expand Down
14 changes: 3 additions & 11 deletions src/passes/Inlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,21 +1501,13 @@ struct Inlining : public Pass {
}

// Checks if the combined size of the code after inlining is under the
// absolute size limit. We have an absolute limit in order to avoid
// extremely-large sizes after inlining, as they may hit limits in VMs and/or
// slow down startup (measurements there indicate something like ~1 second to
// optimize a 100K function). See e.g.
// https://github.com/WebAssembly/binaryen/pull/3730#issuecomment-867939138
// https://github.com/emscripten-core/emscripten/issues/13899#issuecomment-825073344
// absolute size limit.
bool isUnderSizeLimit(Name target, Name source) {
// Estimate the combined binary size from the number of instructions.
auto combinedSize = infos[target].size + infos[source].size;
auto estimatedBinarySize = Measurer::BytesPerExpr * combinedSize;
// The limit is arbitrary, but based on the links above. It is a very high
// value that should appear very rarely in practice (for example, it does
// not occur on the Emscripten benchmark suite of real-world codebases).
const Index MaxCombinedBinarySize = 400 * 1024;
return estimatedBinarySize < MaxCombinedBinarySize;
auto& options = getPassRunner()->options;
return estimatedBinarySize < options.inlining.maxCombinedBinarySize;
}
};

Expand Down
11 changes: 11 additions & 0 deletions src/tools/optimization-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,17 @@ struct OptimizationOptions : public ToolOptions {
passOptions.inlining.oneCallerInlineMaxSize =
static_cast<Index>(atoi(argument.c_str()));
})
.add("--inline-max-combined-binary-size",
"-imcbs",
"Max size of combined functions after inlining. "
"Default: " +
std::to_string(InliningOptions().maxCombinedBinarySize),
OptimizationOptionsCategory,
Options::Arguments::One,
[this](Options* o, const std::string& argument) {
passOptions.inlining.maxCombinedBinarySize =
static_cast<Index>(atoi(argument.c_str()));
})
.add("--inline-functions-with-loops",
"-ifwl",
"Allow inlining functions with loops",
Expand Down
3 changes: 3 additions & 0 deletions test/lit/help/wasm-metadce.test
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,9 @@
;; CHECK-NEXT: caller (default -1, which means
;; CHECK-NEXT: all such functions are inlined)
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-max-combined-binary-size,-imcbs Max size of combined functions
;; CHECK-NEXT: after inlining. Default: 409600
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-functions-with-loops,-ifwl Allow inlining functions with
;; CHECK-NEXT: loops
;; CHECK-NEXT:
Expand Down
3 changes: 3 additions & 0 deletions test/lit/help/wasm-opt.test
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,9 @@
;; CHECK-NEXT: caller (default -1, which means
;; CHECK-NEXT: all such functions are inlined)
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-max-combined-binary-size,-imcbs Max size of combined functions
;; CHECK-NEXT: after inlining. Default: 409600
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-functions-with-loops,-ifwl Allow inlining functions with
;; CHECK-NEXT: loops
;; CHECK-NEXT:
Expand Down
3 changes: 3 additions & 0 deletions test/lit/help/wasm2js.test
Original file line number Diff line number Diff line change
Expand Up @@ -598,6 +598,9 @@
;; CHECK-NEXT: caller (default -1, which means
;; CHECK-NEXT: all such functions are inlined)
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-max-combined-binary-size,-imcbs Max size of combined functions
;; CHECK-NEXT: after inlining. Default: 409600
;; CHECK-NEXT:
;; CHECK-NEXT: --inline-functions-with-loops,-ifwl Allow inlining functions with
;; CHECK-NEXT: loops
;; CHECK-NEXT:
Expand Down
Loading
Loading