Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions test/code_size/embind_val_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 380,
"a.js": 7072,
"a.js.gz": 2997,
"a.wasm": 11625,
"a.wasm.gz": 5814,
"total": 19249,
"total_gz": 9191
"a.wasm": 11588,
"a.wasm.gz": 5778,
"total": 19212,
"total_gz": 9155
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 328,
"a.js": 4521,
"a.js.gz": 2312,
"a.wasm": 10427,
"a.wasm.gz": 6719,
"total": 15402,
"total_gz": 9359
"a.wasm": 10399,
"a.wasm.gz": 6695,
"total": 15374,
"total_gz": 9335
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl2_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 346,
"a.html.gz": 262,
"a.js": 22191,
"a.js.gz": 11586,
"total": 22537,
"total_gz": 11848
"a.js": 22193,
"a.js.gz": 11584,
"total": 22539,
"total_gz": 11846
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
"a.html.gz": 328,
"a.js": 4059,
"a.js.gz": 2153,
"a.wasm": 10427,
"a.wasm.gz": 6719,
"total": 14940,
"total_gz": 9200
"a.wasm": 10399,
"a.wasm.gz": 6695,
"total": 14912,
"total_gz": 9176
}
8 changes: 4 additions & 4 deletions test/code_size/hello_webgl_wasm2js.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"a.html": 346,
"a.html.gz": 262,
"a.js": 21717,
"a.js.gz": 11425,
"total": 22063,
"total_gz": 11687
"a.js": 21719,
"a.js.gz": 11420,
"total": 22065,
"total_gz": 11682
}
8 changes: 4 additions & 4 deletions test/code_size/random_printf_wasm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"a.html": 12746,
"a.html.gz": 6918,
"total": 12746,
"total_gz": 6918
"a.html": 12694,
"a.html.gz": 6891,
"total": 12694,
"total_gz": 6891
}
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_files_wasmfs.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
51018
50957
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_O3.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1741
1737
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Os.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1732
1728
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_Oz.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1290
1286
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_dylink.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
9355
9347
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_hello_wasmfs.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1741
1737
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5265
5261
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_grow.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5266
5262
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5589
5585
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5514
5510
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_mem_O3_standalone_lib.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5277
5273
Original file line number Diff line number Diff line change
@@ -1 +1 @@
5307
5303
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4124
4120
2 changes: 1 addition & 1 deletion test/other/metadce/test_metadce_minimal_pthreads.size
Original file line number Diff line number Diff line change
@@ -1 +1 @@
19481
19421
20 changes: 20 additions & 0 deletions test/test_other.py
Original file line number Diff line number Diff line change
Expand Up @@ -8038,6 +8038,26 @@ def test_eval_ctors_debug_output(self):
self.assertNotContained('ctor_evaller: not successful', err)
self.assertContained('could not eval: call import: env.external_thing', err)

@uses_canonical_tmp
@with_env_modify({'EMCC_DEBUG': '1'})
@parameterized({
# StackIR optimizations should happen once at the end, and not multiple
# times in the middle. In -O2 we simply do them in the main wasm-opt
# invocation, while in -O3 we run wasm-opt later, and so we should disable
# StackIR first and enable it at the end.
'O2': (['-O2'], False),
'O3': (['-O3'], True),
})
def test_binaryen_stack_ir(self, opts, disable_and_enable):
err = self.run_process([EMXX, test_file('hello_world.c')] + opts, stderr=PIPE).stderr
DISABLE = '--no-stack-ir'
ENABLE = '--optimize-stack-ir'
self.assertContainedIf(DISABLE, err, disable_and_enable)
self.assertContainedIf(ENABLE, err, disable_and_enable)
# they should also appear at most once each
self.assertLess(err.count(DISABLE), 2)
self.assertLess(err.count(ENABLE), 2)

def test_override_js_execution_environment(self):
create_file('main.c', r'''
#include <emscripten.h>
Expand Down
47 changes: 30 additions & 17 deletions tools/building.py
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,10 @@ def minify_wasm_js(js_file, wasm_file, expensive_optimizations, debug_info):
# if we are optimizing for size, shrink the combined wasm+JS
# TODO: support this when a symbol map is used
if expensive_optimizations:
js_file = metadce(js_file, wasm_file, debug_info=debug_info)
js_file = metadce(js_file,
wasm_file,
debug_info=debug_info,
last=not settings.MINIFY_WASM_IMPORTS_AND_EXPORTS)
# now that we removed unneeded communication between js and wasm, we can clean up
# the js some more.
passes = ['AJSDCE']
Expand All @@ -743,8 +746,16 @@ def is_internal_global(name):
return name in internal_start_stop_symbols or any(name.startswith(p) for p in internal_prefixes)


# get the flags to pass into the very last binaryen tool invocation, that runs
# the final set of optimizations
def get_last_binaryen_opts():
return [f'--optimize-level={settings.OPT_LEVEL}',
f'--shrink-level={settings.SHRINK_LEVEL}',
'--optimize-stack-ir']


# run binaryen's wasm-metadce to dce both js and wasm
def metadce(js_file, wasm_file, debug_info):
def metadce(js_file, wasm_file, debug_info, last):
logger.debug('running meta-DCE')
temp_files = shared.get_temp_files()
# first, get the JS part of the graph
Expand Down Expand Up @@ -810,10 +821,13 @@ def metadce(js_file, wasm_file, debug_info):
temp = temp_files.get('.json', prefix='emcc_dce_graph_').name
utils.write_file(temp, json.dumps(graph, indent=2))
# run wasm-metadce
args = ['--graph-file=' + temp]
if last:
args += get_last_binaryen_opts()
out = run_binaryen_command('wasm-metadce',
wasm_file,
wasm_file,
['--graph-file=' + temp],
args,
debug=debug_info,
stdout=PIPE)
# find the unused things in js
Expand Down Expand Up @@ -878,23 +892,19 @@ def asyncify_lazy_load_code(wasm_target, debug):
def minify_wasm_imports_and_exports(js_file, wasm_file, minify_exports, debug_info):
logger.debug('minifying wasm imports and exports')
# run the pass
args = []
if minify_exports:
# standalone wasm mode means we need to emit a wasi import module.
# otherwise, minify even the imported module names.
if settings.MINIFY_WASM_IMPORTED_MODULES:
pass_name = '--minify-imports-and-exports-and-modules'
args.append('--minify-imports-and-exports-and-modules')
else:
pass_name = '--minify-imports-and-exports'
args.append('--minify-imports-and-exports')
else:
pass_name = '--minify-imports'
out = run_wasm_opt(wasm_file, wasm_file,
[pass_name],
debug=debug_info,
stdout=PIPE)
# TODO this is the last tool we run, after normal opts and metadce. it
# might make sense to run Stack IR optimizations here or even -O (as
# metadce which runs before us might open up new general optimization
# opportunities). however, the benefit is less than 0.5%.
args.append('--minify-imports')
# this is always the last tool we run (if we run it)
args += get_last_binaryen_opts()
out = run_wasm_opt(wasm_file, wasm_file, args, debug=debug_info, stdout=PIPE)

# get the mapping
SEP = ' => '
Expand Down Expand Up @@ -1057,15 +1067,18 @@ def handle_final_wasm_symbols(wasm_file, symbols_file, debug_info):
args = []
if symbols_file:
args += ['--print-function-map']
if not debug_info:
# to remove debug info, we just write to that same file, and without -g
args += ['-o', wasm_file]
else:
# suppress the wasm-opt warning regarding "no output file specified"
args += ['--quiet']
output = run_wasm_opt(wasm_file, args=args, stdout=PIPE)
if symbols_file:
utils.write_file(symbols_file, output)
if not debug_info:
# strip the names section using llvm-objcopy. this is slightly slower than
# using wasm-opt (we could run wasm-opt without -g here and just tell it to
# write the file back out), but running wasm-opt would undo StackIR
# optimizations, if we did those.
strip(wasm_file, wasm_file, sections=['name'])


def is_ar(filename):
Expand Down
11 changes: 11 additions & 0 deletions tools/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,17 @@ def check_human_readable_list(items):
if settings.WASM_EXNREF:
passes += ['--emit-exnref']

# If we are going to run metadce then that means we will be running binaryen
# tools after the main invocation, whose flags are determined here
# (specifically we will run metadce and possibly also wasm-opt for import/
# export minification). And when we run such a tool it will "undo" any
# StackIR optimizations (since the conversion to BinaryenIR undoes them as it
# restructures the code). We could re-run those opts, but it is most efficient
# to just not do them now if we'll invoke other tools later, and we'll do them
# only in the very last invocation.
if will_metadce():
passes += ['--no-stack-ir']

return passes


Expand Down