From 88ef839433ac0cf58c2a29f369d0268a22b5ae0e Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Fri, 19 Jul 2019 16:01:15 -0700 Subject: 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. --- src/passes/pass.cpp | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'src/passes/pass.cpp') 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 PassRegistry::createPass(std::string name) { if (passInfos.find(name) == passInfos.end()) { return nullptr; } - auto ret = passInfos[name].create(); + std::unique_ptr 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 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->prepareToRun(this, wasm); + passes.emplace_back(std::move(pass)); } // Checks that the state is valid before and after a -- cgit v1.2.3