From 3ac5416a6b070096fcd26c94fcdb7c0c144c72b4 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Wed, 4 Sep 2019 10:26:45 -0700 Subject: 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). --- src/passes/PostEmscripten.cpp | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) (limited to 'src') 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 +#include #include #include +#include #include #include namespace wasm { -struct PostEmscripten : public WalkerPass> { +namespace { + +struct OptimizeCalls : public WalkerPass> { 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> { } }; +} // 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 -- cgit v1.2.3