summaryrefslogtreecommitdiff
path: root/src/passes/Asyncify.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2023-11-28 15:32:01 -0800
committerGitHub <noreply@github.com>2023-11-28 23:32:01 +0000
commitdbcac17d645d8ace8ae2cb69d6ba36b22d59b7cf (patch)
treee1a9d96c28af13c8317f6a71846532dd54bec1cd /src/passes/Asyncify.cpp
parent2ea76680ae728a3f41d493a3fc27344696bb0dcf (diff)
downloadbinaryen-dbcac17d645d8ace8ae2cb69d6ba36b22d59b7cf.tar.gz
binaryen-dbcac17d645d8ace8ae2cb69d6ba36b22d59b7cf.tar.bz2
binaryen-dbcac17d645d8ace8ae2cb69d6ba36b22d59b7cf.zip
[NFC] Move InstrumentedPass logic out and use it in another place (#6132)
Asyncify gained a way to wrap a pass so that it only runs on a given set of functions, rather than on all functions, so the wrapper "filters" what the pass operates on. That was useful in Asyncify as we wanted to only do work on functions that Asyncify actually instrumented. There is another place in the code that needs such functionality, optimizeAfterInlining, which runs optimizations after we inline; again, we only want to optimize on the functions we know are relevant because they changed. To do that, move that logic out to a general place so it can be reused. This makes the code there a lot less hackish. While doing so make the logic only work on function-parallel passes. It never did anyhow, but now it asserts on that. (It can't run on a general pass because a general one does not provide an interface to affect which functions it operates on; a general pass is entirely opaque in that way.)
Diffstat (limited to 'src/passes/Asyncify.cpp')
-rw-r--r--src/passes/Asyncify.cpp64
1 files changed, 12 insertions, 52 deletions
diff --git a/src/passes/Asyncify.cpp b/src/passes/Asyncify.cpp
index b30af085e..838a290bb 100644
--- a/src/passes/Asyncify.cpp
+++ b/src/passes/Asyncify.cpp
@@ -315,6 +315,7 @@
#include "ir/names.h"
#include "ir/utils.h"
#include "pass.h"
+#include "passes/pass-utils.h"
#include "support/file.h"
#include "support/string.h"
#include "wasm-builder.h"
@@ -845,56 +846,6 @@ public:
}
};
-// Proxy that runs wrapped pass for instrumented functions only
-struct InstrumentedProxy : public Pass {
- std::unique_ptr<Pass> create() override {
- return std::make_unique<InstrumentedProxy>(analyzer, pass->create());
- }
-
- InstrumentedProxy(ModuleAnalyzer* analyzer, std::unique_ptr<Pass> pass)
- : analyzer(analyzer), pass(std::move(pass)) {}
-
- bool isFunctionParallel() override { return pass->isFunctionParallel(); }
-
- void runOnFunction(Module* module, Function* func) override {
- if (!analyzer->needsInstrumentation(func)) {
- return;
- }
- if (pass->getPassRunner() == nullptr) {
- pass->setPassRunner(getPassRunner());
- }
- pass->runOnFunction(module, func);
- }
-
- bool modifiesBinaryenIR() override { return pass->modifiesBinaryenIR(); }
-
- bool invalidatesDWARF() override { return pass->invalidatesDWARF(); }
-
- bool addsEffects() override { return pass->addsEffects(); }
-
- bool requiresNonNullableLocalFixups() override {
- return pass->requiresNonNullableLocalFixups();
- }
-
-private:
- ModuleAnalyzer* analyzer;
- std::unique_ptr<Pass> pass;
-};
-
-struct InstrumentedPassRunner : public PassRunner {
- InstrumentedPassRunner(Module* wasm, ModuleAnalyzer* analyzer)
- : PassRunner(wasm), analyzer(analyzer) {}
-
-protected:
- void doAdd(std::unique_ptr<Pass> pass) override {
- PassRunner::doAdd(
- std::unique_ptr<Pass>(new InstrumentedProxy(analyzer, std::move(pass))));
- }
-
-private:
- ModuleAnalyzer* analyzer;
-};
-
// Instrument control flow, around calls and adding skips for rewinding.
struct AsyncifyFlow : public Pass {
bool isFunctionParallel() override { return true; }
@@ -1706,12 +1657,21 @@ struct Asyncify : public Pass {
// Add necessary globals before we emit code to use them.
addGlobals(module, relocatable);
+ // Compute the set of functions we will instrument. All of the passes we run
+ // below only need to run there.
+ PassUtils::FuncSet instrumentedFuncs;
+ for (auto& func : module->functions) {
+ if (analyzer.needsInstrumentation(func.get())) {
+ instrumentedFuncs.insert(func.get());
+ }
+ }
+
// Instrument the flow of code, adding code instrumentation and
// skips for when rewinding. We do this on flat IR so that it is
// practical to add code around each call, without affecting
// anything else.
{
- InstrumentedPassRunner runner(module, &analyzer);
+ PassUtils::FilteredPassRunner runner(module, instrumentedFuncs);
runner.add("flatten");
// Dce is useful here, since AsyncifyFlow makes control flow conditional,
// which may make unreachable code look reachable. It also lets us ignore
@@ -1752,7 +1712,7 @@ struct Asyncify : public Pass {
// restore those locals). We also and optimize after as well to simplify
// the code as much as possible.
{
- InstrumentedPassRunner runner(module, &analyzer);
+ PassUtils::FilteredPassRunner runner(module, instrumentedFuncs);
if (optimize) {
runner.addDefaultFunctionOptimizationPasses();
}