Skip to content

Commit

Permalink
Don't force the use of imported memory under WASM2JS
Browse files Browse the repository at this point in the history
Setting `IMPORTED_MEMORY` can have other implications and don't want t
by default just because we chose WASM2JS.

See #21071
  • Loading branch information
sbc100 committed Jan 28, 2024
1 parent 72c3be3 commit 91c0d75
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 111 deletions.
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.js": 7411,
"a.js.gz": 3140,
"a.js": 7387,
"a.js.gz": 3112,
"a.wasm": 11433,
"a.wasm.gz": 5725,
"total": 19517,
"total_gz": 9296
"total": 19493,
"total_gz": 9268
}
8 changes: 4 additions & 4 deletions test/code_size/hello_wasm_worker_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 737,
"a.html.gz": 433,
"a.js": 667,
"a.js.gz": 458,
"a.js": 665,
"a.js.gz": 455,
"a.wasm": 1852,
"a.wasm.gz": 1049,
"total": 3256,
"total_gz": 1940
"total": 3254,
"total_gz": 1937
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4681,
"a.js.gz": 2412,
"a.js": 4671,
"a.js.gz": 2396,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15638,
"total_gz": 9483
"total": 15628,
"total_gz": 9467
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17917,
"a.js.gz": 8089,
"a.js": 17766,
"a.js.gz": 7996,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21607,
"total_gz": 11161
"total": 21456,
"total_gz": 11068
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 569,
"a.html.gz": 379,
"a.js": 4167,
"a.js.gz": 2236,
"a.js": 4158,
"a.js.gz": 2220,
"a.wasm": 10388,
"a.wasm.gz": 6692,
"total": 15124,
"total_gz": 9307
"total": 15115,
"total_gz": 9291
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 567,
"a.html.gz": 379,
"a.js": 17395,
"a.js.gz": 7900,
"a.js": 17239,
"a.js.gz": 7827,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21085,
"total_gz": 10972
"total": 20929,
"total_gz": 10899
}
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.js": 292,
"a.js.gz": 252,
"a.js": 291,
"a.js.gz": 249,
"a.wasm": 103,
"a.wasm.gz": 112,
"total": 1068,
"total_gz": 795
"total": 1067,
"total_gz": 792
}
89 changes: 47 additions & 42 deletions test/code_size/hello_world_wasm2js.js
Original file line number Diff line number Diff line change
@@ -1,59 +1,64 @@
var b = Module, g = new function(a) {
this.buffer = new ArrayBuffer(65536 * a.initial);
}({
initial: 256,
maximum: 256
}), k = g.buffer, d = new Uint8Array(k), l = new TextDecoder("utf8"), m;
var c = Module, g, h, k = new TextDecoder("utf8"), l;

function c(a) {
this.exports = function(h) {
function n(f) {
f.set = function(e, p) {
this[e] = p;
function d(b) {
this.exports = function(f) {
function m(e) {
e.set = function(a, n) {
this[a] = n;
};
f.get = function(e) {
return this[e];
e.get = function(a) {
return this[a];
};
return f;
return e;
}
return function(f) {
var e = f.a.a;
return function(e) {
var a = new ArrayBuffer(16777216), n = e.a.a;
e = m([]);
return {
b: function() {},
c: function(p, q) {
e(1024);
b: Object.create(Object.prototype, {
grow: {},
buffer: {
get: function() {
return a;
}
}
}),
c: function() {},
d: function(p, q) {
n(1024);
return 0;
},
d: n([])
e: e
};
}(h);
}(a);
}(f);
}(b);
}

(function(a, h) {
(function(b, f) {
return {
then: function(n) {
n({
instance: new c(h)
then: function(m) {
m({
instance: new d(f)
});
}
};
})(b.wasm, {
})(c.wasm, {
a: {
a: a => {
var h = console, n = h.log;
if (a) {
for (var f = a + void 0, e = a; !(e >= f) && d[e]; ) ++e;
a = l.decode(d.subarray(a, e));
} else a = "";
n.call(h, a);
},
memory: g
a: b => {
var f = console, m = f.log;
if (b) {
for (var e = b + void 0, a = b; !(a >= e) && g[a]; ) ++a;
b = k.decode(g.subarray(b, a));
} else b = "";
m.call(f, b);
}
}
}).then((a => {
a = a.instance.exports;
m = a.c;
d.set(new Uint8Array(b.mem), 1024);
a.b();
m();
}).then((b => {
b = b.instance.exports;
l = b.d;
h = b.b;
g = new Uint8Array(h.buffer);
g.set(new Uint8Array(c.mem), 1024);
b.c();
l();
}));
8 changes: 4 additions & 4 deletions test/code_size/hello_world_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 671,
"a.html.gz": 430,
"a.js": 694,
"a.js.gz": 433,
"a.js": 708,
"a.js.gz": 444,
"a.mem": 6,
"a.mem.gz": 32,
"total": 1371,
"total_gz": 895
"total": 1385,
"total_gz": 906
}
8 changes: 4 additions & 4 deletions test/code_size/math_wasm.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
{
"a.html": 673,
"a.html.gz": 431,
"a.js": 111,
"a.js.gz": 127,
"a.js": 110,
"a.js.gz": 125,
"a.wasm": 2728,
"a.wasm.gz": 1672,
"total": 3512,
"total_gz": 2230
"total": 3511,
"total_gz": 2228
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 17331,
"a.html.gz": 7543,
"total": 17331,
"total_gz": 7543
"a.html": 17279,
"a.html.gz": 7541,
"total": 17279,
"total_gz": 7541
}
10 changes: 7 additions & 3 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1445,8 +1445,7 @@ def check_memory_setting(setting):
(options.shell_path == DEFAULT_SHELL_HTML or options.shell_path == utils.path_from_root('src/shell_minimal.html')):
exit_with_error(f'Due to collision in variable name "Module", the shell file "{options.shell_path}" is not compatible with build options "-sMODULARIZE -sEXPORT_NAME=Module". Either provide your own shell file, change the name of the export to something else to avoid the name collision. (see https://github.com/emscripten-core/emscripten/issues/7950 for details)')

# TODO(sbc): Remove WASM2JS here once the size regression it would introduce has been fixed.
if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE or settings.WASM2JS:
if settings.SHARED_MEMORY or settings.RELOCATABLE or settings.ASYNCIFY_LAZY_LOAD_CODE:
settings.IMPORTED_MEMORY = 1

if settings.WASM_BIGINT:
Expand Down Expand Up @@ -1974,6 +1973,7 @@ def fix_es6_import_statements(js_file):
write_file(js_file, src
.replace('EMSCRIPTEN$IMPORT$META', 'import.meta')
.replace('EMSCRIPTEN$AWAIT$IMPORT', 'await import'))
save_intermediate('es6-module')


def create_worker_file(input_file, target_dir, output_file):
Expand Down Expand Up @@ -2019,13 +2019,17 @@ def phase_final_emitting(options, state, target, wasm_target, memfile):
# steps that occurred after Closure.
if settings.MINIMAL_RUNTIME == 2 and settings.USE_CLOSURE_COMPILER and settings.DEBUG_LEVEL == 0:
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
save_intermediate('unsafe-optimizations')
# Finally, rerun Closure compile with simple optimizations. It will be able
# to further minify the code. (n.b. it would not be safe to run in advanced
# mode)
final_js = building.closure_compiler(final_js, advanced=False, extra_closure_args=options.closure_args)
# Run unsafe_optimizations.js once more. This allows the cleanup of newly
# unused things that closure compiler leaves behing (e.g `new Float64Array(x)`).
shared.run_js_tool(utils.path_from_root('tools/unsafe_optimizations.js'), [final_js, '-o', final_js], cwd=utils.path_from_root('.'))
save_intermediate('unsafe-optimizations2')

fix_es6_import_statements(final_js)
save_intermediate('es6-module')

# Apply pre and postjs files
if options.extern_pre_js or options.extern_post_js:
Expand Down
26 changes: 0 additions & 26 deletions tools/unsafe_optimizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,27 +68,6 @@ function optPassSimplifyModularizeFunction(ast) {
});
}

// Closure integration of the Module object generates an awkward "var b; b || (b = Module);" code.
// 'b || (b = Module)' -> 'b = Module'.
function optPassSimplifyModuleInitialization(ast) {
visitNodes(ast, ['BlockStatement', 'Program'], (node) => {
for (const n of node.body) {
if (
n.type == 'ExpressionStatement' &&
n.expression.type == 'LogicalExpression' &&
n.expression.operator == '||' &&
n.expression.left.name === n.expression.right.left?.name &&
n.expression.right.right.name == 'Module'
) {
// Clear out the logical operator.
n.expression = n.expression.right;
// There is only one Module assignment, so can finish the pass here.
return false;
}
}
});
}

// Finds redundant operator new statements that are not assigned anywhere.
// (we aren't interested in side effects of the calls if no assignment)
function optPassRemoveRedundantOperatorNews(ast) {
Expand Down Expand Up @@ -225,7 +204,6 @@ function optPassMergeVarInitializationAssignments(ast) {
function runOnJsText(js, pretty = false) {
const ast = acorn.parse(js, {ecmaVersion: 2021});

optPassSimplifyModuleInitialization(ast);
optPassRemoveRedundantOperatorNews(ast);

let progress = true;
Expand Down Expand Up @@ -273,10 +251,6 @@ function runTests() {
'var Module=function(f){};',
);

// optPassSimplifyModuleInitialization:
test('b || (b = Module);', 'b=Module;');
test('function foo(){b || (b = Module);}', 'function foo(){b=Module}');

// optPassRemoveRedundantOperatorNews:
test('new Uint16Array(a);', '');
test('new Uint16Array(a),new Uint16Array(a);', ';');
Expand Down

0 comments on commit 91c0d75

Please sign in to comment.