diff options
author | Alon Zakai <azakai@google.com> | 2019-07-19 16:01:15 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-19 16:01:15 -0700 |
commit | 88ef839433ac0cf58c2a29f369d0268a22b5ae0e (patch) | |
tree | 13488bb2eda5657d7cb440cacacab4369b7fda1e /src/passes/pass.cpp | |
parent | 7bc5f1891f77e53f5a4e6905e02e80c680c728c3 (diff) | |
download | binaryen-88ef839433ac0cf58c2a29f369d0268a22b5ae0e.tar.gz binaryen-88ef839433ac0cf58c2a29f369d0268a22b5ae0e.tar.bz2 binaryen-88ef839433ac0cf58c2a29f369d0268a22b5ae0e.zip |
Simpify PassRunner.add() and automatically parallelize parallel functions (#2242)
Main change here is in pass.h, everything else is changes to work with the new API.
The add("name") remains as before, while the weird variadic add(..) which constructed the pass now just gets a std::unique_ptr of a pass. This also makes the memory management internally fully automatic. And it makes it trivial to parallelize WalkerPass::run on parallel passes.
As a benefit, this allows removing a lot of code since in many cases there is no need to create a new pass runner, and running a pass can be just a single line.
Diffstat (limited to 'src/passes/pass.cpp')
-rw-r--r-- | src/passes/pass.cpp | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index 0b5c53296..5d8fb6d61 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -46,11 +46,12 @@ void PassRegistry::registerPass(const char* name, passInfos[name] = PassInfo(description, create); } -Pass* PassRegistry::createPass(std::string name) { +std::unique_ptr<Pass> PassRegistry::createPass(std::string name) { if (passInfos.find(name) == passInfos.end()) { return nullptr; } - auto ret = passInfos[name].create(); + std::unique_ptr<Pass> ret; + ret.reset(passInfos[name].create()); ret->name = name; return ret; } @@ -455,13 +456,13 @@ void PassRunner::run() { validationFlags = validationFlags | WasmValidator::Globally; } std::cerr << "[PassRunner] running passes..." << std::endl; - for (auto pass : passes) { + for (auto& pass : passes) { padding = std::max(padding, pass->name.size()); } if (passDebug >= 3) { dumpWast("before", wasm); } - for (auto* pass : passes) { + for (auto& pass : passes) { // ignoring the time, save a printout of the module before, in case this // pass breaks it, so we can print the before and after std::stringstream moduleBefore; @@ -477,9 +478,9 @@ void PassRunner::run() { if (pass->isFunctionParallel()) { // function-parallel passes should get a new instance per function ModuleUtils::iterDefinedFunctions( - *wasm, [&](Function* func) { runPassOnFunction(pass, func); }); + *wasm, [&](Function* func) { runPassOnFunction(pass.get(), func); }); } else { - runPass(pass); + runPass(pass.get()); } auto after = std::chrono::steady_clock::now(); std::chrono::duration<double> diff = after - before; @@ -554,12 +555,12 @@ void PassRunner::run() { } stack.clear(); }; - for (auto* pass : passes) { + for (auto& pass : passes) { if (pass->isFunctionParallel()) { - stack.push_back(pass); + stack.push_back(pass.get()); } else { flush(); - runPass(pass); + runPass(pass.get()); } } flush(); @@ -571,20 +572,14 @@ void PassRunner::runOnFunction(Function* func) { std::cerr << "[PassRunner] running passes on function " << func->name << std::endl; } - for (auto* pass : passes) { - runPassOnFunction(pass, func); + for (auto& pass : passes) { + runPassOnFunction(pass.get(), func); } } -PassRunner::~PassRunner() { - for (auto pass : passes) { - delete pass; - } -} - -void PassRunner::doAdd(Pass* pass) { - passes.push_back(pass); +void PassRunner::doAdd(std::unique_ptr<Pass> pass) { pass->prepareToRun(this, wasm); + passes.emplace_back(std::move(pass)); } // Checks that the state is valid before and after a |