Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 29 additions & 2 deletions src/passes/PostEmscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);

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.

}
};

Pass* createPostEmscriptenPass() { return new PostEmscripten(); }

} // namespace wasm
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
(module
(type $FUNCSIG$i (func (result i32)))
(func $internal (; 0 ;) (type $FUNCSIG$i) (result i32)
(i32.const 4008)
)
)
(module
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(module
(import "env" "emscripten_get_sbrk_ptr" (func $internal(result i32)))
)
(module
)