Skip to content

Commit 5027d8c

Browse files
authored
Rewrite minifyGlobals JS optimizer pass (#12329)
This fixes a bug that only became apparent when wasm2js started to emit more interesting JS code, which confused the pass. Historically the pass was just used on asm.js globals, which are trivial, and it was reused for wasm2js, but as wasm2js output changed we ran into the issue. Specifically, the old code made some assumptions about fastcomp output like that the first item shouldn't be minified, and that we can ignore some things and not others; those hacks are now all removed. The new design is cleaner and more general, but still not 100% fully general, see the corner case it doesn't handle in the comment. It may be good to handle that to prevent future bugs, but it would add significant complexity here. Adding just an assert might be better, but that would still need the complexity to reason about scopes. Unintentionally, this cleaner design can also do better at minifying, improving wasm2js hello world's total size by over 10% (!) This removes the old fastcomp-style handling in minifyGlobals, with the testcase for that. The new pass just focuses on wasm2js's output format. Once this lands we can re-enable minification in binaryen.js CI, which is disabled atm.
1 parent 270f795 commit 5027d8c

15 files changed

+224
-160
lines changed

tests/code_size/hello_webgl2_wasm.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"a.js": 4972,
55
"a.js.gz": 2375,
66
"a.wasm": 10892,
7-
"a.wasm.gz": 6929,
8-
"total": 16407,
9-
"total_gz": 9681
7+
"a.wasm.gz": 6924,
8+
"total": 16427,
9+
"total_gz": 9676
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 588,
33
"a.html.gz": 386,
4-
"a.js": 22040,
5-
"a.js.gz": 8435,
4+
"a.js": 21786,
5+
"a.js.gz": 8416,
66
"a.mem": 3171,
77
"a.mem.gz": 2715,
8-
"total": 25799,
9-
"total_gz": 11536
8+
"total": 25545,
9+
"total_gz": 11517
1010
}

tests/code_size/hello_webgl_wasm.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"a.js": 4457,
55
"a.js.gz": 2196,
66
"a.wasm": 10892,
7-
"a.wasm.gz": 6929,
8-
"total": 15892,
9-
"total_gz": 9502
7+
"a.wasm.gz": 6924,
8+
"total": 15912,
9+
"total_gz": 9497
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 588,
33
"a.html.gz": 386,
4-
"a.js": 21529,
5-
"a.js.gz": 8270,
4+
"a.js": 21275,
5+
"a.js.gz": 8254,
66
"a.mem": 3171,
77
"a.mem.gz": 2715,
8-
"total": 25288,
9-
"total_gz": 11371
8+
"total": 25034,
9+
"total_gz": 11355
1010
}

tests/code_size/hello_world_wasm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@
55
"a.js.gz": 281,
66
"a.wasm": 102,
77
"a.wasm.gz": 114,
8-
"total": 1116,
8+
"total": 1127,
99
"total_gz": 822
1010
}
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.html": 697,
33
"a.html.gz": 437,
4-
"a.js": 1687,
5-
"a.js.gz": 731,
4+
"a.js": 1478,
5+
"a.js.gz": 700,
66
"a.mem": 6,
77
"a.mem.gz": 32,
8-
"total": 2390,
9-
"total_gz": 1200
8+
"total": 2181,
9+
"total_gz": 1169
1010
}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"a.html": 13633,
33
"a.html.gz": 7310,
4-
"total": 13629,
4+
"total": 13633,
55
"total_gz": 7310
66
}
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
2-
"a.html": 19301,
3-
"a.html.gz": 8035,
2+
"a.html": 19300,
3+
"a.html.gz": 8033,
44
"total": 19301,
5-
"total_gz": 8035
5+
"total_gz": 8033
66
}

tests/optimizer/constructor-output.js

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
// EMSCRIPTEN_START_ASM
2-
function a(asmLibraryArg, wasmMemory, wasmTable) {
3-
var scratchBuffer = new ArrayBuffer(16);
4-
var b = new Int32Array(scratchBuffer);
5-
var c = new Float32Array(scratchBuffer);
6-
function d(index) {
7-
return b[index];
2+
function instantiate(o, p, q) {
3+
var a = new ArrayBuffer(16);
4+
var b = new Int32Array(a);
5+
var c = new Float32Array(a);
6+
function j(r) {
7+
return b[r];
88
}
9-
function e(index, value) {
10-
b[index] = value;
9+
function k(r, s) {
10+
b[r] = s;
1111
}
12-
function f(value) {
13-
c[2] = value;
12+
function l(s) {
13+
c[2] = s;
1414
}
15-
function g() {
15+
function m() {
1616
return c[2];
1717
}
18-
function h(global, env, buffer) {
19-
var i = new global.Int8Array(buffer);
20-
var j = env.emscripten_glVertexAttrib4fv;
21-
var k = 6191184;
18+
function n(t, u, v) {
19+
var d = new t.Int8Array(v);
20+
var e = u.emscripten_glVertexAttrib4fv;
21+
var f = 6191184;
2222

2323
// EMSCRIPTEN_START_FUNCS
24-
function n() {
25-
return l(10, 20) + m(30);
24+
function i() {
25+
return g(10, 20) + h(30);
2626
}
2727

2828

2929

30-
function l(a, b) {
31-
return m(a + b);
30+
function g(a, b) {
31+
return h(a + b);
3232
}
33-
function m(a) {
33+
function h(a) {
3434
return a + 1;
3535
}
3636

3737
// EMSCRIPTEN_END_FUNCS
3838

3939
return {
40-
"main": n
40+
"main": i
4141
};
4242
}
43-
return h({
43+
return n({
4444
"Int8Array": Int8Array,
4545
"Int16Array": Int16Array,
4646
"Int32Array": Int32Array,
@@ -52,7 +52,7 @@ function m(a) {
5252
"NaN": NaN,
5353
"Infinity": Infinity,
5454
"Math": Math
55-
}, asmLibraryArg, wasmMemory.buffer);
55+
}, o, p.buffer);
5656
}
5757

5858

tests/optimizer/test-js-optimizer-minifyGlobals-output.js

Lines changed: 0 additions & 10 deletions
This file was deleted.

tests/optimizer/test-js-optimizer-minifyGlobals.js

Lines changed: 0 additions & 12 deletions
This file was deleted.

0 commit comments

Comments
 (0)