Skip to content

Commit db14d04

Browse files
authored
wasm2js2 import fixes (#2031)
* Don't assume function types exist in legalize-js-interface. * Properly handle (ignore) imports in RemoveNonJSOps - do not try to recurse into them. * Run legalize-js-interface and remove-unused-module-elements in wasm2js, the first is necessary, the last is nice to have.
1 parent a4baf21 commit db14d04

65 files changed

Lines changed: 11277 additions & 1027 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

scripts/test/env.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,12 @@
1+
var tempRet0 = 0;
2+
3+
export function setTempRet0(x) {
4+
tempRet0 = x;
5+
}
6+
7+
export function getTempRet0() {
8+
return tempRet0;
9+
}
10+
111
export const __tempMemory__ = 0;
12+

src/passes/LegalizeJSInterface.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,7 @@ struct LegalizeJSInterface : public Pass {
7171
}
7272
// for each illegal import, we must call a legalized stub instead
7373
for (auto* im : originalFunctions) {
74-
if (im->imported() && isIllegal(module->getFunctionType(im->type))
75-
&& shouldBeLegalized(im)) {
74+
if (im->imported() && isIllegal(im) && shouldBeLegalized(im)) {
7675
auto funcName = makeLegalStubForCalledImport(im, module);
7776
illegalImportsToLegal[im->name] = funcName;
7877
// we need to use the legalized version in the table, as the import from JS
@@ -209,7 +208,7 @@ struct LegalizeJSInterface : public Pass {
209208
auto* call = module->allocator.alloc<Call>();
210209
call->target = legal->name;
211210

212-
auto* imFunctionType = module->getFunctionType(im->type);
211+
auto* imFunctionType = ensureFunctionType(getSig(im), module);
213212

214213
for (auto param : imFunctionType->params) {
215214
if (param == i64) {

src/passes/RemoveNonJSOps.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,10 @@ struct RemoveNonJSOpsPass : public WalkerPass<PostWalker<RemoveNonJSOpsPass>> {
128128
auto function = m.getFunction(name);
129129
FindAll<Call> calls(function->body);
130130
for (auto* call : calls.list) {
131-
this->addNeededFunctions(m, call->target, needed);
131+
auto* called = m.getFunction(call->target);
132+
if (!called->imported()) {
133+
this->addNeededFunctions(m, call->target, needed);
134+
}
132135
}
133136
}
134137

src/wasm2js.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,7 @@ class Wasm2JSBuilder {
290290
Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
291291
PassRunner runner(wasm);
292292
runner.add<AutoDrop>();
293+
runner.add("legalize-js-interface");
293294
// First up remove as many non-JS operations we can, including things like
294295
// 64-bit integer multiplication/division, `f32.nearest` instructions, etc.
295296
// This may inject intrinsics which use i64 so it needs to be run before the
@@ -303,6 +304,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
303304
runner.add("simplify-locals-notee-nostructure");
304305
runner.add("reorder-locals");
305306
runner.add("vacuum");
307+
runner.add("remove-unused-module-elements");
306308
runner.setDebug(flags.debug);
307309
runner.run();
308310

@@ -311,7 +313,7 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) {
311313
#ifndef NDEBUG
312314
if (!WasmValidator().validate(*wasm)) {
313315
WasmPrinter::printModule(wasm);
314-
Fatal() << "error in validating input";
316+
Fatal() << "error in validating wasm2js output";
315317
}
316318
#endif
317319

test/binaryen.js/emit_asmjs.js.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function asmFunc(global, env, buffer) {
2121
var abort = env.abort;
2222
var nan = global.NaN;
2323
var infinity = global.Infinity;
24-
var i64toi32_i32$HIGH_BITS = 0;
2524
function main($0) {
2625
$0 = $0 | 0;
2726
return $0 | 0;

test/wasm2js.asserts.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ function asmFunc0(global, env, buffer) {
5252
var abort = env.abort;
5353
var nan = global.NaN;
5454
var infinity = global.Infinity;
55-
var i64toi32_i32$HIGH_BITS = 0;
5655
function $0() {
5756

5857
}
@@ -69,16 +68,11 @@ function asmFunc0(global, env, buffer) {
6968
return (x | 0) / (y | 0) | 0 | 0;
7069
}
7170

72-
function __wasm_fetch_high_bits() {
73-
return i64toi32_i32$HIGH_BITS | 0;
74-
}
75-
7671
var FUNCTION_TABLE = [];
7772
return {
7873
empty: $0,
7974
add: $1,
80-
div_s: $2,
81-
__wasm_fetch_high_bits: __wasm_fetch_high_bits
75+
div_s: $2
8276
};
8377
}
8478

test/wasm2js.traps.js

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ function asmFunc0(global, env, buffer) {
5252
var abort = env.abort;
5353
var nan = global.NaN;
5454
var infinity = global.Infinity;
55-
var i64toi32_i32$HIGH_BITS = 0;
5655
function $0() {
5756

5857
}
@@ -69,16 +68,11 @@ function asmFunc0(global, env, buffer) {
6968
return (x | 0) / (y | 0) | 0 | 0;
7069
}
7170

72-
function __wasm_fetch_high_bits() {
73-
return i64toi32_i32$HIGH_BITS | 0;
74-
}
75-
7671
var FUNCTION_TABLE = [];
7772
return {
7873
empty: $0,
7974
add: $1,
80-
div_s: $2,
81-
__wasm_fetch_high_bits: __wasm_fetch_high_bits
75+
div_s: $2
8276
};
8377
}
8478

test/wasm2js/address.2asm.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ function asmFunc(global, env, buffer) {
2323
var nan = global.NaN;
2424
var infinity = global.Infinity;
2525
var print = env.print;
26-
var i64toi32_i32$HIGH_BITS = 0;
2726
function $0(i) {
2827
i = i | 0;
2928
var wasm2js_i32$0 = 0;

test/wasm2js/base64.2asm.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -21,29 +21,11 @@ function asmFunc(global, env, buffer) {
2121
var abort = env.abort;
2222
var nan = global.NaN;
2323
var infinity = global.Infinity;
24-
var i64toi32_i32$HIGH_BITS = 0;
2524
var FUNCTION_TABLE = [];
2625
return {
2726

2827
};
2928
}
3029

3130
const memasmFunc = new ArrayBuffer(65536);
32-
const assignasmFunc = (
33-
function(mem) {
34-
const _mem = new Uint8Array(mem);
35-
return function(offset, s) {
36-
if (typeof Buffer === 'undefined') {
37-
const bytes = atob(s);
38-
for (let i = 0; i < bytes.length; i++)
39-
_mem[offset + i] = bytes.charCodeAt(i);
40-
} else {
41-
const bytes = Buffer.from(s, 'base64');
42-
for (let i = 0; i < bytes.length; i++)
43-
_mem[offset + i] = bytes[i];
44-
}
45-
}
46-
}
47-
)(memasmFunc);
48-
assignasmFunc(2, "AP7/");
4931
const retasmFunc = asmFunc({Math,Int8Array,Uint8Array,Int16Array,Uint16Array,Int32Array,Uint32Array,Float32Array,Float64Array,NaN,Infinity}, {abort:function() { throw new Error('abort'); }},memasmFunc);

test/wasm2js/block.2asm.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ function asmFunc(global, env, buffer) {
2121
var abort = env.abort;
2222
var nan = global.NaN;
2323
var infinity = global.Infinity;
24-
var i64toi32_i32$HIGH_BITS = 0;
2524
function dummy() {
2625

2726
}

0 commit comments

Comments
 (0)