summaryrefslogtreecommitdiff
path: root/src/passes/PostEmscripten.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2019-09-04 10:26:45 -0700
committerGitHub <noreply@github.com>2019-09-04 10:26:45 -0700
commit3ac5416a6b070096fcd26c94fcdb7c0c144c72b4 (patch)
tree7d19c947643a42499136aa05b4110c11369b4fe0 /src/passes/PostEmscripten.cpp
parent86507ab974542a7ac7a9e9e127321622025e3795 (diff)
downloadbinaryen-3ac5416a6b070096fcd26c94fcdb7c0c144c72b4.tar.gz
binaryen-3ac5416a6b070096fcd26c94fcdb7c0c144c72b4.tar.bz2
binaryen-3ac5416a6b070096fcd26c94fcdb7c0c144c72b4.zip
Add an option for the PostEmscripten pass to set the sbrk ptr location (which is called DYNAMICTOP_PTR in emscripten) (#2325)
The assumption is that an import env.emscripten_get_sbrk_ptr exists, and we replace the value returned from there with a constant. (We can't do all this in wasm-emscripten-finalize, as it happens before the JS compiler runs, which can add more static allocations; we only know where the sbrk ptr is later in compilation.) This just replaces the import with a function returning the constant; inlining etc. can help more later. By setting this at compile time we can reduce code size and avoid importing it at runtime, which makes us more compatible with wasi (less special imports).
Diffstat (limited to 'src/passes/PostEmscripten.cpp')
-rw-r--r--src/passes/PostEmscripten.cpp31
1 files changed, 29 insertions, 2 deletions
diff --git a/src/passes/PostEmscripten.cpp b/src/passes/PostEmscripten.cpp
index 9d2b41094..62fd9d845 100644
--- a/src/passes/PostEmscripten.cpp
+++ b/src/passes/PostEmscripten.cpp
@@ -20,17 +20,21 @@
//
#include <asmjs/shared-constants.h>
+#include <ir/import-utils.h>
#include <ir/localize.h>
#include <pass.h>
+#include <shared-constants.h>
#include <wasm-builder.h>
#include <wasm.h>
namespace wasm {
-struct PostEmscripten : public WalkerPass<PostWalker<PostEmscripten>> {
+namespace {
+
+struct OptimizeCalls : public WalkerPass<PostWalker<OptimizeCalls>> {
bool isFunctionParallel() override { return true; }
- Pass* create() override { return new PostEmscripten; }
+ Pass* create() override { return new OptimizeCalls; }
void visitCall(Call* curr) {
// special asm.js imports can be optimized
@@ -60,6 +64,29 @@ struct PostEmscripten : public WalkerPass<PostWalker<PostEmscripten>> {
}
};
+} // namespace
+
+struct PostEmscripten : public Pass {
+ void run(PassRunner* runner, Module* module) override {
+ // Apply the sbrk ptr, if it was provided.
+ auto sbrkPtrStr =
+ runner->options.getArgumentOrDefault("emscripten-sbrk-ptr", "");
+ if (sbrkPtrStr != "") {
+ auto sbrkPtr = std::stoi(sbrkPtrStr);
+ ImportInfo imports(*module);
+ auto* func = imports.getImportedFunction(ENV, "emscripten_get_sbrk_ptr");
+ if (func) {
+ Builder builder(*module);
+ func->body = builder.makeConst(Literal(int32_t(sbrkPtr)));
+ func->module = func->base = Name();
+ }
+ }
+
+ // Optimize calls
+ OptimizeCalls().run(runner, module);
+ }
+};
+
Pass* createPostEmscriptenPass() { return new PostEmscripten(); }
} // namespace wasm