summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/tools/wasm-emscripten-finalize.cpp1
-rw-r--r--src/wasm/wasm-emscripten.cpp69
-rw-r--r--test/lld/em_asm.wat.mem.out9
-rw-r--r--test/lld/em_asm.wat.out9
-rw-r--r--test/lld/em_asm_O0.wat.out9
-rw-r--r--test/lld/em_asm_main_thread.wat.out9
-rw-r--r--test/lld/em_asm_shared.wat.out9
-rw-r--r--test/lld/em_asm_table.wat.out7
8 files changed, 36 insertions, 86 deletions
diff --git a/src/tools/wasm-emscripten-finalize.cpp b/src/tools/wasm-emscripten-finalize.cpp
index 960c6870c..1d1ea2247 100644
--- a/src/tools/wasm-emscripten-finalize.cpp
+++ b/src/tools/wasm-emscripten-finalize.cpp
@@ -56,6 +56,7 @@ int main(int argc, const char* argv[]) {
bool checkStackOverflow = false;
uint64_t globalBase = INVALID_BASE;
bool standaloneWasm = false;
+ // TODO: remove after https://github.com/WebAssembly/binaryen/issues/3043
bool minimizeWasmChanges = false;
ToolOptions options("wasm-emscripten-finalize",
diff --git a/src/wasm/wasm-emscripten.cpp b/src/wasm/wasm-emscripten.cpp
index 4a1a532dd..fd0560fd9 100644
--- a/src/wasm/wasm-emscripten.cpp
+++ b/src/wasm/wasm-emscripten.cpp
@@ -343,14 +343,11 @@ struct AsmConstWalker : public LinearExecutionWalker<AsmConstWalker> {
void visitLocalSet(LocalSet* curr);
void visitCall(Call* curr);
- void visitTable(Table* curr);
void process();
private:
- Signature fixupName(Name& name, Signature baseSig, Proxying proxy);
- AsmConst&
- createAsmConst(uint32_t id, std::string code, Signature sig, Name name);
+ void createAsmConst(uint32_t id, std::string code, Signature sig, Name name);
Signature asmConstSig(Signature baseSig);
Name nameForImportWithSig(Signature sig, Proxying proxy);
void queueImport(Name importName, Signature baseSig);
@@ -427,10 +424,7 @@ void AsmConstWalker::visitCall(Call* curr) {
auto* value = arg->cast<Const>();
int32_t address = value->value.geti32();
auto code = codeForConstAddr(wasm, segmentOffsets, address);
- auto& asmConst = createAsmConst(address, code, sig, importName);
- if (!minimizeWasmChanges) {
- fixupName(curr->target, baseSig, asmConst.proxy);
- }
+ createAsmConst(address, code, sig, importName);
}
Proxying AsmConstWalker::proxyType(Name name) {
@@ -442,21 +436,6 @@ Proxying AsmConstWalker::proxyType(Name name) {
return Proxying::None;
}
-void AsmConstWalker::visitTable(Table* curr) {
- if (minimizeWasmChanges) {
- return;
- }
- for (auto& segment : curr->segments) {
- for (auto& name : segment.data) {
- auto* func = wasm.getFunction(name);
- if (func->imported() && func->base.hasSubstring(EM_ASM_PREFIX)) {
- auto proxy = proxyType(func->base);
- fixupName(name, func->sig, proxy);
- }
- }
- }
-}
-
void AsmConstWalker::process() {
// Find and queue necessary imports
walkModule(&wasm);
@@ -465,31 +444,16 @@ void AsmConstWalker::process() {
addImports();
}
-Signature
-AsmConstWalker::fixupName(Name& name, Signature baseSig, Proxying proxy) {
- auto sig = asmConstSig(baseSig);
- auto importName = nameForImportWithSig(sig, proxy);
- name = importName;
-
- auto pair = std::make_pair(sig, proxy);
- if (allSigs.count(pair) == 0) {
- allSigs.insert(pair);
- queueImport(importName, baseSig);
- }
- return sig;
-}
-
-AsmConstWalker::AsmConst& AsmConstWalker::createAsmConst(uint32_t id,
- std::string code,
- Signature sig,
- Name name) {
+void AsmConstWalker::createAsmConst(uint32_t id,
+ std::string code,
+ Signature sig,
+ Name name) {
AsmConst asmConst;
asmConst.id = id;
asmConst.code = code;
asmConst.sigs.insert(sig);
asmConst.proxy = proxyType(name);
asmConsts.push_back(asmConst);
- return asmConsts.back();
}
Signature AsmConstWalker::asmConstSig(Signature baseSig) {
@@ -524,28 +488,8 @@ void AsmConstWalker::addImports() {
static AsmConstWalker fixEmAsmConstsAndReturnWalker(Module& wasm,
bool minimizeWasmChanges) {
- // Collect imports to remove
- // This would find our generated functions if we ran it later
- std::vector<Name> toRemove;
- if (!minimizeWasmChanges) {
- for (auto& import : wasm.functions) {
- if (import->imported() && import->base.hasSubstring(EM_ASM_PREFIX)) {
- toRemove.push_back(import->name);
- }
- }
- }
-
- // Walk the module, generate _sig versions of EM_ASM functions
AsmConstWalker walker(wasm, minimizeWasmChanges);
walker.process();
-
- if (!minimizeWasmChanges) {
- // Remove the base functions that we didn't generate
- for (auto importName : toRemove) {
- wasm.removeFunction(importName);
- }
- }
-
return walker;
}
@@ -823,7 +767,6 @@ std::string EmscriptenGlueGenerator::generateEmscriptenMetadata(
commaFirst = true;
ModuleUtils::iterImportedFunctions(wasm, [&](Function* import) {
if (emJsWalker.codeByName.count(import->base.str) == 0 &&
- (minimizeWasmChanges || !import->base.startsWith(EM_ASM_PREFIX.str)) &&
!import->base.startsWith("invoke_")) {
if (declares.insert(import->base.str).second) {
meta << nextElement() << '"' << import->base.str << '"';
diff --git a/test/lld/em_asm.wat.mem.out b/test/lld/em_asm.wat.mem.out
index c840be861..30a4ac1d4 100644
--- a/test/lld/em_asm.wat.mem.out
+++ b/test/lld/em_asm.wat.mem.out
@@ -4,7 +4,7 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
- (import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
+ (import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
(memory $0 2)
(table $0 1 1 funcref)
(global $global$0 (mut i32) (i32.const 66208))
@@ -28,7 +28,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 568)
(i32.const 601)
(i32.const 0)
@@ -40,7 +40,7 @@
)
(i32.store
(local.get $0)
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 602)
(i32.const 622)
(i32.add
@@ -50,7 +50,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 625)
(i32.const 656)
(local.get $0)
@@ -87,6 +87,7 @@
"__wasm_call_ctors"
],
"declares": [
+ "emscripten_asm_const_int"
],
"externs": [
],
diff --git a/test/lld/em_asm.wat.out b/test/lld/em_asm.wat.out
index 64cf00069..501583d36 100644
--- a/test/lld/em_asm.wat.out
+++ b/test/lld/em_asm.wat.out
@@ -4,7 +4,7 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
- (import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
+ (import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
(memory $0 2)
(data (i32.const 568) "{ Module.print(\"Hello world\"); }\00\00{ return $0 + $1; }\00ii\00{ Module.print(\"Got \" + $0); }\00i\00")
(table $0 1 1 funcref)
@@ -29,7 +29,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 568)
(i32.const 601)
(i32.const 0)
@@ -41,7 +41,7 @@
)
(i32.store
(local.get $0)
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 602)
(i32.const 622)
(i32.add
@@ -51,7 +51,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 625)
(i32.const 656)
(local.get $0)
@@ -88,6 +88,7 @@
"__wasm_call_ctors"
],
"declares": [
+ "emscripten_asm_const_int"
],
"externs": [
],
diff --git a/test/lld/em_asm_O0.wat.out b/test/lld/em_asm_O0.wat.out
index 447a06e61..03d750e71 100644
--- a/test/lld/em_asm_O0.wat.out
+++ b/test/lld/em_asm_O0.wat.out
@@ -3,7 +3,7 @@
(type $i32_=>_i32 (func (param i32) (result i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
- (import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
+ (import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
(memory $0 2)
(data (i32.const 568) "{ Module.print(\"Hello world\"); }\00{ return $0 + $1; }\00{ Module.print(\"Got \" + $0); }\00")
(table $0 1 1 funcref)
@@ -33,7 +33,7 @@
(i32.const 0)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 568)
(i32.add
(local.get $2)
@@ -55,7 +55,7 @@
(i64.const 128849018900)
)
(local.set $3
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 601)
(i32.add
(local.get $2)
@@ -76,7 +76,7 @@
(i32.const 42)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.const 621)
(i32.add
(local.get $2)
@@ -113,6 +113,7 @@
"__wasm_call_ctors"
],
"declares": [
+ "emscripten_asm_const_int"
],
"externs": [
],
diff --git a/test/lld/em_asm_main_thread.wat.out b/test/lld/em_asm_main_thread.wat.out
index e6acc68d4..8e53466dd 100644
--- a/test/lld/em_asm_main_thread.wat.out
+++ b/test/lld/em_asm_main_thread.wat.out
@@ -6,7 +6,7 @@
(type $i32_i32_i32_=>_none (func (param i32 i32 i32)))
(type $i32_i32_=>_i32 (func (param i32 i32) (result i32)))
(type $i32_i32_i32_=>_i32 (func (param i32 i32 i32) (result i32)))
- (import "env" "emscripten_asm_const_sync_on_main_thread_iii" (func $emscripten_asm_const_sync_on_main_thread_iii (param i32 i32 i32) (result i32)))
+ (import "env" "emscripten_asm_const_int_sync_on_main_thread" (func $emscripten_asm_const_int_sync_on_main_thread (param i32 i32 i32) (result i32)))
(memory $0 2)
(data (i32.const 568) "{ Module.print(\"Hello world\"); }\00{ return $0 + $1; }\00{ Module.print(\"Got \" + $0); }\00")
(table $0 1 1 funcref)
@@ -38,7 +38,7 @@
(call $__em_asm_sig_builder::inner<>\20const\20__em_asm_sig_builder::__em_asm_sig<>\28\29)
)
(drop
- (call $emscripten_asm_const_sync_on_main_thread_iii
+ (call $emscripten_asm_const_int_sync_on_main_thread
(i32.const 568)
(i32.add
(local.get $0)
@@ -65,7 +65,7 @@
(i32.const 24)
)
(local.tee $1
- (call $emscripten_asm_const_sync_on_main_thread_iii
+ (call $emscripten_asm_const_int_sync_on_main_thread
(i32.const 601)
(i32.add
(local.get $0)
@@ -83,7 +83,7 @@
(local.get $1)
)
(drop
- (call $emscripten_asm_const_sync_on_main_thread_iii
+ (call $emscripten_asm_const_int_sync_on_main_thread
(i32.const 621)
(i32.add
(local.get $0)
@@ -210,6 +210,7 @@
"__wasm_call_ctors"
],
"declares": [
+ "emscripten_asm_const_int_sync_on_main_thread"
],
"externs": [
],
diff --git a/test/lld/em_asm_shared.wat.out b/test/lld/em_asm_shared.wat.out
index bdedaccc1..70ad00444 100644
--- a/test/lld/em_asm_shared.wat.out
+++ b/test/lld/em_asm_shared.wat.out
@@ -9,12 +9,12 @@
(import "env" "table" (table $0 0 funcref))
(import "env" "__memory_base" (global $gimport$3 i32))
(import "env" "__table_base" (global $gimport$4 i32))
+ (import "env" "emscripten_asm_const_int" (func $emscripten_asm_const_int (param i32 i32 i32) (result i32)))
(import "env" "stackSave" (func $stackSave (result i32)))
(import "env" "stackRestore" (func $stackRestore (param i32)))
(import "env" "g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJEEE6bufferE" (func $g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJEEE6bufferE (result i32)))
(import "env" "g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJiiEEE6bufferE" (func $g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJiiEEE6bufferE (result i32)))
(import "env" "g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJiEEE6bufferE" (func $g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJiEEE6bufferE (result i32)))
- (import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
(global $gimport$6 (mut i32) (i32.const 0))
(global $gimport$7 (mut i32) (i32.const 0))
(global $gimport$8 (mut i32) (i32.const 0))
@@ -45,7 +45,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.add
(local.tee $1
(global.get $gimport$3)
@@ -62,7 +62,7 @@
)
(i32.store
(local.get $0)
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.add
(local.get $1)
(i32.const 34)
@@ -75,7 +75,7 @@
)
)
(drop
- (call $emscripten_asm_const_iii
+ (call $emscripten_asm_const_int
(i32.add
(local.get $1)
(i32.const 57)
@@ -122,6 +122,7 @@
"staticBump": 0,
"tableSize": 0,
"declares": [
+ "emscripten_asm_const_int",
"stackSave",
"stackRestore",
"g$_ZN20__em_asm_sig_builderI19__em_asm_type_tupleIJEEE6bufferE",
diff --git a/test/lld/em_asm_table.wat.out b/test/lld/em_asm_table.wat.out
index 401cb86d4..59779b87b 100644
--- a/test/lld/em_asm_table.wat.out
+++ b/test/lld/em_asm_table.wat.out
@@ -6,9 +6,9 @@
(type $i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory $0 8192))
(import "env" "emscripten_log" (func $fimport$0 (param i32 i32)))
- (import "env" "emscripten_asm_const_iii" (func $emscripten_asm_const_iii (param i32 i32 i32) (result i32)))
+ (import "env" "emscripten_asm_const_int" (func $fimport$1 (param i32 i32 i32) (result i32)))
(table $0 159609 funcref)
- (elem (i32.const 1) $fimport$0 $emscripten_asm_const_iii)
+ (elem (i32.const 1) $fimport$0 $fimport$1)
(global $global$0 (mut i32) (i32.const 1024))
(global $global$1 i32 (i32.const 1048))
(export "__data_end" (global $global$1))
@@ -42,7 +42,8 @@
"staticBump": 480,
"tableSize": 159609,
"declares": [
- "emscripten_log"
+ "emscripten_log",
+ "emscripten_asm_const_int"
],
"externs": [
],