diff options
author | Alon Zakai <azakai@google.com> | 2021-03-09 11:53:05 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-09 11:53:05 -0800 |
commit | afc5fbd51759c07492fec4cae86d7e9484547626 (patch) | |
tree | d1fa421bc9c2887e112c4fbb31f4e415531118c9 /src | |
parent | ef52d84bf6204a8a0f9348a9cc89e618526b8aae (diff) | |
download | binaryen-afc5fbd51759c07492fec4cae86d7e9484547626.tar.gz binaryen-afc5fbd51759c07492fec4cae86d7e9484547626.tar.bz2 binaryen-afc5fbd51759c07492fec4cae86d7e9484547626.zip |
Reducer: Improve reduction of function bodies and the factor for text reduction (#3668)
The old code tried to call visitExpression from outside of a walk on the
wasm, which works except that replaceCurrent does nothing as there is
no current node. Perhaps it should assert if called outside of a walk? Might
be an expensive check, but once we have no-assert builds maybe that's
worthwhile.
Replace that with a working check during the walk. Also limit the
frequency of it (do it 1000x more often than a normal reduction, but not
all the time like we used to).
Also optimize the starting factor for text reduction. Text files are much
larger for the same amount of IR, so the initial factor was far too high and
inefficient.
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm-reduce.cpp | 27 |
1 files changed, 21 insertions, 6 deletions
diff --git a/src/tools/wasm-reduce.cpp b/src/tools/wasm-reduce.cpp index 5cd58301e..efe5c1caa 100644 --- a/src/tools/wasm-reduce.cpp +++ b/src/tools/wasm-reduce.cpp @@ -470,6 +470,25 @@ struct Reducer // since we don't need to duplicate work that they do void visitExpression(Expression* curr) { + if (getFunction() && curr == getFunction()->body) { + // At the top level, we can try to reduce anything to an unreachable, and + // it is useful to do so when possible. + if (!curr->is<Unreachable>() && !curr->is<Nop>() && + shouldTryToReduce(1000)) { + auto* save = curr; + Unreachable un; + replaceCurrent(&un); + if (writeAndTestReduction()) { + replaceCurrent(builder->makeUnreachable()); + std::cerr << "| body unreachified (" << getFunction()->name + << ")\n"; + noteReduction(); + return; + } else { + replaceCurrent(save); + } + } + } // type-based reductions if (curr->type == Type::none) { if (tryToReduceCurrentToNop()) { @@ -724,11 +743,6 @@ struct Reducer } void visitFunction(Function* curr) { - if (!curr->imported()) { - // extra chance to work on the function toplevel element, as if it can - // be reduced it's great - visitExpression(curr->body); - } // finish function funcsSeen++; static int last = 0; @@ -1272,7 +1286,8 @@ int main(int argc, const char* argv[]) { std::cerr << "|starting reduction!\n"; - int factor = workingSize * 2; + int factor = binary ? workingSize * 2 : workingSize / 10; + size_t lastDestructiveReductions = 0; size_t lastPostPassesSize = 0; |