Skip to content

Add flags indicating whether a function shall always be inlined #1330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
1,283 changes: 642 additions & 641 deletions bin/binaryen.js

Large diffs are not rendered by default.

645 changes: 323 additions & 322 deletions bin/wasm.js

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions build-js.sh
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export_function "_BinaryenInt64"
export_function "_BinaryenFloat32"
export_function "_BinaryenFloat64"
export_function "_BinaryenUndefined"
export_function "_BinaryenAlwaysInline"
export_function "_BinaryenInvalidId"
export_function "_BinaryenBlockId"
export_function "_BinaryenIfId"
Expand Down Expand Up @@ -389,6 +390,8 @@ export_function "_BinaryenModuleWrite"
export_function "_BinaryenModuleRead"
export_function "_BinaryenModuleInterpret"
export_function "_BinaryenFunctionGetBody"
export_function "_BinaryenFunctionGetFlags"
export_function "_BinaryenFunctionSetFlags"
export_function "_BinaryenFunctionOptimize"
export_function "_BinaryenFunctionRunPasses"
export_function "_RelooperCreate"
Expand Down
18 changes: 17 additions & 1 deletion src/binaryen-c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ BinaryenType BinaryenFloat32(void) { return f32; }
BinaryenType BinaryenFloat64(void) { return f64; }
BinaryenType BinaryenUndefined(void) { return uint32_t(-1); }

// Flags

BinaryenFlags BinaryenAlwaysInline(void) { return Function::Flags::AlwaysInline; }

// Expression ids

BinaryenExpressionId BinaryenInvalidId(void) { return Expression::Id::InvalidId; }
Expand Down Expand Up @@ -1350,7 +1354,20 @@ BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func) {

return ((Function*)func)->body;
}
BinaryenFlags BinaryenFunctionGetFlags(BinaryenFunctionRef func) {
if (tracing) {
std::cout << " BinaryenFunctionGetFlags(functions[" << functions[func] << "]);\n";
}

return ((Function*)func)->flags;
}
void BinaryenFunctionSetFlags(BinaryenFunctionRef func, BinaryenFlags flags) {
if (tracing) {
std::cout << " BinaryenFunctionSetFlags(functions[" << functions[func] << "], " << flags << ");\n";
}

((Function*)func)->flags = (Function::Flags)flags;
}
void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module) {
if (tracing) {
std::cout << " BinaryenFunctionOptimize(functions[" << functions[func] << "], the_module);\n";
Expand All @@ -1361,7 +1378,6 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module
passRunner.addDefaultOptimizationPasses();
passRunner.runFunction((Function*)func);
}

void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) {
if (tracing) {
std::cout << " {\n";
Expand Down
12 changes: 12 additions & 0 deletions src/binaryen-c.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@ BinaryenType BinaryenFloat64(void);
// the API figure out the type instead of providing one.
BinaryenType BinaryenUndefined(void);

// Flags type used for enum-like values.

typedef uint32_t BinaryenFlags;

BinaryenFlags BinaryenAlwaysInline(void);

// Expression ids (call to get the value of each; you can cache them)

typedef uint32_t BinaryenExpressionId;
Expand Down Expand Up @@ -506,6 +512,12 @@ void BinaryenModuleInterpret(BinaryenModuleRef module);
// Gets the body of the function.
BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func);

// Gets the flags set for the function.
BinaryenFlags BinaryenFunctionGetFlags(BinaryenFunctionRef func);

// Sets the function's flags, e.g., 'AlwaysInline'.
void BinaryenFunctionSetFlags(BinaryenFunctionRef func, BinaryenFlags flags);

// Runs the standard optimization passes on the function.
void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module);

Expand Down
10 changes: 10 additions & 0 deletions src/js/binaryen.js-post.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
Module['f64'] = Module['_BinaryenFloat64']();
Module['undefined'] = Module['_BinaryenUndefined']();

Module['AlwaysInline'] = Module['_BinaryenAlwaysInline']();

Module['InvalidId'] = Module['_BinaryenInvalidId']();
Module['BlockId'] = Module['_BinaryenBlockId']();
Module['IfId'] = Module['_BinaryenIfId']();
Expand Down Expand Up @@ -1231,6 +1233,14 @@
return Module['_BinaryenFunctionGetBody'](func);
};

Module['getFunctionFlags'] = function(func) {
return Module['_BinaryenFunctionGetFlags'](func);
};

Module['setFunctionFlags'] = function(func, flags) {
return Module['_BinaryenFunctionSetFlags'](func, flags);
};

// emit text of an expression or a module
Module['emitText'] = function(expr) {
if (typeof expr === 'object') {
Expand Down
5 changes: 5 additions & 0 deletions src/passes/Inlining.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,11 @@ struct FunctionInfo {
Index size;
bool lightweight = true;
bool usedGlobally = false; // in a table or export
bool alwaysInline = false;

bool worthInlining(PassOptions& options, bool allowMultipleInliningsPerFunction) {
// if it's explicitly set to be inlined, just do it
if (alwaysInline) return true;
// if it's big, it's just not worth doing (TODO: investigate more)
if (size > FLEXIBLE_SIZE_LIMIT) return false;
// if it has one use, then inlining it would likely reduce code size
Expand Down Expand Up @@ -94,6 +97,8 @@ struct FunctionInfoScanner : public WalkerPass<PostWalker<FunctionInfoScanner>>

void visitFunction(Function* curr) {
(*infos)[curr->name].size = Measurer::measure(curr->body);
if (curr->flags & Function::Flags::AlwaysInline)
(*infos)[curr->name].alwaysInline = true;
}

private:
Expand Down
7 changes: 6 additions & 1 deletion src/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,11 @@ class Function {
std::vector<WasmType> vars; // params plus vars
Name type; // if null, it is implicit in params and result
Expression* body;
enum Flags {
None = 0,
AlwaysInline = 1 << 0
};
Flags flags;

// local names. these are optional.
std::map<Index, Name> localNames;
Expand All @@ -608,7 +613,7 @@ class Function {
};
std::unordered_map<Expression*, DebugLocation> debugLocations;

Function() : result(none) {}
Function() : result(none), flags(None) {}

size_t getNumParams();
size_t getNumVars();
Expand Down
12 changes: 12 additions & 0 deletions test/binaryen.js/kitchen-sink.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ function test_types() {
console.log("BinaryenFloat64: " + Binaryen.f64);
}

function test_flags() {
console.log("BinaryenAlwaysInline: " + Binaryen.AlwaysInline);
}

function test_ids() {
console.log("BinaryenInvalidId: " + Binaryen.InvalidId);
console.log("BinaryenBlockId: " + Binaryen.BlockId);
Expand Down Expand Up @@ -449,6 +453,13 @@ function test_relooper() {
module.addFunction("return", i, localTypes, body);
}

{ // setting flags
var func = module.addFunction("set-flags", i, [], module.i32.const(0));
console.log("flags=" + Binaryen.getFunctionFlags(func));
Binaryen.setFunctionFlags(func, Binaryen.AlwaysInline);
console.log("flags=" + Binaryen.getFunctionFlags(func));
}

console.log("raw:");
console.log(module.emitText());

Expand Down Expand Up @@ -562,6 +573,7 @@ function test_parsing() {

function main() {
test_types();
test_flags();
test_ids();
test_core();
test_relooper();
Expand Down
19 changes: 19 additions & 0 deletions test/binaryen.js/kitchen-sink.js.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ BinaryenInt32: 1
BinaryenInt64: 2
BinaryenFloat32: 3
BinaryenFloat64: 4
BinaryenAlwaysInline: 1
BinaryenInvalidId: 0
BinaryenBlockId: 1
BinaryenIfId: 2
Expand Down Expand Up @@ -572,6 +573,8 @@ getExpressionType=3
)
)

flags=0
flags=1
raw:
(module
(type $v (func))
Expand Down Expand Up @@ -1058,6 +1061,9 @@ raw:
)
)
)
(func $set-flags (; 16 ;) (type $i) (result i32)
(i32.const 0)
)
)

optimized:
Expand Down Expand Up @@ -2453,6 +2459,16 @@ getExpressionType=3
BinaryenType varTypes[] = { 1 };
functions[14] = BinaryenAddFunction(the_module, "return", functionTypes[2], varTypes, 1, expressions[141]);
}
expressions[142] = BinaryenConst(the_module, BinaryenLiteralInt32(0));
{
BinaryenType varTypes[] = { 0 };
functions[15] = BinaryenAddFunction(the_module, "set-flags", functionTypes[2], varTypes, 0, expressions[142]);
}
BinaryenFunctionGetFlags(functions[15]);
flags=0
BinaryenFunctionSetFlags(functions[15], 1);
BinaryenFunctionGetFlags(functions[15]);
flags=1
raw:
BinaryenModulePrint(the_module);
(module
Expand Down Expand Up @@ -2940,6 +2956,9 @@ raw:
)
)
)
(func $set-flags (; 16 ;) (type $i) (result i32)
(i32.const 0)
)
)

BinaryenModuleValidate(the_module);
Expand Down