Skip to content

Commit

Permalink
[RUNTIME] Add compile_shared option to linux compile utility fn (apac…
Browse files Browse the repository at this point in the history
…he#5751)

* feat: Add compile_shared option to linux compile fn

* feat: Add compile_shared option for linux compile util fn

* fix: Fix minrpc testcase use executable compilation

* fix: Fix binutil case where call create_shared to create executable

Co-authored-by: baoxinqi <baoxinqi@4paradigm.com>
  • Loading branch information
2 people authored and Trevor Morris committed Jun 18, 2020
1 parent 615052f commit 1168a57
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
35 changes: 32 additions & 3 deletions python/tvm/contrib/cc.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,39 @@ def create_shared(output,
The compiler command.
"""
if sys.platform == "darwin" or sys.platform.startswith("linux"):
_linux_compile(output, objects, options, cc)
_linux_compile(output, objects, options, cc, compile_shared=True)
elif sys.platform == "win32":
_windows_shared(output, objects, options)
else:
raise ValueError("Unsupported platform")


def create_executable(output,
objects,
options=None,
cc="g++"):
"""Create executable binary.
Parameters
----------
output : str
The target executable.
objects : List[str]
List of object files.
options : List[str]
The list of additional options string.
cc : Optional[str]
The compiler command.
"""
if sys.platform == "darwin" or sys.platform.startswith("linux"):
_linux_compile(output, objects, options, cc)
else:
raise ValueError("Unsupported platform")


def get_target_by_dump_machine(compiler):
""" Functor of get_target_triple that can get the target triple using compiler.
Expand Down Expand Up @@ -164,9 +191,10 @@ def _fcompile(outputs, objects, options=None):
return _fcompile


def _linux_compile(output, objects, options, compile_cmd="g++"):
def _linux_compile(output, objects, options,
compile_cmd="g++", compile_shared=False):
cmd = [compile_cmd]
if output.endswith(".so") or output.endswith(".dylib"):
if compile_shared or output.endswith(".so") or output.endswith(".dylib"):
cmd += ["-shared", "-fPIC"]
if sys.platform == "darwin":
cmd += ["-undefined", "dynamic_lookup"]
Expand All @@ -185,6 +213,7 @@ def _linux_compile(output, objects, options, compile_cmd="g++"):
if proc.returncode != 0:
msg = "Compilation error:\n"
msg += py_str(out)
msg += "\nCommand line: " + " ".join(cmd)
raise RuntimeError(msg)


Expand Down
2 changes: 1 addition & 1 deletion tests/python/contrib/test_binutil.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def make_binary():
tmp_obj = tmp_dir.relpath("obj.obj")
with open(tmp_source, "w") as f:
f.write(prog)
cc.create_shared(tmp_obj, tmp_source, [],
cc.create_executable(tmp_obj, tmp_source, [],
cc="{}gcc".format(TOOLCHAIN_PREFIX))
prog_bin = bytearray(open(tmp_obj, "rb").read())
return prog_bin
Expand Down
6 changes: 3 additions & 3 deletions tests/python/unittest/test_runtime_rpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
import pytest
import numpy as np
from tvm import rpc
from tvm.contrib import util
from tvm.contrib import util, cc
from tvm.rpc.tracker import Tracker


Expand Down Expand Up @@ -142,7 +142,7 @@ def check(remote):
# Test minrpc server.
temp = util.tempdir()
minrpc_exec = temp.relpath("minrpc")
tvm.rpc.with_minrpc("g++")(minrpc_exec, [])
tvm.rpc.with_minrpc(cc.create_executable)(minrpc_exec, [])
check(rpc.PopenSession(minrpc_exec))
# minrpc on the remote
server = rpc.Server("localhost")
Expand Down Expand Up @@ -208,7 +208,7 @@ def check_minrpc():
temp = util.tempdir()
f = tvm.build(s, [A, B], "llvm --system-lib", name="myadd")
path_minrpc = temp.relpath("dev_lib.minrpc")
f.export_library(path_minrpc, rpc.with_minrpc("g++"))
f.export_library(path_minrpc, rpc.with_minrpc(cc.create_executable))

with pytest.raises(RuntimeError):
rpc.PopenSession("filenotexist")
Expand Down

0 comments on commit 1168a57

Please sign in to comment.