summaryrefslogtreecommitdiff
path: root/src/passes/NoExitRuntime.cpp
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-12-13 15:45:21 -0800
committerGitHub <noreply@github.com>2018-12-13 15:45:21 -0800
commit0fd96e6b07f0a0907737c0f44e55060e057c2bb9 (patch)
tree62c169433f1dcf67644df59c102bb31852208b1d /src/passes/NoExitRuntime.cpp
parent802dd8ff76d423f438c1d69dd5da6c47bb762c88 (diff)
downloadbinaryen-0fd96e6b07f0a0907737c0f44e55060e057c2bb9.tar.gz
binaryen-0fd96e6b07f0a0907737c0f44e55060e057c2bb9.tar.bz2
binaryen-0fd96e6b07f0a0907737c0f44e55060e057c2bb9.zip
No exit runtime pass (#1816)
When emscripten knows that the runtime will not be exited, it can tell codegen to not emit atexit() calls (since those callbacks will never be run). This saves both code size and startup time. In asm2wasm the JSBackend does it directly. For the wasm backend, this pass does the same on the output wasm.
Diffstat (limited to 'src/passes/NoExitRuntime.cpp')
-rw-r--r--src/passes/NoExitRuntime.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/passes/NoExitRuntime.cpp b/src/passes/NoExitRuntime.cpp
new file mode 100644
index 000000000..d76bb34ba
--- /dev/null
+++ b/src/passes/NoExitRuntime.cpp
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2016 WebAssembly Community Group participants
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+//
+// Assumes the program will never exit the runtime (as in the emscripten
+// NO_EXIT_RUNTIME option). That means that atexit()s do not need to be
+// run.
+//
+
+#include <pass.h>
+#include <wasm.h>
+#include <wasm-builder.h>
+#include <asmjs/shared-constants.h>
+
+using namespace std;
+
+namespace wasm {
+
+struct NoExitRuntime : public WalkerPass<PostWalker<NoExitRuntime>> {
+ bool isFunctionParallel() override { return true; }
+
+ Pass* create() override { return new NoExitRuntime; }
+
+ void visitCall(Call* curr) {
+ auto* import = getModule()->getFunctionOrNull(curr->target);
+ if (!import || !import->imported() || import->module != ENV) return;
+ // Remove all possible manifestations of atexit, across asm2wasm and llvm wasm backend.
+ for (auto* name : {
+ "___cxa_atexit",
+ "_atexit",
+ "__cxa_atexit",
+ "atexit",
+ }) {
+ if (strcmp(name, import->base.str) == 0) {
+ replaceCurrent(
+ Builder(*getModule()).replaceWithIdenticalType(curr)
+ );
+ }
+ }
+ }
+};
+
+Pass* createNoExitRuntimePass() {
+ return new NoExitRuntime();
+}
+
+} // namespace wasm