This is an automated email from the ASF dual-hosted git repository.

ruihangl pushed a commit to branch unity
in repository https://gitbox.apache.org/repos/asf/tvm.git


The following commit(s) were added to refs/heads/unity by this push:
     new b40391961a [Unity] Add system lib build option to relax (#14734)
b40391961a is described below

commit b40391961a3ae6ffe194364db18bc7bd79570bbb
Author: Tianqi Chen <tqc...@users.noreply.github.com>
AuthorDate: Thu Apr 27 16:00:35 2023 -0400

    [Unity] Add system lib build option to relax (#14734)
    
    This PR add system lib build option to relax
    also adds an util that creates an archived static
    library in compilation
---
 python/tvm/contrib/cc.py     | 30 ++++++++++++++++++++++++++++++
 python/tvm/relax/vm_build.py | 24 ++++++++++++++++++------
 2 files changed, 48 insertions(+), 6 deletions(-)

diff --git a/python/tvm/contrib/cc.py b/python/tvm/contrib/cc.py
index 867cbd6012..add74396a0 100644
--- a/python/tvm/contrib/cc.py
+++ b/python/tvm/contrib/cc.py
@@ -83,6 +83,36 @@ def create_shared(output, objects, options=None, cc=None):
         raise ValueError("Unsupported platform")
 
 
+def _linux_ar(output, objects):
+    cmd = ["ar", "-crs", output]
+    cmd += objects
+    proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, 
stderr=subprocess.STDOUT)
+    (out, _) = proc.communicate()
+    if proc.returncode != 0:
+        msg = "AR error:\n"
+        msg += py_str(out)
+        msg += "\nCommand line: " + " ".join(cmd)
+        raise RuntimeError(msg)
+
+
+def create_staticlib(output, objects):
+    """Create shared library.
+
+    Parameters
+    ----------
+    output : str
+        The target shared library.
+
+    objects : List[str]
+        List of object files.
+    """
+
+    if _is_linux_like():
+        return _linux_ar(output, objects)
+    else:
+        raise ValueError("Unsupported platform")
+
+
 def create_executable(output, objects, options=None, cc=None):
     """Create executable binary.
 
diff --git a/python/tvm/relax/vm_build.py b/python/tvm/relax/vm_build.py
index c89c64cd81..a833939833 100644
--- a/python/tvm/relax/vm_build.py
+++ b/python/tvm/relax/vm_build.py
@@ -180,12 +180,13 @@ def _vmcodegen(
     raise ValueError("Unknown exec_mode %s" % exec_mode)
 
 
-def _autodetect_system_lib_req(target: tvm.target.Target):
+def _autodetect_system_lib_req(target: tvm.target.Target, system_lib):
     """Automatically detect system lib requirement"""
     host = target if target.host is None else target.host
-    system_lib = False
-    if "wasm" in host.attrs.get("mtriple", ""):
-        system_lib = True
+    if system_lib is None:
+        system_lib = False
+        if "wasm" in host.attrs.get("mtriple", ""):
+            system_lib = True
     if system_lib:
         # use packed-func to avoid relay dep.
         return tvm.get_global_func("relay.backend.CreateRuntime")("cpp", 
{"system-lib": system_lib})
@@ -198,6 +199,8 @@ def _vmlink(
     tir_mod: Optional[tvm.IRModule] = None,
     ext_libs: List[tvm.runtime.Module] = None,
     params: Optional[Dict[str, list]] = None,
+    *,
+    system_lib: Optional[bool] = None,
 ):
     """
     Internal codegen function to make executable.
@@ -236,7 +239,9 @@ def _vmlink(
         ext_libs = []
     lib = None
     if tir_mod is not None:
-        lib = tvm.build(tir_mod, target=target, 
runtime=_autodetect_system_lib_req(target))
+        lib = tvm.build(
+            tir_mod, target=target, runtime=_autodetect_system_lib_req(target, 
system_lib)
+        )
     return Executable(_ffi_api.VMLink(builder, target, lib, ext_libs, params)) 
 # type: ignore
 
 
@@ -245,6 +250,8 @@ def build(
     target: Union[str, tvm.target.Target],
     params: Optional[Dict[str, list]] = None,
     exec_mode: str = "bytecode",
+    *,
+    system_lib: Optional[bool] = None,
 ) -> Executable:
     """
     Build an IRModule to VM executable.
@@ -270,6 +277,11 @@ def build(
     exec_mode: {"bytecode", "compiled"}
         The execution mode.
 
+    system_lib: Optional[bool]
+        Whether to build system lib that is being packed statically and
+        auto registers generated functions to the system.
+        By default auto detects based on the target.
+
     Returns
     -------
     ex: tvm.relax.Executable
@@ -322,7 +334,7 @@ def build(
     builder = relax.ExecBuilder()
     leftover_mod = _vmcodegen(builder, new_mod, exec_mode=exec_mode)
     tir_mod = _filter_tir(leftover_mod)
-    return _vmlink(builder, target, tir_mod, ext_libs, params)
+    return _vmlink(builder, target, tir_mod, ext_libs, params, 
system_lib=system_lib)
 
 
 def _filter_tir(mod: tvm.IRModule) -> tvm.IRModule:

Reply via email to