diff options
author | Daniel Wirtz <dcode@dcode.io> | 2017-11-21 21:43:12 +0100 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2017-11-21 12:43:12 -0800 |
commit | eedcc291164a46474116e0e54ede3133214a7621 (patch) | |
tree | 6350e315b74464f48c4f02c6c77a1338b630e144 /src | |
parent | 07c54750eb626ea7434341e439f6cee75efbf4b5 (diff) | |
download | binaryen-eedcc291164a46474116e0e54ede3133214a7621.tar.gz binaryen-eedcc291164a46474116e0e54ede3133214a7621.tar.bz2 binaryen-eedcc291164a46474116e0e54ede3133214a7621.zip |
Running passes on a single function in binaryen-c/.js (#1295)
* Also other function utilities in C and JS APIs
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 60 | ||||
-rw-r--r-- | src/binaryen-c.h | 21 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 28 |
3 files changed, 107 insertions, 2 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 9b46f0aba..70c907666 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -912,6 +912,22 @@ BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* na return ret; } +BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module, const char* name) { + if (tracing) { + std::cout << " BinaryenGetFunction(the_module, \"" << name << "\");\n"; + } + + auto* wasm = (Module*)module; + return wasm->getFunction(name); +} +void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name) { + if (tracing) { + std::cout << " BinaryenRemoveFunction(the_module, \"" << name << "\");\n"; + } + + auto* wasm = (Module*)module; + wasm->removeFunction(name); +} BinaryenGlobalRef BinaryenAddGlobal(BinaryenModuleRef module, const char* name, BinaryenType type, int8_t mutable_, BinaryenExpressionRef init) { if (tracing) { @@ -1214,6 +1230,50 @@ void BinaryenModuleInterpret(BinaryenModuleRef module) { } // +// ========== Function Operations ========== +// + +BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func) { + if (tracing) { + std::cout << " BinaryenFunctionGetBody(functions[" << functions[func] << "]);\n"; + } + + return ((Function*)func)->body; +} + +void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module) { + if (tracing) { + std::cout << " BinaryenFunctionOptimize(functions[" << functions[func] << "], the_module);\n"; + } + + Module* wasm = (Module*)module; + PassRunner passRunner(wasm); + passRunner.addDefaultOptimizationPasses(); + passRunner.runFunction((Function*)func); +} + +void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) { + if (tracing) { + std::cout << " {\n"; + std::cout << " const char* passes[] = { "; + for (BinaryenIndex i = 0; i < numPasses; i++) { + if (i > 0) std::cout << ", "; + std::cout << "\"" << passes[i] << "\""; + } + std::cout << " };\n"; + std::cout << " BinaryenFunctionRunPasses(functions[" << functions[func] << ", the_module, passes, " << numPasses << ");\n"; + std::cout << " }\n"; + } + + Module* wasm = (Module*)module; + PassRunner passRunner(wasm); + for (BinaryenIndex i = 0; i < numPasses; i++) { + passRunner.add(passes[i]); + } + passRunner.runFunction((Function*)func); +} + +// // ========== CFG / Relooper ========== // diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 54cc73ace..da5362d24 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -400,6 +400,12 @@ typedef void* BinaryenFunctionRef; // at indexes 1 and 2, etc., that is, they share an index space. BinaryenFunctionRef BinaryenAddFunction(BinaryenModuleRef module, const char* name, BinaryenFunctionTypeRef type, BinaryenType* varTypes, BinaryenIndex numVarTypes, BinaryenExpressionRef body); +// Gets a function reference by name. +BinaryenFunctionRef BinaryenGetFunction(BinaryenModuleRef module, const char* name); + +// Removes a function by name. +void BinaryenRemoveFunction(BinaryenModuleRef module, const char* name); + // Imports typedef void* BinaryenImportRef; @@ -451,7 +457,7 @@ void BinaryenModulePrintAsmjs(BinaryenModuleRef module); // @return 0 if an error occurred, 1 if validated succesfully int BinaryenModuleValidate(BinaryenModuleRef module); -// Run the standard optimization passes on the module. +// Runs the standard optimization passes on the module. void BinaryenModuleOptimize(BinaryenModuleRef module); // Runs the specified passes on the module. @@ -475,6 +481,19 @@ BinaryenModuleRef BinaryenModuleRead(char* input, size_t inputSize); void BinaryenModuleInterpret(BinaryenModuleRef module); // +// ========== Function Operations ========== +// + +// Gets the body of the function. +BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func); + +// Runs the standard optimization passes on the function. +void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module); + +// Runs the specified passes on the function. +void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses); + +// // ========== CFG / Relooper ========== // // General usage is (1) create a relooper, (2) create blocks, (3) add diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index e6d248715..c6313f2d7 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -1019,6 +1019,16 @@ return Module['_BinaryenAddFunction'](module, strToStack(name), functionType, i32sToStack(varTypes), varTypes.length, body); }); }; + this['getFunction'] = function(name) { + return preserveStack(function() { + return Module['_BinaryenGetFunction'](module, strToStack(name)); + }); + }; + this['removeFunction'] = function(name) { + return preserveStack(function() { + return Module['_BinaryenRemoveFunction'](module, strToStack(name)); + }); + }; this['addGlobal'] = function(name, type, mutable, init) { return preserveStack(function() { return Module['_BinaryenAddGlobal'](module, strToStack(name), type, mutable, init); @@ -1098,13 +1108,25 @@ this['optimize'] = function() { return Module['_BinaryenModuleOptimize'](module); }; + this['optimizeFunction'] = function(func) { + if (typeof func === "string") func = this['getFunction'](func); + return Module['_BinaryenFunctionOptimize'](func, module); + }; this['runPasses'] = function(passes) { return preserveStack(function() { return Module['_BinaryenModuleRunPasses'](module, i32sToStack( passes.map(strToStack) ), passes.length); }); - } + }; + this['runPassesOnFunction'] = function(func, passes) { + if (typeof func === "string") func = this['getFunction'](func); + return preserveStack(function() { + return Module['_BinaryenFunctionRunPasses'](func, module, i32sToStack( + passes.map(strToStack) + ), passes.length); + }); + }; this['autoDrop'] = function() { return Module['_BinaryenModuleAutoDrop'](module); }; @@ -1172,6 +1194,10 @@ return Module['_BinaryenConstGetValueF64'](expr); }; + Module['getFunctionBody'] = function(func) { + return Module['_BinaryenFunctionGetBody'](func); + }; + // emit text of an expression or a module Module['emitText'] = function(expr) { if (typeof expr === 'object') { |