summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-04-30 11:14:18 -0700
committerGitHub <noreply@github.com>2018-04-30 11:14:18 -0700
commit23f53674fc7aed90313cc70cbdde7dffa0ea6a5b (patch)
tree46b1fd904ba084d3c52f5774f0cd8972036cd362 /src
parentbe12baa0384c06e26effaff2ea45870c3030b589 (diff)
downloadbinaryen-23f53674fc7aed90313cc70cbdde7dffa0ea6a5b.tar.gz
binaryen-23f53674fc7aed90313cc70cbdde7dffa0ea6a5b.tar.bz2
binaryen-23f53674fc7aed90313cc70cbdde7dffa0ea6a5b.zip
add --converge option to wasm-opt (#1524)
The option keeps running the passes (that we were told to run) in cycles until we converge in terms of the binary size, that is, keep optimizing until we can't shrink any more. Also fix a --metrics bug this uncovered: we can't expect the Metrics object to still be around if running passes later in another PassRunner.
Diffstat (limited to 'src')
-rw-r--r--src/passes/Metrics.cpp42
-rw-r--r--src/tools/wasm-opt.cpp37
2 files changed, 51 insertions, 28 deletions
diff --git a/src/passes/Metrics.cpp b/src/passes/Metrics.cpp
index 5b5e1c8ab..8cbf96db5 100644
--- a/src/passes/Metrics.cpp
+++ b/src/passes/Metrics.cpp
@@ -22,19 +22,21 @@
#include <wasm-binary.h>
#include <ir/module-utils.h>
+using namespace std;
+
namespace wasm {
-using namespace std;
+typedef map<const char *, int> Counts;
+
+static Counts lastCounts;
// Prints metrics between optimization passes.
struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor<Metrics>>> {
bool byFunction;
- Metrics(bool byFunction) : byFunction(byFunction) {}
-
- static Metrics *lastMetricsPass;
+ Counts counts;
- map<const char *, int> counts;
+ Metrics(bool byFunction) : byFunction(byFunction) {}
void visitExpression(Expression* curr) {
auto name = getExpressionName(curr);
@@ -130,7 +132,7 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor<
printCounts(std::string("start: ") + module->start.str);
}
// can't compare detailed info between passes yet
- lastMetricsPass = nullptr;
+ lastCounts.clear();
} else {
// add function info
size_t vars = 0;
@@ -142,7 +144,7 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor<
// print
printCounts("total");
// compare to next time
- lastMetricsPass = this;
+ lastCounts = counts;
}
}
@@ -170,20 +172,18 @@ struct Metrics : public WalkerPass<PostWalker<Metrics, UnifiedExpressionVisitor<
if (value == 0) continue;
o << " " << left << setw(15) << key << ": " << setw(8)
<< value;
- if (lastMetricsPass) {
- if (lastMetricsPass->counts.count(key)) {
- int before = lastMetricsPass->counts[key];
- int after = value;
- if (after - before) {
- if (after > before) {
- Colors::red(o);
- } else {
- Colors::green(o);
- }
- o << right << setw(8);
- o << showpos << after - before << noshowpos;
- Colors::normal(o);
+ if (lastCounts.count(key)) {
+ int before = lastCounts[key];
+ int after = value;
+ if (after - before) {
+ if (after > before) {
+ Colors::red(o);
+ } else {
+ Colors::green(o);
}
+ o << right << setw(8);
+ o << showpos << after - before << noshowpos;
+ Colors::normal(o);
}
}
o << "\n";
@@ -199,6 +199,4 @@ Pass *createFunctionMetricsPass() {
return new Metrics(true);
}
-Metrics *Metrics::lastMetricsPass;
-
} // namespace wasm
diff --git a/src/tools/wasm-opt.cpp b/src/tools/wasm-opt.cpp
index a82dc4a2f..7d7d44b2b 100644
--- a/src/tools/wasm-opt.cpp
+++ b/src/tools/wasm-opt.cpp
@@ -64,6 +64,7 @@ int main(int argc, const char* argv[]) {
Name entry;
bool emitBinary = true;
bool debugInfo = false;
+ bool converge = false;
bool fuzzExec = false;
bool fuzzBinary = false;
std::string extraFuzzCommand;
@@ -86,6 +87,9 @@ int main(int argc, const char* argv[]) {
.add("--debuginfo", "-g", "Emit names section and debug info",
Options::Arguments::Zero,
[&](Options *o, const std::string& arguments) { debugInfo = true; })
+ .add("--converge", "-c", "Run passes to convergence, continuing while binary size decreases",
+ Options::Arguments::Zero,
+ [&](Options *o, const std::string& arguments) { converge = true; })
.add("--fuzz-exec", "-fe", "Execute functions before and after optimization, helping fuzzing find bugs",
Options::Arguments::Zero,
[&](Options *o, const std::string& arguments) { fuzzExec = true; })
@@ -211,13 +215,34 @@ int main(int argc, const char* argv[]) {
if (options.runningPasses()) {
if (options.debug) std::cerr << "running passes...\n";
- options.runPasses(*curr);
- if (options.passOptions.validate) {
- bool valid = WasmValidator().validate(*curr, features);
- if (!valid) {
- WasmPrinter::printModule(&*curr);
+ auto runPasses = [&]() {
+ options.runPasses(*curr);
+ if (options.passOptions.validate) {
+ bool valid = WasmValidator().validate(*curr, features);
+ if (!valid) {
+ WasmPrinter::printModule(&*curr);
+ }
+ assert(valid);
+ }
+ };
+ runPasses();
+ if (converge) {
+ // Keep on running passes to convergence, defined as binary
+ // size no longer decreasing.
+ auto getSize = [&]() {
+ BufferWithRandomAccess buffer;
+ WasmBinaryWriter writer(curr, buffer);
+ writer.write();
+ return buffer.size();
+ };
+ auto lastSize = getSize();
+ while (1) {
+ if (options.debug) std::cerr << "running iteration for convergence (" << lastSize << ")...\n";
+ runPasses();
+ auto currSize = getSize();
+ if (currSize >= lastSize) break;
+ lastSize = currSize;
}
- assert(valid);
}
}