Skip to content

Add an option for the PostEmscripten pass to set the sbrk ptr location (which is called DYNAMICTOP_PTR in emscripten)#2325

Merged
kripken merged 2 commits intomasterfrom
sbrk
Sep 4, 2019
Merged

Add an option for the PostEmscripten pass to set the sbrk ptr location (which is called DYNAMICTOP_PTR in emscripten)#2325
kripken merged 2 commits intomasterfrom
sbrk

Conversation

@kripken
Copy link
Copy Markdown
Member

@kripken kripken commented Sep 2, 2019

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).

…n (DYNAMICTOP_PTR in emscripten). By setting this at compile time we can reduce code size and avoid importing it at runtime.
@kripken kripken requested a review from tlively September 2, 2019 23:16
}

// Optimize calls
OptimizeCalls().run(runner, module);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels awkward, but I'm not sure if there's a better way to replace sbrk keeping the order before this

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. I think some of the fastcomp-specific parts can be removed when we remove fastcomp, and then this pass can get simpler again.

@kripken kripken merged commit 3ac5416 into master Sep 4, 2019
@kripken kripken deleted the sbrk branch September 4, 2019 17:26
kripken added a commit that referenced this pull request Sep 6, 2019
Currently emscripten links the wasm, then links the JS, then computes the final static allocations and in particular the location of the sbrk ptr (i.e. the location in memory of the brk location). Emscripten then imports that into the asm.js or wasm as env.DYNAMICTOP_PTR. However, this didn't work in the wasm backend where we didn't have support for importing globals from JS, so we implement sbrk in JS.

I am proposing that we change this model to allow us to write sbrk in C and compile it to wasm. To do so, that C code can import an extern C function, emscripten_get_sbrk_ptr(), which basically just returns that location. The PostEmscripten pass can even apply that value at compile time, so we avoid the function call, and end up in the optimal place, see #2325 and emscripten PRs will be opened once other stuff lands.

However, the SafeHeap pass must be updated to handle this, or our CI will break in the middle. This PR fixes that, basically making it check if env.DYNAMICTOP_PTR exists, or if not then looking for env.emscripten_get_sbrk_ptr, so that it can handle both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants