Skip to content

Commit

Permalink
[Web] Allow custom bc files in emcc making (#16825)
Browse files Browse the repository at this point in the history
When running `emcc` for building a wasm, we currently only pass
in libraries `wasm_runtime.bc`, `tvmjs_support.bc`, and
`webgpu_runtime.bc`. This PR allows users to optionally pass
in their own `.bc` files by adding a kwarg `libs`.
  • Loading branch information
CharlieFRuan authored Apr 1, 2024
1 parent a39067b commit b4b97f8
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions python/tvm/contrib/emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,13 @@
"""Util to invoke emscripten compilers in the system."""
# pylint: disable=invalid-name
import subprocess
from pathlib import Path

from tvm._ffi.base import py_str
from tvm._ffi.libinfo import find_lib_path


def create_tvmjs_wasm(output, objects, options=None, cc="emcc"):
def create_tvmjs_wasm(output, objects, options=None, cc="emcc", libs=None):
"""Create wasm that is supposed to run with the tvmjs.
Parameters
Expand All @@ -37,6 +39,9 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"):
cc : str, optional
The compile string.
libs : list
List of user-defined library files (e.g. .bc files) to add into the wasm.
"""
cmd = [cc]
cmd += ["-O3"]
Expand All @@ -63,17 +68,27 @@ def create_tvmjs_wasm(output, objects, options=None, cc="emcc"):
if obj.find("wasm_runtime.bc") != -1:
with_runtime = True

libs = []
all_libs = []
if not with_runtime:
libs += [find_lib_path("wasm_runtime.bc")[0]]
all_libs += [find_lib_path("wasm_runtime.bc")[0]]

all_libs += [find_lib_path("tvmjs_support.bc")[0]]
all_libs += [find_lib_path("webgpu_runtime.bc")[0]]

libs += [find_lib_path("tvmjs_support.bc")[0]]
libs += [find_lib_path("webgpu_runtime.bc")[0]]
if libs:
if not isinstance(libs, list):
raise ValueError("Expect `libs` to be a list of paths in string.")
for lib in libs:
if not Path(lib).exists():
raise RuntimeError(
"Cannot find file from libs:" + lib + "\n Try pass in an absolute path."
)
all_libs += libs

cmd += ["-o", output]

# let libraries go before normal object
cmd += libs + objects
cmd += all_libs + objects

if options:
cmd += options
Expand Down

0 comments on commit b4b97f8

Please sign in to comment.