Skip to content

Commit 3ac5416

Browse files
authored
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).
1 parent 86507ab commit 3ac5416

3 files changed

Lines changed: 42 additions & 2 deletions

File tree

src/passes/PostEmscripten.cpp

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,21 @@
2020
//
2121

2222
#include <asmjs/shared-constants.h>
23+
#include <ir/import-utils.h>
2324
#include <ir/localize.h>
2425
#include <pass.h>
26+
#include <shared-constants.h>
2527
#include <wasm-builder.h>
2628
#include <wasm.h>
2729

2830
namespace wasm {
2931

30-
struct PostEmscripten : public WalkerPass<PostWalker<PostEmscripten>> {
32+
namespace {
33+
34+
struct OptimizeCalls : public WalkerPass<PostWalker<OptimizeCalls>> {
3135
bool isFunctionParallel() override { return true; }
3236

33-
Pass* create() override { return new PostEmscripten; }
37+
Pass* create() override { return new OptimizeCalls; }
3438

3539
void visitCall(Call* curr) {
3640
// special asm.js imports can be optimized
@@ -60,6 +64,29 @@ struct PostEmscripten : public WalkerPass<PostWalker<PostEmscripten>> {
6064
}
6165
};
6266

67+
} // namespace
68+
69+
struct PostEmscripten : public Pass {
70+
void run(PassRunner* runner, Module* module) override {
71+
// Apply the sbrk ptr, if it was provided.
72+
auto sbrkPtrStr =
73+
runner->options.getArgumentOrDefault("emscripten-sbrk-ptr", "");
74+
if (sbrkPtrStr != "") {
75+
auto sbrkPtr = std::stoi(sbrkPtrStr);
76+
ImportInfo imports(*module);
77+
auto* func = imports.getImportedFunction(ENV, "emscripten_get_sbrk_ptr");
78+
if (func) {
79+
Builder builder(*module);
80+
func->body = builder.makeConst(Literal(int32_t(sbrkPtr)));
81+
func->module = func->base = Name();
82+
}
83+
}
84+
85+
// Optimize calls
86+
OptimizeCalls().run(runner, module);
87+
}
88+
};
89+
6390
Pass* createPostEmscriptenPass() { return new PostEmscripten(); }
6491

6592
} // namespace wasm
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
(module
2+
(type $FUNCSIG$i (func (result i32)))
3+
(func $internal (; 0 ;) (type $FUNCSIG$i) (result i32)
4+
(i32.const 4008)
5+
)
6+
)
7+
(module
8+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(module
2+
(import "env" "emscripten_get_sbrk_ptr" (func $internal(result i32)))
3+
)
4+
(module
5+
)

0 commit comments

Comments
 (0)