Skip to content

Commit

Permalink
Run unsafe_optimizations.js again after closure (#21196)
Browse files Browse the repository at this point in the history
This allows `unsafe_optimizations.js` to cleanup some thing that
closure compiler was able to remove usages of.

Split out from #21195
  • Loading branch information
sbc100 authored Jan 29, 2024
1 parent 0e5b3a8 commit 1cd1031
Show file tree
Hide file tree
Showing 12 changed files with 46 additions and 67 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": 4593,
"a.js.gz": 2369,
"a.js": 4584,
"a.js.gz": 2351,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15613,
"total_gz": 9472
"total": 15604,
"total_gz": 9454
}
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": 17937,
"a.js.gz": 8074,
"a.js": 17760,
"a.js.gz": 7958,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21627,
"total_gz": 11146
"total": 21450,
"total_gz": 11030
}
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": 4080,
"a.js.gz": 2198,
"a.js": 4071,
"a.js.gz": 2179,
"a.wasm": 10451,
"a.wasm.gz": 6724,
"total": 15100,
"total_gz": 9301
"total": 15091,
"total_gz": 9282
}
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": 17415,
"a.js.gz": 7891,
"a.js": 17239,
"a.js.gz": 7781,
"a.mem": 3123,
"a.mem.gz": 2693,
"total": 21105,
"total_gz": 10963
"total": 20929,
"total_gz": 10853
}
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
}
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": 692,
"a.js.gz": 430,
"a.mem": 6,
"a.mem.gz": 32,
"total": 1371,
"total_gz": 895
"total": 1369,
"total_gz": 892
}
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": 17264,
"a.html.gz": 7529,
"total": 17264,
"total_gz": 7529
}
7 changes: 6 additions & 1 deletion tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -1974,6 +1974,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 +2020,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 1cd1031

Please sign in to comment.