Skip to content

Commit 8f638f8

Browse files
authored
Simplify em_js func types check. NFC (#24821)
As far as I can tell, the only reason we had to re-parse the module here is because update_metadata didn't update em_js_func_types which might've been changed by legalization. I've consolidates imports reading into read_module_imports so it now consistently reads any relevant info both during the initial module parsing and in the update_metadata.
1 parent 0cb8646 commit 8f638f8

File tree

2 files changed

+31
-49
lines changed

2 files changed

+31
-49
lines changed

tools/emscripten.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -317,26 +317,15 @@ def emscript(in_wasm, out_wasm, outfile_js, js_syms, finalize=True, base_metadat
317317
update_settings_glue(out_wasm, metadata, base_metadata)
318318

319319
if not settings.WASM_BIGINT and metadata.em_js_funcs:
320-
import_map = {}
321-
322-
with webassembly.Module(in_wasm) as module:
323-
types = module.get_types()
324-
for imp in module.get_imports():
325-
if imp.module not in ('GOT.mem', 'GOT.func'):
326-
import_map[imp.field] = imp
327-
328320
for em_js_func, raw in metadata.em_js_funcs.items():
329321
c_sig = raw.split('<::>')[0].strip('()')
330322
if not c_sig or c_sig == 'void':
331323
c_sig = []
332324
else:
333325
c_sig = c_sig.split(',')
334-
if em_js_func in import_map:
335-
imp = import_map[em_js_func]
336-
assert imp.kind == webassembly.ExternType.FUNC
337-
signature = types[imp.type]
338-
if len(signature.params) != len(c_sig):
339-
diagnostics.warning('em-js-i64', 'using 64-bit arguments in EM_JS function without WASM_BIGINT is not yet fully supported: `%s` (%s, %s)', em_js_func, c_sig, signature.params)
326+
signature = metadata.em_js_func_types.get(em_js_func)
327+
if signature and len(signature.params) != len(c_sig):
328+
diagnostics.warning('em-js-i64', 'using 64-bit arguments in EM_JS function without WASM_BIGINT is not yet fully supported: `%s` (%s, %s)', em_js_func, c_sig, signature.params)
340329

341330
asm_consts = create_asm_consts(metadata)
342331
em_js_funcs = create_em_js(metadata)

tools/extract_metadata.py

Lines changed: 28 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -264,25 +264,32 @@ def get_tag_exports(module):
264264
return rtn
265265

266266

267-
def update_metadata(filename, metadata):
268-
imports = []
269-
invoke_funcs = []
270-
with webassembly.Module(filename) as module:
271-
for i in module.get_imports():
272-
if i.kind == webassembly.ExternType.FUNC:
273-
if i.field.startswith('invoke_'):
274-
invoke_funcs.append(i.field)
275-
else:
276-
imports.append(i.field)
277-
elif i.kind in (webassembly.ExternType.GLOBAL, webassembly.ExternType.TAG):
267+
def read_module_imports(module, metadata):
268+
em_js_funcs = metadata.em_js_funcs
269+
types = module.get_types()
270+
271+
imports = metadata.imports = []
272+
invoke_funcs = metadata.invoke_funcs = []
273+
em_js_func_types = metadata.em_js_func_types = {}
274+
275+
for i in module.get_imports():
276+
if i.kind == webassembly.ExternType.FUNC:
277+
if i.field.startswith('invoke_'):
278+
invoke_funcs.append(i.field)
279+
else:
280+
if i.field in em_js_funcs:
281+
em_js_func_types[i.field] = types[i.type]
278282
imports.append(i.field)
283+
elif i.kind in (webassembly.ExternType.GLOBAL, webassembly.ExternType.TAG):
284+
imports.append(i.field)
279285

286+
287+
def update_metadata(filename, metadata):
288+
with webassembly.Module(filename) as module:
280289
metadata.function_exports = get_function_exports(module)
281290
metadata.tag_exports = get_tag_exports(module)
282291
metadata.all_exports = [utils.removeprefix(e.name, '__em_js__') for e in module.get_exports()]
283-
284-
metadata.imports = imports
285-
metadata.invoke_funcs = invoke_funcs
292+
read_module_imports(module, metadata)
286293

287294

288295
def get_string_at(module, address):
@@ -298,25 +305,24 @@ class Metadata:
298305
em_asm_consts: Dict[int, str]
299306
js_deps: List[str]
300307
em_js_funcs: Dict[str, str]
301-
em_js_func_types: Dict[str, str]
308+
em_js_func_types: Dict[str, webassembly.FuncType]
302309
features: List[str]
303310
invoke_funcs: List[str]
304311
main_reads_params: bool
305-
global_exports: List[str]
312+
global_exports: Dict[str, str]
313+
function_exports: Dict[str, webassembly.FuncType]
314+
tag_exports: List[str]
315+
all_exports: List[str]
306316

307317
def __init__(self):
308318
pass
309319

310320

311321
def extract_metadata(filename):
312-
import_names = []
313-
invoke_funcs = []
314322
em_js_funcs = {}
315-
em_js_func_types = {}
316323

317324
with webassembly.Module(filename) as module:
318325
exports = module.get_exports()
319-
imports = module.get_imports()
320326

321327
export_map = {e.name: e for e in exports}
322328
for e in exports:
@@ -326,18 +332,6 @@ def extract_metadata(filename):
326332
string_address = to_unsigned(get_global_value(globl))
327333
em_js_funcs[name] = get_string_at(module, string_address)
328334

329-
for i in imports:
330-
if i.kind == webassembly.ExternType.FUNC:
331-
if i.field.startswith('invoke_'):
332-
invoke_funcs.append(i.field)
333-
else:
334-
if i.field in em_js_funcs:
335-
types = module.get_types()
336-
em_js_func_types[i.field] = types[i.type]
337-
import_names.append(i.field)
338-
elif i.kind in (webassembly.ExternType.GLOBAL, webassembly.ExternType.TAG):
339-
import_names.append(i.field)
340-
341335
features = module.parse_features_section()
342336
features = ['--enable-' + f[1] for f in features if f[0] == '+']
343337
features = [f.replace('--enable-atomics', '--enable-threads') for f in features]
@@ -347,18 +341,17 @@ def extract_metadata(filename):
347341
# If main does not read its parameters, it will just be a stub that
348342
# calls __original_main (which has no parameters).
349343
metadata = Metadata()
350-
metadata.imports = import_names
351344
metadata.function_exports = get_function_exports(module)
352345
metadata.tag_exports = get_tag_exports(module)
353346
metadata.all_exports = [utils.removeprefix(e.name, '__em_js__') for e in exports]
354347
metadata.em_asm_consts = get_section_strings(module, export_map, 'em_asm')
355348
metadata.js_deps = [d for d in get_section_strings(module, export_map, 'em_lib_deps').values() if d]
356349
metadata.em_js_funcs = em_js_funcs
357-
metadata.em_js_func_types = em_js_func_types
358350
metadata.features = features
359-
metadata.invoke_funcs = invoke_funcs
360351
metadata.main_reads_params = get_main_reads_params(module, export_map)
361352
metadata.global_exports = get_global_exports(module, exports)
362353

354+
read_module_imports(module, metadata)
355+
363356
# print("Metadata parsed: " + pprint.pformat(metadata))
364357
return metadata

0 commit comments

Comments
 (0)