Skip to content

Commit bd5fb74

Browse files
authored
[wasm64] Enable table64 lowering (#21992)
This change prepares for the LLVM change which actually enables the use of table64 in the output: llvm/llvm-project#92042
1 parent e2cf42e commit bd5fb74

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

tools/emscripten.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -662,18 +662,18 @@ def create_tsd(metadata, embind_tsd):
662662
out += create_tsd_exported_runtime_methods(metadata)
663663
# Manually generate defintions for any Wasm function exports.
664664
out += 'interface WasmModule {\n'
665-
for name, types in metadata.function_exports.items():
665+
for name, functype in metadata.function_exports.items():
666666
mangled = asmjs_mangle(name)
667667
should_export = settings.EXPORT_KEEPALIVE and mangled in settings.EXPORTED_FUNCTIONS
668668
if not should_export:
669669
continue
670670
arguments = []
671-
for index, type in enumerate(types.params):
671+
for index, type in enumerate(functype.params):
672672
arguments.append(f"_{index}: {type_to_ts_type(type)}")
673673
out += f' {mangled}({", ".join(arguments)}): '
674-
assert len(types.returns) <= 1, 'One return type only supported'
675-
if types.returns:
676-
out += f'{type_to_ts_type(types.returns[0])}'
674+
assert len(functype.returns) <= 1, 'One return type only supported'
675+
if functype.returns:
676+
out += f'{type_to_ts_type(functype.returns[0])}'
677677
else:
678678
out += 'void'
679679
out += ';\n'
@@ -791,6 +791,8 @@ def add_standard_wasm_imports(send_items_map):
791791

792792
if settings.RELOCATABLE:
793793
send_items_map['__indirect_function_table'] = 'wasmTable'
794+
if settings.MEMORY64:
795+
send_items_map['__table_base32'] = '___table_base32'
794796

795797
if settings.AUTODEBUG:
796798
extra_sent_items += [
@@ -1100,7 +1102,17 @@ def create_pointer_conversion_wrappers(metadata):
11001102

11011103
sigs_seen = set()
11021104
wrap_functions = []
1103-
for symbol in metadata.function_exports:
1105+
for symbol, functype in metadata.function_exports.items():
1106+
# dynCall_ functions are generated by binaryen. They all take a
1107+
# function pointer as their first argument.
1108+
# The second part of this check can be removed once the table64
1109+
# llvm changs lands.
1110+
if symbol.startswith('dynCall_') and functype.params[0] == webassembly.Type.I64:
1111+
sig = symbol.split('_')[-1]
1112+
sig = ['p' if t == 'p' else '_' for t in sig]
1113+
sig.insert(1, 'p')
1114+
sig = ''.join(sig)
1115+
mapping[symbol] = sig
11041116
sig = mapping.get(symbol)
11051117
if sig:
11061118
if settings.MEMORY64:

tools/link.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,9 @@ def phase_linker_setup(options, state, newargs):
13361336
# Any "pointers" passed to JS will now be i64's, in both modes.
13371337
settings.WASM_BIGINT = 1
13381338

1339+
if settings.MEMORY64 and settings.RELOCATABLE:
1340+
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE.append('__table_base32')
1341+
13391342
if settings.WASM_WORKERS:
13401343
settings.DEFAULT_LIBRARY_FUNCS_TO_INCLUDE += ['$_wasmWorkerInitializeRuntime']
13411344
# set location of Wasm Worker bootstrap JS file
@@ -1957,11 +1960,15 @@ def run_embind_gen(wasm_target, js_syms, extra_settings):
19571960
outfile_js = in_temp('tsgen_a.out.js')
19581961
# The Wasm outfile may be modified by emscripten.emscript, so use a temporary file.
19591962
outfile_wasm = in_temp('tsgen_a.out.wasm')
1960-
emscripten.emscript(wasm_target, outfile_wasm, outfile_js, js_syms, False)
1963+
emscripten.emscript(wasm_target, outfile_wasm, outfile_js, js_syms, finalize=False)
19611964
# Build the flags needed by Node.js to properly run the output file.
19621965
node_args = []
19631966
if settings.MEMORY64:
19641967
node_args += shared.node_memory64_flags()
1968+
# Currently we don't have any engines that support table64 so we need
1969+
# to lower it in order to run the output.
1970+
# In the normal flow this happens later in `phase_binaryen`
1971+
building.run_wasm_opt(outfile_wasm, outfile_wasm, ['--table64-lowering'])
19651972
if settings.WASM_EXCEPTIONS:
19661973
node_args += shared.node_exception_flags(config.NODE_JS)
19671974
# Run the generated JS file with the proper flags to generate the TypeScript bindings.

0 commit comments

Comments
 (0)