Skip to content

Commit

Permalink
Speed up running of JS compiler in LLD_REPORT_UNDEFINED mode. NFC
Browse files Browse the repository at this point in the history
Avoid generating the full JS output, only generate the metadata.  This
saves about 10% of the cost.

See: #16003
  • Loading branch information
sbc100 committed Dec 5, 2022
1 parent 7c9b97a commit 025e2eb
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 28 deletions.
25 changes: 8 additions & 17 deletions emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,23 +508,14 @@ def get_all_js_syms():
# TODO(sbc): Find a way to optimize this. Potentially we could add a super-set
# mode of the js compiler that would generate a list of all possible symbols
# that could be checked in.
old_full = settings.INCLUDE_FULL_LIBRARY
try:
# Temporarily define INCLUDE_FULL_LIBRARY since we want a full list
# of all available JS library functions.
settings.INCLUDE_FULL_LIBRARY = True
settings.ONLY_CALC_JS_SYMBOLS = True
emscripten.generate_struct_info()
glue, forwarded_data = emscripten.compile_settings()
forwarded_json = json.loads(forwarded_data)
library_syms = set()
for name in forwarded_json['librarySymbols']:
if shared.is_c_symbol(name):
name = shared.demangle_c_symbol_name(name)
library_syms.add(name)
finally:
settings.ONLY_CALC_JS_SYMBOLS = False
settings.INCLUDE_FULL_LIBRARY = old_full
emscripten.generate_struct_info()
glue, forwarded_data = emscripten.compile_javascript(symbols_only=True)
forwarded_json = json.loads(forwarded_data)
library_syms = set()
for name in forwarded_json:
if shared.is_c_symbol(name):
name = shared.demangle_c_symbol_name(name)
library_syms.add(name)

return library_syms

Expand Down
17 changes: 12 additions & 5 deletions emscripten.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def apply_static_code_hooks(forwarded_json, code):
return code


def compile_settings():
def compile_javascript(symbols_only=False):
stderr_file = os.environ.get('EMCC_STDERR_FILE')
if stderr_file:
stderr_file = os.path.abspath(stderr_file)
Expand All @@ -199,11 +199,18 @@ def compile_settings():
# Call js compiler
env = os.environ.copy()
env['EMCC_BUILD_DIR'] = os.getcwd()
args = [settings_file]
if symbols_only:
args += ['--symbols-only']
out = shared.run_js_tool(path_from_root('src/compiler.js'),
[settings_file], stdout=subprocess.PIPE, stderr=stderr_file,
args, stdout=subprocess.PIPE, stderr=stderr_file,
cwd=path_from_root('src'), env=env, encoding='utf-8')
assert '//FORWARDED_DATA:' in out, 'Did not receive forwarded data in pre output - process failed?'
glue, forwarded_data = out.split('//FORWARDED_DATA:')
if symbols_only:
glue = None
forwarded_data = out
else:
assert '//FORWARDED_DATA:' in out, 'Did not receive forwarded data in pre output - process failed?'
glue, forwarded_data = out.split('//FORWARDED_DATA:')
return glue, forwarded_data


Expand Down Expand Up @@ -367,7 +374,7 @@ def emscript(in_wasm, out_wasm, outfile_js, memfile):
if invoke_funcs:
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$getWasmTableEntry']

glue, forwarded_data = compile_settings()
glue, forwarded_data = compile_javascript()
if DEBUG:
logger.debug(' emscript: glue took %s seconds' % (time.time() - t))
t = time.time()
Expand Down
14 changes: 13 additions & 1 deletion src/compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,17 @@ function load(f) {
// Basic utilities
load('utility.js');


const argv = process.argv.slice(2);
const symbolsOnly = argv.indexOf('--symbols-only');
if (symbolsOnly != -1) {
argv.splice(symbolsOnly, 1);
}

global.ONLY_CALC_JS_SYMBOLS = symbolsOnly != -1;

// Load settings from JSON passed on the command line
const settingsFile = process.argv[2];
const settingsFile = argv[0];
assert(settingsFile);

const settings = JSON.parse(read(settingsFile));
Expand All @@ -55,6 +64,9 @@ WASM_EXPORTS = new Set(WASM_EXPORTS);
SIDE_MODULE_EXPORTS = new Set(SIDE_MODULE_EXPORTS);
INCOMING_MODULE_JS_API = new Set(INCOMING_MODULE_JS_API);
WEAK_IMPORTS = new Set(WEAK_IMPORTS);
if (ONLY_CALC_JS_SYMBOLS) {
INCLUDE_FULL_LIBRARY = 1;
}

// Side modules are pure wasm and have no JS
assert(!SIDE_MODULE, 'JS compiler should not run on side modules');
Expand Down
5 changes: 5 additions & 0 deletions src/jsifier.js
Original file line number Diff line number Diff line change
Expand Up @@ -540,5 +540,10 @@ function ${name}(${args}) {
// Data
functionStubs.forEach(functionStubHandler);

if (ONLY_CALC_JS_SYMBOLS) {
print(JSON.stringify(librarySymbols));
return;
}

finalCombiner();
}
5 changes: 0 additions & 5 deletions src/settings_internal.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,6 @@ var SEPARATE_DWARF = false;
// New WebAssembly exception handling
var WASM_EXCEPTIONS = false;

// Used internally when running the JS compiler simply to generate list of all
// JS symbols. This is used by LLD_REPORT_UNDEFINED to generate a list of all
// JS library symbols.
var ONLY_CALC_JS_SYMBOLS = false;

// Set to true if the program has a main function. By default this is
// enabled, but if `--no-entry` is passed, or if `_main` is not part of
// EXPORTED_FUNCTIONS then this gets set to 0.
Expand Down

0 comments on commit 025e2eb

Please sign in to comment.