diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/binaryen-c.cpp | 68 | ||||
-rw-r--r-- | src/binaryen-c.h | 39 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 53 | ||||
-rw-r--r-- | src/js/binaryen.js-pre.js | 2 | ||||
-rw-r--r-- | src/support/defaults.h | 24 | ||||
-rw-r--r-- | src/tools/optimization-options.h | 7 |
6 files changed, 182 insertions, 11 deletions
diff --git a/src/binaryen-c.cpp b/src/binaryen-c.cpp index 0c687c249..b13c0a11e 100644 --- a/src/binaryen-c.cpp +++ b/src/binaryen-c.cpp @@ -33,6 +33,7 @@ #include "cfg/Relooper.h" #include "ir/utils.h" #include "shell-interface.h" +#include "support/defaults.h" using namespace wasm; @@ -70,6 +71,11 @@ Literal fromBinaryenLiteral(BinaryenLiteral x) { static std::mutex BinaryenFunctionMutex; static std::mutex BinaryenFunctionTypeMutex; +// Optimization options +static int optimizeLevel = BINARYEN_DEFAULT_OPTIMIZE_LEVEL; +static int shrinkLevel = BINARYEN_DEFAULT_SHRINK_LEVEL; +static bool debugInfo = BINARYEN_DEFAULT_DEBUG_INFO; + // Tracing support static int tracing = 0; @@ -2007,10 +2013,61 @@ void BinaryenModuleOptimize(BinaryenModuleRef module) { Module* wasm = (Module*)module; PassRunner passRunner(wasm); + passRunner.options.optimizeLevel = optimizeLevel; + passRunner.options.shrinkLevel = shrinkLevel; + passRunner.options.debugInfo = debugInfo; passRunner.addDefaultOptimizationPasses(); passRunner.run(); } +int BinaryenGetOptimizeLevel() { + if (tracing) { + std::cout << " BinaryenGetOptimizeLevel();\n"; + } + + return optimizeLevel; +} + +void BinaryenSetOptimizeLevel(int level) { + if (tracing) { + std::cout << " BinaryenSetOptimizeLevel(" << level << ");\n"; + } + + optimizeLevel = level; +} + +int BinaryenGetShrinkLevel() { + if (tracing) { + std::cout << " BinaryenGetShrinkLevel();\n"; + } + + return shrinkLevel; +} + +void BinaryenSetShrinkLevel(int level) { + if (tracing) { + std::cout << " BinaryenSetShrinkLevel(" << level << ");\n"; + } + + shrinkLevel = level; +} + +int BinaryenGetDebugInfo() { + if (tracing) { + std::cout << " BinaryenGetDebugInfo();\n"; + } + + return debugInfo; +} + +void BinaryenSetDebugInfo(int on) { + if (tracing) { + std::cout << " BinaryenSetDebugInfo(" << on << ");\n"; + } + + debugInfo = bool(on); +} + void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses) { if (tracing) { std::cout << " {\n"; @@ -2026,6 +2083,9 @@ void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, Bina Module* wasm = (Module*)module; PassRunner passRunner(wasm); + passRunner.options.optimizeLevel = optimizeLevel; + passRunner.options.shrinkLevel = shrinkLevel; + passRunner.options.debugInfo = debugInfo; for (BinaryenIndex i = 0; i < numPasses; i++) { passRunner.add(passes[i]); } @@ -2039,6 +2099,7 @@ void BinaryenModuleAutoDrop(BinaryenModuleRef module) { Module* wasm = (Module*)module; PassRunner passRunner(wasm); + passRunner.options.debugInfo = debugInfo; passRunner.add<AutoDrop>(); passRunner.run(); } @@ -2051,6 +2112,7 @@ size_t BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t output Module* wasm = (Module*)module; BufferWithRandomAccess buffer(false); WasmBinaryWriter writer(wasm, buffer, false); + writer.setNamesSection(debugInfo); writer.write(); size_t bytes = std::min(buffer.size(), outputSize); std::copy_n(buffer.begin(), bytes, output); @@ -2192,6 +2254,9 @@ void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module Module* wasm = (Module*)module; PassRunner passRunner(wasm); + passRunner.options.optimizeLevel = optimizeLevel; + passRunner.options.shrinkLevel = shrinkLevel; + passRunner.options.debugInfo = debugInfo; passRunner.addDefaultOptimizationPasses(); passRunner.runOnFunction((Function*)func); } @@ -2210,6 +2275,9 @@ void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef modul Module* wasm = (Module*)module; PassRunner passRunner(wasm); + passRunner.options.optimizeLevel = optimizeLevel; + passRunner.options.shrinkLevel = shrinkLevel; + passRunner.options.debugInfo = debugInfo; for (BinaryenIndex i = 0; i < numPasses; i++) { passRunner.add(passes[i]); } diff --git a/src/binaryen-c.h b/src/binaryen-c.h index 78866d189..b17715a4b 100644 --- a/src/binaryen-c.h +++ b/src/binaryen-c.h @@ -48,6 +48,7 @@ #include <stdint.h> #include "compiler-support.h" +#include "support/defaults.h" #ifdef __cplusplus extern "C" { @@ -656,10 +657,36 @@ void BinaryenModulePrintAsmjs(BinaryenModuleRef module); // @return 0 if an error occurred, 1 if validated succesfully int BinaryenModuleValidate(BinaryenModuleRef module); -// Runs the standard optimization passes on the module. +// Runs the standard optimization passes on the module. Uses the currently set +// global optimize and shrink level. void BinaryenModuleOptimize(BinaryenModuleRef module); -// Runs the specified passes on the module. +// Gets the currently set optimize level. Applies to all modules, globally. +// 0, 1, 2 correspond to -O0, -O1, -O2 (default), etc. +int BinaryenGetOptimizeLevel(); + +// Sets the optimization level to use. Applies to all modules, globally. +// 0, 1, 2 correspond to -O0, -O1, -O2 (default), etc. +void BinaryenSetOptimizeLevel(int level); + +// Gets the currently set shrink level. Applies to all modules, globally. +// 0, 1, 2 correspond to -O0, -Os (default), -Oz. +int BinaryenGetShrinkLevel(); + +// Sets the shrink level to use. Applies to all modules, globally. +// 0, 1, 2 correspond to -O0, -Os (default), -Oz. +void BinaryenSetShrinkLevel(int level); + +// Gets whether generating debug information is currently enabled or not. +// Applies to all modules, globally. +int BinaryenGetDebugInfo(); + +// Enables or disables debug information in emitted binaries. +// Applies to all modules, globally. +void BinaryenSetDebugInfo(int on); + +// Runs the specified passes on the module. Uses the currently set global +// optimize and shrink level. void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses); // Auto-generate drop() operations where needed. This lets you generate code without @@ -667,7 +694,7 @@ void BinaryenModuleRunPasses(BinaryenModuleRef module, const char **passes, Bina // but simpler to use autodrop). void BinaryenModuleAutoDrop(BinaryenModuleRef module); -// Serialize a module into binary form. +// Serialize a module into binary form. Uses the currently set global debugInfo option. // @return how many bytes were written. This will be less than or equal to outputSize size_t BinaryenModuleWrite(BinaryenModuleRef module, char* output, size_t outputSize); @@ -713,10 +740,12 @@ BinaryenType BinaryenFunctionGetVar(BinaryenFunctionRef func, BinaryenIndex inde // Gets the body of the specified `Function`. BinaryenExpressionRef BinaryenFunctionGetBody(BinaryenFunctionRef func); -// Runs the standard optimization passes on the function. +// Runs the standard optimization passes on the function. Uses the currently set +// global optimize and shrink level. void BinaryenFunctionOptimize(BinaryenFunctionRef func, BinaryenModuleRef module); -// Runs the specified passes on the function. +// Runs the specified passes on the function. Uses the currently set global +// optimize and shrink level. void BinaryenFunctionRunPasses(BinaryenFunctionRef func, BinaryenModuleRef module, const char **passes, BinaryenIndex numPasses); // diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 76b038d8d..c4e0d62d8 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -1256,7 +1256,7 @@ Module['getExpressionInfo'] = function(expr) { 'type': type, 'condition': Module['_BinaryenIfGetCondition'](expr), 'ifTrue': Module['_BinaryenIfGetIfTrue'](expr), - 'ifFalse': Module['_BinaryenIfGetIfFalse'](expr) + 'ifFalse': Module['_BinaryenIfGetIfFalse'](expr) }; case Module['LoopId']: return { @@ -1460,7 +1460,7 @@ Module['getExpressionInfo'] = function(expr) { // Obtains information about a 'FunctionType' Module['getFunctionTypeInfo'] = function(func) { return { - 'name': Module['_BinaryenFunctionTypeGetName'](func), + 'name': Pointer_stringify(Module['_BinaryenFunctionTypeGetName'](func)), 'params': getAllNested(func, Module['_BinaryenFunctionTypeGetNumParams'], Module['_BinaryenFunctionTypeGetParam']), 'result': Module['_BinaryenFunctionTypeGetResult'](func) }; @@ -1469,8 +1469,8 @@ Module['getFunctionTypeInfo'] = function(func) { // Obtains information about a 'Function' Module['getFunctionInfo'] = function(func) { return { - 'name': Module['_BinaryenFunctionGetName'](func), - 'type': Module['_BinaryenFunctionGetType'](func), + 'name': Pointer_stringify(Module['_BinaryenFunctionGetName'](func)), + 'type': Pointer_stringify(Module['_BinaryenFunctionGetType'](func)), 'params': getAllNested(func, Module['_BinaryenFunctionGetNumParams'], Module['_BinaryenFunctionGetParam']), 'result': Module['_BinaryenFunctionGetResult'](func), 'vars': getAllNested(func, Module['_BinaryenFunctionGetNumVars'], Module['_BinaryenFunctionGetVar']), @@ -1529,8 +1529,53 @@ Module['parseText'] = function(text) { return new Module['Module'](ptr); }; +// Gets the currently set optimize level. 0, 1, 2 correspond to -O0, -O1, -O2, etc. +Module['getOptimizeLevel'] = function() { + return Module['_BinaryenGetOptimizeLevel'](); +}; + +// Sets the optimization level to use. 0, 1, 2 correspond to -O0, -O1, -O2, etc. +Module['setOptimizeLevel'] = function(level) { + return Module['_BinaryenSetOptimizeLevel'](level); +}; + +// Gets the currently set shrink level. 0, 1, 2 correspond to -O0, -Os, -Oz. +Module['getShrinkLevel'] = function() { + return Module['_BinaryenGetShrinkLevel'](); +}; + +// Sets the shrink level to use. 0, 1, 2 correspond to -O0, -Os, -Oz. +Module['setShrinkLevel'] = function(level) { + return Module['_BinaryenSetShrinkLevel'](level); +}; + +// Gets whether generating debug information is currently enabled or not. +Module['getDebugInfo'] = function() { + return Boolean(Module['_BinaryenGetDebugInfo']()); +}; + +// Enables or disables debug information in emitted binaries. +Module['setDebugInfo'] = function(on) { + return Module['_BinaryenSetDebugInfo'](on); +}; + // Enables or disables C-API tracing Module['setAPITracing'] = function(on) { return Module['_BinaryenSetAPITracing'](on); }; +// Instantiates a new unique instance of the API with its own memory etc. +Module['instantiate'] = instantiate; +return Module; + +} // end of instantiate + +// Module loader code borrowed from webpack +if (typeof exports === 'object' && typeof module === 'object') + module.exports = instantiate(); +else if (typeof define === 'function' && define['amd']) + define([], instantiate); +else if (typeof exports === 'object') + exports['Binaryen'] = instantiate(); +else + (typeof self !== "undefined" ? self : this)['Binaryen'] = instantiate(); diff --git a/src/js/binaryen.js-pre.js b/src/js/binaryen.js-pre.js new file mode 100644 index 000000000..da0e8e6c8 --- /dev/null +++ b/src/js/binaryen.js-pre.js @@ -0,0 +1,2 @@ +function instantiate(Module) { + Module = Module || {}; diff --git a/src/support/defaults.h b/src/support/defaults.h new file mode 100644 index 000000000..0b9ca3f6d --- /dev/null +++ b/src/support/defaults.h @@ -0,0 +1,24 @@ +/* +* Copyright 2016 WebAssembly Community Group participants +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ + +// Default optimization level used by tools and the C/JS APIs +#define BINARYEN_DEFAULT_OPTIMIZE_LEVEL 2 + +// Default shrink level used by tools and the C/JS APIs +#define BINARYEN_DEFAULT_SHRINK_LEVEL 1 + +// Default debug info setting used by tools and the C/JS APIs +#define BINARYEN_DEFAULT_DEBUG_INFO 0 diff --git a/src/tools/optimization-options.h b/src/tools/optimization-options.h index c2f7f44af..7ef3f9fa6 100644 --- a/src/tools/optimization-options.h +++ b/src/tools/optimization-options.h @@ -14,6 +14,9 @@ * limitations under the License. */ +#include "support/command-line.h" +#include "support/defaults.h" + // // Shared optimization options for commandline tools // @@ -31,8 +34,8 @@ struct OptimizationOptions : public Options { (*this).add("", "-O", "execute default optimization passes", Options::Arguments::Zero, [this](Options*, const std::string&) { - passOptions.optimizeLevel = 2; - passOptions.shrinkLevel = 1; + passOptions.optimizeLevel = BINARYEN_DEFAULT_OPTIMIZE_LEVEL; + passOptions.shrinkLevel = BINARYEN_DEFAULT_SHRINK_LEVEL; passes.push_back(DEFAULT_OPT_PASSES); }) .add("", "-O0", "execute no optimization passes", |