From 76aea2e1d11c61a6de5bba192c3ea64124b02fc4 Mon Sep 17 00:00:00 2001 From: Sam Clegg Date: Sun, 28 Jan 2024 14:11:56 -0800 Subject: [PATCH] Run unsafe_optimizations.js again after closure This allows `unsafe_optimizations.js` to cleanup some thing that closure compiler was able to remove usages of. Split out from #21195 --- test/code_size/embind_val_wasm.json | 8 +++---- test/code_size/hello_wasm_worker_wasm.json | 8 +++---- test/code_size/hello_webgl2_wasm.json | 8 +++---- test/code_size/hello_webgl2_wasm2js.json | 8 +++---- test/code_size/hello_webgl_wasm.json | 8 +++---- test/code_size/hello_webgl_wasm2js.json | 8 +++---- test/code_size/hello_world_wasm.json | 8 +++---- test/code_size/hello_world_wasm2js.json | 8 +++---- test/code_size/math_wasm.json | 8 +++---- test/code_size/random_printf_wasm2js.json | 8 +++---- tools/link.py | 7 +++++- tools/unsafe_optimizations.js | 26 ---------------------- 12 files changed, 46 insertions(+), 67 deletions(-) diff --git a/test/code_size/embind_val_wasm.json b/test/code_size/embind_val_wasm.json index 77462a448e15a..5943d126972e8 100644 --- a/test/code_size/embind_val_wasm.json +++ b/test/code_size/embind_val_wasm.json @@ -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 } diff --git a/test/code_size/hello_wasm_worker_wasm.json b/test/code_size/hello_wasm_worker_wasm.json index 174ae41255448..b384a4163717b 100644 --- a/test/code_size/hello_wasm_worker_wasm.json +++ b/test/code_size/hello_wasm_worker_wasm.json @@ -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 } diff --git a/test/code_size/hello_webgl2_wasm.json b/test/code_size/hello_webgl2_wasm.json index 708a528a29e1f..c7dbff7f256eb 100644 --- a/test/code_size/hello_webgl2_wasm.json +++ b/test/code_size/hello_webgl2_wasm.json @@ -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 } diff --git a/test/code_size/hello_webgl2_wasm2js.json b/test/code_size/hello_webgl2_wasm2js.json index 823857f3e7a81..ba1e069b59f8a 100644 --- a/test/code_size/hello_webgl2_wasm2js.json +++ b/test/code_size/hello_webgl2_wasm2js.json @@ -1,10 +1,10 @@ { "a.html": 567, "a.html.gz": 379, - "a.js": 17917, - "a.js.gz": 8089, + "a.js": 17736, + "a.js.gz": 7971, "a.mem": 3123, "a.mem.gz": 2693, - "total": 21607, - "total_gz": 11161 + "total": 21426, + "total_gz": 11043 } diff --git a/test/code_size/hello_webgl_wasm.json b/test/code_size/hello_webgl_wasm.json index 1d73a396b7380..684fe7701a196 100644 --- a/test/code_size/hello_webgl_wasm.json +++ b/test/code_size/hello_webgl_wasm.json @@ -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 } diff --git a/test/code_size/hello_webgl_wasm2js.json b/test/code_size/hello_webgl_wasm2js.json index cb6d778b2b99e..e6d53f1feb31d 100644 --- a/test/code_size/hello_webgl_wasm2js.json +++ b/test/code_size/hello_webgl_wasm2js.json @@ -1,10 +1,10 @@ { "a.html": 567, "a.html.gz": 379, - "a.js": 17395, - "a.js.gz": 7900, + "a.js": 17215, + "a.js.gz": 7787, "a.mem": 3123, "a.mem.gz": 2693, - "total": 21085, - "total_gz": 10972 + "total": 20905, + "total_gz": 10859 } diff --git a/test/code_size/hello_world_wasm.json b/test/code_size/hello_world_wasm.json index ab32b871f761a..b61bdb6c8f74c 100644 --- a/test/code_size/hello_world_wasm.json +++ b/test/code_size/hello_world_wasm.json @@ -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 } diff --git a/test/code_size/hello_world_wasm2js.json b/test/code_size/hello_world_wasm2js.json index f4e43530f0c1a..6317ae8e69f2b 100644 --- a/test/code_size/hello_world_wasm2js.json +++ b/test/code_size/hello_world_wasm2js.json @@ -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 } diff --git a/test/code_size/math_wasm.json b/test/code_size/math_wasm.json index 5526b37a985d7..e5040389b0724 100644 --- a/test/code_size/math_wasm.json +++ b/test/code_size/math_wasm.json @@ -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 } diff --git a/test/code_size/random_printf_wasm2js.json b/test/code_size/random_printf_wasm2js.json index b2c93945084b3..4f5a16e576285 100644 --- a/test/code_size/random_printf_wasm2js.json +++ b/test/code_size/random_printf_wasm2js.json @@ -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 } diff --git a/tools/link.py b/tools/link.py index 97882439c9eb4..ab1f3293392ec 100644 --- a/tools/link.py +++ b/tools/link.py @@ -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): @@ -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: diff --git a/tools/unsafe_optimizations.js b/tools/unsafe_optimizations.js index 45e8d01c3545b..67d1d67a2dfa0 100644 --- a/tools/unsafe_optimizations.js +++ b/tools/unsafe_optimizations.js @@ -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) { @@ -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; @@ -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);', ';');